custom.go 12 KB

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