custom.go 6.0 KB

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