clueByPhone.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb"
  7. "database/sql"
  8. "encoding/json"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "time"
  12. )
  13. // @Author jianghan
  14. // @Description 根据手机号查询线索, 线索不存在创建线索并插入记录
  15. // @Date 2024/5/28
  16. func CreateClue(in *pb.CreateCuleReq) (result *pb.BiReply, err error) {
  17. info := entity.JyBiTidb.FindOne("dwd_f_userbase_contacts", bson.M{"phone": in.Phone}, "id, baseinfo_id", "")
  18. if info == nil || len(*info) == 0 {
  19. return &pb.BiReply{
  20. ErrorCode: -1,
  21. ErrorMsg: "手机号不存在",
  22. Data: nil,
  23. }, nil
  24. }
  25. uId := common.ObjToString((*info)["baseinfo_id"])
  26. userId, userName := "", ""
  27. clueInfo := entity.JyBiTidb.FindOne("dwd_f_crm_clue_info", bson.M{"uid": uId}, "", "")
  28. if clueInfo != nil && len(*clueInfo) > 0 {
  29. return &pb.BiReply{
  30. ErrorCode: -1,
  31. ErrorMsg: "线索已经存在",
  32. Data: nil,
  33. }, nil
  34. }
  35. userInfo := entity.JyBiTidb.FindOne("dwd_f_userbase_baseinfo", bson.M{"phone": in.Phone}, "userid,name", "")
  36. if userInfo != nil && len(*userInfo) > 0 {
  37. userId = common.ObjToString((*userInfo)["userid"])
  38. userName = common.ObjToString((*userInfo)["name"])
  39. }
  40. isGroup, isCommerce := CompanyType(in.Phone) //判断是否集团公司、工商库
  41. clueId := int64(0)
  42. if entity.JyBiTidb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  43. nowTime := time.Now().Format("2006-01-02 15:04:05")
  44. clueId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  45. "userid": userId,
  46. "uid": uId,
  47. "is_assign": 0,
  48. "createtime": nowTime,
  49. "updatetime": nowTime,
  50. "cluename": in.Phone,
  51. "top_cluetype": "4",
  52. "sub_cluetype": "154",
  53. "trailstatus": "01",
  54. "name": userName,
  55. "phone": in.Phone,
  56. "company_nature": isGroup,
  57. "company_verification": isCommerce,
  58. "comeintime_open": nowTime,
  59. })
  60. uodateId1 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  61. "clue_id": clueId,
  62. "position_id": -1,
  63. "change_type": "创建线索",
  64. "new_value": "系统自动创建",
  65. "createtime": nowTime,
  66. "BCPCID": common.GetRandom(32),
  67. "operator_id": -1,
  68. "change_reason": "根据手机号创建线索(接口)",
  69. })
  70. uodateId2 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  71. "clue_id": clueId,
  72. "change_field": "top_cluetype",
  73. "position_id": -1,
  74. "change_type": "基本信息变更",
  75. "old_value": "/",
  76. "new_value": "新增注册",
  77. "createtime": nowTime,
  78. "BCPCID": common.GetRandom(32),
  79. "operator_id": -1,
  80. "change_reason": "根据手机号创建线索(接口)",
  81. })
  82. uodateId3 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  83. "clue_id": clueId,
  84. "change_field": "sub_cluetype",
  85. "position_id": 0,
  86. "change_type": "基本信息变更",
  87. "old_value": "/",
  88. "new_value": "新增注册用户",
  89. "createtime": nowTime,
  90. "BCPCID": common.GetRandom(32),
  91. "operator_id": -1,
  92. "change_reason": "根据手机号创建线索(接口)",
  93. })
  94. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1
  95. }) {
  96. return &pb.BiReply{
  97. ErrorCode: 0,
  98. ErrorMsg: "线索创建成功",
  99. Data: gconv.Bytes(bson.M{"id": clueId}),
  100. }, nil
  101. } else {
  102. return &pb.BiReply{
  103. ErrorCode: -1,
  104. ErrorMsg: "线索创建失败",
  105. Data: nil,
  106. }, nil
  107. }
  108. }
  109. // @Author jianghan
  110. // @Description 客成移交线索
  111. // @Date 2024/5/28
  112. func TransferClue(in *pb.TransferClueReq) (result *pb.BiReply, err error) {
  113. uId, entId, product := "", "", 0
  114. query := make(map[string]interface{})
  115. if in.OrderNo != "" {
  116. query["order_code"] = in.OrderNo
  117. } else if in.Phone != "" {
  118. query["phone"] = in.Phone
  119. }
  120. orderInfo := entity.JyBiTidb.FindOne("dwd_f_userbase_order_info", query, "", "")
  121. if orderInfo == nil || len(*orderInfo) == 0 {
  122. return &pb.BiReply{
  123. ErrorCode: -1,
  124. ErrorMsg: "订单编号或手机号不存在",
  125. Data: nil,
  126. }, nil
  127. }
  128. uId = common.ObjToString((*orderInfo)["uid"])
  129. company_name := common.ObjToString((*orderInfo)["company_name"])
  130. starttime := common.ObjToString((*orderInfo)["vip_starttime"])
  131. endtime := common.ObjToString((*orderInfo)["vip_endtime"])
  132. t, _ := time.Parse(date.Date_Full_Layout, endtime)
  133. isExpired := false // 过期订单
  134. if time.Now().Unix() > t.Unix() {
  135. return &pb.BiReply{
  136. ErrorCode: 1,
  137. ErrorMsg: "订单已经过期",
  138. Data: nil,
  139. }, nil
  140. }
  141. if t.Unix() < time.Now().Unix()+3*30*24*3600 && t.Unix() > time.Now().Unix() {
  142. isExpired = true
  143. }
  144. buy_subject := common.IntAll((*orderInfo)["buy_subject"])
  145. product_type := common.ObjToString((*orderInfo)["product_type"])
  146. data_spec := common.ObjToString((*orderInfo)["data_spec"])
  147. if buy_subject == 1 || buy_subject == 0 {
  148. entId = common.ObjToString((*orderInfo)["userid"])
  149. }
  150. productMap := map[string]int{
  151. "dhy6": 1,
  152. "dhy7": 2,
  153. "dhy3": 4,
  154. "dhy1": 5,
  155. "dhy2": 6,
  156. "dhy5": 7,
  157. "企业商机管理": 8,
  158. }
  159. if product_type == "企业商机管理" {
  160. product = productMap[product_type]
  161. } else {
  162. if data_spec == "dhy6" {
  163. product = 1
  164. filter := common.ObjToString((*orderInfo)["filter"])
  165. filterMap := map[string]interface{}{}
  166. json.Unmarshal([]byte(filter), &filterMap)
  167. if len(filterMap) > 0 {
  168. areaCount := common.IntAll(filterMap["areaCount"])
  169. if areaCount == 1 {
  170. product = 3
  171. }
  172. }
  173. } else {
  174. product = productMap[data_spec]
  175. }
  176. }
  177. clueData := entity.JyBiTidb.FindOne("dwd_f_crm_clue_info", bson.M{"uid": uId}, "", "")
  178. if clueData != nil && len(*clueData) > 0 {
  179. clueId := (*clueData)["id"]
  180. userName := common.ObjToString((*clueData)["name"])
  181. nowTime := time.Now().Format("2006-01-02 15:04:05")
  182. csmdata := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"clue_id": clueId}, "", "")
  183. if csmdata != nil && len(*csmdata) > 0 {
  184. if common.IntAll((*csmdata)["is_transfer"]) == 0 {
  185. return &pb.BiReply{
  186. ErrorCode: 0,
  187. ErrorMsg: "线索已经移交至客成",
  188. Data: gconv.Bytes(bson.M{"id": clueId}),
  189. }, nil
  190. } else {
  191. entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{
  192. "is_transfer": 0,
  193. "is_renewal_protection": 0,
  194. "product_access": product,
  195. "buy_subject": buy_subject,
  196. "transfertime": nowTime,
  197. "service_starttime": starttime,
  198. "service_endtime": endtime,
  199. "ent_id": entId,
  200. "company_name": company_name,
  201. })
  202. entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  203. "clue_id": clueId,
  204. "position_id": (*csmdata)["position_id"],
  205. "change_type": "成交客户移交",
  206. "new_value": "移交至客户成功组",
  207. "createtime": nowTime,
  208. "BCPCID": common.GetRandom(32),
  209. "operator_id": -1,
  210. "change_reason": "根据手机号或者订单号移交线索(接口)",
  211. })
  212. entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  213. return &pb.BiReply{
  214. ErrorCode: 0,
  215. ErrorMsg: "线索移交成功",
  216. Data: gconv.Bytes(bson.M{"id": clueId, "isExpired": isExpired}),
  217. }, nil
  218. }
  219. } else {
  220. saveMap := map[string]interface{}{
  221. "clue_id": clueId,
  222. "transfertime": nowTime,
  223. "position_id": in.PositionId,
  224. "name": "",
  225. "service_starttime": starttime,
  226. "service_endtime": endtime,
  227. "ent_id": entId,
  228. "is_task": 1,
  229. "tasktime": nowTime,
  230. "taskstatus": 0,
  231. "tasksource": "1",
  232. "is_admin": 1,
  233. "product_access": product,
  234. "buy_subject": buy_subject,
  235. "relationship_building_way": 1,
  236. "inventory_way": 1,
  237. "training_way": 1,
  238. "is_pre_sales_training": 0,
  239. "service_stage": 1,
  240. "company_name": company_name,
  241. }
  242. cId, ok, updateId1 := int64(-1), false, int64(-1)
  243. if entity.JyBiTidb.ExecTx("保存客户", func(tx *sql.Tx) bool {
  244. cId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
  245. ok = entity.JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  246. updateId1 = entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  247. "clue_id": clueId,
  248. "position_id": in.PositionId,
  249. "change_type": "成交客户移交",
  250. "new_value": "移交至客户成功组",
  251. "createtime": nowTime,
  252. "BCPCID": common.GetRandom(32),
  253. "operator_id": -1,
  254. "change_reason": "根据手机号或者订单号移交线索(接口)",
  255. })
  256. return cId > -1 && ok && updateId1 > -1
  257. }) {
  258. return &pb.BiReply{
  259. ErrorCode: 0,
  260. ErrorMsg: "线索移交成功",
  261. Data: gconv.Bytes(bson.M{"id": clueId, "isExpired": isExpired}),
  262. }, nil
  263. } else {
  264. return &pb.BiReply{
  265. ErrorCode: -1,
  266. ErrorMsg: "线索移交失败",
  267. Data: nil,
  268. }, nil
  269. }
  270. }
  271. } else {
  272. return &pb.BiReply{
  273. ErrorCode: -1,
  274. ErrorMsg: "线索查询失败",
  275. Data: nil,
  276. }, nil
  277. }
  278. }