sale_chance.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "log"
  7. "time"
  8. "strings"
  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. "github.com/gogf/gf/v2/util/gconv"
  15. )
  16. // 线索相关
  17. type SaleChanceService struct {
  18. BaseUserId int64
  19. PositionId int64
  20. EntUserId int64
  21. EntId int64
  22. AppId string
  23. AccountId int64
  24. EntDeptId int64
  25. ChanceName string //机会名称
  26. Owner string //机会所有者
  27. Summary string //概要信息
  28. ChanceClassify int64 //机会分类
  29. ExpectedOrderTime int64 //最初预计落单段时间 时间戳
  30. ExpectedMoney int64 //最初预计落单金额
  31. CustomName string //客户全称
  32. BusinessType int64 //业务类型
  33. Remarks string //备注
  34. NextfollowUpTime int64 //下次跟进时间戳
  35. Types int64 //处理方式 1自办;2转办
  36. User []int64 //企业用户id
  37. EmployInfoId int64 //资讯收录id
  38. CustomId int64 //客户id
  39. CreateName string
  40. }
  41. // Add 创建机会
  42. func (this *SaleChanceService) Add(ctx context.Context) (int64, string) {
  43. nowtime := time.Now().Format(date.Date_Full_Layout)
  44. args := []interface{}{}
  45. argsTask := []interface{}{}
  46. argsTaskTeam := []interface{}{}
  47. //落单时间戳转时间
  48. expect_deal_time := time.Unix(this.ExpectedOrderTime, 0).Format(date.Date_Full_Layout)
  49. //下次跟进时间
  50. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  51. baseUserIdArr := []int64{}
  52. chanceId, taskId := int64(-1), int64(-1)
  53. groupId := ""
  54. //判断处理方式
  55. stageId := 0
  56. if stageData := cm.CrmMysql.SelectBySql("select id from config_tpl_stage where tpl_id =? order by `order` limit 1", this.BusinessType); stageData != nil && len(*stageData) > 0 {
  57. stageId = gconv.Int((*stageData)[0]["id"])
  58. }
  59. //转办
  60. if this.Types == 2 {
  61. transferArr := []int64{}
  62. for _, v := range this.User {
  63. i_entuserid := v
  64. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  65. Id: i_entuserid,
  66. })
  67. if err != nil {
  68. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  69. return -1, groupId
  70. } else if resp == nil {
  71. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  72. return -1, groupId
  73. }
  74. transferArr = append(transferArr, resp.PositionId)
  75. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  76. }
  77. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.CustomId, this.BusinessType, this.ChanceName, this.Summary, this.ChanceClassify, qu.If(this.ExpectedOrderTime == 0, nil, expect_deal_time), qu.If(this.ExpectedMoney == 0, nil, this.ExpectedMoney), qu.If(this.Remarks == "", nil, this.Remarks), nowtime, this.CreateName)
  78. //任务
  79. argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
  80. chanceId, taskId = SaveChange(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.CustomId, this.PositionId, this.BusinessType, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId)
  81. if chanceId < 0 {
  82. return -1, groupId
  83. }
  84. } else if this.Types == 1 {
  85. //线索机会
  86. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.CustomId, this.BusinessType, this.ChanceName, this.Summary, this.ChanceClassify, qu.If(this.ExpectedOrderTime == 0, nil, expect_deal_time), qu.If(this.ExpectedMoney == 0, nil, this.ExpectedMoney), qu.If(this.Remarks == "", nil, this.Remarks), nowtime, this.CreateName)
  87. //任务
  88. argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
  89. //任务团队
  90. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  91. //存库
  92. chanceId, taskId = SaveChange(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.CustomId, this.PositionId, this.BusinessType, this.CreateName, []int64{}, this.EntId, this.EntUserId, this.EntDeptId)
  93. if chanceId < 0 {
  94. return -1, groupId
  95. }
  96. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  97. // //自办 创建 群聊
  98. // gp := &Group{
  99. // EntId: this.EntId,
  100. // PositionId: this.PositionId,
  101. // UserIdArr: []int64{},
  102. // AppId: this.AppId,
  103. // EntUserId: this.EntUserId,
  104. // AccountId: this.AccountId,
  105. // }
  106. // groupId = gp.GroupAdd()
  107. }
  108. u := &User{BaseUserIds: baseUserIdArr}
  109. var kb strings.Builder
  110. var vb strings.Builder
  111. for k, v := range u.GetUserId(this.EntId) {
  112. kb.WriteString(k + ",")
  113. vb.WriteString(gconv.String(v) + ",")
  114. userId := strings.TrimRight(kb.String(), ",")
  115. positionId := strings.TrimRight(vb.String(), ",")
  116. next := strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)
  117. content := ""
  118. pcHref := ""
  119. if this.Types == 1 {
  120. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.MyselfPcHref, positionId, taskId, chanceId)
  121. content = fmt.Sprintf(cm.Push.SaleChance.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  122. } else if this.Types == 2 {
  123. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.TransferPcHref, positionId)
  124. content = fmt.Sprintf(cm.Push.SaleChance.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  125. }
  126. StationMailPush(userId, positionId, cm.Push.SaleChance.Create.Title, content, pcHref, cm.Push.SaleChance.Create.MobileHref)
  127. }
  128. return 1, groupId
  129. }
  130. // SaleChangeAdd 机会存储
  131. func SaleChangeAdd(tx *sql.Tx, args []interface{}) int64 {
  132. fields := []string{"position_id", "ent_id", "ent_user_id", "employ_info_id", "custom_id", "tpl_id", "name", "summary", "classify", "expect_deal_time", "expect_deal_amount", "remark", "create_time", "create_person"}
  133. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALE_CHANCE, fields, args)
  134. return id
  135. }
  136. // Save 存库
  137. func SaveChange(ctx context.Context, argsChange, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId, tplId int64, createName string, transferArr []int64, entId, entUserId, entDeptId int64) (int64, int64) {
  138. //存库
  139. changeId, taskId := int64(-1), int64(-1)
  140. cm.CrmMysql.ExecTx("创建机会", func(tx *sql.Tx) bool {
  141. //
  142. employ_info_id_new, employ_custom_id_new := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_chance", entId, entUserId, entDeptId)
  143. argsChange[3] = employ_info_id_new
  144. argsChange[4] = employ_custom_id_new
  145. //插入机会
  146. changeId = SaleChangeAdd(tx, argsChange)
  147. //传过来的argTask没有来源id,需要append
  148. argsTask = append(argsTask, changeId)
  149. //任务车存储
  150. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  151. //操作台帐
  152. ok1 := SaveLedger(ctx, positionId, changeId, taskId, "创建销售机会", fmt.Sprintf("%s创建了销售机会", createName), createName)
  153. //任务执行计划
  154. planId := TaskExecutePlanAdd(tx, tplId, taskId)
  155. if changeId > 0 && taskId > 0 && ok1 && planId > 0 {
  156. return true
  157. }
  158. log.Println("save change err: ", changeId, taskId, ok1, planId)
  159. return false
  160. })
  161. return changeId, taskId
  162. }
  163. // TaskExecutePlanAdd 任务执行计划存储
  164. func TaskExecutePlanAdd(tx *sql.Tx, tplId, taskId int64) int64 {
  165. args := []interface{}{}
  166. data := cm.CrmMysql.SelectBySqlByTx(tx, `SELECT a.name,b.name,b.id FROM config_tpl_stage a
  167. INNER JOIN config_stage_matter b ON (b.stage_id=a.id)
  168. where a.tpl_id=?
  169. order by a.id,b.id `, tplId)
  170. if data != nil && len(*data) > 0 {
  171. for _, v := range *data {
  172. matterId := gconv.Int(v["id"])
  173. args = append(args, taskId, matterId)
  174. }
  175. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_EXECUTE_PLAN, []string{"task_id", "matter_id"}, args)
  176. return id
  177. }
  178. return 0
  179. }