custom.go 11 KB

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