custom.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "log"
  7. "time"
  8. qu "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/date"
  10. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  11. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  12. "bp.jydev.jianyu360.cn/CRM/application/entity"
  13. baseC "bp.jydev.jianyu360.cn/CRM/baseCenter/rpc/pb"
  14. )
  15. //客户相关
  16. type CustomService struct {
  17. BaseUserId int64
  18. PositionId int64
  19. EntUserId int64
  20. EntId int64
  21. CustomType int64 //客户类型
  22. Summary string //概要信息
  23. CustomAllName string //客户全称
  24. CustomAbbreviation string //客户简称
  25. CustomLevel int64 //客户级别
  26. CustomIndustry int64 //客户行业
  27. CustomDetailIndustry int64 //客户细分行业
  28. Province string //省份
  29. City string //城市
  30. District string //地区
  31. Address string //详细地址
  32. CompanyPhone string //公司电话
  33. NextfollowUpTime int64 //下次跟进时间戳
  34. Types int64 //处理方式 1自办;2转办
  35. User []int64 //转办用户
  36. EmployCustomId int64 //客户收录id
  37. EmployInfoId int64 //资讯收录id
  38. Remarks string //备注
  39. CreateName string //创建人
  40. }
  41. //Add 创建客户
  42. func (this *CustomService) Add(ctx context.Context) int64 {
  43. nowtime := time.Now().Format(date.Date_Full_Layout)
  44. nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout)
  45. args := []interface{}{}
  46. argsTask := []interface{}{}
  47. argsTaskTeam := []interface{}{}
  48. //判断处理方式
  49. //转办
  50. if this.Types == 2 {
  51. transferArr := []int64{}
  52. for _, v := range this.User {
  53. i_entuserid := v
  54. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  55. Id: i_entuserid,
  56. })
  57. if err != nil {
  58. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  59. return 0
  60. } else if resp == nil {
  61. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  62. return 0
  63. }
  64. transferArr = append(transferArr, resp.PositionId)
  65. }
  66. //客户
  67. 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)
  68. //任务
  69. argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  70. if !SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, transferArr) {
  71. return 0
  72. }
  73. } else if this.Types == 1 {
  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, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  78. //任务团队
  79. argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime)
  80. //存库
  81. if !SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, []int64{}) {
  82. return 0
  83. }
  84. }
  85. return 1
  86. }
  87. //
  88. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  89. 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"}
  90. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  91. return id
  92. }
  93. //EmployCustomUpdate 是否创建客户修改
  94. func CustomUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64) bool {
  95. tablename := ""
  96. id := int64(0)
  97. if employ_custom_id > 0 {
  98. tablename = entity.EMPLOY_CUSTOM
  99. id = employ_custom_id
  100. } else if employ_info_id > 0 {
  101. tablename = entity.EMPLOY_INFO
  102. id = employ_info_id
  103. }
  104. if tablename == "" || id == 0 {
  105. return true
  106. }
  107. return cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{"is_create_custom": 1})
  108. }
  109. func SaveCustom(ctx context.Context, argsCustom, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string, transferArr []int64) bool {
  110. //存库
  111. return cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  112. //插入客户
  113. customId := SaleCustomAdd(tx, argsCustom)
  114. //传过来的argTask没有来源id,需要append
  115. argsTask = append(argsTask, customId)
  116. //任务车存储
  117. taskId := TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId)
  118. ok := CustomUpdate(tx, employ_info_id, employ_custom_id, positionId)
  119. //插入台账
  120. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  121. if customId > 0 && taskId > 0 && ok && ok2 {
  122. return true
  123. }
  124. log.Println("SaveCustom err:", customId, taskId, ok)
  125. return false
  126. })
  127. }
  128. //SaveLedger 操作台帐相关
  129. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  130. //操作台账
  131. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  132. PositionId: positionId,
  133. BusinessId: businessId, //业务id
  134. TaskId: taskId, //任务id
  135. Types: types, //类型
  136. Content: content, //内容
  137. CreateWay: 1, //创建方式 1:人 2:系统
  138. CreatePerson: createPerson,
  139. })
  140. if err != nil {
  141. log.Println("save ledger err:", err)
  142. return false
  143. }
  144. if resp == nil {
  145. log.Println("save ledger resp is nil")
  146. return false
  147. }
  148. return resp.State
  149. }