custom.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "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. baseC "bp.jydev.jianyu360.cn/CRM/baseCenter/rpc/pb"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. )
  17. //客户相关
  18. type CustomService struct {
  19. BaseUserId int64
  20. PositionId int64
  21. EntUserId int64
  22. EntId int64
  23. CustomType int64 //客户类型
  24. Summary string //概要信息
  25. CustomAllName string //客户全称
  26. CustomAbbreviation string //客户简称
  27. CustomLevel int64 //客户级别
  28. CustomIndustry int64 //客户行业
  29. CustomDetailIndustry int64 //客户细分行业
  30. Province string //省份
  31. City string //城市
  32. District string //地区
  33. Address string //详细地址
  34. CompanyPhone string //公司电话
  35. NextfollowUpTime int64 //下次跟进时间戳
  36. Types int64 //处理方式 1自办;2转办
  37. User []int64 //转办用户
  38. EmployCustomId int64 //客户收录id
  39. EmployInfoId int64 //资讯收录id
  40. Remarks string //备注
  41. CreateName string //创建人
  42. }
  43. //Add 创建客户
  44. func (this *CustomService) Add(ctx context.Context) int64 {
  45. nowtime := time.Now().Format(date.Date_Full_Layout)
  46. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  47. if this.NextfollowUpTime == 0 {
  48. nextFollowTime = time.Now().AddDate(0, 0, cm.C.NextFollowUpTime).Format(date.Date_Full_Layout)
  49. }
  50. args := []interface{}{}
  51. argsTask := []interface{}{}
  52. argsTaskTeam := []interface{}{}
  53. //判断处理方式
  54. //转办
  55. baseUserIdArr := []int64{}
  56. customId, taskId := int64(-1), int64(-1)
  57. if this.Types == 2 {
  58. transferArr := []int64{}
  59. for _, v := range this.User {
  60. i_entuserid := v
  61. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  62. Id: i_entuserid,
  63. })
  64. if err != nil {
  65. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  66. return -1
  67. } else if resp == nil {
  68. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  69. return -1
  70. }
  71. transferArr = append(transferArr, resp.PositionId)
  72. baseUserIdArr = append(baseUserIdArr, resp.UserId)
  73. }
  74. //客户
  75. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime)
  76. //任务
  77. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  78. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, transferArr)
  79. if customId < 0 {
  80. return -1
  81. }
  82. } else if this.Types == 1 {
  83. //客户
  84. args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime)
  85. //任务s
  86. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  87. //任务团队
  88. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  89. //存库
  90. customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, []int64{})
  91. if customId < 0 {
  92. return -1
  93. }
  94. baseUserIdArr = append(baseUserIdArr, this.BaseUserId)
  95. }
  96. u := &User{BaseUserIds: baseUserIdArr}
  97. var kb strings.Builder
  98. var vb strings.Builder
  99. for k, v := range u.GetUserId(this.EntId) {
  100. kb.WriteString(k + ",")
  101. vb.WriteString(gconv.String(v) + ",")
  102. userId := strings.TrimRight(kb.String(), ",")
  103. positionId := strings.TrimRight(vb.String(), ",")
  104. StationMailPush(userId, positionId, cm.Push.Custom.Create.Title, fmt.Sprintf(cm.Push.Custom.Create.Content, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)), fmt.Sprintf(cm.Push.Custom.Create.PcHref, positionId, taskId, customId), cm.Push.Custom.Create.MobileHref)
  105. }
  106. return 1
  107. }
  108. //
  109. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  110. fields := []string{"position_id", "ent_id", "ent_user_id", "employ_info_id", "employ_custom_id", "type", "full_name", "sort_name", "summary", "level", "industry", "subdivision_industry", "province", "city", "county", "address", "phone", "remark", "create_person", "create_time"}
  111. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  112. return id
  113. }
  114. //EmployUpdate 是否创建线索/机会/客户修改
  115. func EmployUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64, key string) bool {
  116. tablename := ""
  117. id := int64(0)
  118. if employ_custom_id > 0 {
  119. tablename = entity.EMPLOY_CUSTOM
  120. id = employ_custom_id
  121. } else if employ_info_id > 0 {
  122. tablename = entity.EMPLOY_INFO
  123. id = employ_info_id
  124. }
  125. if tablename == "" || id == 0 {
  126. return true
  127. }
  128. return cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{key: 1})
  129. }
  130. func SaveCustom(ctx context.Context, argsCustom, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string, transferArr []int64) (int64, int64) {
  131. customId := int64(-1)
  132. taskId := int64(-1)
  133. //存库
  134. cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  135. //插入客户
  136. customId = SaleCustomAdd(tx, argsCustom)
  137. //传过来的argTask没有来源id,需要append
  138. argsTask = append(argsTask, customId)
  139. //任务车存储
  140. taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  141. ok := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_custom")
  142. //插入台账
  143. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  144. if customId > 0 && taskId > 0 && ok && ok2 {
  145. return true
  146. }
  147. log.Println("SaveCustom err:", customId, taskId, ok)
  148. return false
  149. })
  150. return customId, taskId
  151. }
  152. //SaveLedger 操作台帐相关
  153. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  154. //操作台账
  155. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  156. PositionId: positionId,
  157. BusinessId: businessId, //业务id
  158. TaskId: taskId, //任务id
  159. Types: types, //类型
  160. Content: content, //内容
  161. CreateWay: 1, //创建方式 1:人 2:系统
  162. CreatePerson: createPerson,
  163. })
  164. if err != nil {
  165. log.Println("save ledger err:", err)
  166. return false
  167. }
  168. if resp == nil {
  169. log.Println("save ledger resp is nil")
  170. return false
  171. }
  172. return resp.State
  173. }