custom.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. }
  47. // Add 创建客户
  48. func (this *CustomService) Add(ctx context.Context) (int64, string) {
  49. nowtime := time.Now().Format(date.Date_Full_Layout)
  50. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  51. args := []interface{}{}
  52. argsTask := []interface{}{}
  53. argsTaskTeam := []interface{}{}
  54. //判断处理方式
  55. //转办
  56. baseUserIdArr := []int64{}
  57. customId, taskId := int64(-1), int64(-1)
  58. groupId := ""
  59. if this.Types == 2 {
  60. transferArr := []int64{}
  61. for _, v := range this.User {
  62. i_entuserid := v
  63. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  64. Id: i_entuserid,
  65. })
  66. if err != nil {
  67. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  68. return -1, groupId
  69. } else if resp == nil {
  70. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  71. return -1, groupId
  72. }
  73. transferArr = append(transferArr, resp.PositionId)
  74. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  75. }
  76. //客户
  77. 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)
  78. //任务
  79. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), nil, nil)
  80. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId)
  81. if customId < 0 {
  82. return -1, groupId
  83. }
  84. } else if this.Types == 1 {
  85. //客户
  86. 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)
  87. //任务s
  88. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), nil, nil)
  89. //任务团队
  90. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  91. //存库
  92. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, []int64{}, this.EntId, this.EntUserId, this.EntDeptId)
  93. if customId < 0 {
  94. return -1, groupId
  95. }
  96. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  97. //自办 创建 群聊
  98. // gp := &Group{
  99. // EntId: this.EntId,
  100. // PositionId: this.PositionId,
  101. // UserIdArr: []int64{},
  102. // AppId: this.AppId,
  103. // EntUserId: this.EntUserId,
  104. // AccountId: this.AccountId,
  105. // }
  106. // groupId = gp.GroupAdd()
  107. } else if this.Types == 3 {
  108. transferArr := []int64{}
  109. transferArr = append(transferArr, this.PositionId)
  110. //客户
  111. 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)
  112. //任务
  113. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), nil, nil)
  114. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId)
  115. if customId < 0 {
  116. return -1, groupId
  117. }
  118. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  119. }
  120. u := &User{BaseUserIds: baseUserIdArr}
  121. if this.Types != 3 {
  122. var kb strings.Builder
  123. var vb strings.Builder
  124. for k, v := range u.GetUserId(this.EntId) {
  125. kb.WriteString(k + ",")
  126. vb.WriteString(gconv.String(v) + ",")
  127. userId := strings.TrimRight(kb.String(), ",")
  128. positionId := strings.TrimRight(vb.String(), ",")
  129. next := strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)
  130. pcHref := ""
  131. content := ""
  132. if this.Types == 1 {
  133. pcHref = fmt.Sprintf(cm.Push.Custom.Create.MyselfPcHref, positionId, taskId, customId)
  134. content = fmt.Sprintf(cm.Push.Custom.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  135. } else if this.Types == 2 {
  136. pcHref = fmt.Sprintf(cm.Push.Custom.Create.TransferPcHref, positionId)
  137. content = fmt.Sprintf(cm.Push.Custom.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  138. }
  139. StationMailPush(userId, positionId, cm.Push.Custom.Create.Title, content, pcHref, cm.Push.Custom.Create.MobileHref, "11")
  140. }
  141. }
  142. return 1, groupId
  143. }
  144. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  145. fields := []string{"position_id", "ent_id", "ent_user_id", "employ_info_id", "employ_custom_id", "type", "full_name", "sort_name", "summary", "level", "industry", "subdivision_industry", "province", "city", "county", "address", "phone", "remark", "create_person", "create_time", "source"}
  146. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  147. return id
  148. }
  149. // EmployUpdate 是否创建线索/机会/客户修改
  150. // return employ_info_id,employ_custom_id
  151. func EmployUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64, key string, entId, entUserId, entDeptId int64) (int64, int64) {
  152. tablename := ""
  153. id := int64(0)
  154. if employ_custom_id > 0 {
  155. tablename = entity.EMPLOY_CUSTOM
  156. id = employ_custom_id
  157. } else if employ_info_id > 0 {
  158. tablename = entity.EMPLOY_INFO
  159. id = employ_info_id
  160. }
  161. fmt.Println(tablename, id)
  162. if tablename == "" || id == 0 {
  163. return employ_info_id, employ_custom_id
  164. }
  165. //判断该条信息是不是当前资讯分配员的
  166. d := cm.CrmMysql.SelectBySqlByTx(tx, fmt.Sprintf(`select * from %v where id = ?`, tablename), id)
  167. fmt.Println(d)
  168. if d != nil && len(*d) > 0 {
  169. data := (*d)[0]
  170. position_id := gconv.Int64(data["position_id"])
  171. if position_id == positionId {
  172. cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{key: 1, "is_handle": 1})
  173. } else {
  174. //判断是否已创建该条,但是是取消收录状态、如果是则改为已收录,不是则创建一条新的
  175. d2 := cm.CrmMysql.SelectBySqlByTx(tx, fmt.Sprintf(`select * from %v where source_id = ? and position_id =?`, tablename), data["source_id"], positionId)
  176. if d2 != nil && len(*d2) > 0 {
  177. //已创建
  178. qwe := cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{
  179. "source_id": data["source_id"],
  180. "position_id": positionId,
  181. }, map[string]interface{}{
  182. key: 1,
  183. "status": 1,
  184. })
  185. log.Println(qwe, data["source_id"], positionId)
  186. } else {
  187. data["position_id"] = positionId
  188. data["ent_id"] = entId
  189. data["ent_dept_id"] = entDeptId
  190. data["ent_user_id"] = entUserId
  191. //当前咨询不是分配员的 创建该条
  192. if employ_custom_id > 0 {
  193. //
  194. data["is_handle"] = 1
  195. data["is_ignore"] = 0
  196. data["is_create_custom"] = 1
  197. } else if employ_info_id > 0 {
  198. data["is_handle"] = 1
  199. data["is_ignore"] = 0
  200. data["is_dis"] = 0
  201. data[key] = 1
  202. for _, v := range GetOtherKey(key) {
  203. data[v] = 0
  204. }
  205. }
  206. delete(data, "id")
  207. newId := cm.CrmMysql.InsertByTx(tx, tablename, data)
  208. if tablename == entity.EMPLOY_INFO {
  209. employ_info_id = newId
  210. } else if tablename == entity.EMPLOY_CUSTOM {
  211. employ_custom_id = newId
  212. }
  213. }
  214. }
  215. }
  216. return employ_info_id, employ_custom_id
  217. }
  218. func SaveCustom(ctx context.Context, argsCustom, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string, transferArr []int64, entId, entUserId, entDeptId int64) (int64, int64) {
  219. customId := int64(-1)
  220. taskId := int64(-1)
  221. //存库
  222. cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  223. //修改状态
  224. employ_info_id_new, employ_custom_id_new := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_custom", entId, entUserId, entDeptId)
  225. //插入客户
  226. //修改客户id、咨询id
  227. argsCustom[3] = employ_info_id_new
  228. argsCustom[4] = employ_custom_id_new
  229. customId = SaleCustomAdd(tx, argsCustom)
  230. //传过来的argTask没有来源id,需要append
  231. argsTask = append(argsTask, customId)
  232. //任务车存储
  233. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  234. //插入台账
  235. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  236. if customId > 0 && taskId > 0 && ok2 {
  237. return true
  238. }
  239. log.Println("SaveCustom err:", customId, taskId)
  240. return false
  241. })
  242. return customId, taskId
  243. }
  244. // SaveLedger 操作台帐相关
  245. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  246. //操作台账
  247. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  248. PositionId: positionId,
  249. BusinessId: businessId, //业务id
  250. TaskId: taskId, //任务id
  251. Types: types, //类型
  252. Content: content, //内容
  253. CreateWay: 1, //创建方式 1:人 2:系统
  254. CreatePerson: createPerson,
  255. })
  256. if err != nil {
  257. log.Println("save ledger err:", err)
  258. return false
  259. }
  260. if resp == nil {
  261. log.Println("save ledger resp is nil")
  262. return false
  263. }
  264. return resp.State
  265. }
  266. // 消息漏斗保存
  267. func saveSalesFunnel(tx *sql.Tx, args []interface{}, tpl_id int64) int64 {
  268. 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`}
  269. //当前阶段
  270. stageId := int64(0)
  271. stageData := cm.CrmMysql.Find(entity.CONFIG_TPL_STAGE, map[string]interface{}{
  272. "tpl_id": tpl_id}, "id, `order`", "", -1, -1)
  273. if stageData != nil && len(*stageData) > 0 {
  274. var a1 []interface{}
  275. for _, m := range *stageData {
  276. stageId = gconv.Int64(m["id"])
  277. order := gconv.Int(m["order"])
  278. a1 = append(a1, args...)
  279. if order == 1 {
  280. a1 = append(a1, stageId)
  281. } else {
  282. a1 = append(a1, 0)
  283. }
  284. a1 = append(a1, 0)
  285. a1 = append(a1, stageId)
  286. //进度查询
  287. /*matterData := cm.CrmMysql.SelectBySql("select COUNT(1) as ratio from config_stage_matter where stage_id=? and is_must == 1", stageId)
  288. if matterData != nil && len(*matterData) > 0 {
  289. ratio := gconv.Int64((*matterData)[0]["ratio"])
  290. a1 = append(a1, ratio)
  291. } else {
  292. a1 = append(a1, 0)
  293. }*/
  294. }
  295. //当前阶段进展
  296. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALES_FUNNEL, fields, a1)
  297. return id
  298. /**/
  299. }
  300. return 0
  301. }
  302. func GetOtherKey(key string) []string {
  303. parameters := map[string][]string{
  304. "is_create_clue": []string{"is_create_chance", "is_create_custom"},
  305. "is_create_chance": []string{"is_create_clue", "is_create_custom"},
  306. "is_create_custom": []string{"is_create_chance", "is_create_clue"},
  307. }
  308. return parameters[key]
  309. }