custom.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "app.yhyue.com/moapp/jybase/encrypt"
  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 []string //转办用户
  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. args := []interface{}{}
  48. argsTask := []interface{}{}
  49. //判断处理方式
  50. //转办
  51. if this.Types == 2 {
  52. for _, v := range this.User {
  53. entuserid := encrypt.SE.Decode4Hex(v)
  54. i_entuserid := gconv.Int64(entuserid)
  55. resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{
  56. Id: i_entuserid,
  57. })
  58. if err != nil {
  59. log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err)
  60. return 0
  61. } else if resp == nil {
  62. log.Println("entuser用户", i_entuserid, "没有找到职位信息")
  63. return 0
  64. }
  65. positionid := resp.PositionId
  66. //线索
  67. //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)
  68. //任务
  69. //argsTask = append(argsTask, positionid, this.EntId, this.EntUserId, this.ClueName+"的跟进任务", this.CluesSource, this.EmployInfoId, positionid, 1, this.PositionId, nowtime, 1)
  70. if !SaveCustom(ctx, args, argsTask, this.EmployInfoId, this.EmployCustomId, positionid, this.CreateName) {
  71. return 0
  72. }
  73. }
  74. } else if this.Types == 1 {
  75. //客户
  76. 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)
  77. //任务
  78. argsTask = append(argsTask, this.PositionId, this.EntId, this.EntUserId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime))
  79. //存库
  80. if !SaveCustom(ctx, args, argsTask, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName) {
  81. return 0
  82. }
  83. }
  84. return 1
  85. }
  86. //
  87. func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 {
  88. 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"}
  89. _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args)
  90. return id
  91. }
  92. //EmployCustomUpdate 是否创建客户修改
  93. func CustomUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64) bool {
  94. tablename := ""
  95. id := int64(0)
  96. if employ_custom_id > 0 {
  97. tablename = entity.EMPLOY_CUSTOM
  98. id = employ_custom_id
  99. } else if employ_info_id > 0 {
  100. tablename = entity.EMPLOY_INFO
  101. id = employ_info_id
  102. }
  103. if tablename == "" || id == 0 {
  104. return true
  105. }
  106. return cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{"is_create_custom": 1})
  107. }
  108. func SaveCustom(ctx context.Context, argsCustom, argsTask []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string) bool {
  109. //存库
  110. return cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool {
  111. //插入客户
  112. customId := SaleCustomAdd(tx, argsCustom)
  113. //传过来的argTask没有来源id,需要append
  114. argsTask = append(argsTask, customId)
  115. //任务车存储
  116. taskId := TaskAdd(tx, argsTask)
  117. ok := CustomUpdate(tx, employ_info_id, employ_custom_id, positionId)
  118. //插入台账
  119. ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson)
  120. if customId > 0 && taskId > 0 && ok && ok2 {
  121. return true
  122. }
  123. log.Println("SaveCustom err:", customId, taskId, ok)
  124. return false
  125. })
  126. }
  127. //SaveLedger 操作台帐相关
  128. func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool {
  129. //操作台账
  130. resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{
  131. PositionId: positionId,
  132. BusinessId: businessId, //业务id
  133. TaskId: taskId, //任务id
  134. Types: types, //类型
  135. Content: content, //内容
  136. CreateWay: 1, //创建方式 1:人 2:系统
  137. CreatePerson: createPerson,
  138. })
  139. if err != nil {
  140. log.Println("save ledger err:", err)
  141. return false
  142. }
  143. if resp == nil {
  144. log.Println("save ledger resp is nil")
  145. return false
  146. }
  147. return resp.State
  148. }