sale_chance.go 9.8 KB

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