custom.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "log"
  7. "strings"
  8. "time"
  9. qu "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/date"
  11. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  12. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  13. "bp.jydev.jianyu360.cn/CRM/application/entity"
  14. baseC "bp.jydev.jianyu360.cn/CRM/baseCenter/rpc/pb"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. )
  17. // 客户相关
  18. type CustomService struct {
  19. BaseUserId int64
  20. PositionId int64
  21. EntUserId int64
  22. EntId int64
  23. AppId string
  24. AccountId int64
  25. EntDeptId int64
  26. CustomType int64 //客户类型
  27. Summary string //概要信息
  28. CustomAllName string //客户全称
  29. CustomAbbreviation string //客户简称
  30. CustomLevel int64 //客户级别
  31. CustomIndustry int64 //客户行业
  32. CustomDetailIndustry int64 //客户细分行业
  33. Province string //省份
  34. City string //城市
  35. District string //地区
  36. Address string //详细地址
  37. CompanyPhone string //公司电话
  38. NextfollowUpTime int64 //下次跟进时间戳
  39. Types int64 //处理方式 1自办;2转办
  40. User []int64 //转办用户
  41. EmployCustomId int64 //客户收录id
  42. EmployInfoId int64 //资讯收录id
  43. Remarks string //备注
  44. CreateName string //创建人
  45. Source string
  46. OwnerId string //潜客收录id(人脉)
  47. BusinessId string //商机收录id(人脉)
  48. }
  49. // Add 创建客户
  50. func (this *CustomService) Add(ctx context.Context) (int64, string) {
  51. nowtime := time.Now().Format(date.Date_Full_Layout)
  52. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  53. args := []interface{}{}
  54. argsTask := []interface{}{}
  55. argsTaskTeam := []interface{}{}
  56. //判断处理方式
  57. //转办
  58. baseUserIdArr := []int64{}
  59. customId, taskId := int64(-1), int64(-1)
  60. groupId := ""
  61. if this.Types == 2 {
  62. transferArr := []int64{}
  63. for _, v := range this.User {
  64. i_entuserid := v
  65. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  66. Id: i_entuserid,
  67. })
  68. if err != nil {
  69. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  70. return -1, groupId
  71. } else if resp == nil {
  72. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  73. return -1, groupId
  74. }
  75. transferArr = append(transferArr, resp.PositionId)
  76. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  77. }
  78. //客户
  79. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime, this.Source, this.OwnerId, this.BusinessId)
  80. //任务
  81. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), nil, nil)
  82. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.OwnerId, this.BusinessId, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId)
  83. if customId < 0 {
  84. return -1, groupId
  85. }
  86. } else if this.Types == 1 {
  87. //客户
  88. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime, this.Source, this.OwnerId, this.BusinessId)
  89. //任务s
  90. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), nil, nil)
  91. //任务团队
  92. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  93. //存库
  94. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.OwnerId, this.BusinessId, this.PositionId, this.CreateName, []int64{}, this.EntId, this.EntUserId, this.EntDeptId)
  95. if customId < 0 {
  96. return -1, groupId
  97. }
  98. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  99. //自办 创建 群聊
  100. // gp := &Group{
  101. // EntId: this.EntId,
  102. // PositionId: this.PositionId,
  103. // UserIdArr: []int64{},
  104. // AppId: this.AppId,
  105. // EntUserId: this.EntUserId,
  106. // AccountId: this.AccountId,
  107. // }
  108. // groupId = gp.GroupAdd()
  109. } else if this.Types == 3 {
  110. transferArr := []int64{}
  111. transferArr = append(transferArr, this.PositionId)
  112. //客户
  113. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime, this.Source, this.OwnerId, this.BusinessId)
  114. //任务
  115. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), nil, nil)
  116. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.OwnerId, this.BusinessId, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId)
  117. if customId < 0 {
  118. return -1, groupId
  119. }
  120. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  121. }
  122. u := &User{BaseUserIds: baseUserIdArr}
  123. if this.Types != 3 {
  124. var kb strings.Builder
  125. var vb strings.Builder
  126. for k, v := range u.GetUserId(this.EntId) {
  127. kb.WriteString(k + ",")
  128. vb.WriteString(gconv.String(v) + ",")
  129. userId := strings.TrimRight(kb.String(), ",")
  130. positionId := strings.TrimRight(vb.String(), ",")
  131. next := strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)
  132. pcHref := ""
  133. content := ""
  134. if this.Types == 1 {
  135. pcHref = fmt.Sprintf(cm.Push.Custom.Create.MyselfPcHref, positionId, taskId, customId)
  136. content = fmt.Sprintf(cm.Push.Custom.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  137. } else if this.Types == 2 {
  138. pcHref = fmt.Sprintf(cm.Push.Custom.Create.TransferPcHref, positionId)
  139. content = fmt.Sprintf(cm.Push.Custom.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  140. }
  141. StationMailPush(userId, positionId, cm.Push.Custom.Create.Title, content, pcHref, cm.Push.Custom.Create.MobileHref, "11")
  142. }
  143. }
  144. return 1, groupId
  145. }
  146. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  147. fields := []string{"position_id", "ent_id", "ent_user_id", "employ_info_id", "employ_custom_id", "type", "full_name", "sort_name", "summary", "level_1", "industry", "subdivision_industry", "province", "city", "county", "address", "phone", "remark", "create_person", "create_time", "source", "connection_owner_id", "connection_business_id"}
  148. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  149. return id
  150. }
  151. // EmployUpdate 是否创建线索/机会/客户修改
  152. // return employ_info_id,employ_custom_id
  153. func EmployUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64, key string, entId, entUserId, entDeptId int64) (int64, int64) {
  154. tablename := ""
  155. id := int64(0)
  156. if employ_custom_id > 0 {
  157. tablename = entity.EMPLOY_CUSTOM
  158. id = employ_custom_id
  159. } else if employ_info_id > 0 {
  160. tablename = entity.EMPLOY_INFO
  161. id = employ_info_id
  162. }
  163. fmt.Println(tablename, id)
  164. if tablename == "" || id == 0 {
  165. return employ_info_id, employ_custom_id
  166. }
  167. //判断该条信息是不是当前资讯分配员的
  168. d := cm.CrmMysql.SelectBySqlByTx(tx, fmt.Sprintf(`select * from %v where id = ?`, tablename), id)
  169. fmt.Println(d)
  170. if d != nil && len(*d) > 0 {
  171. data := (*d)[0]
  172. position_id := gconv.Int64(data["position_id"])
  173. if position_id == positionId {
  174. cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{key: 1, "is_handle": 1})
  175. } else {
  176. //判断是否已创建该条,但是是取消收录状态、如果是则改为已收录,不是则创建一条新的
  177. d2 := cm.CrmMysql.SelectBySqlByTx(tx, fmt.Sprintf(`select * from %v where source_id = ? and position_id =?`, tablename), data["source_id"], positionId)
  178. if d2 != nil && len(*d2) > 0 {
  179. //已创建
  180. qwe := cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{
  181. "source_id": data["source_id"],
  182. "position_id": positionId,
  183. }, map[string]interface{}{
  184. key: 1,
  185. "status": 1,
  186. })
  187. log.Println(qwe, data["source_id"], positionId)
  188. } else {
  189. data["position_id"] = positionId
  190. data["ent_id"] = entId
  191. data["ent_dept_id"] = entDeptId
  192. data["ent_user_id"] = entUserId
  193. //当前咨询不是分配员的 创建该条
  194. if employ_custom_id > 0 {
  195. //
  196. data["is_handle"] = 1
  197. data["is_ignore"] = 0
  198. data["is_create_custom"] = 1
  199. } else if employ_info_id > 0 {
  200. data["is_handle"] = 1
  201. data["is_ignore"] = 0
  202. data["is_dis"] = 0
  203. data[key] = 1
  204. for _, v := range GetOtherKey(key) {
  205. data[v] = 0
  206. }
  207. }
  208. delete(data, "id")
  209. newId := cm.CrmMysql.InsertByTx(tx, tablename, data)
  210. if tablename == entity.EMPLOY_INFO {
  211. employ_info_id = newId
  212. } else if tablename == entity.EMPLOY_CUSTOM {
  213. employ_custom_id = newId
  214. }
  215. }
  216. }
  217. }
  218. return employ_info_id, employ_custom_id
  219. }
  220. func SaveCustom(ctx context.Context, argsCustom, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id int64, ownerId, connectionBusinessId string, positionId int64, createPerson string, transferArr []int64, entId, entUserId, entDeptId int64) (int64, int64) {
  221. customId := int64(-1)
  222. taskId := int64(-1)
  223. //存库
  224. cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  225. //修改状态
  226. employ_info_id_new, employ_custom_id_new := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_custom", entId, entUserId, entDeptId)
  227. //插入客户
  228. //修改客户id、咨询id
  229. argsCustom[3] = employ_info_id_new
  230. argsCustom[4] = employ_custom_id_new
  231. customId = SaleCustomAdd(tx, argsCustom)
  232. //传过来的argTask没有来源id,需要append
  233. argsTask = append(argsTask, customId)
  234. //任务车存储
  235. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  236. //插入台账
  237. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  238. //人脉信息添加
  239. if ownerId != "" || connectionBusinessId != "" {
  240. SaveConnection(tx, positionId, connectionBusinessId, ownerId, entId, entDeptId, entUserId)
  241. }
  242. if customId > 0 && taskId > 0 && ok2 {
  243. return true
  244. }
  245. log.Println("SaveCustom err:", customId, taskId)
  246. return false
  247. })
  248. return customId, taskId
  249. }
  250. // SaveLedger 操作台帐相关
  251. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  252. //操作台账
  253. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  254. PositionId: positionId,
  255. BusinessId: businessId, //业务id
  256. TaskId: taskId, //任务id
  257. Types: types, //类型
  258. Content: content, //内容
  259. CreateWay: 1, //创建方式 1:人 2:系统
  260. CreatePerson: createPerson,
  261. })
  262. if err != nil {
  263. log.Println("save ledger err:", err)
  264. return false
  265. }
  266. if resp == nil {
  267. log.Println("save ledger resp is nil")
  268. return false
  269. }
  270. return resp.State
  271. }
  272. // 消息漏斗保存
  273. func saveSalesFunnel(tx *sql.Tx, args []interface{}, tpl_id int64) int64 {
  274. fields := []string{`chance_id`, `task_id`, `chance_name`, `outline`, `next_follow_time`, `expected_transaction_amount`, `business_type`, `ent_id`, `employ_info_id`, `current_generation`, `current_generation_progress`, `generation`}
  275. //当前阶段
  276. stageId := int64(0)
  277. stageData := cm.CrmMysql.Find(entity.CONFIG_TPL_STAGE, map[string]interface{}{
  278. "tpl_id": tpl_id}, "id, `order`", "", -1, -1)
  279. if stageData != nil && len(*stageData) > 0 {
  280. var a1 []interface{}
  281. for _, m := range *stageData {
  282. stageId = gconv.Int64(m["id"])
  283. order := gconv.Int(m["order"])
  284. a1 = append(a1, args...)
  285. if order == 1 {
  286. a1 = append(a1, stageId)
  287. } else {
  288. a1 = append(a1, 0)
  289. }
  290. a1 = append(a1, 0)
  291. a1 = append(a1, stageId)
  292. //进度查询
  293. /*matterData := cm.CrmMysql.SelectBySql("select COUNT(1) as ratio from config_stage_matter where stage_id=? and is_must == 1", stageId)
  294. if matterData != nil && len(*matterData) > 0 {
  295. ratio := gconv.Int64((*matterData)[0]["ratio"])
  296. a1 = append(a1, ratio)
  297. } else {
  298. a1 = append(a1, 0)
  299. }*/
  300. }
  301. //当前阶段进展
  302. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALES_FUNNEL, fields, a1)
  303. return id
  304. /**/
  305. }
  306. return 0
  307. }
  308. func GetOtherKey(key string) []string {
  309. parameters := map[string][]string{
  310. "is_create_clue": {"is_create_chance", "is_create_custom"},
  311. "is_create_chance": {"is_create_clue", "is_create_custom"},
  312. "is_create_custom": {"is_create_chance", "is_create_clue"},
  313. }
  314. return parameters[key]
  315. }
  316. func SaveConnection(tx *sql.Tx, positionId int64, businessId, ownerId string, entId, entDeptId, entUserId int64) bool {
  317. if businessId != "" || ownerId != "" {
  318. table := entity.CONNECTION_STATUS
  319. sourceType := 2
  320. if businessId == "" {
  321. businessId = ownerId
  322. sourceType = 1
  323. }
  324. if cm.CrmMysql.Count(table, map[string]interface{}{
  325. "position_id": positionId,
  326. "relate_id": businessId,
  327. "itype": sourceType,
  328. }) > 0 {
  329. cm.CrmMysql.UpdateByTx(tx, table, map[string]interface{}{
  330. "position_id": positionId,
  331. "relate_id": businessId,
  332. "itype": sourceType,
  333. }, map[string]interface{}{
  334. "is_handle": 1,
  335. "relate_id": businessId,
  336. "update_time": time.Now().Format(date.Date_Full_Layout),
  337. "is_create": 1,
  338. })
  339. return true
  340. } else {
  341. id := cm.CrmMysql.InsertByTx(tx, table, map[string]interface{}{
  342. "position_id": positionId,
  343. "ent_id": entId,
  344. "ent_dept_id": entDeptId,
  345. "ent_user_id": entUserId,
  346. "relate_id": businessId,
  347. "is_handle": 1,
  348. "is_create": 1,
  349. "itype": sourceType,
  350. "create_time": time.Now().Format(date.Date_Full_Layout),
  351. "update_time": time.Now().Format(date.Date_Full_Layout),
  352. })
  353. return id > 0
  354. }
  355. }
  356. return true
  357. }