sale_clue.go 8.1 KB

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