kc.go 37 KB

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