sale_clue.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "app.yhyue.com/moapp/jybase/encrypt"
  12. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  13. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  14. "bp.jydev.jianyu360.cn/CRM/application/entity"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. )
  17. var Se = encrypt.SimpleEncrypt{Key: "topnet"}
  18. var YYYYMMDDHHMM = "2006-01-02 15:04" //%20用于转义
  19. //线索相关
  20. type SaleClueService struct {
  21. AppId string
  22. PositionId int64
  23. EntId int64
  24. EntUserId int64
  25. BaseUserId int64
  26. AccountId int64
  27. EntDeptId int64
  28. ClueName string
  29. CluesSource string
  30. Summary string
  31. EmployInfoId int64
  32. Types int64
  33. User []int64
  34. FollowUpTime int64
  35. CreateName string
  36. }
  37. //Add 创建线索
  38. //return 是否成功,群组id
  39. func (this *SaleClueService) Add(ctx context.Context) (int64, string) {
  40. nowtime := time.Now().Format(date.Date_Full_Layout)
  41. nextFollowTime := time.Unix(this.FollowUpTime, 0).Format(date.Date_Full_Layout)
  42. groupId := ""
  43. args := []interface{}{}
  44. argsTask := []interface{}{}
  45. argsTaskTeam := []interface{}{}
  46. baseUserIdArr := []int64{}
  47. clueId, taskId := int64(-1), int64(-1)
  48. //判断处理方式
  49. //转办
  50. if this.Types == 2 {
  51. transferArr := []int64{}
  52. for _, v := range this.User {
  53. i_entuserid := v
  54. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  55. Id: i_entuserid,
  56. })
  57. if err != nil {
  58. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  59. return -1, groupId
  60. } else if resp == nil {
  61. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  62. return -1, groupId
  63. }
  64. transferArr = append(transferArr, resp.PositionId)
  65. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  66. }
  67. //线索
  68. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.ClueName, this.CluesSource, this.Summary, this.EmployInfoId, 0, nowtime, this.CreateName)
  69. //任务
  70. argsTask = append(argsTask, this.EntId, this.ClueName+"的跟进任务", 1, this.PositionId, 1, nowtime, 1, 0, qu.If(this.FollowUpTime == 0, nil, nextFollowTime), nil, nil)
  71. clueId, taskId = Save(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, 0, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId)
  72. if clueId < 0 {
  73. return -1, groupId
  74. }
  75. } else if this.Types == 1 {
  76. //线索
  77. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.ClueName, this.CluesSource, this.Summary, this.EmployInfoId, 0, time.Now().Format(date.Date_Full_Layout), this.CreateName)
  78. //任务
  79. argsTask = append(argsTask, this.EntId, this.ClueName+"的跟进任务", 1, this.PositionId, 2, nowtime, 1, 0, qu.If(this.FollowUpTime == 0, nil, nextFollowTime), nil, nil)
  80. //任务团队
  81. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  82. //存库
  83. clueId, taskId = Save(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, 0, this.PositionId, this.CreateName, []int64{}, this.EntId, this.EntUserId, this.EntDeptId)
  84. if clueId < 0 {
  85. return -1, groupId
  86. }
  87. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  88. //自办 创建 群聊
  89. // gp := &Group{
  90. // EntId: this.EntId,
  91. // PositionId: this.PositionId,
  92. // UserIdArr: []int64{},
  93. // AppId: this.AppId,
  94. // EntUserId: this.EntUserId,
  95. // AccountId: this.AccountId,
  96. // }
  97. // groupId = gp.GroupAdd()
  98. }
  99. u := &User{BaseUserIds: baseUserIdArr}
  100. var kb strings.Builder
  101. var vb strings.Builder
  102. for k, v := range u.GetUserId(this.EntId) {
  103. kb.WriteString(k + ",")
  104. vb.WriteString(gconv.String(v) + ",")
  105. userId := strings.TrimRight(kb.String(), ",")
  106. positionId := strings.TrimRight(vb.String(), ",")
  107. content, pcHref := "", ""
  108. next := strings.Replace(time.Unix(this.FollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)
  109. if this.Types == 1 {
  110. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.MyselfPcHref, positionId, taskId, clueId)
  111. content = fmt.Sprintf(cm.Push.SaleClue.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  112. } else if this.Types == 2 {
  113. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.TransferPcHref, positionId)
  114. content = fmt.Sprintf(cm.Push.SaleClue.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  115. }
  116. StationMailPush(userId, positionId, cm.Push.SaleClue.Create.Title, content, pcHref, cm.Push.SaleClue.Create.MobileHref)
  117. }
  118. return 1, groupId
  119. }
  120. //SaleClueAdd 线索存储
  121. func SaleClueAdd(tx *sql.Tx, args []interface{}) int64 {
  122. fields := []string{"position_id", "ent_id", "ent_user_id", "name", "source", "summary", "employ_info_id", "is_close", "create_time", "create_person"}
  123. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALE_CLUE, fields, args)
  124. return id
  125. }
  126. //TaskAdd 任务车存储
  127. /*创建任务
  128. taskTransferMap 自办的map
  129. transferArr 转办的用户职位id
  130. */
  131. func TaskAdd(tx *sql.Tx, args, taskTeamArgs []interface{}, transferArr []int64, positionId int64) int64 {
  132. fields := []string{"ent_id", "name", "source", "create_person", "status", "create_time", "create_way", "join_task_vehicle", "next_follow_time", "tpl_id", "current_stage", "source_id"}
  133. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.TASK, fields, args)
  134. teamId, transferId := int64(0), int64(0)
  135. if len(taskTeamArgs) > 0 {
  136. //自办
  137. taskTeamFields := []string{"position_id", "ent_user_id", "name", "role", "create_time", "task_id"}
  138. taskTeamArgs = append(taskTeamArgs, id)
  139. _, teamId = cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_TEAM, taskTeamFields, taskTeamArgs)
  140. } else if len(transferArr) > 0 {
  141. //转办
  142. taskTransferFields := []string{"transfer_id", "responsible_id", "create_time", "task_id"}
  143. taskTransferArgs := []interface{}{}
  144. for _, v := range transferArr {
  145. taskTransferArgs = append(taskTransferArgs, positionId, v, time.Now().Format(date.Date_Full_Layout), id)
  146. }
  147. _, transferId = cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_TRANSFER, taskTransferFields, taskTransferArgs)
  148. }
  149. if transferId > 0 || teamId > 0 {
  150. return id
  151. }
  152. log.Println("task add err :", id, len(taskTeamArgs), teamId, len(transferArr), transferId)
  153. return -1
  154. }
  155. //Save 存库
  156. func Save(ctx context.Context, argsClue, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createName string, transferArr []int64, entId, entUserId, entDeptId int64) (int64, int64) {
  157. clueId, taskId := int64(-1), int64(-1)
  158. //存库
  159. cm.CrmMysql.ExecTx("创建线索", func(tx *sql.Tx) bool {
  160. //是否创建销售线索
  161. employ_info_id_new, _ := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_clue", entId, entUserId, entDeptId)
  162. //插入线索
  163. argsClue[6] = employ_info_id_new
  164. clueId = SaleClueAdd(tx, argsClue)
  165. //传过来的argTask没有来源id,需要append
  166. argsTask = append(argsTask, clueId)
  167. //任务车存储
  168. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  169. //操作台帐
  170. ok2 := SaveLedger(ctx, positionId, clueId, taskId, "创建销售线索", fmt.Sprintf("%s创建了销售线索", createName), createName)
  171. if clueId > 0 && taskId > 0 && ok2 {
  172. return true
  173. }
  174. log.Println("save clue err: ", clueId, taskId, ok2)
  175. return false
  176. })
  177. return clueId, taskId
  178. }