custom.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  6. "database/sql"
  7. "errors"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "log"
  11. "strings"
  12. "time"
  13. )
  14. func RelatedCompanyIsCustom(companyName string) bool {
  15. if companyName == "" {
  16. return false
  17. }
  18. return entity.JyBiTidb.CountBySql("SELECT count(1) FROM dwd_f_csm_customer_info WHERE company_name = ? and account_type= 1 and is_transfer = 0", companyName) > 0
  19. }
  20. // 修改线索名称
  21. func ClueTransfer(clueId, changeType, kcType, mainClueId int64, clueName string) (status int, err error) {
  22. if changeType == 1 { //电销中修改线索名称
  23. status, err = DxUpdateClueName(clueId, clueName)
  24. } else { //客成修改名称
  25. if kcType == 1 { //回电销
  26. status, err = ClueTransferDx(clueId, mainClueId)
  27. } else { //划转到关联客户客成
  28. status, err = ClueTransferKc(clueId, clueName)
  29. }
  30. }
  31. return status, err
  32. }
  33. func DxUpdateClueName(clueId int64, clueName string) (status int, err error) {
  34. //查询关联客户客成信息
  35. relatedData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"company_name": clueName, "account_type": 1, "is_transfer": 0}, "id,position_id,name,ent_id,company_name", "")
  36. if relatedData == nil || len(*relatedData) == 0 {
  37. return -1, errors.New("未查询到关联客户信息")
  38. }
  39. positionId := common.Int64All((*relatedData)["position_id"])
  40. name := common.InterfaceToStr((*relatedData)["name"]) //客成经理名称
  41. entId := common.Int64All((*relatedData)["ent_id"])
  42. cusId := common.Int64All((*relatedData)["id"])
  43. nowTime := time.Now().Format("2006-01-02 15:04:05")
  44. //判断之前该线索是否移交过客成
  45. cusData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"clue_id": clueId}, "is_transfer", "")
  46. if cusData != nil && len(*cusData) > 0 { //该线索曾经移交过客成
  47. if common.IntAll((*cusData)["is_transfer"]) == 0 {
  48. return 1, nil
  49. } else {
  50. up1 := entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{
  51. "is_transfer": 0,
  52. "is_renewal_protection": 0,
  53. "transfertime": nowTime,
  54. "ent_id": entId,
  55. "company_name": clueName,
  56. "position_id": positionId,
  57. "name": name,
  58. "is_admin": 0,
  59. "primary_id": cusId,
  60. "account_type": 4,
  61. "is_task": 0,
  62. })
  63. in := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  64. "clue_id": clueId,
  65. "position_id": positionId,
  66. "change_type": "成交客户移交",
  67. "new_value": "移交至客户成功部",
  68. "createtime": nowTime,
  69. "BCPCID": common.GetRandom(32),
  70. "operator_id": -1,
  71. "change_reason": "与主账号线索名称保持一致",
  72. })
  73. up2 := entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime})
  74. if up1 && up2 && in > 0 {
  75. return 1, nil
  76. }
  77. return -1, errors.New("更新客成信息出错")
  78. }
  79. } else {
  80. saveMap := map[string]interface{}{
  81. "clue_id": clueId,
  82. "transfertime": nowTime,
  83. "position_id": positionId,
  84. "name": name,
  85. "ent_id": entId,
  86. "is_task": 0,
  87. "tasktime": nowTime,
  88. "taskstatus": 0,
  89. "tasksource": "1",
  90. "is_admin": 0,
  91. "relationship_building_way": 1,
  92. "inventory_way": 1,
  93. "training_way": 1,
  94. "is_pre_sales_training": 0,
  95. "service_stage": 1,
  96. "company_name": clueName,
  97. "primary_id": cusId,
  98. "account_type": 4,
  99. }
  100. cId, ok, updateId1 := int64(-1), false, int64(-1)
  101. if entity.JyBiTidb.ExecTx("保存客户", func(tx *sql.Tx) bool {
  102. cId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
  103. ok = entity.JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime})
  104. updateId1 = entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  105. "clue_id": clueId,
  106. "position_id": positionId,
  107. "change_type": "成交客户移交",
  108. "new_value": "移交至客户成功部",
  109. "createtime": nowTime,
  110. "BCPCID": common.GetRandom(32),
  111. "operator_id": -1,
  112. "change_reason": "与主账号线索名称保持一致",
  113. })
  114. return cId > -1 && ok && updateId1 > -1
  115. }) {
  116. return 1, nil
  117. }
  118. return -1, errors.New("移交客成失败")
  119. }
  120. }
  121. // 客成系统修改线索名称,移交电销
  122. func ClueTransferDx(clueId, mainClueId int64) (status int, err error) {
  123. //查询原主账号电销人员
  124. is_assign := 1
  125. status = 1
  126. positionId, kcPositionId, seatNumber, kcName, isFreeze := GetSalePerson(mainClueId)
  127. if isFreeze || positionId == 0 {
  128. is_assign = 0
  129. }
  130. nowTime := time.Now().Format(date.Date_Full_Layout)
  131. ok := entity.JyBiTidb.ExecTx("退出客成系统,进入电销公海", func(tx *sql.Tx) bool {
  132. updateClueMap := map[string]interface{}{
  133. "is_assign": is_assign,
  134. "updatetime": nowTime,
  135. "comeintime_open": nowTime,
  136. "position_id": positionId,
  137. "seatNumber": seatNumber,
  138. "is_transfer": 0,
  139. //"is_renewal_protection": 0,
  140. }
  141. ok1 := entity.JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClueMap)
  142. ok2 := entity.JyBiTidb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  143. "is_transfer": 1,
  144. "position_id": 0,
  145. "name": "",
  146. "account_type": 0,
  147. "primary_id": "",
  148. })
  149. in1 := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  150. "clue_id": clueId,
  151. "position_id": common.If(kcPositionId > 0, kcPositionId, -1),
  152. "change_type": "退回公海",
  153. "new_value": "不再是非集团公司成交客户关联线索,移交销售",
  154. "createtime": nowTime,
  155. "BCPCID": common.GetRandom(32),
  156. "operator_id": -1,
  157. })
  158. in2 := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  159. "clue_id": clueId,
  160. "position_id": common.If(kcPositionId > 0, kcPositionId, -1),
  161. "change_type": "移交销售",
  162. "new_value": "不再是非集团公司成交客户关联线索,移交销售",
  163. "createtime": nowTime,
  164. "BCPCID": common.GetRandom(32),
  165. "operator_id": -1,
  166. })
  167. in3 := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  168. "clue_id": clueId,
  169. "position_id": common.If(kcPositionId > 0, kcPositionId, -1),
  170. "change_field": "position_id",
  171. "change_type": "客户成功经理",
  172. "old_value": kcName,
  173. "new_value": "/",
  174. "createtime": nowTime,
  175. "BCPCID": common.GetRandom(32),
  176. "operator_id": -1,
  177. })
  178. return ok1 && ok2 && in1 > 0 && in2 > 0 && in3 > 0
  179. })
  180. if !ok {
  181. log.Printf("子账号或免费账号移交销售失败:clueId:%d\n", clueId)
  182. status = -1
  183. err = errors.New("子账号或免费账号移交销售失败")
  184. }
  185. return status, err
  186. }
  187. // 移交客成
  188. func ClueTransferKc(clueId int64, clueName string) (int, error) {
  189. mainKcData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"company_name": clueName, "is_transfer": 0, "is_admin": 1}, "", "")
  190. if mainKcData == nil || len(*mainKcData) == 0 {
  191. return -1, errors.New("未查询到关联客户")
  192. }
  193. nowTime := time.Now().Format(date.Date_Full_Layout)
  194. cusId := common.Int64All((*mainKcData)["id"])
  195. entId := common.Int64All((*mainKcData)["ent_id"])
  196. companyName := common.InterfaceToStr((*mainKcData)["company_name"])
  197. positionId := common.Int64All((*mainKcData)["position_id"])
  198. name := common.Int64All((*mainKcData)["name"])
  199. up1 := entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{
  200. "is_transfer": 0,
  201. "is_renewal_protection": 0,
  202. "transfertime": nowTime,
  203. "ent_id": entId,
  204. "company_name": companyName,
  205. "position_id": positionId,
  206. "name": name,
  207. "is_admin": 0,
  208. "primary_id": cusId,
  209. "account_type": 4,
  210. "is_task": 0,
  211. })
  212. in := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  213. "clue_id": clueId,
  214. "position_id": positionId,
  215. "change_type": "成交客户移交",
  216. "new_value": "移交至客户成功部",
  217. "createtime": nowTime,
  218. "BCPCID": common.GetRandom(32),
  219. "operator_id": -1,
  220. "change_reason": "与主账号线索名称保持一致",
  221. })
  222. up2 := entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime})
  223. if up1 && up2 && in > 0 {
  224. return 1, nil
  225. }
  226. return -1, errors.New("更新客成信息出错")
  227. }
  228. func GetSalePerson(mainClueId int64) (positionId, kcPosId int64, seatNumber, kcName string, isFreeze bool) {
  229. isFreeze = false
  230. clueData := entity.JyBiTidb.SelectBySql("SELECT a.position_id,a.seatNumber,b.position_id as kcPosId,b.name as kcName FROM dwd_f_crm_clue_info a LEFT JOIN dwd_f_csm_customer_info b ON a.id = b.clue_id WHERE a.id = ?", mainClueId)
  231. if clueData != nil && len(*clueData) > 0 {
  232. positionId = common.Int64All((*clueData)[0]["position_id"])
  233. seatNumber = common.InterfaceToStr((*clueData)[0]["seatNumber"])
  234. kcName = common.InterfaceToStr((*clueData)[0]["kcName"])
  235. kcPosId = common.Int64All((*clueData)[0]["kcPosId"])
  236. }
  237. pdata := entity.JyBiTidb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)
  238. if pdata == nil {
  239. positionId = 0
  240. seatNumber = ""
  241. return
  242. }
  243. saleData := []map[string]interface{}{}
  244. saleData = *pdata
  245. //(1)该销售人员未离职,则继续分配给该销售人员(保持现状);
  246. //该销售人员已离职,则随机分配
  247. for _, v := range saleData {
  248. resign := common.IntAll(v["resign"])
  249. assign_type := common.IntAll(v["assign_type"])
  250. role_id := common.IntAll(v["role_id"])
  251. if positionId == common.Int64All(v["position_id"]) {
  252. if resign == 0 && (assign_type == 1 || role_id == 8) {
  253. if FindUpperLimit(positionId) {
  254. isFreeze = true
  255. positionId = 0
  256. seatNumber = ""
  257. break
  258. } else {
  259. break
  260. }
  261. } else { //如果人离职了,放到离职销售人员所属部门的电销经理的私海里
  262. sdata := &[]map[string]interface{}{}
  263. userData := FindPersonOne(positionId)
  264. if userData != nil {
  265. deptName := gconv.String(userData["dept_name"])
  266. if strings.Contains(deptName, "一部") {
  267. sdata = entity.JyBiTidb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) and b.role_id = 3`, positionId)
  268. if len(*sdata) == 0 {
  269. sdata = entity.JyBiTidb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) and b.role_id = 8`, positionId)
  270. }
  271. } else if strings.Contains(deptName, "三部") {
  272. sdata = entity.JyBiTidb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) and b.role_id = 8`, positionId)
  273. }
  274. }
  275. if sdata != nil && len(*sdata) > 0 {
  276. positionId = common.Int64All((*sdata)[0]["position_id"])
  277. seatNumber = gconv.String((*sdata)[0]["seat_number"])
  278. //newPerson = gconv.String((*sdata)[0]["name"])
  279. }
  280. break
  281. }
  282. }
  283. }
  284. return
  285. }
  286. func FindPersonOne(positionId int64) map[string]interface{} {
  287. personData := entity.JyBiTidb.SelectBySql(`SELECT
  288. b.name,
  289. b.position_id,
  290. b.seat_number,
  291. b.role_id,
  292. a.dept_name
  293. FROM
  294. dwd_d_crm_department_level_succbi a
  295. INNER JOIN dwd_f_crm_personnel_management b ON a.position_id = ?
  296. AND a.position_id = b.position_id`, positionId)
  297. if personData == nil || len(*personData) == 0 {
  298. return nil
  299. }
  300. return (*personData)[0]
  301. }