kc.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/gogf/gf/v2/util/gconv"
  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 payable_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{}{"uid": uId, "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("客成移交权限未查到--", uId)
  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) 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. data := Mysql.SelectBySql(sql)
  406. if data != nil && *data != nil && len(*data) > 0 {
  407. for _, v := range *data {
  408. order_status := common.IntAll(v["order_status"])
  409. is_backstage_order := common.IntAll(v["is_backstage_order"])
  410. pay_money := common.IntAll(v["pay_money"])
  411. saleDep := common.ObjToString(v["saleDep"])
  412. salesperson := common.ObjToString(v["salesperson"])
  413. user_phone := common.ObjToString(v["user_phone"])
  414. createperson := common.ObjToString(v["create_person"])
  415. saleMoney := common.ObjToString(v["saleMoney"])
  416. userId, uId, cluename, seatNumber, positionId, trailstatus, clueId := common.ObjToString(v["user_id"]), "", "", "", int64(0), "", int64(0)
  417. product_type_str2 := `"大会员","企业商机管理","VIP订阅"`
  418. product_type := common.ObjToString(v["product_type"])
  419. if order_status == 1 && is_backstage_order == 1 && salesperson != "" && !strings.HasPrefix(user_phone, "9") && strings.Contains(product_type_str2, product_type) {
  420. //销售部
  421. query := map[string]interface{}{}
  422. source := ""
  423. if product_type == "企业商机管理" {
  424. query["phone"] = user_phone
  425. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  426. if userInfo != nil && len(*userInfo) > 0 {
  427. uId = common.ObjToString((*userInfo)["uid"])
  428. userId = common.ObjToString((*userInfo)["userid"])
  429. source = common.ObjToString((*userInfo)["source"])
  430. } else {
  431. log.Println("后台订单--未查询到 ", user_phone)
  432. //break
  433. continue
  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. continue
  444. }
  445. }
  446. query["userid"] = userId
  447. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  448. if userInfo != nil && len(*userInfo) > 0 {
  449. uId = common.ObjToString((*userInfo)["uid"])
  450. source = common.ObjToString((*userInfo)["source"])
  451. } else {
  452. log.Println("后台订单--未查询到 ", query, user_phone)
  453. //break
  454. continue
  455. }
  456. }
  457. is_assign := 1
  458. mailContent := ""
  459. cluename = common.ObjToString(v["company_name"])
  460. mailContent = fmt.Sprintf("%s(%s)", cluename, user_phone)
  461. if cluename == "" {
  462. cluename = user_phone
  463. mailContent = user_phone
  464. }
  465. if pay_money > 0 {
  466. trailstatus = "08"
  467. } else {
  468. trailstatus = "01"
  469. }
  470. isGroup, isCommerce := GetCompanyType(cluename) //判断是否集团公司、工商库
  471. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  472. if clueData == nil || len(*clueData) == 0 {
  473. log.Println("saleDep", saleDep)
  474. if strings.HasPrefix(saleDep, "01") || strings.HasPrefix(saleDep, "05") {
  475. //撞单
  476. log.Println("销售部订单", saleDep)
  477. if strings.Contains(salesperson, ",") {
  478. log.Println("销售部撞单 ", salesperson)
  479. if saleMoney != "" {
  480. var saleMoneyStu = struct {
  481. Data []struct {
  482. Name string
  483. Money interface{}
  484. }
  485. }{}
  486. json.Unmarshal([]byte(saleMoney), &saleMoneyStu)
  487. areAllEqual := true
  488. if firstMoney, ok := saleMoneyStu.Data[0].Money.(string); ok {
  489. maxMoney := "0"
  490. log.Println("销售部订单firstMoney", saleMoneyStu, saleMoneyStu.Data[0].Money)
  491. for _, item := range saleMoneyStu.Data {
  492. if item.Name != "合计" && item.Money != firstMoney {
  493. areAllEqual = false
  494. break
  495. }
  496. }
  497. log.Println("销售部订单areAllEqual", areAllEqual)
  498. if areAllEqual {
  499. salesperson = createperson
  500. } else {
  501. for _, item := range saleMoneyStu.Data {
  502. money1, _ := strconv.ParseFloat(item.Money.(string), 64)
  503. money2, _ := strconv.ParseFloat(maxMoney, 64)
  504. if item.Name != "合计" && money1 > money2 {
  505. maxMoney = item.Money.(string)
  506. salesperson = item.Name
  507. }
  508. }
  509. }
  510. } else {
  511. maxMoney := 0.0
  512. log.Println("销售部订单firstMoney222", saleMoneyStu, saleMoneyStu.Data[0].Money)
  513. for _, item := range saleMoneyStu.Data {
  514. if item.Name != "合计" && item.Money != firstMoney {
  515. areAllEqual = false
  516. break
  517. }
  518. }
  519. log.Println("销售部订单areAllEqual222", areAllEqual)
  520. if areAllEqual {
  521. salesperson = createperson
  522. } else {
  523. for _, item := range saleMoneyStu.Data {
  524. if item.Name != "合计" && item.Money.(float64) > maxMoney {
  525. maxMoney = item.Money.(float64)
  526. salesperson = item.Name
  527. }
  528. }
  529. }
  530. }
  531. } else {
  532. salesperson = createperson
  533. }
  534. }
  535. log.Println("销售部订单salesperson", salesperson)
  536. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"name": salesperson}, "", "")
  537. if saleData != nil {
  538. seatNumber = common.ObjToString((*saleData)["seat_number"])
  539. positionId = common.Int64All((*saleData)["position_id"])
  540. }
  541. if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
  542. //线索需要冻结
  543. is_assign = -3
  544. }
  545. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  546. "userid": userId,
  547. "uid": uId,
  548. "seatNumber": seatNumber,
  549. "position_id": positionId,
  550. "is_assign": is_assign,
  551. "comeintime": nowTime,
  552. "createtime": nowTime,
  553. "updatetime": nowTime,
  554. "cluename": cluename,
  555. "top_cluetype": "4",
  556. "sub_cluetype": "154",
  557. "trailstatus": trailstatus,
  558. "name": user_phone,
  559. "phone": user_phone,
  560. "comeinsource_private": 2,
  561. "is_task": 0,
  562. "taskstatus": 0,
  563. "company_nature": isGroup,
  564. "company_verification": isCommerce,
  565. "FREEZE_TIME": nowTime,
  566. })
  567. } else if strings.HasPrefix(saleDep, "03") {
  568. log.Println("市场部订单", saleDep)
  569. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  570. "userid": userId,
  571. "uid": uId,
  572. "is_assign": -2,
  573. "createtime": nowTime,
  574. "updatetime": nowTime,
  575. "cluename": cluename,
  576. "top_cluetype": "4",
  577. "sub_cluetype": "154",
  578. "trailstatus": trailstatus,
  579. "name": user_phone,
  580. "phone": user_phone,
  581. "is_task": 0,
  582. "taskstatus": 0,
  583. "company_nature": isGroup,
  584. "company_verification": isCommerce,
  585. "FREEZE_TIME": nowTime,
  586. })
  587. }
  588. if clueId > 0 {
  589. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  590. "clue_id": clueId,
  591. "position_id": common.If(positionId > 0, positionId, -1),
  592. "change_type": "创建线索",
  593. "new_value": "系统自动创建",
  594. "createtime": nowTime,
  595. "BCPCID": common.GetRandom(32),
  596. "operator_id": -1,
  597. })
  598. if is_assign == -3 {
  599. //新增冻结记录
  600. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  601. "clue_id": clueId,
  602. "position_id": positionId,
  603. "change_type": "线索冻结",
  604. "new_value": "销售人员私海已达上限",
  605. "createtime": nowTime,
  606. "BCPCID": common.GetRandom(32),
  607. "operator_id": -1,
  608. })
  609. //移交失败发送消息
  610. if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
  611. //发送邮件信息
  612. OrderCreateFail(positionId, mailContent)
  613. }
  614. }
  615. }
  616. }
  617. }
  618. cfg.LastOrderClueId = common.ObjToString(v["autoUpdate"])
  619. }
  620. }
  621. common.WriteSysConfig(&cfg)
  622. log.Println("后台订单线索定时任务结束")
  623. }
  624. func refundAuto() {
  625. log.Println("自动移交销售定时任务开始")
  626. nowTime := time.Now().Format(date.Date_Full_Layout)
  627. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  628. for _, v := range *l {
  629. saleId, cluename, company_nature, company_verification, uid, phone := int64(0), "", 0, 0, "", ""
  630. clueId := common.Int64All(v["clue_id"])
  631. name := common.ObjToString(v["name"])
  632. kcposition_id := common.Int64All(v["position_id"])
  633. isRenewalProtection := common.IntAll(v["is_renewal_protection"])
  634. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "phone,company_nature,company_verification,cluename,userid,position_id,uid", "")
  635. if clueData != nil && len(*clueData) > 0 {
  636. saleId = common.Int64All((*clueData)["position_id"])
  637. cluename = common.ObjToString((*clueData)["cluename"])
  638. company_nature = common.IntAll((*clueData)["company_nature"])
  639. company_verification = common.IntAll((*clueData)["company_verification"])
  640. uid = common.ObjToString((*clueData)["uid"])
  641. phone = common.ObjToString((*clueData)["phone"])
  642. }
  643. isFull := FindUpperLimit(gconv.String(saleId), "", false)
  644. isAllRefund := false
  645. 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)
  646. if myOrders != nil {
  647. refundCount := 0
  648. for _, v := range *myOrders {
  649. if common.IntAll(v["refund_status"]) == 1 {
  650. refundCount++
  651. }
  652. }
  653. if len(*myOrders) > 0 && len(*myOrders) == refundCount {
  654. isAllRefund = true
  655. }
  656. }
  657. if isAllRefund {
  658. //全退款了
  659. 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)
  660. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  661. "clue_id": clueId,
  662. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  663. "change_type": "退回公海",
  664. "new_value": "成交客户申请退款",
  665. "createtime": nowTime,
  666. "BCPCID": common.GetRandom(32),
  667. "operator_id": -1,
  668. })
  669. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  670. "clue_id": clueId,
  671. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  672. "change_field": "trailstatus",
  673. "change_type": "基本信息变更",
  674. "old_value": "成交客户",
  675. "new_value": "商机线索",
  676. "createtime": nowTime,
  677. "BCPCID": common.GetRandom(32),
  678. "operator_id": -1,
  679. })
  680. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  681. "clue_id": clueId,
  682. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  683. "change_type": "客户成功经理",
  684. "change_field": "position_id",
  685. "old_value": name,
  686. "new_value": "/",
  687. "createtime": nowTime,
  688. "BCPCID": common.GetRandom(32),
  689. "operator_id": -1,
  690. })
  691. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  692. "clue_id": clueId,
  693. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  694. "change_type": "移交销售",
  695. "new_value": "成交客户申请退款",
  696. "createtime": nowTime,
  697. "BCPCID": common.GetRandom(32),
  698. "operator_id": -1,
  699. })
  700. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
  701. } else {
  702. 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)
  703. if order1 == nil || len(*order1) == 0 {
  704. isOk := false
  705. 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)
  706. if order3 != nil && len(*order3) > 0 {
  707. for _, vv := range *order3 {
  708. if isRenewalProtection == 1 {
  709. vip_endtime := common.ObjToString(vv["vip_endtime"])
  710. vip_endtimes, _ := time.ParseInLocation(date.Date_Full_Layout, vip_endtime, time.Local)
  711. if time.Now().Unix()-vip_endtimes.Unix() < 3*30*86400 {
  712. isOk = true
  713. }
  714. }
  715. }
  716. }
  717. if !isOk {
  718. //全到期了
  719. if saleId > 0 {
  720. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": saleId, "resign": 0}, "", "")
  721. if saleData != nil && len(*saleData) > 0 {
  722. if FindUpperLimit(gconv.String(saleId), "", false) {
  723. 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)
  724. } else {
  725. 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)
  726. }
  727. } else {
  728. if company_nature == 0 && company_verification == 1 && cluename != "" {
  729. adata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "seatNumber,position_id", "", -1, -1)
  730. if adata != nil && len(*adata) > 0 {
  731. isOks := false
  732. for _, vv := range *adata {
  733. position_ids := common.Int64All(vv["position_id"])
  734. seatNumber := common.ObjToString(vv["seatNumber"])
  735. saleDatas := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": position_ids, "resign": 0}, "", "")
  736. if saleDatas != nil && len(*saleDatas) > 0 {
  737. saleId = position_ids
  738. if isFull {
  739. 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)
  740. } else {
  741. 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)
  742. }
  743. isOks = true
  744. break
  745. }
  746. }
  747. if !isOks {
  748. 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)
  749. if sdata != nil && len(*sdata) > 0 {
  750. saleId = common.Int64All((*sdata)[0]["position_id"])
  751. seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
  752. if isFull {
  753. 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)
  754. } else {
  755. 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)
  756. }
  757. }
  758. }
  759. }
  760. } else {
  761. 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)
  762. if sdata != nil && len(*sdata) > 0 {
  763. saleId = common.Int64All((*sdata)[0]["position_id"])
  764. seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
  765. if isFull {
  766. 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)
  767. } else {
  768. 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)
  769. }
  770. }
  771. }
  772. }
  773. } else {
  774. if isFull {
  775. 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)
  776. } else {
  777. 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)
  778. }
  779. }
  780. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  781. "clue_id": clueId,
  782. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  783. "change_type": "基本信息变更",
  784. "old_value": "成交客户",
  785. "change_field": "trailstatus",
  786. "new_value": "商机线索",
  787. "createtime": nowTime,
  788. "BCPCID": common.GetRandom(32),
  789. "operator_id": -1,
  790. })
  791. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  792. "clue_id": clueId,
  793. "position_id": saleId,
  794. "change_type": "加入任务车",
  795. "new_value": "线索自动分配-其他-成交客户续约失败",
  796. "createtime": nowTime,
  797. "BCPCID": common.GetRandom(32),
  798. "operator_id": -1,
  799. })
  800. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  801. "clue_id": clueId,
  802. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  803. "change_type": "客户成功经理",
  804. "change_field": "position_id",
  805. "old_value": name,
  806. "new_value": "/",
  807. "createtime": nowTime,
  808. "BCPCID": common.GetRandom(32),
  809. "operator_id": -1,
  810. })
  811. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  812. "clue_id": clueId,
  813. "position_id": saleId,
  814. "change_type": "移交销售",
  815. "new_value": "成交客户续费失败,到期自动移交",
  816. "createtime": nowTime,
  817. "BCPCID": common.GetRandom(32),
  818. "operator_id": -1,
  819. })
  820. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
  821. if isFull {
  822. //新增冻结记录
  823. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  824. "clue_id": clueId,
  825. "position_id": saleId,
  826. "change_type": "线索冻结",
  827. "new_value": "销售人员私海已达上限",
  828. "createtime": nowTime,
  829. "BCPCID": common.GetRandom(32),
  830. "operator_id": -1,
  831. })
  832. //发送邮件
  833. mailContent := ""
  834. if cluename != "" {
  835. if phone != "" {
  836. if cluename == phone {
  837. mailContent = phone
  838. } else {
  839. mailContent = fmt.Sprintf("%s(%s)", cluename, phone)
  840. }
  841. } else {
  842. mailContent = cluename
  843. }
  844. } else {
  845. mailContent = phone
  846. }
  847. HandOverFail(saleId, mailContent)
  848. }
  849. }
  850. }
  851. }
  852. }
  853. return true
  854. }, `select ent_id,clue_id,position_id,name,is_renewal_protection from dwd_f_csm_customer_info where is_transfer = 0`)
  855. log.Println("自动移交销售定时任务结束")
  856. }
  857. func refundAutoHistory() {
  858. data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1)
  859. if data != nil {
  860. for _, v := range *data {
  861. seatNumber := common.ObjToString(v["seatNumber"])
  862. adata := TiDb.Find("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "", -1, -1)
  863. if adata != nil {
  864. for _, vv := range *adata {
  865. if common.IntAll(vv["resign"]) == 0 {
  866. position_id := common.Int64All(vv["position_id"])
  867. TiDb.Update("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"seatNumber": seatNumber}, map[string]interface{}{"position_id": position_id})
  868. break
  869. }
  870. }
  871. }
  872. }
  873. }
  874. }
  875. func IsFreeze(source string, pay_money int, positionId, trailstatus string) bool {
  876. log.Println(source, pay_money == 0, FindUpperLimit(gconv.String(
  877. positionId), "", false), trailstatus)
  878. if source == "0104" && pay_money == 0 && FindUpperLimit(gconv.String(
  879. positionId), "", false) {
  880. return true
  881. } else if source == "0104" && pay_money > 0 && trailstatus == "08" && FindUpperLimit(gconv.String(
  882. positionId), "", false) {
  883. return true
  884. }
  885. return false
  886. }