kc.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "strings"
  8. "time"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/date"
  12. "app.yhyue.com/moapp/jybase/mongodb"
  13. "app.yhyue.com/moapp/jybase/redis"
  14. )
  15. func kcSync() {
  16. log.Println("客户成功系统移交定时任务开始")
  17. sql := `select * from dwd_f_userbase_order_info where payable_money > 0 and vip_endtime > "` + time.Now().Format(date.Date_Full_Layout) + `" and autoUpdate > "` + cfg.LastkcTime + `" order by autoUpdate asc`
  18. data := TiDb.SelectBySql(sql)
  19. if data != nil && *data != nil && len(*data) > 0 {
  20. for _, v := range *data {
  21. product_type := common.ObjToString(v["product_type"])
  22. data_spec := common.ObjToString(v["data_spec"])
  23. starttime := common.ObjToString(v["vip_starttime"])
  24. order_status := common.IntAll(v["order_status"])
  25. refund_status := common.IntAll(v["refund_status"])
  26. if (product_type == "大会员" || product_type == "企业商机管理") && order_status == 1 && data_spec != "dhy4" && !strings.HasPrefix(starttime, "2099") && refund_status != 1 {
  27. status := kcJob(v)
  28. if status == 0 {
  29. break
  30. }
  31. }
  32. cfg.LastkcTime = common.ObjToString(v["autoUpdate"])
  33. }
  34. }
  35. common.WriteSysConfig(&cfg)
  36. log.Println("客户成功系统移交定时任务结束")
  37. }
  38. func kcJob(data map[string]interface{}) int {
  39. nowTime := time.Now().Format(date.Date_Full_Layout)
  40. uId, entId, clueId, saveMap, name, positionId, status := common.ObjToString(data["uid"]), fmt.Sprint(data["ent_id"]), int64(0), map[string]interface{}{}, "", int64(0), 1
  41. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  42. if clueData != nil && len(*clueData) > 0 {
  43. clueId = common.Int64All((*clueData)["id"])
  44. starttime := common.ObjToString(data["vip_starttime"])
  45. endtime := common.ObjToString(data["vip_endtime"])
  46. // user_role := common.IntAll(data["user_role"])
  47. buy_subject := common.IntAll(data["buy_subject"])
  48. product_type := common.ObjToString(data["product_type"])
  49. data_spec := common.ObjToString(data["data_spec"])
  50. userName := common.ObjToString((*clueData)["name"])
  51. //phone := common.ObjToString((*clueData)["phone"])
  52. product, company_name := 0, common.ObjToString(data["company_name"])
  53. productMap := map[string]int{
  54. "dhy6": 1,
  55. "dhy7": 2,
  56. "dhy3": 4,
  57. "dhy1": 5,
  58. "dhy2": 6,
  59. "dhy5": 7,
  60. "企业商机管理": 8,
  61. }
  62. if buy_subject == 1 || buy_subject == 0 {
  63. entId = common.ObjToString(data["userid"])
  64. }
  65. if product_type == "企业商机管理" {
  66. product = productMap[product_type]
  67. powerData := TiDb.FindOne("dwd_f_data_equity_info", map[string]interface{}{"uid": uId, "product_type": "商机管理"}, "", "comeintime desc")
  68. if powerData != nil {
  69. starttime = common.ObjToString((*powerData)["starttime"])
  70. endtime = common.ObjToString((*powerData)["endtime"])
  71. } else {
  72. log.Println("客成移交权限未查到--", uId)
  73. return 0
  74. }
  75. } else {
  76. if data_spec == "dhy6" {
  77. product = 1
  78. filter := common.ObjToString(data["filter"])
  79. filterMap := map[string]interface{}{}
  80. json.Unmarshal([]byte(filter), &filterMap)
  81. if len(filterMap) > 0 {
  82. areaCount := common.IntAll(filterMap["areaCount"])
  83. if areaCount == 1 {
  84. product = 3
  85. }
  86. }
  87. } else {
  88. product = productMap[data_spec]
  89. }
  90. }
  91. //同一公司名称(以客户详情-组织机构-公司名称)下的线索需分配给同1人
  92. //合力亿捷线索同步
  93. /*customerData := TiDbData.FindOne("customer", map[string]interface{}{"phone": phone}, "", "")
  94. if customerData != nil {
  95. log.Println("移交客成查询到合力亿捷")
  96. status999 := common.ObjToString((*customerData)["status999"]) //线索状态
  97. source := common.ObjToString((*customerData)["source"]) //销售来源
  98. customerNeeds := common.ObjToString((*customerData)["customerNeeds"]) //客户需求
  99. wantGoods := common.ObjToString((*customerData)["wantGoods"]) //意向产品
  100. job := common.ObjToString((*customerData)["job"]) //职位
  101. createTime := common.ObjToString((*customerData)["createTime"]) //创建时间
  102. lastUpdateTime := common.ObjToString((*customerData)["lastUpdateTime"]) //更新时间
  103. area := common.ObjToString((*customerData)["area"]) //关注区域
  104. customerBudget := common.ObjToString((*customerData)["customerBudget"]) //客户预算
  105. isPolicymaker := common.ObjToString((*customerData)["customerBudget"]) //是否为决策人
  106. belongToIndustry := common.ObjToString((*customerData)["belongToIndustry"]) //所属行业
  107. seatNumber := common.ObjToString((*customerData)["empNo"]) //坐席号
  108. statusMap := map[string]string{
  109. "status0": "08",
  110. "status1": "07",
  111. "status2": "06",
  112. "status3": "05",
  113. "status4": "04",
  114. "status6": "00",
  115. "status5": "01",
  116. "spaceNumber": "02",
  117. "phoneDown": "02",
  118. }
  119. if status999 != "" {
  120. status999 = statusMap[status999]
  121. } else {
  122. status999 = "01"
  123. }
  124. top_cluetype := "172"
  125. sub_cluetype := ""
  126. cluetypeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"name": "其他-" + source}, "", "")
  127. if cluetypeData != nil {
  128. sub_cluetype = common.ObjToString((*cluetypeData)["code"])
  129. }
  130. wantGoods = strings.ReplaceAll(wantGoods, "剑鱼大会员", "大会员")
  131. wantGoods = strings.ReplaceAll(wantGoods, "数据导出", "数据流量包")
  132. if isPolicymaker == "是" {
  133. isPolicymaker = "决策人"
  134. } else {
  135. isPolicymaker = "使用人"
  136. }
  137. areaCodeArr := []string{}
  138. areaCode := ""
  139. for _, v := range strings.Split(area, ",") {
  140. areaCodeArr = append(areaCodeArr, AreaCode[v])
  141. }
  142. if len(areaCodeArr) > 0 {
  143. areaCode = strings.Join(areaCodeArr, ",")
  144. }
  145. SalePositionId := getSeatNumberPositionId(seatNumber)
  146. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, map[string]interface{}{
  147. "position_id": SalePositionId,
  148. "seatNumber": seatNumber,
  149. "is_assign": -2,
  150. "createtime": createTime,
  151. "updatetime": lastUpdateTime,
  152. "top_cluetype": top_cluetype,
  153. "sub_cluetype": sub_cluetype,
  154. "customer_demand": customerNeeds,
  155. "intended_products": wantGoods,
  156. "customer_budget": customerBudget,
  157. "contact_type": isPolicymaker,
  158. "position": job,
  159. "industry": belongToIndustry,
  160. "follow_project_area": areaCode,
  161. })
  162. }*/
  163. //
  164. entIds := common.IntAll(data["ent_id"])
  165. positionId, name = cAutoDraw(entIds)
  166. log.Println("移交客成positionId", positionId, name)
  167. saveMap = map[string]interface{}{
  168. "clue_id": clueId,
  169. "transfertime": nowTime,
  170. "position_id": positionId,
  171. "name": name,
  172. "service_starttime": starttime,
  173. "service_endtime": endtime,
  174. "ent_id": entId,
  175. "is_task": 1,
  176. "tasktime": nowTime,
  177. "taskstatus": 0,
  178. "tasksource": "1",
  179. "is_admin": 1,
  180. "product_access": product,
  181. "buy_subject": buy_subject,
  182. "relationship_building_way": 1,
  183. "inventory_way": 1,
  184. "training_way": 1,
  185. "is_pre_sales_training": 0,
  186. "service_stage": 1,
  187. "company_name": company_name,
  188. }
  189. if TiDb.Count("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}) > 0 {
  190. csmdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
  191. if csmdata != nil && len(*csmdata) > 0 {
  192. TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  193. "is_transfer": 0,
  194. "is_renewal_protection": 0,
  195. "product_access": product,
  196. "buy_subject": buy_subject,
  197. "transfertime": nowTime,
  198. "service_starttime": starttime,
  199. "service_endtime": endtime,
  200. "ent_id": entId,
  201. "company_name": company_name,
  202. })
  203. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  204. "clue_id": clueId,
  205. "position_id": (*csmdata)["position_id"],
  206. "change_type": "成交客户移交",
  207. "new_value": "移交至客户成功组",
  208. "createtime": nowTime,
  209. "BCPCID": common.GetRandom(32),
  210. "operator_id": -1,
  211. })
  212. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  213. "clue_id": clueId,
  214. "position_id": (*csmdata)["position_id"],
  215. "change_field": "position_id",
  216. "change_type": "客户成功经理",
  217. "old_value": "/",
  218. "new_value": (*csmdata)["name"],
  219. "createtime": nowTime,
  220. "BCPCID": common.GetRandom(32),
  221. "operator_id": -1,
  222. })
  223. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  224. }
  225. } else {
  226. cId, ok, updateId1, updateId2, updateId3 := int64(-1), false, int64(-1), int64(-1), int64(-1)
  227. if TiDb.ExecTx("保存客户", func(tx *sql.Tx) bool {
  228. cId = TiDb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
  229. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  230. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  231. "clue_id": clueId,
  232. "position_id": positionId,
  233. "change_type": "加入任务车",
  234. "new_value": "未建联",
  235. "createtime": nowTime,
  236. "BCPCID": common.GetRandom(32),
  237. "operator_id": -1,
  238. })
  239. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  240. "clue_id": clueId,
  241. "position_id": positionId,
  242. "change_type": "成交客户移交",
  243. "new_value": "移交至客户成功组",
  244. "createtime": nowTime,
  245. "BCPCID": common.GetRandom(32),
  246. "operator_id": -1,
  247. })
  248. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  249. "clue_id": clueId,
  250. "position_id": positionId,
  251. "change_field": "position_id",
  252. "change_type": "客户成功经理",
  253. "old_value": "/",
  254. "new_value": name,
  255. "createtime": nowTime,
  256. "BCPCID": common.GetRandom(32),
  257. "operator_id": -1,
  258. })
  259. return cId > -1 && ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1
  260. }) {
  261. TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, name)
  262. log.Println("保存客户成功")
  263. } else {
  264. log.Println("保存客户失败!!!", clueId, cId, ok, updateId1, updateId2, updateId3, " 用户信息 ", name, positionId, uId)
  265. }
  266. }
  267. return status
  268. } else {
  269. log.Println("客户未查到线索!!!", uId)
  270. if isExists, _ := redis.Exists("bidx", "bidx_uId_"+uId); isExists {
  271. redisInt := redis.GetInt("bidx", "bidx_uId_"+uId)
  272. if redisInt > 2 {
  273. return 1
  274. } else {
  275. redis.Incr("bidx", "bidx_uId_"+uId)
  276. return 0
  277. }
  278. } else {
  279. redis.Put("bidx", "bidx_uId_"+uId, 1, 3600)
  280. return 0
  281. }
  282. }
  283. }
  284. func cAutoDraw(entId int) (positionId int64, name string) {
  285. query := `SELECT name,position_id FROM dwd_d_crm_department_level_succbi WHERE bi_pcode = (SELECT bi_code FROM dwd_d_crm_department_level_succbi WHERE name = "客户成功组" and resign=0) and resign = 0`
  286. data := TiDb.SelectBySql(query)
  287. if data != nil && len(*data) > 0 {
  288. if entId > 0 {
  289. entdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"ent_id": entId}, "", "")
  290. if entdata != nil && len(*entdata) > 0 {
  291. positionId = common.Int64All((*entdata)["position_id"])
  292. name = common.ObjToString((*entdata)["name"])
  293. return
  294. }
  295. }
  296. sql := `select a.name,a.count from dwd_f_csm_customer_autodraw_record a INNER JOIN dwd_d_crm_department_level_succbi b on (a.name = b.name) where b.resign = 0`
  297. countData := TiDb.SelectBySql(sql)
  298. if countData != nil && len(*countData) > 0 {
  299. for _, v := range *data {
  300. //判断是否有新员工
  301. isOk := false
  302. for _, vv := range *countData {
  303. if common.ObjToString(v["name"]) == common.ObjToString(vv["name"]) {
  304. isOk = true
  305. }
  306. }
  307. //有新员工直接分给新员工
  308. if !isOk {
  309. name = common.ObjToString(v["name"])
  310. rData := TiDb.FindOne("dwd_f_csm_customer_autodraw_record", map[string]interface{}{}, "", "count desc")
  311. TiDb.Insert("dwd_f_csm_customer_autodraw_record", map[string]interface{}{
  312. "name": name,
  313. "count": common.Int64All((*rData)["count"]),
  314. })
  315. break
  316. }
  317. }
  318. res := int64(0)
  319. countres := 0
  320. for _, v := range *countData {
  321. if countres == 0 {
  322. res = common.Int64All(v["count"])
  323. name = common.ObjToString(v["name"])
  324. } else {
  325. if common.Int64All(v["count"]) <= res {
  326. res = common.Int64All(v["count"])
  327. name = common.ObjToString(v["name"])
  328. }
  329. }
  330. countres++
  331. }
  332. }
  333. for _, v := range *data {
  334. if name == common.ObjToString(v["name"]) {
  335. positionId = common.Int64All(v["position_id"])
  336. }
  337. }
  338. }
  339. return
  340. }
  341. func kcAuto() {
  342. log.Println("客户成功系统自动进入任务车定时任务开始")
  343. nowTime := time.Now().Format(date.Date_Full_Layout)
  344. sql := `select * from dwd_f_csm_customer_info where ((relationship_building_way != 1 and inventory_way = 1) or (inventory_way != 1 and training_way = 1)) and is_task = 0`
  345. data := TiDb.SelectBySql(sql)
  346. if data != nil && *data != nil && len(*data) > 0 {
  347. for _, v := range *data {
  348. relationship_building_way := common.IntAll(v["relationship_building_way"])
  349. inventory_way := common.IntAll(v["inventory_way"])
  350. training_way := common.IntAll(v["training_way"])
  351. positionId := common.Int64All(v["position_id"])
  352. clueId := common.Int64All(v["clue_id"])
  353. tasksource, tasksources := common.ObjToString(v["tasksource"]), ""
  354. taskstatus := common.IntAll(v["taskstatus"])
  355. if relationship_building_way != 1 && inventory_way == 1 {
  356. tasksources = "2"
  357. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  358. "clue_id": clueId,
  359. "position_id": positionId,
  360. "change_type": "加入任务车",
  361. "new_value": "未盘点",
  362. "createtime": nowTime,
  363. "BCPCID": common.GetRandom(32),
  364. "operator_id": -1,
  365. })
  366. } else if inventory_way != 1 && training_way == 1 {
  367. tasksources = "3"
  368. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  369. "clue_id": clueId,
  370. "position_id": positionId,
  371. "change_type": "加入任务车",
  372. "new_value": "未培训",
  373. "createtime": nowTime,
  374. "BCPCID": common.GetRandom(32),
  375. "operator_id": -1,
  376. })
  377. }
  378. if taskstatus == 1 {
  379. tasksource = tasksources
  380. } else {
  381. if tasksource != "" {
  382. if !strings.Contains(tasksource, tasksources) {
  383. tasksource += "," + tasksources
  384. }
  385. } else {
  386. tasksource = tasksources
  387. }
  388. }
  389. TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  390. "tasksource": tasksource,
  391. "tasktime": nowTime,
  392. "is_task": 1,
  393. "taskstatus": 0,
  394. })
  395. }
  396. }
  397. log.Println("客户成功系统进入任务车定时任务结束")
  398. }
  399. func ordersClue() {
  400. log.Println("后台订单线索定时任务开始")
  401. lastOrderClueId := cfg.LastOrderClueId
  402. nowTime := time.Now().Format(date.Date_Full_Layout)
  403. sql := fmt.Sprintf(`select order_code,order_status,is_backstage_order,pay_money,saleDep,salesperson,user_phone,create_person,saleMoney,user_id,product_type,company_name,autoUpdate from dataexport_order where autoUpdate >= "%s" order by autoUpdate asc`, lastOrderClueId)
  404. data := Mysql.SelectBySql(sql)
  405. if data != nil && *data != nil && len(*data) > 0 {
  406. for _, v := range *data {
  407. order_status := common.IntAll(v["order_status"])
  408. is_backstage_order := common.IntAll(v["is_backstage_order"])
  409. pay_money := common.IntAll(v["pay_money"])
  410. orderCode := gconv.String(v["order_code"])
  411. saleDep, _, salesperson := FindSaleRecord(orderCode)
  412. user_phone := common.ObjToString(v["user_phone"])
  413. createperson := common.ObjToString(v["create_person"])
  414. userId, uId, cluename, seatNumber, positionId, trailstatus, clueId := common.ObjToString(v["user_id"]), "", "", "", int64(0), "", int64(0)
  415. product_type_str2 := `"大会员","企业商机管理","VIP订阅"`
  416. product_type := common.ObjToString(v["product_type"])
  417. if order_status == 1 && is_backstage_order == 1 && saleDep != "" && !strings.HasPrefix(user_phone, "9") && strings.Contains(product_type_str2, product_type) {
  418. //销售部
  419. query := map[string]interface{}{}
  420. source := ""
  421. if product_type == "企业商机管理" {
  422. query["phone"] = user_phone
  423. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  424. if userInfo != nil && len(*userInfo) > 0 {
  425. uId = common.ObjToString((*userInfo)["uid"])
  426. userId = common.ObjToString((*userInfo)["userid"])
  427. source = common.ObjToString((*userInfo)["source"])
  428. } else {
  429. log.Println("后台订单--未查询到 ", user_phone)
  430. break
  431. }
  432. } else {
  433. if !mongodb.IsObjectIdHex(userId) {
  434. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  435. if userMapping != nil && len(*userMapping) > 0 {
  436. userId = common.ObjToString((*userMapping)["userid"])
  437. } else {
  438. log.Println("后台订单--未查询到 ", user_phone)
  439. break
  440. }
  441. }
  442. query["userid"] = userId
  443. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  444. if userInfo != nil && len(*userInfo) > 0 {
  445. uId = common.ObjToString((*userInfo)["uid"])
  446. source = common.ObjToString((*userInfo)["source"])
  447. } else {
  448. log.Println("后台订单--未查询到 ", query, user_phone)
  449. break
  450. }
  451. }
  452. is_assign := 1
  453. mailContent := ""
  454. cluename = common.ObjToString(v["company_name"])
  455. mailContent = fmt.Sprintf("%s(%s)", cluename, user_phone)
  456. if cluename == "" {
  457. cluename = user_phone
  458. mailContent = user_phone
  459. }
  460. if pay_money > 0 {
  461. trailstatus = "08"
  462. } else {
  463. trailstatus = "01"
  464. }
  465. isGroup, isCommerce := GetCompanyType(cluename) //判断是否集团公司、工商库
  466. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  467. if clueData == nil || len(*clueData) == 0 {
  468. log.Println("saleDep", saleDep)
  469. if saleDep == "销售部" {
  470. //撞单
  471. log.Println("销售部订单", saleDep)
  472. if strings.Contains(salesperson, ",") {
  473. log.Println("销售部撞单 ", salesperson)
  474. if salesperson == "" {
  475. salesperson = createperson
  476. }
  477. }
  478. log.Println("销售部订单salesperson", salesperson)
  479. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"name": salesperson}, "", "")
  480. if saleData != nil {
  481. seatNumber = common.ObjToString((*saleData)["seat_number"])
  482. positionId = common.Int64All((*saleData)["position_id"])
  483. }
  484. if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
  485. //线索需要冻结
  486. is_assign = -3
  487. }
  488. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  489. "userid": userId,
  490. "uid": uId,
  491. "seatNumber": seatNumber,
  492. "position_id": positionId,
  493. "is_assign": is_assign,
  494. "comeintime": nowTime,
  495. "createtime": nowTime,
  496. "updatetime": nowTime,
  497. "cluename": cluename,
  498. "top_cluetype": "4",
  499. "sub_cluetype": "154",
  500. "trailstatus": trailstatus,
  501. "name": user_phone,
  502. "phone": user_phone,
  503. "comeinsource_private": 2,
  504. "is_task": 0,
  505. "taskstatus": 0,
  506. "company_nature": isGroup,
  507. "company_verification": isCommerce,
  508. "FREEZE_TIME": nowTime,
  509. })
  510. } else if saleDep == "市场部" {
  511. log.Println("市场部订单", saleDep)
  512. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  513. "userid": userId,
  514. "uid": uId,
  515. "is_assign": -2,
  516. "createtime": nowTime,
  517. "updatetime": nowTime,
  518. "cluename": cluename,
  519. "top_cluetype": "4",
  520. "sub_cluetype": "154",
  521. "trailstatus": trailstatus,
  522. "name": user_phone,
  523. "phone": user_phone,
  524. "is_task": 0,
  525. "taskstatus": 0,
  526. "company_nature": isGroup,
  527. "company_verification": isCommerce,
  528. "FREEZE_TIME": nowTime,
  529. })
  530. }
  531. if clueId > 0 {
  532. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  533. "clue_id": clueId,
  534. "position_id": common.If(positionId > 0, positionId, -1),
  535. "change_type": "创建线索",
  536. "new_value": "系统自动创建",
  537. "createtime": nowTime,
  538. "BCPCID": common.GetRandom(32),
  539. "operator_id": -1,
  540. })
  541. if is_assign == -3 {
  542. //新增冻结记录
  543. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  544. "clue_id": clueId,
  545. "position_id": positionId,
  546. "change_type": "线索冻结",
  547. "new_value": "销售人员私海已达上限",
  548. "createtime": nowTime,
  549. "BCPCID": common.GetRandom(32),
  550. "operator_id": -1,
  551. })
  552. //移交失败发送消息
  553. if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
  554. //发送邮件信息
  555. OrderCreateFail(positionId, mailContent)
  556. }
  557. }
  558. }
  559. }
  560. }
  561. cfg.LastOrderClueId = common.ObjToString(v["autoUpdate"])
  562. }
  563. }
  564. common.WriteSysConfig(&cfg)
  565. log.Println("后台订单线索定时任务结束")
  566. }
  567. func refundAuto() {
  568. log.Println("自动移交销售定时任务开始")
  569. nowTime := time.Now().Format(date.Date_Full_Layout)
  570. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  571. for _, v := range *l {
  572. saleId, cluename, company_nature, company_verification, uid, phone := int64(0), "", 0, 0, "", ""
  573. clueId := common.Int64All(v["clue_id"])
  574. name := common.ObjToString(v["name"])
  575. kcposition_id := common.Int64All(v["position_id"])
  576. isRenewalProtection := common.IntAll(v["is_renewal_protection"])
  577. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "phone,company_nature,company_verification,cluename,userid,position_id,uid", "")
  578. if clueData != nil && len(*clueData) > 0 {
  579. saleId = common.Int64All((*clueData)["position_id"])
  580. cluename = common.ObjToString((*clueData)["cluename"])
  581. company_nature = common.IntAll((*clueData)["company_nature"])
  582. company_verification = common.IntAll((*clueData)["company_verification"])
  583. uid = common.ObjToString((*clueData)["uid"])
  584. phone = common.ObjToString((*clueData)["phone"])
  585. }
  586. isFull := FindUpperLimit(gconv.String(saleId), "", false)
  587. isAllRefund := false
  588. myOrders := TiDb.SelectBySql(`select refund_status from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and order_status = 1 and vip_endtime < ?`, uid, nowTime)
  589. if myOrders != nil {
  590. refundCount := 0
  591. for _, v := range *myOrders {
  592. if common.IntAll(v["refund_status"]) == 1 {
  593. refundCount++
  594. }
  595. }
  596. if len(*myOrders) > 0 && len(*myOrders) == refundCount {
  597. isAllRefund = true
  598. }
  599. }
  600. if isAllRefund {
  601. //全退款了
  602. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,is_assign=0,trailstatus="01",position_id=null,seatNumber=null,updatetime=?,comeintime_open=?,comeinsource_open=1,level_open=3,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null,is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId)
  603. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  604. "clue_id": clueId,
  605. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  606. "change_type": "退回公海",
  607. "new_value": "成交客户申请退款",
  608. "createtime": nowTime,
  609. "BCPCID": common.GetRandom(32),
  610. "operator_id": -1,
  611. })
  612. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  613. "clue_id": clueId,
  614. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  615. "change_field": "trailstatus",
  616. "change_type": "基本信息变更",
  617. "old_value": "成交客户",
  618. "new_value": "商机线索",
  619. "createtime": nowTime,
  620. "BCPCID": common.GetRandom(32),
  621. "operator_id": -1,
  622. })
  623. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  624. "clue_id": clueId,
  625. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  626. "change_type": "客户成功经理",
  627. "change_field": "position_id",
  628. "old_value": name,
  629. "new_value": "/",
  630. "createtime": nowTime,
  631. "BCPCID": common.GetRandom(32),
  632. "operator_id": -1,
  633. })
  634. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  635. "clue_id": clueId,
  636. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  637. "change_type": "移交销售",
  638. "new_value": "成交客户申请退款",
  639. "createtime": nowTime,
  640. "BCPCID": common.GetRandom(32),
  641. "operator_id": -1,
  642. })
  643. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
  644. } else {
  645. order1 := TiDb.SelectBySql(`select id from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and vip_endtime > ? and order_status = 1 `, uid, nowTime)
  646. if order1 == nil || len(*order1) == 0 {
  647. isOk := false
  648. order3 := TiDb.SelectBySql(`select vip_endtime from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and vip_endtime < ? and order_status = 1 `, uid, nowTime)
  649. if order3 != nil && len(*order3) > 0 {
  650. for _, vv := range *order3 {
  651. if isRenewalProtection == 1 {
  652. vip_endtime := common.ObjToString(vv["vip_endtime"])
  653. vip_endtimes, _ := time.ParseInLocation(date.Date_Full_Layout, vip_endtime, time.Local)
  654. if time.Now().Unix()-vip_endtimes.Unix() < 3*30*86400 {
  655. isOk = true
  656. }
  657. }
  658. }
  659. }
  660. if !isOk {
  661. //全到期了
  662. if saleId > 0 {
  663. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": saleId, "resign": 0}, "", "")
  664. if saleData != nil && len(*saleData) > 0 {
  665. if FindUpperLimit(gconv.String(saleId), "", false) {
  666. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, nowTime, nowTime, clueId)
  667. } else {
  668. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId)
  669. }
  670. } else {
  671. if company_nature == 0 && company_verification == 1 && cluename != "" {
  672. adata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "seatNumber,position_id", "", -1, -1)
  673. if adata != nil && len(*adata) > 0 {
  674. isOks := false
  675. for _, vv := range *adata {
  676. position_ids := common.Int64All(vv["position_id"])
  677. seatNumber := common.ObjToString(vv["seatNumber"])
  678. saleDatas := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": position_ids, "resign": 0}, "", "")
  679. if saleDatas != nil && len(*saleDatas) > 0 {
  680. saleId = position_ids
  681. if isFull {
  682. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId)
  683. } else {
  684. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId)
  685. }
  686. isOks = true
  687. break
  688. }
  689. }
  690. if !isOks {
  691. sdata := TiDb.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`, saleId)
  692. if sdata != nil && len(*sdata) > 0 {
  693. saleId = common.Int64All((*sdata)[0]["position_id"])
  694. seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
  695. if isFull {
  696. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId)
  697. } else {
  698. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId)
  699. }
  700. }
  701. }
  702. }
  703. } else {
  704. sdata := TiDb.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`, saleId)
  705. if sdata != nil && len(*sdata) > 0 {
  706. saleId = common.Int64All((*sdata)[0]["position_id"])
  707. seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
  708. if isFull {
  709. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId)
  710. } else {
  711. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId)
  712. }
  713. }
  714. }
  715. }
  716. } else {
  717. if isFull {
  718. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",top_cluetype="532",sub_cluetype="537",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, nowTime, clueId)
  719. } else {
  720. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",top_cluetype="532",sub_cluetype="537",is_transfer=0 WHERE id = ?`, nowTime, clueId)
  721. }
  722. }
  723. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  724. "clue_id": clueId,
  725. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  726. "change_type": "基本信息变更",
  727. "old_value": "成交客户",
  728. "change_field": "trailstatus",
  729. "new_value": "商机线索",
  730. "createtime": nowTime,
  731. "BCPCID": common.GetRandom(32),
  732. "operator_id": -1,
  733. })
  734. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  735. "clue_id": clueId,
  736. "position_id": saleId,
  737. "change_type": "加入任务车",
  738. "new_value": "线索自动分配-其他-成交客户续约失败",
  739. "createtime": nowTime,
  740. "BCPCID": common.GetRandom(32),
  741. "operator_id": -1,
  742. })
  743. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  744. "clue_id": clueId,
  745. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  746. "change_type": "客户成功经理",
  747. "change_field": "position_id",
  748. "old_value": name,
  749. "new_value": "/",
  750. "createtime": nowTime,
  751. "BCPCID": common.GetRandom(32),
  752. "operator_id": -1,
  753. })
  754. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  755. "clue_id": clueId,
  756. "position_id": saleId,
  757. "change_type": "移交销售",
  758. "new_value": "成交客户续费失败,到期自动移交",
  759. "createtime": nowTime,
  760. "BCPCID": common.GetRandom(32),
  761. "operator_id": -1,
  762. })
  763. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
  764. if isFull {
  765. //新增冻结记录
  766. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  767. "clue_id": clueId,
  768. "position_id": saleId,
  769. "change_type": "线索冻结",
  770. "new_value": "销售人员私海已达上限",
  771. "createtime": nowTime,
  772. "BCPCID": common.GetRandom(32),
  773. "operator_id": -1,
  774. })
  775. //发送邮件
  776. mailContent := ""
  777. if cluename != "" {
  778. if phone != "" {
  779. if cluename == phone {
  780. mailContent = phone
  781. } else {
  782. mailContent = fmt.Sprintf("%s(%s)", cluename, phone)
  783. }
  784. } else {
  785. mailContent = cluename
  786. }
  787. } else {
  788. mailContent = phone
  789. }
  790. HandOverFail(saleId, mailContent)
  791. }
  792. }
  793. }
  794. }
  795. }
  796. return true
  797. }, `select ent_id,clue_id,position_id,name,is_renewal_protection from dwd_f_csm_customer_info where is_transfer = 0`)
  798. log.Println("自动移交销售定时任务结束")
  799. }
  800. func refundAutoHistory() {
  801. data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1)
  802. if data != nil {
  803. for _, v := range *data {
  804. seatNumber := common.ObjToString(v["seatNumber"])
  805. adata := TiDb.Find("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "", -1, -1)
  806. if adata != nil {
  807. for _, vv := range *adata {
  808. if common.IntAll(vv["resign"]) == 0 {
  809. position_id := common.Int64All(vv["position_id"])
  810. TiDb.Update("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"seatNumber": seatNumber}, map[string]interface{}{"position_id": position_id})
  811. break
  812. }
  813. }
  814. }
  815. }
  816. }
  817. }
  818. func IsFreeze(source string, pay_money int, positionId, trailstatus string) bool {
  819. log.Println(source, pay_money == 0, FindUpperLimit(gconv.String(
  820. positionId), "", false), trailstatus)
  821. if source == "0104" && pay_money == 0 && FindUpperLimit(gconv.String(
  822. positionId), "", false) {
  823. return true
  824. } else if source == "0104" && pay_money > 0 && trailstatus == "08" && FindUpperLimit(gconv.String(
  825. positionId), "", false) {
  826. return true
  827. }
  828. return false
  829. }
  830. func FindSaleRecord(orderCode string) (dept string, positionId int64, position string) {
  831. recordList := Mysql.SelectBySql("select * from order_sale_record where ordercode=? ORDER BY state ,money ", orderCode)
  832. if recordList != nil && len(*recordList) > 0 {
  833. saler_dept := gconv.String((*recordList)[0]["saler_dept"])
  834. if strings.Contains(saler_dept, "销售") {
  835. saler_dept = "销售部"
  836. } else if strings.Contains(saler_dept, "市场") {
  837. saler_dept = "市场部"
  838. } else {
  839. saler_dept = ""
  840. }
  841. return saler_dept, gconv.Int64((*recordList)[0]["saler_Id"]), gconv.String((*recordList)[0]["saler_name"])
  842. }
  843. return "", int64(0), ""
  844. }