custom.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.SaleClue.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.SaleClue.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. //
  130. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  131. 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"}
  132. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  133. return id
  134. }
  135. //EmployUpdate 是否创建线索/机会/客户修改
  136. //return employ_info_id,employ_custom_id
  137. func EmployUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64, key string, entId, entUserId, entDeptId int64) (int64, int64) {
  138. tablename := ""
  139. id := int64(0)
  140. if employ_custom_id > 0 {
  141. tablename = entity.EMPLOY_CUSTOM
  142. id = employ_custom_id
  143. } else if employ_info_id > 0 {
  144. tablename = entity.EMPLOY_INFO
  145. id = employ_info_id
  146. }
  147. fmt.Println(tablename, id)
  148. if tablename == "" || id == 0 {
  149. return employ_info_id, employ_custom_id
  150. }
  151. //判断该条信息是不是当前资讯分配员的
  152. d := cm.CrmMysql.SelectBySqlByTx(tx, fmt.Sprintf(`select * from %v where id = ?`, tablename), id)
  153. fmt.Println(d)
  154. if d != nil && len(*d) > 0 {
  155. data := (*d)[0]
  156. position_id := gconv.Int64(data["position_id"])
  157. if position_id == positionId {
  158. cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{key: 1, "is_handle": 1})
  159. } else {
  160. //判断是否已创建该条,但是是取消收录状态、如果是则改为已收录,不是则创建一条新的
  161. d2 := cm.CrmMysql.SelectBySqlByTx(tx, fmt.Sprintf(`select * from %v where source_id = ? and position_id =?`, tablename), data["source_id"], positionId)
  162. if d2 != nil && len(*d2) > 0 {
  163. //已创建
  164. qwe := cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{
  165. "source_id": data["source_id"],
  166. "position_id": positionId,
  167. }, map[string]interface{}{
  168. key: 1,
  169. "status": 1,
  170. })
  171. log.Println(qwe, data["source_id"], positionId)
  172. } else {
  173. data["position_id"] = positionId
  174. data["ent_id"] = entId
  175. data["ent_dept_id"] = entDeptId
  176. data["ent_user_id"] = entUserId
  177. //当前咨询不是分配员的 创建该条
  178. if employ_custom_id > 0 {
  179. //
  180. data["is_handle"] = 1
  181. data["is_ignore"] = 0
  182. data["is_create_custom"] = 1
  183. } else if employ_info_id > 0 {
  184. data["is_handle"] = 1
  185. data["is_ignore"] = 0
  186. data["is_dis"] = 0
  187. data[key] = 1
  188. for _, v := range GetOtherKey(key) {
  189. data[v] = 0
  190. }
  191. }
  192. delete(data, "id")
  193. newId := cm.CrmMysql.InsertByTx(tx, tablename, data)
  194. if tablename == entity.EMPLOY_INFO {
  195. employ_info_id = newId
  196. } else if tablename == entity.EMPLOY_CUSTOM {
  197. employ_custom_id = newId
  198. }
  199. }
  200. }
  201. }
  202. return employ_info_id, employ_custom_id
  203. }
  204. 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) {
  205. customId := int64(-1)
  206. taskId := int64(-1)
  207. //存库
  208. cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  209. //修改状态
  210. employ_info_id_new, employ_custom_id_new := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_custom", entId, entUserId, entDeptId)
  211. //插入客户
  212. //修改客户id、咨询id
  213. argsCustom[3] = employ_info_id_new
  214. argsCustom[4] = employ_custom_id_new
  215. customId = SaleCustomAdd(tx, argsCustom)
  216. //传过来的argTask没有来源id,需要append
  217. argsTask = append(argsTask, customId)
  218. //任务车存储
  219. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  220. //插入台账
  221. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  222. if customId > 0 && taskId > 0 && ok2 {
  223. return true
  224. }
  225. log.Println("SaveCustom err:", customId, taskId)
  226. return false
  227. })
  228. return customId, taskId
  229. }
  230. //SaveLedger 操作台帐相关
  231. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  232. //操作台账
  233. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  234. PositionId: positionId,
  235. BusinessId: businessId, //业务id
  236. TaskId: taskId, //任务id
  237. Types: types, //类型
  238. Content: content, //内容
  239. CreateWay: 1, //创建方式 1:人 2:系统
  240. CreatePerson: createPerson,
  241. })
  242. if err != nil {
  243. log.Println("save ledger err:", err)
  244. return false
  245. }
  246. if resp == nil {
  247. log.Println("save ledger resp is nil")
  248. return false
  249. }
  250. return resp.State
  251. }
  252. func GetOtherKey(key string) []string {
  253. parameters := map[string][]string{
  254. "is_create_clue": []string{"is_create_chance", "is_create_custom"},
  255. "is_create_chance": []string{"is_create_clue", "is_create_custom"},
  256. "is_create_custom": []string{"is_create_chance", "is_create_clue"},
  257. }
  258. return parameters[key]
  259. }