kc.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jybase/mail"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  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. )
  15. func kcSync() {
  16. log.Println("客户成功系统移交定时任务开始")
  17. sql := `select * from dwd_f_userbase_order_info where (payable_money > 0 or (payable_money=0 and ( filter like "%分期付款补充权益%" or filter like "%原订单不支持开通多项权益%" or filter like "%原订单不支持开通多项权益%")))
  18. and data_spec!="dhy4" and order_status=1 and refund_status!=1 and vip_starttime<"2099-01-01"
  19. and vip_endtime > "` + time.Now().Format(date.Date_Full_Layout) + `" and autoUpdate > "` + cfg.LastkcTime + `" order by autoUpdate asc`
  20. //sql := `select * from dwd_f_userbase_order_info where order_code="152504013602" `
  21. data := TiDb.SelectBySql(sql)
  22. if data != nil && *data != nil && len(*data) > 0 {
  23. arr := []string{}
  24. for _, v := range *data {
  25. product_type := common.ObjToString(v["product_type"])
  26. vip_starttime := common.ObjToString(v["vip_starttime"])
  27. vip_endtime := common.ObjToString(v["vip_endtime"])
  28. order_change := gconv.Int64(v["order_change"])
  29. if (product_type == "大会员" || product_type == "企业商机管理") && ((order_change != 0) || (order_change == 0 && TimeStrcount(vip_starttime, vip_endtime) > 95)) {
  30. orderCode := gconv.String(v["order_code"])
  31. arr = append(arr, orderCode)
  32. status := kcJob(v)
  33. if status == 0 {
  34. break
  35. }
  36. }
  37. cfg.LastkcTime = common.ObjToString(v["autoUpdate"])
  38. }
  39. log.Println(strings.Join(arr, "`,`"))
  40. }
  41. common.WriteSysConfig(&cfg)
  42. log.Println("客户成功系统移交定时任务结束")
  43. }
  44. func kcJob(data map[string]interface{}) int {
  45. nowTime := time.Now().Format(date.Date_Full_Layout)
  46. uId, entId, clueId, saveMap, status := common.ObjToString(data["uid"]), fmt.Sprint(data["ent_id"]), int64(0), map[string]interface{}{}, 1
  47. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  48. orderCode := gconv.String(data["order_code"])
  49. saleDep, orderPositionId, salesperson := FindSaleRecord(orderCode)
  50. log.Println(data["order_code"], saleDep, orderPositionId, salesperson)
  51. if clueData == nil || len(*clueData) == 0 {
  52. if saleDep == "销售部" || saleDep == "市场部" {
  53. return 1
  54. }
  55. //创建线索
  56. //原始订单获取
  57. position_id := int64(0)
  58. orderData := Mysql.FindOne("dataexport_order", map[string]interface{}{
  59. "order_code": orderCode,
  60. }, "", "")
  61. if orderData == nil {
  62. log.Println("原始订单查询不到:", orderCode)
  63. }
  64. userId, cluename, phone := common.ObjToString((*orderData)["user_id"]), "", ""
  65. seatNumber := ""
  66. if saleDep == "客户成功组" {
  67. //新增线索
  68. position_id = 0
  69. } else if saleDep == "销售部" {
  70. log.Println("该订单数据销售部业绩,不会创建线索")
  71. return 0
  72. } else {
  73. position_id = orderPositionId
  74. //其他信息
  75. seatNumber = "0000"
  76. }
  77. cluename = common.ObjToString(data["company_name"])
  78. phone = common.ObjToString(data["user_phone"])
  79. if cluename == "" {
  80. cluename = phone
  81. }
  82. trailstatus := "08"
  83. isGroup, isCommerce := GetCompanyType(cluename, uId) //判断是否集团公司、工商库
  84. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  85. "userid": userId,
  86. "uid": uId,
  87. "is_assign": common.If(position_id > 0, 1, 0),
  88. "createtime": nowTime,
  89. "updatetime": nowTime,
  90. "cluename": cluename,
  91. "seatNumber": seatNumber,
  92. "position_id": position_id,
  93. "top_cluetype": "4",
  94. "sub_cluetype": "154",
  95. "trailstatus": trailstatus,
  96. "name": phone,
  97. "phone": phone,
  98. "is_task": 0,
  99. "taskstatus": 0,
  100. "company_nature": isGroup,
  101. "company_verification": isCommerce,
  102. "FREEZE_TIME": nowTime,
  103. "label": common.If(position_id > 0, 1, nil),
  104. })
  105. if clueId > 0 {
  106. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  107. "clue_id": clueId,
  108. "position_id": common.If(position_id > 0, position_id, -1),
  109. "change_type": "创建线索",
  110. "new_value": "系统自动创建",
  111. "createtime": nowTime,
  112. "BCPCID": common.GetRandom(32),
  113. "operator_id": -1,
  114. })
  115. clueData = TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  116. if clueData == nil || len(*clueData) == 0 {
  117. return 0
  118. }
  119. } else {
  120. return 1
  121. }
  122. }
  123. clueId = common.Int64All((*clueData)["id"])
  124. starttime := common.ObjToString(data["vip_starttime"])
  125. endtime := common.ObjToString(data["vip_endtime"])
  126. buy_subject := common.IntAll(data["buy_subject"])
  127. product_type := common.ObjToString(data["product_type"])
  128. data_spec := common.ObjToString(data["data_spec"])
  129. userName := common.ObjToString((*clueData)["name"])
  130. product, company_name := 0, common.ObjToString(data["company_name"])
  131. productMap := map[string]int{
  132. "dhy6": 1,
  133. "dhy7": 2,
  134. "dhy3": 4,
  135. "dhy1": 5,
  136. "dhy2": 6,
  137. "dhy5": 7,
  138. "企业商机管理": 8,
  139. }
  140. if buy_subject == 1 || buy_subject == 0 {
  141. entId = common.ObjToString(data["userid"])
  142. }
  143. if product_type == "企业商机管理" {
  144. product = productMap[product_type]
  145. powerData := TiDb.FindOne("dwd_f_data_equity_info", map[string]interface{}{"uid": uId, "product_type": "商机管理"}, "", "comeintime desc")
  146. if powerData != nil {
  147. starttime = common.ObjToString((*powerData)["starttime"])
  148. endtime = common.ObjToString((*powerData)["endtime"])
  149. } else {
  150. log.Println("客成移交权限未查到--", uId)
  151. return 0
  152. }
  153. } else {
  154. if data_spec == "dhy6" {
  155. product = 1
  156. filter := common.ObjToString(data["filter"])
  157. filterMap := map[string]interface{}{}
  158. json.Unmarshal([]byte(filter), &filterMap)
  159. if len(filterMap) > 0 {
  160. areaCount := common.IntAll(filterMap["areaCount"])
  161. if areaCount == 1 {
  162. product = 3
  163. }
  164. }
  165. } else {
  166. product = productMap[data_spec]
  167. }
  168. }
  169. //同一公司名称(以客户详情-组织机构-公司名称)下的线索需分配给同1人
  170. saveMap = map[string]interface{}{
  171. "clue_id": clueId,
  172. "transfertime": nowTime,
  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. id := gconv.Int64((*csmdata)["id"])
  194. log.Println(1111111, id, orderCode)
  195. //'已移交销售,0移交客成 1移交销售',
  196. is_transfer := gconv.Int64((*csmdata)["is_transfer"])
  197. customerPositionId, customerName := cAutoDraw(0, orderPositionId, salesperson, saleDep, true, csmdata)
  198. log.Println("移交客成positionId", customerPositionId, customerName, saleDep, orderPositionId, salesperson, true)
  199. oldName := gconv.String((*csmdata)["name"])
  200. updateMap := map[string]interface{}{
  201. "is_transfer": 0,
  202. "is_renewal_protection": 0,
  203. "product_access": product,
  204. "buy_subject": buy_subject,
  205. "transfertime": nowTime,
  206. "service_starttime": starttime,
  207. "service_endtime": endtime,
  208. "ent_id": entId,
  209. "company_name": company_name,
  210. "name": customerName,
  211. "position_id": customerPositionId,
  212. }
  213. if is_transfer == 1 {
  214. //重新进去
  215. updateMap["relationship_building_way"] = 1
  216. updateMap["tasksource"] = "1"
  217. updateMap["inventory_way"] = 1
  218. updateMap["training_way"] = 1
  219. updateMap["is_pre_sales_training"] = 0
  220. updateMap["service_stage"] = 1
  221. }
  222. TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, updateMap)
  223. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  224. "clue_id": clueId,
  225. "position_id": customerPositionId,
  226. "change_type": "成交客户移交",
  227. "new_value": "移交至客户成功组",
  228. "createtime": nowTime,
  229. "BCPCID": common.GetRandom(32),
  230. "operator_id": -1,
  231. })
  232. if customerPositionId > 0 && is_transfer == 1 {
  233. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  234. "clue_id": clueId,
  235. "position_id": customerPositionId,
  236. "change_field": "position_id",
  237. "change_type": "客户成功经理",
  238. "old_value": common.If(oldName != customerName, customerName, common.If(oldName == "", "/", oldName)),
  239. "new_value": customerName,
  240. "createtime": nowTime,
  241. "BCPCID": common.GetRandom(32),
  242. "operator_id": -1,
  243. })
  244. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  245. "clue_id": clueId,
  246. "position_id": customerPositionId,
  247. "change_type": "加入任务车",
  248. "new_value": "未建联",
  249. "createtime": nowTime,
  250. "BCPCID": common.GetRandom(32),
  251. "operator_id": -1,
  252. })
  253. }
  254. KcSend(orderCode, customerName)
  255. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  256. }
  257. } else {
  258. entIds := common.IntAll(data["ent_id"])
  259. customerPositionId, customerName := cAutoDraw(entIds, orderPositionId, salesperson, saleDep, false, nil)
  260. log.Println("移交客成positionId", customerPositionId, customerName, saleDep, orderPositionId, salesperson, false)
  261. cId, ok, updateId1, updateId2, updateId3 := int64(0), false, int64(0), int64(0), int64(0)
  262. if TiDb.ExecTx("保存客户", func(tx *sql.Tx) bool {
  263. saveMap["position_id"] = customerPositionId
  264. saveMap["name"] = customerName
  265. cId = TiDb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
  266. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
  267. if customerPositionId > 0 {
  268. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  269. "clue_id": clueId,
  270. "position_id": customerPositionId,
  271. "change_type": "加入任务车",
  272. "new_value": "未建联",
  273. "createtime": nowTime,
  274. "BCPCID": common.GetRandom(32),
  275. "operator_id": -1,
  276. })
  277. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  278. "clue_id": clueId,
  279. "position_id": customerPositionId,
  280. "change_field": "position_id",
  281. "change_type": "客户成功经理",
  282. "old_value": "/",
  283. "new_value": customerName,
  284. "createtime": nowTime,
  285. "BCPCID": common.GetRandom(32),
  286. "operator_id": -1,
  287. })
  288. }
  289. KcSend(orderCode, customerName)
  290. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  291. "clue_id": clueId,
  292. "position_id": customerPositionId,
  293. "change_type": "成交客户移交",
  294. "new_value": "移交至客户成功组",
  295. "createtime": nowTime,
  296. "BCPCID": common.GetRandom(32),
  297. "operator_id": -1,
  298. })
  299. return cId > -1 && ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1
  300. }) {
  301. //TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, name)
  302. log.Println("保存客户成功")
  303. } else {
  304. log.Println("保存客户失败!!!", clueId, cId, ok, updateId1, updateId2, updateId3, " 用户信息 ", customerName, customerPositionId, uId)
  305. }
  306. }
  307. return status
  308. }
  309. func cAutoDraw(entId int, orderPositionId int64, salesperson, saleDep string, isExist bool, csmdata *map[string]interface{}) (positionId int64, name string) {
  310. 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 `
  311. data := TiDb.SelectBySql(query)
  312. if data == nil || len(*data) == 0 {
  313. return
  314. }
  315. //判断是否需要新增客成数据
  316. if isExist {
  317. //用户存在客成数据
  318. positionid := common.Int64All((*csmdata)["position_id"])
  319. if positionid != 0 {
  320. //以前没有,需要找一个新的
  321. for _, v := range *data {
  322. deptPositionId := gconv.Int64(v["position_id"])
  323. if deptPositionId == positionid {
  324. positionId = positionid
  325. name = common.ObjToString((*csmdata)["name"])
  326. return
  327. }
  328. }
  329. }
  330. //原始用户已离职 查看订单是否有合适的订单
  331. if saleDep == "客户成功组" {
  332. positionId = orderPositionId
  333. name = salesperson
  334. return
  335. }
  336. }
  337. //客成数据不存在的时候
  338. //查找企业有没有其他信息
  339. if entId > 0 {
  340. entdata := TiDb.Find("dwd_f_csm_customer_info", map[string]interface{}{"ent_id": entId}, "", "", -1, -1)
  341. if entdata != nil && len(*entdata) > 0 {
  342. for _, v := range *entdata {
  343. positionid := common.Int64All(v["position_id"])
  344. name = common.ObjToString(v["name"])
  345. for _, v := range *data {
  346. deptPositionId := gconv.Int64(v["position_id"])
  347. if deptPositionId == positionId {
  348. positionId = positionid
  349. name = common.ObjToString((*csmdata)["name"])
  350. return
  351. }
  352. }
  353. }
  354. }
  355. //原始用户已离职 或者没有查找相关企业客成信息 以订单信息为主
  356. if saleDep == "客户成功组" {
  357. positionId = orderPositionId
  358. name = salesperson
  359. }
  360. return
  361. //没有相同企业的客成信息
  362. } else {
  363. //没有人 分配给自己
  364. if saleDep == "客户成功组" {
  365. positionId = orderPositionId
  366. name = salesperson
  367. return
  368. }
  369. return
  370. }
  371. }
  372. func kcAuto() {
  373. log.Println("客户成功系统自动进入任务车定时任务开始")
  374. nowTime := time.Now().Format(date.Date_Full_Layout)
  375. 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`
  376. data := TiDb.SelectBySql(sql)
  377. if data != nil && *data != nil && len(*data) > 0 {
  378. for _, v := range *data {
  379. relationship_building_way := common.IntAll(v["relationship_building_way"])
  380. inventory_way := common.IntAll(v["inventory_way"])
  381. training_way := common.IntAll(v["training_way"])
  382. positionId := common.Int64All(v["position_id"])
  383. clueId := common.Int64All(v["clue_id"])
  384. tasksource, tasksources := common.ObjToString(v["tasksource"]), ""
  385. taskstatus := common.IntAll(v["taskstatus"])
  386. if relationship_building_way != 1 && inventory_way == 1 {
  387. tasksources = "2"
  388. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  389. "clue_id": clueId,
  390. "position_id": positionId,
  391. "change_type": "加入任务车",
  392. "new_value": "未盘点",
  393. "createtime": nowTime,
  394. "BCPCID": common.GetRandom(32),
  395. "operator_id": -1,
  396. })
  397. } else if inventory_way != 1 && training_way == 1 {
  398. tasksources = "3"
  399. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  400. "clue_id": clueId,
  401. "position_id": positionId,
  402. "change_type": "加入任务车",
  403. "new_value": "未培训",
  404. "createtime": nowTime,
  405. "BCPCID": common.GetRandom(32),
  406. "operator_id": -1,
  407. })
  408. }
  409. if taskstatus == 1 {
  410. tasksource = tasksources
  411. } else {
  412. if tasksource != "" {
  413. if !strings.Contains(tasksource, tasksources) {
  414. tasksource += "," + tasksources
  415. }
  416. } else {
  417. tasksource = tasksources
  418. }
  419. }
  420. TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  421. "tasksource": tasksource,
  422. "tasktime": nowTime,
  423. "is_task": 1,
  424. "taskstatus": 0,
  425. })
  426. }
  427. }
  428. log.Println("客户成功系统进入任务车定时任务结束")
  429. }
  430. func ordersClue() {
  431. log.Println("后台订单线索定时任务开始")
  432. lastOrderClueId := cfg.LastOrderClueId
  433. nowTime := time.Now().Format(date.Date_Full_Layout)
  434. 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)
  435. data := Mysql.SelectBySql(sql)
  436. if data != nil && *data != nil && len(*data) > 0 {
  437. for _, v := range *data {
  438. order_status := common.IntAll(v["order_status"])
  439. is_backstage_order := common.IntAll(v["is_backstage_order"])
  440. pay_money := common.IntAll(v["pay_money"])
  441. orderCode := gconv.String(v["order_code"])
  442. saleDep, _, salesperson := FindSaleRecord(orderCode)
  443. user_phone := common.ObjToString(v["user_phone"])
  444. createperson := common.ObjToString(v["create_person"])
  445. userId, uId, cluename, seatNumber, positionId, trailstatus, clueId := common.ObjToString(v["user_id"]), "", "", "", int64(0), "", int64(0)
  446. product_type_str2 := `"大会员","企业商机管理","VIP订阅"`
  447. product_type := common.ObjToString(v["product_type"])
  448. if order_status == 1 && is_backstage_order == 1 && saleDep != "" && !strings.HasPrefix(user_phone, "9") && strings.Contains(product_type_str2, product_type) {
  449. //销售部
  450. query := map[string]interface{}{}
  451. source := ""
  452. if product_type == "企业商机管理" {
  453. query["phone"] = user_phone
  454. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  455. if userInfo != nil && len(*userInfo) > 0 {
  456. uId = common.ObjToString((*userInfo)["uid"])
  457. userId = common.ObjToString((*userInfo)["userid"])
  458. source = common.ObjToString((*userInfo)["source"])
  459. } else {
  460. log.Println("后台订单--未查询到 ", user_phone)
  461. break
  462. }
  463. } else {
  464. if !mongodb.IsObjectIdHex(userId) {
  465. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  466. if userMapping != nil && len(*userMapping) > 0 {
  467. userId = common.ObjToString((*userMapping)["userid"])
  468. } else {
  469. log.Println("后台订单--未查询到 ", user_phone)
  470. break
  471. }
  472. }
  473. query["userid"] = userId
  474. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  475. if userInfo != nil && len(*userInfo) > 0 {
  476. uId = common.ObjToString((*userInfo)["uid"])
  477. source = common.ObjToString((*userInfo)["source"])
  478. } else {
  479. log.Println("后台订单--未查询到 ", query, user_phone)
  480. break
  481. }
  482. }
  483. is_assign := 1
  484. mailContent := ""
  485. cluename = common.ObjToString(v["company_name"])
  486. mailContent = fmt.Sprintf("%s(%s)", cluename, user_phone)
  487. if cluename == "" {
  488. cluename = user_phone
  489. mailContent = user_phone
  490. }
  491. if pay_money > 0 {
  492. trailstatus = "08"
  493. } else {
  494. trailstatus = "01"
  495. }
  496. isGroup, isCommerce := GetCompanyType(cluename, uId) //判断是否集团公司、工商库
  497. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  498. if clueData == nil || len(*clueData) == 0 {
  499. log.Println("saleDep", saleDep)
  500. if saleDep == "销售部" {
  501. //撞单
  502. log.Println("销售部订单", saleDep)
  503. if strings.Contains(salesperson, ",") {
  504. log.Println("销售部撞单 ", salesperson)
  505. if salesperson == "" {
  506. salesperson = createperson
  507. }
  508. }
  509. log.Println("销售部订单salesperson", salesperson)
  510. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"name": salesperson}, "", "")
  511. if saleData != nil {
  512. seatNumber = common.ObjToString((*saleData)["seat_number"])
  513. positionId = common.Int64All((*saleData)["position_id"])
  514. }
  515. if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
  516. //线索需要冻结
  517. is_assign = -3
  518. }
  519. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  520. "userid": userId,
  521. "uid": uId,
  522. "seatNumber": seatNumber,
  523. "position_id": positionId,
  524. "is_assign": is_assign,
  525. "comeintime": nowTime,
  526. "createtime": nowTime,
  527. "updatetime": nowTime,
  528. "cluename": cluename,
  529. "top_cluetype": "4",
  530. "sub_cluetype": "154",
  531. "trailstatus": trailstatus,
  532. "name": user_phone,
  533. "phone": user_phone,
  534. "comeinsource_private": 2,
  535. "is_task": 0,
  536. "taskstatus": 0,
  537. "company_nature": isGroup,
  538. "company_verification": isCommerce,
  539. "FREEZE_TIME": nowTime,
  540. })
  541. } else if saleDep == "市场部" {
  542. log.Println("市场部订单", saleDep)
  543. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  544. "userid": userId,
  545. "uid": uId,
  546. "is_assign": -2,
  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. "is_task": 0,
  556. "taskstatus": 0,
  557. "company_nature": isGroup,
  558. "company_verification": isCommerce,
  559. "FREEZE_TIME": nowTime,
  560. })
  561. }
  562. if clueId > 0 {
  563. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  564. "clue_id": clueId,
  565. "position_id": common.If(positionId > 0, positionId, -1),
  566. "change_type": "创建线索",
  567. "new_value": "系统自动创建",
  568. "createtime": nowTime,
  569. "BCPCID": common.GetRandom(32),
  570. "operator_id": -1,
  571. })
  572. if is_assign == -3 {
  573. //新增冻结记录
  574. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  575. "clue_id": clueId,
  576. "position_id": positionId,
  577. "change_type": "线索冻结",
  578. "new_value": "销售人员私海已达上限",
  579. "createtime": nowTime,
  580. "BCPCID": common.GetRandom(32),
  581. "operator_id": -1,
  582. })
  583. //移交失败发送消息
  584. if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
  585. //发送邮件信息
  586. OrderCreateFail(positionId, mailContent)
  587. }
  588. }
  589. }
  590. }
  591. }
  592. cfg.LastOrderClueId = common.ObjToString(v["autoUpdate"])
  593. }
  594. }
  595. common.WriteSysConfig(&cfg)
  596. log.Println("后台订单线索定时任务结束")
  597. }
  598. func refundAuto() {
  599. log.Println("自动移交销售定时任务开始")
  600. findNowTime := time.Now().AddDate(0, 0, -db.ExpirationPeriod).Format(date.Date_Full_Layout)
  601. nowTime := time.Now().Format(date.Date_Full_Layout)
  602. mailData := map[string][]map[string]interface{}{}
  603. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  604. for _, v := range *l {
  605. saleId, cluename, company_nature, company_verification, uid, phone, userName := int64(0), "", 0, 0, "", "", ""
  606. clueId := common.Int64All(v["clue_id"])
  607. company_name := gconv.String(v["company_name"])
  608. name := common.ObjToString(v["name"])
  609. kcposition_id := common.Int64All(v["position_id"])
  610. isRenewalProtection := common.IntAll(v["is_renewal_protection"])
  611. clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "name,phone,company_nature,company_verification,cluename,userid,position_id,uid", "")
  612. if clueData != nil && len(*clueData) > 0 {
  613. saleId = common.Int64All((*clueData)["position_id"])
  614. cluename = common.ObjToString((*clueData)["cluename"])
  615. userName = gconv.String((*clueData)["name"])
  616. company_nature = common.IntAll((*clueData)["company_nature"])
  617. company_verification = common.IntAll((*clueData)["company_verification"])
  618. uid = common.ObjToString((*clueData)["uid"])
  619. phone = common.ObjToString((*clueData)["phone"])
  620. }
  621. //查询即将到期数据
  622. if isRenewalProtection == 0 {
  623. aaa := TiDb.CountBySql(`select count(1) from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and order_status = 1 and refund_status!=1 and vip_endtime < ? and vip_endtime > ? `, uid, time.Now().AddDate(0, 0, -db.ExpirationPeriod+1).Format(date.Date_Short_Layout), time.Now().AddDate(0, 0, -db.ExpirationPeriod).Format(date.Date_Short_Layout))
  624. log.Println(aaa)
  625. if aaa > 0 {
  626. //即将到期
  627. mailData[name] = append(mailData[name], map[string]interface{}{
  628. "company_name": company_name,
  629. "phone": phone,
  630. "userName": userName,
  631. "remrk": "1天后即将移交",
  632. "reason": "成交客户续费失败",
  633. })
  634. }
  635. continue
  636. }
  637. isFull := FindUpperLimit(gconv.String(saleId), "", false)
  638. isAllRefund := false
  639. myOrders := TiDb.SelectBySql(`select refund_status from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and order_status = 1 `, uid)
  640. if myOrders != nil {
  641. refundCount := 0
  642. for _, v := range *myOrders {
  643. if common.IntAll(v["refund_status"]) == 1 {
  644. refundCount++
  645. }
  646. }
  647. if len(*myOrders) > 0 && len(*myOrders) == refundCount {
  648. isAllRefund = true
  649. }
  650. }
  651. if isAllRefund {
  652. //全退款了
  653. 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)
  654. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  655. "clue_id": clueId,
  656. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  657. "change_type": "退回公海",
  658. "new_value": "成交客户申请退款",
  659. "createtime": nowTime,
  660. "BCPCID": common.GetRandom(32),
  661. "operator_id": -1,
  662. })
  663. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  664. "clue_id": clueId,
  665. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  666. "change_field": "trailstatus",
  667. "change_type": "基本信息变更",
  668. "old_value": "成交客户",
  669. "new_value": "商机线索",
  670. "createtime": nowTime,
  671. "BCPCID": common.GetRandom(32),
  672. "operator_id": -1,
  673. })
  674. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  675. "clue_id": clueId,
  676. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  677. "change_type": "客户成功经理",
  678. "change_field": "position_id",
  679. "old_value": name,
  680. "new_value": "/",
  681. "createtime": nowTime,
  682. "BCPCID": common.GetRandom(32),
  683. "operator_id": -1,
  684. })
  685. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  686. "clue_id": clueId,
  687. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  688. "change_type": "移交销售",
  689. "new_value": "成交客户申请退款",
  690. "createtime": nowTime,
  691. "BCPCID": common.GetRandom(32),
  692. "operator_id": -1,
  693. })
  694. mailData[name] = append(mailData[name], map[string]interface{}{
  695. "company_name": company_name,
  696. "phone": phone,
  697. "userName": userName,
  698. "remrk": "已移交",
  699. "reason": "成交客户申请退款",
  700. })
  701. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
  702. } else {
  703. 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, findNowTime)
  704. if order1 == nil || len(*order1) == 0 {
  705. isOk := false
  706. 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, findNowTime)
  707. if order3 != nil && len(*order3) > 0 {
  708. for _, vv := range *order3 {
  709. if isRenewalProtection == 1 {
  710. vip_endtime := common.ObjToString(vv["vip_endtime"])
  711. vip_endtimes, _ := time.ParseInLocation(date.Date_Full_Layout, vip_endtime, time.Local)
  712. //查询申请保护时间
  713. renewalTime := time.Now()
  714. timeData := TiDb.SelectBySql(`select max(createtime) as createtime from dwd_f_crm_clue_change_record where clue_id = ? and change_type="申请续费保护"`, clueId)
  715. if timeData != nil && len(*timeData) > 0 {
  716. if gconv.String(gconv.String((*timeData)[0]["createtime"])) != "" {
  717. renewalTime, _ = time.ParseInLocation(date.Date_Short_Layout, gconv.String((*timeData)[0]["createtime"]), time.Local)
  718. }
  719. }
  720. renewalInt := renewalTime.Unix()
  721. log.Println("申请保护期时间", clueId, renewalInt)
  722. log.Println("申请保护期时间222", time.Now().Unix()-vip_endtimes.Unix(), (db.HandoverCycle-1)*86400, (db.HandoverCycle)*86400)
  723. log.Println("申请保护期时间333", (db.HandoverCycle-1)*86400 < time.Now().Unix()-vip_endtimes.Unix(), time.Now().Unix()-vip_endtimes.Unix() < (db.HandoverCycle)*86400)
  724. //查看服务到期时间
  725. if renewalInt != 0 && renewalInt < db.CustomerTime {
  726. //三个月
  727. if time.Now().Unix()-vip_endtimes.Unix() < 3*30*86400 {
  728. isOk = true
  729. }
  730. } else if (db.HandoverCycle-2)*86400 < time.Now().Unix()-vip_endtimes.Unix() && time.Now().Unix()-vip_endtimes.Unix() < (db.HandoverCycle-1)*86400 {
  731. //提前一天提示
  732. mailData[name] = append(mailData[name], map[string]interface{}{
  733. "company_name": company_name,
  734. "phone": phone,
  735. "userName": userName,
  736. "remrk": "1天后即将移交",
  737. "reason": "成交客户续费失败",
  738. })
  739. } else if time.Now().Unix()-vip_endtimes.Unix() < db.HandoverCycle*86400 {
  740. isOk = true
  741. }
  742. }
  743. }
  744. }
  745. if !isOk {
  746. //全到期了
  747. if saleId > 0 {
  748. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": saleId, "resign": 0}, "", "")
  749. if saleData != nil && len(*saleData) > 0 {
  750. if FindUpperLimit(gconv.String(saleId), "", false) {
  751. 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)
  752. } else {
  753. 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)
  754. }
  755. } else {
  756. if company_nature == 0 && company_verification == 1 && cluename != "" {
  757. adata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "seatNumber,position_id", "", -1, -1)
  758. if adata != nil && len(*adata) > 0 {
  759. isOks := false
  760. for _, vv := range *adata {
  761. position_ids := common.Int64All(vv["position_id"])
  762. seatNumber := common.ObjToString(vv["seatNumber"])
  763. saleDatas := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": position_ids, "resign": 0}, "", "")
  764. if saleDatas != nil && len(*saleDatas) > 0 {
  765. saleId = position_ids
  766. if isFull {
  767. 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)
  768. } else {
  769. 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)
  770. }
  771. isOks = true
  772. break
  773. }
  774. }
  775. if !isOks {
  776. 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)
  777. if sdata != nil && len(*sdata) > 0 {
  778. saleId = common.Int64All((*sdata)[0]["position_id"])
  779. seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
  780. if isFull {
  781. 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)
  782. } else {
  783. 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)
  784. }
  785. }
  786. }
  787. }
  788. } else {
  789. 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)
  790. if sdata != nil && len(*sdata) > 0 {
  791. saleId = common.Int64All((*sdata)[0]["position_id"])
  792. seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
  793. if isFull {
  794. 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)
  795. } else {
  796. 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)
  797. }
  798. }
  799. }
  800. }
  801. } else {
  802. if isFull {
  803. 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)
  804. } else {
  805. 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)
  806. }
  807. }
  808. if kcposition_id > 0 {
  809. mailData[name] = append(mailData[name], map[string]interface{}{
  810. "company_name": company_name,
  811. "phone": phone,
  812. "userName": userName,
  813. "remrk": "已移交",
  814. "reason": "成交客户续费失败",
  815. })
  816. }
  817. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  818. "clue_id": clueId,
  819. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  820. "change_type": "基本信息变更",
  821. "old_value": "成交客户",
  822. "change_field": "trailstatus",
  823. "new_value": "商机线索",
  824. "createtime": nowTime,
  825. "BCPCID": common.GetRandom(32),
  826. "operator_id": -1,
  827. })
  828. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  829. "clue_id": clueId,
  830. "position_id": saleId,
  831. "change_type": "加入任务车",
  832. "new_value": "线索自动分配-其他-成交客户续约失败",
  833. "createtime": nowTime,
  834. "BCPCID": common.GetRandom(32),
  835. "operator_id": -1,
  836. })
  837. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  838. "clue_id": clueId,
  839. "position_id": common.If(kcposition_id > 0, kcposition_id, -1),
  840. "change_type": "客户成功经理",
  841. "change_field": "position_id",
  842. "old_value": name,
  843. "new_value": "/",
  844. "createtime": nowTime,
  845. "BCPCID": common.GetRandom(32),
  846. "operator_id": -1,
  847. })
  848. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  849. "clue_id": clueId,
  850. "position_id": saleId,
  851. "change_type": "移交销售",
  852. "new_value": "成交客户续费失败,到期自动移交",
  853. "createtime": nowTime,
  854. "BCPCID": common.GetRandom(32),
  855. "operator_id": -1,
  856. })
  857. TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
  858. if isFull {
  859. //新增冻结记录
  860. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  861. "clue_id": clueId,
  862. "position_id": saleId,
  863. "change_type": "线索冻结",
  864. "new_value": "销售人员私海已达上限",
  865. "createtime": nowTime,
  866. "BCPCID": common.GetRandom(32),
  867. "operator_id": -1,
  868. })
  869. //发送邮件
  870. mailContent := ""
  871. if cluename != "" {
  872. if phone != "" {
  873. if cluename == phone {
  874. mailContent = phone
  875. } else {
  876. mailContent = fmt.Sprintf("%s(%s)", cluename, phone)
  877. }
  878. } else {
  879. mailContent = cluename
  880. }
  881. } else {
  882. mailContent = phone
  883. }
  884. HandOverFail(saleId, mailContent)
  885. }
  886. }
  887. }
  888. }
  889. }
  890. return true
  891. }, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where is_transfer = 0`)
  892. //}, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where id =180007`)
  893. //移交电销提醒
  894. for i, v := range mailData {
  895. if i != "" {
  896. ExitKcSend(i, v)
  897. }
  898. }
  899. log.Println("自动移交销售定时任务结束")
  900. }
  901. func refundAutoHistory() {
  902. data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1)
  903. if data != nil {
  904. for _, v := range *data {
  905. seatNumber := common.ObjToString(v["seatNumber"])
  906. adata := TiDb.Find("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "", -1, -1)
  907. if adata != nil {
  908. for _, vv := range *adata {
  909. if common.IntAll(vv["resign"]) == 0 {
  910. position_id := common.Int64All(vv["position_id"])
  911. TiDb.Update("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"seatNumber": seatNumber}, map[string]interface{}{"position_id": position_id})
  912. break
  913. }
  914. }
  915. }
  916. }
  917. }
  918. }
  919. func IsFreeze(source string, pay_money int, positionId, trailstatus string) bool {
  920. log.Println(source, pay_money == 0, FindUpperLimit(gconv.String(
  921. positionId), "", false), trailstatus)
  922. if source == "0104" && pay_money == 0 && FindUpperLimit(gconv.String(
  923. positionId), "", false) {
  924. return true
  925. } else if source == "0104" && pay_money > 0 && trailstatus == "08" && FindUpperLimit(gconv.String(
  926. positionId), "", false) {
  927. return true
  928. }
  929. return false
  930. }
  931. func FindSaleRecord(orderCode string) (dept string, positionId int64, position string) {
  932. recordList := Mysql.SelectBySql("select * from order_sale_record where ordercode=? ORDER BY state ,money ", orderCode)
  933. if recordList != nil && len(*recordList) > 0 {
  934. saler_dept := gconv.String((*recordList)[0]["saler_dept"])
  935. if strings.Contains(saler_dept, "销售") {
  936. saler_dept = "销售部"
  937. } else if strings.Contains(saler_dept, "市场") {
  938. saler_dept = "市场部"
  939. } else if strings.Contains(saler_dept, "客户成功") {
  940. saler_dept = "客户成功组"
  941. } else {
  942. saler_dept = ""
  943. }
  944. return saler_dept, gconv.Int64((*recordList)[0]["saler_Id"]), gconv.String((*recordList)[0]["saler_name"])
  945. }
  946. return "", int64(0), ""
  947. }
  948. func kcClue(userId, uId, seatNumber, cluename, top_cluetype, sub_cluetype,
  949. name, phone, position, sourceCode, industry, follow_project_area,
  950. role, item, subname, topname, remark, demand, department, departments, saleName, source string,
  951. positionId int64, isGroup, isCommerce int,
  952. keywords []string) bool {
  953. clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
  954. nowTime := time.Now().Format("2006-01-02 15:04:05")
  955. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  956. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  957. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  958. "userid": userId,
  959. "uid": uId,
  960. "seatNumber": seatNumber,
  961. "position_id": positionId,
  962. "is_assign": common.If(positionId > 0, 1, 0),
  963. "comeintime": nowTime,
  964. "createtime": nowTime,
  965. "updatetime": nowTime,
  966. "cluename": cluename,
  967. "top_cluetype": top_cluetype,
  968. "sub_cluetype": sub_cluetype,
  969. "trailstatus": "01",
  970. "name": name,
  971. "phone": phone,
  972. "position": position,
  973. "department": common.If(sourceCode == "app_xzcyh", departments, department),
  974. "industry": industry,
  975. "follow_project_area": follow_project_area,
  976. "role": role,
  977. "comeinsource_private": 2,
  978. "is_task": 1,
  979. "task_time": nowTime,
  980. "tasktime": common.If(item == "users", nowTimes, nowTime),
  981. "taskstatus": 0,
  982. "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
  983. "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
  984. "company_nature": isGroup,
  985. "company_verification": isCommerce,
  986. "remark": remark,
  987. "customer_demand": demand,
  988. "FREEZE_TIME": nowTime,
  989. "label": 1,
  990. "labelChangeTime": time.Now().Format("2006-01-02"),
  991. })
  992. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  993. "clue_id": clueId,
  994. "position_id": positionId,
  995. "change_type": "创建线索",
  996. "new_value": "系统自动创建",
  997. "createtime": nowTime,
  998. "BCPCID": common.GetRandom(32),
  999. "operator_id": -1,
  1000. })
  1001. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1002. "clue_id": clueId,
  1003. "position_id": positionId,
  1004. "change_field": "position_id",
  1005. "change_type": "所属人变更",
  1006. "old_value": "/",
  1007. "new_value": saleName,
  1008. "createtime": nowTime,
  1009. "BCPCID": common.GetRandom(32),
  1010. "operator_id": -1,
  1011. })
  1012. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1013. "clue_id": clueId,
  1014. "position_id": positionId,
  1015. "change_field": "trailstatus",
  1016. "change_type": "基本信息变更",
  1017. "old_value": "商机线索",
  1018. "new_value": "新增",
  1019. "createtime": nowTime,
  1020. "BCPCID": common.GetRandom(32),
  1021. "operator_id": -1,
  1022. })
  1023. uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1024. "clue_id": clueId,
  1025. "position_id": positionId,
  1026. "change_field": "top_cluetype",
  1027. "change_type": "基本信息变更",
  1028. "old_value": "/",
  1029. "new_value": topname,
  1030. "createtime": nowTime,
  1031. "BCPCID": common.GetRandom(32),
  1032. "operator_id": -1,
  1033. })
  1034. uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1035. "clue_id": clueId,
  1036. "position_id": positionId,
  1037. "change_field": "sub_cluetype", //222
  1038. "change_type": "基本信息变更",
  1039. "old_value": "/",
  1040. "new_value": subname,
  1041. "createtime": nowTime,
  1042. "BCPCID": common.GetRandom(32),
  1043. "operator_id": -1,
  1044. })
  1045. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1
  1046. }) {
  1047. log.Println("线索分配成功")
  1048. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  1049. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  1050. "status": 1,
  1051. "is_delete": 1,
  1052. "createtime": nowTime,
  1053. "updatetime": nowTime,
  1054. "phone": phone,
  1055. "baseinfo_id": uId,
  1056. "SOURCE": source,
  1057. })
  1058. }
  1059. return true
  1060. }
  1061. return false
  1062. }
  1063. type OrderInfo struct {
  1064. Id int64
  1065. UserId string
  1066. CompanyName string
  1067. ProductType string
  1068. OrderCode string
  1069. PayMoney float64
  1070. ReturnMoney float64
  1071. UserPhone string
  1072. StartEnd string
  1073. DisKcName string
  1074. UserName string
  1075. SeriveList []string
  1076. SaleName string
  1077. }
  1078. // 客成发送邮箱
  1079. func KcSend(orderCode, personName string) {
  1080. log.Println("客成发邮件", orderCode)
  1081. bigmemberService := map[int64]string{}
  1082. combo := map[int64]string{}
  1083. Mysql.SelectByBath(1, func(l *[]map[string]interface{}) bool {
  1084. bigmemberService[common.Int64All((*l)[0]["id"])] = common.ObjToString((*l)[0]["s_name"])
  1085. return true
  1086. }, `select id,s_name from jianyu.bigmember_service`)
  1087. Mysql.SelectByBath(1, func(l *[]map[string]interface{}) bool {
  1088. combo[common.Int64All((*l)[0]["id"])] = common.ObjToString((*l)[0]["s_name"])
  1089. return true
  1090. }, `select id,s_name from jianyu.bigmember_combo`)
  1091. orderInfo := KcOrderFormat(orderCode, bigmemberService, combo)
  1092. if orderInfo != nil {
  1093. tableAppend := ""
  1094. if len(orderInfo.SeriveList) > 0 {
  1095. tableAppend = fmt.Sprintf(db.KeCheng.Mail.ServiceList, strings.Join(orderInfo.SeriveList, ","))
  1096. }
  1097. if orderInfo.ReturnMoney > 0 {
  1098. tableAppend += fmt.Sprintf(db.KeCheng.Mail.ReturnMoney, orderInfo.ReturnMoney)
  1099. }
  1100. table := ""
  1101. orderInfo.DisKcName = personName
  1102. deptData := Mysql.SelectBySql(`select b.mail as mail from jianyu.entniche_department_user a inner join jianyu.entniche_user b on a.dept_id=? and a.user_id=b.id `, db.KeCheng.DeptId)
  1103. table += fmt.Sprintf(db.KeCheng.Mail.Table, orderInfo.ProductType, orderInfo.CompanyName, orderInfo.UserPhone, orderInfo.UserName, orderInfo.OrderCode, orderInfo.PayMoney, orderInfo.StartEnd, orderInfo.DisKcName, orderInfo.SaleName, tableAppend)
  1104. //发送邮件
  1105. if deptData != nil && len(*deptData) > 0 && table != "" {
  1106. mailMap := map[string]bool{}
  1107. for _, m := range *deptData {
  1108. mailStr := gconv.String(m["mail"])
  1109. if mailStr == "" {
  1110. continue
  1111. }
  1112. if _, exists := mailMap[mailStr]; exists {
  1113. continue
  1114. }
  1115. mailMap[mailStr] = true
  1116. gmail := &mail.GmailAuth{
  1117. SmtpHost: db.Mail.SmtpHost,
  1118. SmtpPort: db.Mail.SmtpPort,
  1119. User: db.Mail.User,
  1120. Pwd: db.Mail.Pwd,
  1121. }
  1122. status := mail.GSendMail_q("剑鱼标讯", mailStr, "", "", db.KeCheng.Title, fmt.Sprintf(db.KeCheng.Mail.Content, table), "", "", gmail)
  1123. if status {
  1124. log.Println("客成发邮件 send mail success", table, mailStr)
  1125. }
  1126. }
  1127. }
  1128. }
  1129. }
  1130. func ExitKcSend(personName string, infoList []map[string]interface{}) {
  1131. //退出客成 即将退出客成信息
  1132. deptData := Mysql.SelectBySql("select b.mail from jianyu.entniche_department_user a inner join jianyu.entniche_user b on a.dept_id=? and a.user_id=b.id and b.name=? ", db.KeCheng.DeptId, personName)
  1133. adminData := Mysql.SelectBySql(` select c.mail from entniche_department_user a INNER JOIN entniche_user_role b on a.dept_id=59005 and a.user_id = b.user_id and b.role_id=2 INNER JOIN entniche_user c on a.user_id=c.id`)
  1134. if personName == "" {
  1135. //没有客成人员
  1136. adminMailStr := ""
  1137. if adminData != nil && len(*adminData) > 0 {
  1138. adminMailStr = gconv.String((*adminData)[0]["mail"])
  1139. }
  1140. if adminMailStr != "" {
  1141. gmail := &mail.GmailAuth{
  1142. SmtpHost: db.Mail.SmtpHost,
  1143. SmtpPort: db.Mail.SmtpPort,
  1144. User: db.Mail.User,
  1145. Pwd: db.Mail.Pwd,
  1146. }
  1147. //正文拼接
  1148. startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:</p><table><thead><tr><th>序号</th><th>公司名称</th><th>联系人</th><th>姓名</th><th>移交状态</th><th>移交销售原因</th></tr></thead><tbody>`
  1149. for i, v := range infoList {
  1150. startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["company_name"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["remrk"]), gconv.String(v["reason"]))
  1151. }
  1152. endStr := `</tbody></table><p></body></html>`
  1153. startStr += endStr
  1154. status := mail.GSendMail_q("剑鱼标讯", adminMailStr, "", "", "客户退出客成系统通知", startStr, "", "", gmail)
  1155. if status {
  1156. log.Println("客成发邮件 send mail success", startStr, adminMailStr)
  1157. }
  1158. }
  1159. return
  1160. }
  1161. if deptData != nil && len(*deptData) > 0 {
  1162. mailStr := gconv.String((*deptData)[0]["mail"])
  1163. adminMailStr := ""
  1164. if adminData != nil && len(*adminData) > 0 {
  1165. adminMailStr = gconv.String((*adminData)[0]["mail"])
  1166. }
  1167. if mailStr != "" {
  1168. gmail := &mail.GmailAuth{
  1169. SmtpHost: db.Mail.SmtpHost,
  1170. SmtpPort: db.Mail.SmtpPort,
  1171. User: db.Mail.User,
  1172. Pwd: db.Mail.Pwd,
  1173. }
  1174. //正文拼接
  1175. startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:</p><table><thead><tr><th>序号</th><th>公司名称</th><th>联系人</th><th>姓名</th><th>移交状态</th><th>移交销售原因</th></tr></thead><tbody>`
  1176. for i, v := range infoList {
  1177. startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["company_name"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["remrk"]), gconv.String(v["reason"]))
  1178. }
  1179. endStr := `</tbody></table><p></body></html>`
  1180. startStr += endStr
  1181. status := mail.GSendMail_q("剑鱼标讯", mailStr, adminMailStr, "", "客户退出客成系统通知", startStr, "", "", gmail)
  1182. if status {
  1183. log.Println("客成发邮件 send mail success", startStr, mailStr)
  1184. }
  1185. }
  1186. }
  1187. }
  1188. func KcOrderFormat(orderCode string, bigmemberService, combo map[int64]string) *OrderInfo {
  1189. orderData := Mysql.SelectBySql(`SELECT a.id,a.order_code,a.pay_money,a.user_phone,a.vip_starttime,a.vip_endtime,a.product_type,a.filter,a.user_id,a.ent_id,a.company_name,a.create_person,SUM(b.return_money) AS return_money
  1190. FROM jianyu.dataexport_order a
  1191. LEFT JOIN return_money_record b ON (a.order_code=b.order_code)
  1192. WHERE a.order_code=? AND a.order_status=1 AND (a.product_type='大会员' OR a.product_type='大会员-子账号')
  1193. GROUP BY a.id ORDER BY a.autoUpdate`, orderCode)
  1194. data := &OrderInfo{}
  1195. if orderData != nil && len(*orderData) > 0 {
  1196. id := common.Int64All((*orderData)[0]["id"])
  1197. user_phone := common.ObjToString((*orderData)[0]["user_phone"])
  1198. user_id := common.ObjToString((*orderData)[0]["user_id"])
  1199. product_type := common.ObjToString((*orderData)[0]["product_type"])
  1200. serviceList := []string{}
  1201. if filter := common.ObjToString((*orderData)[0]["filter"]); filter != "" {
  1202. filterMap := map[string]interface{}{}
  1203. json.Unmarshal([]byte(filter), &filterMap)
  1204. if level := common.Int64All(filterMap["level"]); level == 5 {
  1205. product_type += "自定义版"
  1206. for _, serversId := range strings.Split(common.ObjToString(filterMap["serversId"]), ",") {
  1207. if serviceName := bigmemberService[common.Int64All(serversId)]; serviceName != "" {
  1208. serviceList = append(serviceList, serviceName)
  1209. }
  1210. }
  1211. } else {
  1212. product_type += combo[common.Int64All(filterMap["comboId"])]
  1213. }
  1214. }
  1215. data = &OrderInfo{
  1216. Id: id,
  1217. CompanyName: common.ObjToString((*orderData)[0]["company_name"]),
  1218. UserId: user_id,
  1219. ProductType: product_type,
  1220. OrderCode: orderCode,
  1221. PayMoney: common.Float64All((*orderData)[0]["pay_money"]) / 100,
  1222. ReturnMoney: common.Float64All((*orderData)[0]["return_money"]) / 100,
  1223. UserPhone: user_phone,
  1224. StartEnd: fmt.Sprintf("%s--%s", strings.Split(common.ObjToString((*orderData)[0]["vip_starttime"]), " ")[0], strings.Split(common.ObjToString((*orderData)[0]["vip_endtime"]), " ")[0]),
  1225. SeriveList: serviceList,
  1226. SaleName: common.ObjToString((*orderData)[0]["create_person"]),
  1227. }
  1228. }
  1229. return data
  1230. }