clueByPhone.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. } else if buy_subject == 2 {
  150. entId = common.InterfaceToStr((*orderInfo)["ent_id"])
  151. }
  152. productMap := map[string]int{
  153. "dhy6": 1,
  154. "dhy7": 2,
  155. "dhy3": 4,
  156. "dhy1": 5,
  157. "dhy2": 6,
  158. "dhy5": 7,
  159. "企业商机管理": 8,
  160. }
  161. if product_type == "企业商机管理" {
  162. product = productMap[product_type]
  163. } else {
  164. if data_spec == "dhy6" {
  165. product = 1
  166. filter := common.ObjToString((*orderInfo)["filter"])
  167. filterMap := map[string]interface{}{}
  168. json.Unmarshal([]byte(filter), &filterMap)
  169. if len(filterMap) > 0 {
  170. areaCount := common.IntAll(filterMap["areaCount"])
  171. if areaCount == 1 {
  172. product = 3
  173. }
  174. }
  175. } else {
  176. product = productMap[data_spec]
  177. }
  178. }
  179. clueData := entity.JyBiTidb.FindOne("dwd_f_crm_clue_info", bson.M{"uid": uId}, "", "")
  180. if clueData != nil && len(*clueData) > 0 {
  181. clueId := (*clueData)["id"]
  182. userName := common.ObjToString((*clueData)["name"])
  183. nowTime := time.Now().Format("2006-01-02 15:04:05")
  184. csmdata := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"clue_id": clueId}, "", "")
  185. if csmdata != nil && len(*csmdata) > 0 {
  186. if common.IntAll((*csmdata)["is_transfer"]) == 0 {
  187. return &pb.BiReply{
  188. ErrorCode: 0,
  189. ErrorMsg: "线索已经移交至客成",
  190. Data: gconv.Bytes(bson.M{"id": clueId}),
  191. }, nil
  192. } else {
  193. entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{
  194. "is_transfer": 0,
  195. "is_renewal_protection": 0,
  196. "product_access": product,
  197. "buy_subject": buy_subject,
  198. "transfertime": nowTime,
  199. "service_starttime": starttime,
  200. "service_endtime": endtime,
  201. "ent_id": entId,
  202. "company_name": company_name,
  203. })
  204. entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  205. "clue_id": clueId,
  206. "position_id": (*csmdata)["position_id"],
  207. "change_type": "成交客户移交",
  208. "new_value": "移交至客户成功组",
  209. "createtime": nowTime,
  210. "BCPCID": common.GetRandom(32),
  211. "operator_id": -1,
  212. "change_reason": "根据手机号或者订单号移交线索(接口)",
  213. })
  214. entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  215. return &pb.BiReply{
  216. ErrorCode: 0,
  217. ErrorMsg: "线索移交成功",
  218. Data: gconv.Bytes(bson.M{"id": clueId, "isExpired": isExpired}),
  219. }, nil
  220. }
  221. } else {
  222. saveMap := map[string]interface{}{
  223. "clue_id": clueId,
  224. "transfertime": nowTime,
  225. //"position_id": in.PositionId,
  226. "name": "",
  227. "service_starttime": starttime,
  228. "service_endtime": endtime,
  229. "ent_id": entId,
  230. "is_task": 1,
  231. "tasktime": nowTime,
  232. "taskstatus": 0,
  233. "tasksource": "1",
  234. "is_admin": 1,
  235. "product_access": product,
  236. "buy_subject": buy_subject,
  237. "relationship_building_way": 1,
  238. "inventory_way": 1,
  239. "training_way": 1,
  240. "is_pre_sales_training": 0,
  241. "service_stage": 1,
  242. "company_name": company_name,
  243. }
  244. cId, ok, updateId1 := int64(-1), false, int64(-1)
  245. if entity.JyBiTidb.ExecTx("保存客户", func(tx *sql.Tx) bool {
  246. cId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
  247. 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})
  248. updateId1 = entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  249. "clue_id": clueId,
  250. "position_id": 0,
  251. "change_type": "成交客户移交",
  252. "new_value": "移交至客户成功组",
  253. "createtime": nowTime,
  254. "BCPCID": common.GetRandom(32),
  255. "operator_id": -1,
  256. "change_reason": "根据手机号或者订单号移交线索(接口)",
  257. })
  258. return cId > -1 && ok && updateId1 > -1
  259. }) {
  260. return &pb.BiReply{
  261. ErrorCode: 0,
  262. ErrorMsg: "线索移交成功",
  263. Data: gconv.Bytes(bson.M{"id": clueId, "isExpired": isExpired}),
  264. }, nil
  265. } else {
  266. return &pb.BiReply{
  267. ErrorCode: -1,
  268. ErrorMsg: "线索移交失败",
  269. Data: nil,
  270. }, nil
  271. }
  272. }
  273. } else {
  274. return &pb.BiReply{
  275. ErrorCode: -1,
  276. ErrorMsg: "线索查询失败",
  277. Data: nil,
  278. }, nil
  279. }
  280. }