sale_chance.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 float64 //最初预计落单金额
  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. ChanceSource int64
  41. }
  42. // Add 创建机会
  43. func (this *SaleChanceService) Add(ctx context.Context) (int64, string) {
  44. nowtime := time.Now().Format(date.Date_Full_Layout)
  45. args := []interface{}{}
  46. argsTask := []interface{}{}
  47. argsTaskTeam := []interface{}{}
  48. //落单时间戳转时间
  49. expect_deal_time := time.Unix(this.ExpectedOrderTime, 0).Format(date.Date_Full_Layout)
  50. //下次跟进时间
  51. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  52. baseUserIdArr := []int64{}
  53. chanceId, taskId := int64(-1), int64(-1)
  54. groupId := ""
  55. //判断处理方式
  56. stageId := 0
  57. 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 {
  58. stageId = gconv.Int((*stageData)[0]["id"])
  59. }
  60. //转办
  61. if this.Types == 2 {
  62. transferArr := []int64{}
  63. for _, v := range this.User {
  64. i_entuserid := v
  65. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  66. Id: i_entuserid,
  67. })
  68. if err != nil {
  69. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  70. return -1, groupId
  71. } else if resp == nil {
  72. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  73. return -1, groupId
  74. }
  75. transferArr = append(transferArr, resp.PositionId)
  76. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  77. }
  78. 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, this.ChanceSource)
  79. //任务
  80. argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
  81. chanceId, taskId = this.SaveChange(ctx, args, argsTask, argsTaskTeam, transferArr)
  82. if chanceId < 0 {
  83. return -1, groupId
  84. }
  85. } else if this.Types == 1 {
  86. //线索机会
  87. 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, this.ChanceSource)
  88. //任务
  89. argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
  90. //任务团队
  91. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  92. //存库
  93. chanceId, taskId = this.SaveChange(ctx, args, argsTask, argsTaskTeam, []int64{})
  94. if chanceId < 0 {
  95. return -1, groupId
  96. }
  97. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  98. // //自办 创建 群聊
  99. // gp := &Group{
  100. // EntId: this.EntId,
  101. // PositionId: this.PositionId,
  102. // UserIdArr: []int64{},
  103. // AppId: this.AppId,
  104. // EntUserId: this.EntUserId,
  105. // AccountId: this.AccountId,
  106. // }
  107. // groupId = gp.GroupAdd()
  108. } else if this.Types == 3 {
  109. transferArr := []int64{}
  110. transferArr = append(transferArr, this.PositionId)
  111. 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, this.ChanceSource)
  112. //任务
  113. argsTask = append(argsTask, this.EntId, this.ChanceName+"的跟进任务", 2, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime), this.BusinessType, stageId)
  114. chanceId, taskId = this.SaveChange(ctx, args, argsTask, argsTaskTeam, transferArr)
  115. if chanceId < 0 {
  116. return -1, groupId
  117. }
  118. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  119. }
  120. u := &User{BaseUserIds: baseUserIdArr}
  121. if this.Types != 3 {
  122. var kb strings.Builder
  123. var vb strings.Builder
  124. for k, v := range u.GetUserId(this.EntId) {
  125. kb.WriteString(k + ",")
  126. vb.WriteString(gconv.String(v) + ",")
  127. userId := strings.TrimRight(kb.String(), ",")
  128. positionId := strings.TrimRight(vb.String(), ",")
  129. next := strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)
  130. content := ""
  131. pcHref := ""
  132. if this.Types == 1 {
  133. pcHref = fmt.Sprintf(cm.Push.SaleChance.Create.MyselfPcHref, positionId, taskId, chanceId)
  134. content = fmt.Sprintf(cm.Push.SaleChance.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  135. } else if this.Types == 2 {
  136. pcHref = fmt.Sprintf(cm.Push.SaleChance.Create.TransferPcHref, positionId)
  137. content = fmt.Sprintf(cm.Push.SaleChance.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next)
  138. }
  139. StationMailPush(userId, positionId, cm.Push.SaleChance.Create.Title, content, pcHref, cm.Push.SaleChance.Create.MobileHref, "11")
  140. }
  141. }
  142. return 1, groupId
  143. }
  144. // SaleChangeAdd 机会存储
  145. func SaleChangeAdd(tx *sql.Tx, args []interface{}) int64 {
  146. 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", "source"}
  147. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALE_CHANCE, fields, args)
  148. //
  149. return id
  150. }
  151. // Save 存库
  152. func (this *SaleChanceService) SaveChange(ctx context.Context, argsChange, argsTask, argsTaskTeam []interface{}, transferArr []int64) (int64, int64) {
  153. //存库
  154. changeId, taskId := int64(-1), int64(-1)
  155. cm.CrmMysql.ExecTx("创建机会", func(tx *sql.Tx) bool {
  156. //
  157. employ_info_id_new, _ := EmployUpdate(tx, this.EmployInfoId, 0, this.PositionId, "is_create_chance", this.EntId, this.EntUserId, this.EntDeptId)
  158. argsChange[3] = employ_info_id_new
  159. //插入机会
  160. changeId = SaleChangeAdd(tx, argsChange)
  161. //传过来的argTask没有来源id,需要append
  162. argsTask = append(argsTask, changeId)
  163. //任务车存储
  164. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, this.PositionId)
  165. //操作台帐
  166. ok1 := SaveLedger(ctx, this.PositionId, changeId, taskId, "创建销售机会", fmt.Sprintf("%s创建了销售机会", this.CreateName), this.CreateName)
  167. //任务执行计划
  168. planId := TaskExecutePlanAdd(tx, this.BusinessType, taskId)
  169. //销售漏斗
  170. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  171. argsSalesFunnel := []interface{}{
  172. changeId,
  173. taskId,
  174. this.ChanceName,
  175. this.Summary,
  176. nextFollowTime,
  177. this.ExpectedMoney,
  178. this.BusinessType,
  179. this.EntId,
  180. this.EmployInfoId,
  181. }
  182. SalesFunnel := saveSalesFunnel(tx, argsSalesFunnel, this.BusinessType)
  183. if changeId > 0 && taskId > 0 && ok1 && planId > 0 && SalesFunnel > 0 {
  184. return true
  185. }
  186. log.Println("save change err: ", changeId, taskId, ok1, planId, SalesFunnel)
  187. return false
  188. })
  189. return changeId, taskId
  190. }
  191. // TaskExecutePlanAdd 任务执行计划存储
  192. func TaskExecutePlanAdd(tx *sql.Tx, tplId, taskId int64) int64 {
  193. args := []interface{}{}
  194. data := cm.CrmMysql.SelectBySqlByTx(tx, `SELECT a.name,b.name,b.id FROM config_tpl_stage a
  195. INNER JOIN config_stage_matter b ON (b.stage_id=a.id)
  196. where a.tpl_id=?
  197. order by a.id,b.id `, tplId)
  198. if data != nil && len(*data) > 0 {
  199. for _, v := range *data {
  200. matterId := gconv.Int(v["id"])
  201. args = append(args, taskId, matterId)
  202. }
  203. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_EXECUTE_PLAN, []string{"task_id", "matter_id"}, args)
  204. return id
  205. }
  206. return 0
  207. }