sale_clue.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "log"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/date"
  9. "app.yhyue.com/moapp/jybase/encrypt"
  10. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  11. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  12. "bp.jydev.jianyu360.cn/CRM/application/entity"
  13. "github.com/gogf/gf/v2/util/gconv"
  14. )
  15. //线索相关
  16. type SaleClueService struct {
  17. PositionId int64
  18. EntId int64
  19. EntUserId int64
  20. ClueName string
  21. CluesSource string
  22. Summary string
  23. EmployInfoId int64
  24. Types int64
  25. User []string
  26. FollowUpTime int64
  27. CreateName string
  28. }
  29. //Add 创建线索
  30. func (this *SaleClueService) Add(ctx context.Context) int64 {
  31. nowtime := time.Now().Format(date.Date_Full_Layout)
  32. log.Println(this.FollowUpTime, time.Unix(this.FollowUpTime, 0))
  33. nextFollowTime := time.Unix(this.FollowUpTime, 0).Format(date.Date_Full_Layout)
  34. args := []interface{}{}
  35. argsTask := []interface{}{}
  36. argsFollowRecord := []interface{}{}
  37. //判断处理方式
  38. //转办
  39. if this.Types == 2 {
  40. for _, v := range this.User {
  41. entuserid := encrypt.SE.Decode4Hex(v)
  42. i_entuserid := gconv.Int64(entuserid)
  43. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  44. Id: i_entuserid,
  45. })
  46. if err != nil {
  47. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  48. return 0
  49. } else if resp == nil {
  50. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  51. return 0
  52. }
  53. positionid := resp.PositionId
  54. createName := resp.EntUserName
  55. //线索
  56. args = append(args, positionid, this.EntId, i_entuserid, this.ClueName, this.CluesSource, this.Summary, this.EmployInfoId, 0, nowtime)
  57. //任务
  58. argsTask = append(argsTask, positionid, this.EntId, this.EntUserId, this.ClueName+"的跟进任务", this.CluesSource, positionid, 1, nowtime, 1, nextFollowTime)
  59. if !Save(ctx, args, argsTask, argsFollowRecord, positionid, createName) {
  60. return 0
  61. }
  62. }
  63. } else if this.Types == 1 {
  64. //线索
  65. 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))
  66. //任务
  67. argsTask = append(argsTask, this.PositionId, this.EntId, this.EntUserId, this.ClueName+"的跟进任务", 1, this.PositionId, 1, nowtime, 1, nextFollowTime)
  68. //存库
  69. if !Save(ctx, args, argsTask, argsFollowRecord, this.PositionId, this.CreateName) {
  70. return 0
  71. }
  72. }
  73. return 1
  74. }
  75. //SaleClueAdd 线索存储
  76. func SaleClueAdd(tx *sql.Tx, args []interface{}) int64 {
  77. fields := []string{"position_id", "ent_id", "ent_user_id", "name", "source", "summary", "employ_info_id", "is_close", "create_time"}
  78. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALE_CLUE, fields, args)
  79. return id
  80. }
  81. //TaskAdd 任务车存储
  82. func TaskAdd(tx *sql.Tx, args []interface{}) int64 {
  83. fields := []string{"position_id", "ent_id", "ent_user_id", "name", "source", "owner_id", "status", "create_time", "create_way", "next_follow_time", "source_id"}
  84. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.TASK, fields, args)
  85. return id
  86. }
  87. //FollowRecordAdd 跟进记录存储
  88. func FollowRecordAdd(tx *sql.Tx, args []interface{}) int64 {
  89. fields := []string{"position_id", "task_id"}
  90. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.FOLLOW_RECORD, fields, args)
  91. return id
  92. }
  93. //Save 存库
  94. func Save(ctx context.Context, argsClue, argsTask, argsFollowRecord []interface{}, positionId int64, createName string) bool {
  95. //存库
  96. return cm.CrmMysql.ExecTx("创建线索", func(tx *sql.Tx) bool {
  97. //插入线索
  98. clueId := SaleClueAdd(tx, argsClue)
  99. //传过来的argTask没有来源id,需要append
  100. argsTask = append(argsTask, clueId)
  101. //任务车存储
  102. taskId := TaskAdd(tx, argsTask)
  103. //操作台帐
  104. ok := SaveLedger(ctx, positionId, clueId, taskId, "创建销售线索", fmt.Sprintf("%s创建了销售线索", createName), createName)
  105. if clueId > 0 && taskId > 0 && ok {
  106. return true
  107. }
  108. log.Println("save clue err: ", clueId, taskId, ok)
  109. return false
  110. })
  111. }