sale_chance.go 9.6 KB

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