custom.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. CustomType int64 //客户类型
  24. Summary string //概要信息
  25. CustomAllName string //客户全称
  26. CustomAbbreviation string //客户简称
  27. CustomLevel int64 //客户级别
  28. CustomIndustry int64 //客户行业
  29. CustomDetailIndustry int64 //客户细分行业
  30. Province string //省份
  31. City string //城市
  32. District string //地区
  33. Address string //详细地址
  34. CompanyPhone string //公司电话
  35. NextfollowUpTime int64 //下次跟进时间戳
  36. Types int64 //处理方式 1自办;2转办
  37. User []int64 //转办用户
  38. EmployCustomId int64 //客户收录id
  39. EmployInfoId int64 //资讯收录id
  40. Remarks string //备注
  41. CreateName string //创建人
  42. }
  43. //Add 创建客户
  44. func (this *CustomService) Add(ctx context.Context) int64 {
  45. nowtime := time.Now().Format(date.Date_Full_Layout)
  46. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  47. args := []interface{}{}
  48. argsTask := []interface{}{}
  49. argsTaskTeam := []interface{}{}
  50. //判断处理方式
  51. //转办
  52. baseUserIdArr := []int64{}
  53. customId, taskId := int64(-1), int64(-1)
  54. if this.Types == 2 {
  55. transferArr := []int64{}
  56. for _, v := range this.User {
  57. i_entuserid := v
  58. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  59. Id: i_entuserid,
  60. })
  61. if err != nil {
  62. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  63. return -1
  64. } else if resp == nil {
  65. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  66. return -1
  67. }
  68. transferArr = append(transferArr, resp.PositionId)
  69. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  70. }
  71. //客户
  72. 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)
  73. //任务
  74. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  75. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, transferArr)
  76. if customId < 0 {
  77. return -1
  78. }
  79. } else if this.Types == 1 {
  80. //客户
  81. 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)
  82. //任务s
  83. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  84. //任务团队
  85. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  86. //存库
  87. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, []int64{})
  88. if customId < 0 {
  89. return -1
  90. }
  91. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  92. }
  93. u := &User{BaseUserIds: baseUserIdArr}
  94. var kb strings.Builder
  95. var vb strings.Builder
  96. for k, v := range u.GetUserId(this.EntId) {
  97. kb.WriteString(k + ",")
  98. vb.WriteString(gconv.String(v) + ",")
  99. userId := strings.TrimRight(kb.String(), ",")
  100. positionId := strings.TrimRight(vb.String(), ",")
  101. StationMailPush(userId, positionId, cm.Push.Custom.Create.Title, fmt.Sprintf(cm.Push.Custom.Create.Content, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)), fmt.Sprintf(cm.Push.Custom.Create.PcHref, positionId, taskId, customId), cm.Push.Custom.Create.MobileHref)
  102. }
  103. return 1
  104. }
  105. //
  106. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  107. 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"}
  108. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  109. return id
  110. }
  111. //EmployUpdate 是否创建线索/机会/客户修改
  112. func EmployUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64, key string) bool {
  113. tablename := ""
  114. id := int64(0)
  115. if employ_custom_id > 0 {
  116. tablename = entity.EMPLOY_CUSTOM
  117. id = employ_custom_id
  118. } else if employ_info_id > 0 {
  119. tablename = entity.EMPLOY_INFO
  120. id = employ_info_id
  121. }
  122. if tablename == "" || id == 0 {
  123. return true
  124. }
  125. return cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{key: 1})
  126. }
  127. func SaveCustom(ctx context.Context, argsCustom, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string, transferArr []int64) (int64, int64) {
  128. customId := int64(-1)
  129. taskId := int64(-1)
  130. //存库
  131. cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  132. //插入客户
  133. customId = SaleCustomAdd(tx, argsCustom)
  134. //传过来的argTask没有来源id,需要append
  135. argsTask = append(argsTask, customId)
  136. //任务车存储
  137. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  138. ok := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_custom")
  139. //插入台账
  140. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  141. if customId > 0 && taskId > 0 && ok && ok2 {
  142. return true
  143. }
  144. log.Println("SaveCustom err:", customId, taskId, ok)
  145. return false
  146. })
  147. return customId, taskId
  148. }
  149. //SaveLedger 操作台帐相关
  150. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  151. //操作台账
  152. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  153. PositionId: positionId,
  154. BusinessId: businessId, //业务id
  155. TaskId: taskId, //任务id
  156. Types: types, //类型
  157. Content: content, //内容
  158. CreateWay: 1, //创建方式 1:人 2:系统
  159. CreatePerson: createPerson,
  160. })
  161. if err != nil {
  162. log.Println("save ledger err:", err)
  163. return false
  164. }
  165. if resp == nil {
  166. log.Println("save ledger resp is nil")
  167. return false
  168. }
  169. return resp.State
  170. }