job.go 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/gogf/gf/v2/util/gconv"
  6. "log"
  7. "strings"
  8. "time"
  9. "app.yhyue.com/moapp/jybase/date"
  10. "app.yhyue.com/moapp/jybase/redis"
  11. "app.yhyue.com/moapp/jybase/common"
  12. "app.yhyue.com/moapp/jybase/mongodb"
  13. )
  14. func FormatData(data map[string]interface{}, item string) (bool, bool) {
  15. userId, uId, positionId, source, cluename, phone, sourceCode, keyword := common.ObjToString(data["user_id"]), "", "", "", "", "", "", []string{}
  16. role, industry, department, departments, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", "", ""
  17. query, topname, subname, belong_to, sourceName, remark, sourceId := map[string]interface{}{}, "", "", "01", "", "", int64(0)
  18. nowTime := time.Now().Format("2006-01-02 15:04:05")
  19. if item == "orders" { //订单
  20. if !mongodb.IsObjectIdHex(userId) {
  21. positionId = userId
  22. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  23. if userMapping != nil && len(*userMapping) > 0 {
  24. userId = common.ObjToString((*userMapping)["userid"])
  25. }
  26. }
  27. query["userid"] = userId
  28. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  29. if userInfo != nil && len(*userInfo) > 0 {
  30. uId = common.ObjToString((*userInfo)["uid"])
  31. source = common.ObjToString((*userInfo)["source"]) //用户来源
  32. belong_to = common.ObjToString((*userInfo)["belong_to"]) //用户归属
  33. }
  34. cluename = common.ObjToString(data["company_name"])
  35. phone = common.ObjToString(data["user_phone"])
  36. } else if item == "message" {
  37. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"base_user_id": common.Int64All(data["own_id"])}, "", "")
  38. if userMapping != nil && len(*userMapping) > 0 {
  39. userId = common.ObjToString((*userMapping)["userid"])
  40. positionId = fmt.Sprint((*userMapping)["position_id"])
  41. }
  42. query["userid"] = userId
  43. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  44. if userInfo != nil && len(*userInfo) > 0 {
  45. uId = common.ObjToString((*userInfo)["uid"])
  46. source = common.ObjToString((*userInfo)["source"])
  47. belong_to = common.ObjToString((*userInfo)["belong_to"])
  48. phone = common.ObjToString((*userInfo)["phone"])
  49. if phone != "" {
  50. phoneMapping := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  51. if phoneMapping != nil && len(*phoneMapping) > 0 {
  52. cluename = common.ObjToString((*phoneMapping)["cluename"])
  53. }
  54. }
  55. }
  56. } else if item == "users" || item == "xcxusers" { //注册用户
  57. userId = mongodb.BsonIdToSId(data["userid"])
  58. //新用户没有uid、source要等5分钟
  59. cluename = common.ObjToString(data["company_name"])
  60. phone = common.ObjToString(data["phone"])
  61. if phone != "" {
  62. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  63. if contactsData != nil && len(*contactsData) > 0 {
  64. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  65. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  66. query["uid"] = uId
  67. } else {
  68. query["userid"] = userId
  69. }
  70. } else {
  71. query["userid"] = userId
  72. }
  73. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  74. if userInfo != nil && len(*userInfo) > 0 {
  75. uId = common.ObjToString((*userInfo)["uid"])
  76. source = common.ObjToString((*userInfo)["source"])
  77. belong_to = common.ObjToString((*userInfo)["belong_to"])
  78. }
  79. if item == "xcxusers" {
  80. sourceCode = common.ObjToString(data["s_sourceid"])
  81. if sourceCode == "" {
  82. log.Println("留资没有source", phone)
  83. return true, true
  84. }
  85. }
  86. }
  87. } else if item == "saleLeads" { //留资
  88. userId = common.ObjToString(data["userid"])
  89. if !mongodb.IsObjectIdHex(userId) {
  90. positionId = userId
  91. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  92. if userMapping != nil && len(*userMapping) > 0 {
  93. userId = common.ObjToString((*userMapping)["userid"])
  94. }
  95. }
  96. cluename = common.ObjToString(data["company"])
  97. phone = common.ObjToString(data["phone"])
  98. role = common.ObjToString(data["companyType"])
  99. industry = common.ObjToString(data["industry"])
  100. department = common.ObjToString(data["branch"])
  101. departments = common.ObjToString(data["department"])
  102. position = common.ObjToString(data["position"])
  103. name = common.ObjToString(data["name"])
  104. sourceCode = common.ObjToString(data["source"])
  105. if sourceCode == "" {
  106. log.Println("留资没有source", phone)
  107. return true, true
  108. }
  109. remark = common.ObjToString(data["jyRemark"]) //荟聚线索备注
  110. keywordArr := data["keyword"]
  111. if keywordArr != nil {
  112. keyword = common.ObjArrToStringArr(data["keyword"].([]interface{}))
  113. }
  114. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  115. if contactsData != nil && len(*contactsData) > 0 {
  116. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  117. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  118. query["uid"] = uId
  119. } else {
  120. query["userid"] = userId
  121. }
  122. } else {
  123. query["userid"] = userId
  124. }
  125. if userId != "" || uId != "" {
  126. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  127. if userInfo != nil && len(*userInfo) > 0 {
  128. uId = common.ObjToString((*userInfo)["uid"])
  129. source = common.ObjToString((*userInfo)["source"])
  130. belong_to = common.ObjToString((*userInfo)["belong_to"])
  131. userId = common.ObjToString((*userInfo)["userid"])
  132. }
  133. } else {
  134. if sourceCode == "report_retention" || sourceCode == "marketing_retention" { //荟聚线索来源,不是剑鱼用户,需要等待用户归集
  135. log.Println("不是剑鱼用户留资,等待用户", phone)
  136. return false, false
  137. } else if remark != "" {
  138. log.Println("留资source过滤", phone, sourceCode)
  139. return true, true
  140. }
  141. }
  142. } else if item == "eventReg" { //渠道
  143. userId = common.ObjToString(data["userid"])
  144. cluename = common.ObjToString(data["company"])
  145. phone = common.ObjToString(data["sign_phone"])
  146. role = common.ObjToString(data["company_type"])
  147. department = common.ObjToString(data["branch"])
  148. position = common.ObjToString(data["position"])
  149. name = common.ObjToString(data["sign_name"])
  150. sourceCode = common.ObjToString(data["source_code"])
  151. sourceName = common.ObjToString(data["source_name"])
  152. log.Println(sourceName)
  153. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  154. if contactsData != nil && len(*contactsData) > 0 {
  155. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  156. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  157. query["uid"] = uId
  158. } else {
  159. query["userid"] = userId
  160. }
  161. } else {
  162. query["userid"] = userId
  163. }
  164. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  165. if userInfo != nil && len(*userInfo) > 0 {
  166. uId = common.ObjToString((*userInfo)["uid"])
  167. source = common.ObjToString((*userInfo)["source"])
  168. belong_to = common.ObjToString((*userInfo)["belong_to"])
  169. userId = common.ObjToString((*userInfo)["userid"])
  170. }
  171. if role == "集成商" || role == "设计院" {
  172. role = "其他-" + role
  173. }
  174. } else if item == "readClue" {
  175. userId = common.ObjToString(data["userId"])
  176. query["userid"] = userId
  177. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  178. if userInfo != nil && len(*userInfo) > 0 {
  179. uId = common.ObjToString((*userInfo)["uid"])
  180. source = common.ObjToString((*userInfo)["source"])
  181. belong_to = common.ObjToString((*userInfo)["belong_to"])
  182. }
  183. // cluename = common.ObjToString(data["companyName"])
  184. phone = common.ObjToString(data["phone"])
  185. sourceId = common.Int64All(data["sourceId"])
  186. }
  187. if cluename == "" && item != "message" && item != "orders" && item != "readClue" {
  188. cluename = phone //没有线索名,手机号代替
  189. }
  190. isGroup, isCommerce := GetCompanyType(cluename) //判断是否集团公司、工商库
  191. if source == "0104" || strings.HasPrefix(belong_to, "02") || source == "0102" || source == "0103" || phone == "" { //参照用户来源代码表
  192. //log.Println("线索分配失败,线索过滤!!", item, source, phone, userId)
  193. //saveHlyj(belong_to, item, phone, name, sourceName, cluename, position, nowTime, isGroup, isCommerce)
  194. return true, true
  195. }
  196. if uId == "" { //没有进用户归集,等待进入,结束任务,放在下次继续执行
  197. if isExists, _ := redis.Exists("bidx", "bidx_userId_"+userId); isExists {
  198. redisInt := redis.GetInt("bidx", "bidx_userId_"+userId)
  199. if redisInt > 4 {
  200. log.Println("线索分配失败,线索缺少信息,任务已执行超过1次", item, userId, phone)
  201. return true, true
  202. } else {
  203. redis.Incr("bidx", "bidx_userId_"+userId)
  204. }
  205. } else {
  206. redis.Put("bidx", "bidx_userId_"+userId, 1, 3600)
  207. }
  208. log.Println("线索分配失败,线索缺少信息", item, phone, userId)
  209. return false, false
  210. }
  211. if item == "orders" { //订单没信息再查一遍留资
  212. qid := positionId
  213. if qid == "" {
  214. qid = userId
  215. }
  216. saleLeadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"userid": qid}, map[string]interface{}{"_id": -1}, nil, false, 0, 1)
  217. if ok && saleLeadsData != nil && len(*saleLeadsData) > 0 {
  218. sdata := (*saleLeadsData)[0]
  219. role = common.ObjToString(sdata["companyType"])
  220. industry = common.ObjToString(sdata["industry"])
  221. department = common.ObjToString(sdata["branch"])
  222. departments = common.ObjToString(sdata["department"])
  223. position = common.ObjToString(sdata["position"])
  224. name = common.ObjToString(sdata["name"])
  225. }
  226. }
  227. top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data, sourceCode, sourceId) //查留资来源名字
  228. if topname == "市场活动" && item == "saleLeads" { //市场活动的不要
  229. log.Println("市场活动留资过滤 ", userId, phone)
  230. return true, true
  231. }
  232. if strings.HasPrefix(belong_to, "03") { //一切都好的不进
  233. isOk := saveEverything(userId, phone, item, subname, sourceCode)
  234. log.Println("渠道线索电销", userId, phone, item, subname, sourceCode)
  235. if !isOk {
  236. return true, true
  237. }
  238. }
  239. follow_project_area = getAreaCode(userId) //关注项目区域
  240. log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area, isGroup, isCommerce)
  241. if top_cluetype == "" || sub_cluetype == "" || level == "" {
  242. log.Println("线索分配失败,线索过滤top_cluetype!!", item, uId, phone, userId)
  243. return true, true
  244. }
  245. position_id, seatNumber, saleName, saleData, pIsOk, isFreeze, noticePositionId := autoDraw(level, cluename, phone, isGroup, isCommerce) //查询当前分配次数最少的,如果当前线索有销售,此次找出的不会+1
  246. log.Println("data -------", position_id, seatNumber, saleName, pIsOk, isFreeze, noticePositionId)
  247. if position_id > 0 && seatNumber != "" {
  248. uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
  249. if uCount != nil && len(*uCount) > 0 { //已存在,走更新
  250. batch_import := common.ObjToString((*uCount)["batch_import"])
  251. if batch_import != "" && item == "users" { //有导入批次号还是新用户,不执行
  252. return true, true
  253. }
  254. //oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, pIsOk, "")
  255. oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, pIsOk)
  256. } else { //不存在走新增
  257. oks = SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, isFreeze, "")
  258. if oks { //新增成功,销售分配次数+1
  259. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, position_id, level)
  260. }
  261. }
  262. if !oks {
  263. log.Println("线索分配失败!!", item, position_id, seatNumber, uId, userId, phone)
  264. return false, false
  265. }
  266. } else {
  267. if isFreeze {
  268. //达上限放公海处理
  269. uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
  270. if uCount == nil || len(*uCount) == 0 {
  271. oks = SaveClue0(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, isFreeze)
  272. log.Println(oks)
  273. } else {
  274. //留资线索更新
  275. if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
  276. BCPCID := common.GetRandom(32)
  277. clueId := common.Int64All((*uCount)["id"])
  278. old_name := common.ObjToString((*uCount)["name"])
  279. old_position := common.ObjToString((*uCount)["position"])
  280. old_department := common.ObjToString((*uCount)["department"])
  281. old_follow_project_area := common.ObjToString((*uCount)["follow_project_area"])
  282. old_role := common.ObjToString((*uCount)["role"])
  283. old_cluename := common.ObjToString((*uCount)["cluename"])
  284. old_top_cluetype := common.ObjToString((*uCount)["top_cluetype"])
  285. old_sub_cluetype := common.ObjToString((*uCount)["sub_cluetype"])
  286. clueUpdateData := map[string]interface{}{}
  287. if old_name != name && name != "" {
  288. clueUpdateData["name"] = name
  289. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  290. "clue_id": clueId,
  291. "change_field": "name",
  292. "position_id": 0,
  293. "change_type": "基本信息变更",
  294. "old_value": common.If(old_name != "", old_name, "/"),
  295. "new_value": common.If(name != "", name, "/"),
  296. "createtime": nowTime,
  297. "BCPCID": BCPCID,
  298. "operator_id": -1})
  299. }
  300. if old_position != position && position != "" {
  301. clueUpdateData["position"] = position
  302. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  303. "clue_id": clueId,
  304. "change_field": "position",
  305. "position_id": 0,
  306. "change_type": "基本信息变更",
  307. "old_value": common.If(old_position != "", old_position, "/"),
  308. "new_value": common.If(position != "", position, "/"),
  309. "createtime": nowTime,
  310. "BCPCID": BCPCID,
  311. "operator_id": -1})
  312. }
  313. if old_department != department && department != "" {
  314. clueUpdateData["department"] = department
  315. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  316. "clue_id": clueId,
  317. "change_field": "department",
  318. "position_id": 0,
  319. "change_type": "基本信息变更",
  320. "old_value": common.If(old_department != "", old_department, "/"),
  321. "new_value": common.If(department != "", department, "/"),
  322. "createtime": nowTime,
  323. "BCPCID": BCPCID,
  324. "operator_id": -1})
  325. }
  326. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  327. clueUpdateData["follow_project_area"] = follow_project_area
  328. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  329. "clue_id": clueId,
  330. "change_field": "follow_project_area",
  331. "position_id": 0,
  332. "change_type": "基本信息变更",
  333. "old_value": common.If(old_follow_project_area != "", old_follow_project_area, "/"),
  334. "new_value": common.If(follow_project_area != "", follow_project_area, "/"),
  335. "createtime": nowTime,
  336. "BCPCID": BCPCID,
  337. "operator_id": -1})
  338. }
  339. if old_role != role && role != "" {
  340. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  341. "clue_id": clueId,
  342. "change_field": "role",
  343. "change_type": "基本信息变更",
  344. "old_value": common.If(old_role != "", old_role, "/"),
  345. "new_value": common.If(role != "", role, "/"),
  346. "createtime": nowTime,
  347. "position_id": 0,
  348. "BCPCID": BCPCID,
  349. "operator_id": -1})
  350. clueUpdateData["role"] = role
  351. }
  352. if old_cluename != cluename && cluename != "" {
  353. clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
  354. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  355. "clue_id": clueId,
  356. "change_field": "cluename",
  357. "change_type": "基本信息变更",
  358. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  359. "new_value": common.If(cluename != "", cluename, "/"),
  360. "createtime": nowTime,
  361. "BCPCID": BCPCID,
  362. "position_id": 0,
  363. "operator_id": -1})
  364. }
  365. old_topname := ""
  366. old_subname := ""
  367. if old_top_cluetype != "" {
  368. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  369. if pcodeData != nil && len(*pcodeData) > 0 {
  370. old_topname = common.ObjToString((*pcodeData)["name"])
  371. }
  372. }
  373. if old_sub_cluetype != "" {
  374. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  375. if pcodeData != nil && len(*pcodeData) > 0 {
  376. old_subname = common.ObjToString((*pcodeData)["name"])
  377. }
  378. }
  379. if old_top_cluetype != top_cluetype && top_cluetype != "" {
  380. clueUpdateData["top_cluetype"] = top_cluetype //新的线索名不为空才替换
  381. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  382. "clue_id": clueId,
  383. "change_field": "top_cluetype",
  384. "position_id": 0,
  385. "change_type": "基本信息变更",
  386. "old_value": common.If(old_topname != "", old_topname, "/"),
  387. "new_value": common.If(topname != "", topname, "/"),
  388. "createtime": nowTime,
  389. "BCPCID": BCPCID,
  390. "operator_id": -1})
  391. }
  392. if old_sub_cluetype != sub_cluetype && sub_cluetype != "" {
  393. clueUpdateData["sub_cluetype"] = sub_cluetype //新的线索名不为空才替换
  394. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  395. "clue_id": clueId,
  396. "change_field": "sub_cluetype",
  397. "position_id": 0,
  398. "change_type": "基本信息变更",
  399. "old_value": common.If(old_subname != "", old_subname, "/"),
  400. "new_value": common.If(subname != "", subname, "/"),
  401. "createtime": nowTime,
  402. "BCPCID": BCPCID,
  403. "operator_id": -1})
  404. }
  405. clueUpdateData["company_nature"] = isGroup
  406. clueUpdateData["company_verification"] = isCommerce
  407. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  408. }
  409. }
  410. AssFail(noticePositionId, cluename, phone)
  411. return true, true
  412. }
  413. return true, false
  414. }
  415. return true, true
  416. }
  417. func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, isFreeze bool, demand string) bool {
  418. nowTime := time.Now().Format("2006-01-02 15:04:05")
  419. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  420. clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
  421. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  422. keywords := ""
  423. if sourceCode == "app_xzcyh" {
  424. if len(keyword) > 0 && keyword[0] != "" {
  425. keywords = strings.Join(keyword, ",")
  426. }
  427. }
  428. if cluename == "" {
  429. cluename = phone
  430. }
  431. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  432. "userid": userId,
  433. "uid": uId,
  434. "seatNumber": seatNumber,
  435. "position_id": positionId,
  436. "is_assign": common.If(isFreeze, -3, 1),
  437. "comeintime": nowTime,
  438. "createtime": nowTime,
  439. "updatetime": nowTime,
  440. "cluename": cluename,
  441. "top_cluetype": top_cluetype,
  442. "sub_cluetype": sub_cluetype,
  443. "trailstatus": "01",
  444. "name": name,
  445. "phone": phone,
  446. "position": position,
  447. "department": common.If(sourceCode == "app_xzcyh", departments, department),
  448. "industry": industry,
  449. "follow_project_area": follow_project_area,
  450. "role": role,
  451. "comeinsource_private": 2,
  452. "is_task": 1,
  453. "task_time": nowTime,
  454. "tasktime": common.If(item == "users", nowTimes, nowTime),
  455. "taskstatus": 0,
  456. "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
  457. "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
  458. "company_nature": isGroup,
  459. "company_verification": isCommerce,
  460. "remark": remark,
  461. "customer_demand": demand,
  462. "FREEZE_TIME": nowTime,
  463. })
  464. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  465. "clue_id": clueId,
  466. "position_id": positionId,
  467. "change_type": "创建线索",
  468. "new_value": "系统自动创建",
  469. "createtime": nowTime,
  470. "BCPCID": common.GetRandom(32),
  471. "operator_id": -1,
  472. })
  473. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  474. "clue_id": clueId,
  475. "position_id": positionId,
  476. "change_field": "position_id",
  477. "change_type": "所属人变更",
  478. "old_value": "/",
  479. "new_value": saleName,
  480. "createtime": nowTime,
  481. "BCPCID": common.GetRandom(32),
  482. "operator_id": -1,
  483. })
  484. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  485. "clue_id": clueId,
  486. "position_id": positionId,
  487. "change_field": "trailstatus",
  488. "change_type": "基本信息变更",
  489. "old_value": "商机线索",
  490. "new_value": "新增",
  491. "createtime": nowTime,
  492. "BCPCID": common.GetRandom(32),
  493. "operator_id": -1,
  494. })
  495. uodateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  496. "clue_id": clueId,
  497. "position_id": positionId,
  498. "change_type": "加入任务车",
  499. "new_value": "线索自动分配" + "-" + topname + "-" + subname,
  500. "createtime": nowTime,
  501. "BCPCID": common.GetRandom(32),
  502. "operator_id": -1,
  503. })
  504. uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  505. "clue_id": clueId,
  506. "position_id": positionId,
  507. "change_field": "top_cluetype",
  508. "change_type": "基本信息变更",
  509. "old_value": "/",
  510. "new_value": topname,
  511. "createtime": nowTime,
  512. "BCPCID": common.GetRandom(32),
  513. "operator_id": -1,
  514. })
  515. uodateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  516. "clue_id": clueId,
  517. "position_id": positionId,
  518. "change_field": "sub_cluetype",
  519. "change_type": "基本信息变更",
  520. "old_value": "/",
  521. "new_value": subname,
  522. "createtime": nowTime,
  523. "BCPCID": common.GetRandom(32),
  524. "operator_id": -1,
  525. })
  526. //冻结处理
  527. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1 && uodateId6 > -1
  528. }) {
  529. log.Println("线索分配成功")
  530. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  531. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  532. "status": 1,
  533. "is_delete": 1,
  534. "createtime": nowTime,
  535. "updatetime": nowTime,
  536. "phone": phone,
  537. "baseinfo_id": uId,
  538. "SOURCE": source,
  539. })
  540. }
  541. return true
  542. } else {
  543. log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  544. return false
  545. }
  546. }
  547. func SaveClue0(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, isFreeze bool) bool {
  548. nowTime := time.Now().Format("2006-01-02 15:04:05")
  549. clueId, uodateId1, uodateId2, uodateId3 := int64(0), int64(0), int64(0), int64(0)
  550. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  551. keywords := ""
  552. if sourceCode == "app_xzcyh" {
  553. if len(keyword) > 0 && keyword[0] != "" {
  554. keywords = strings.Join(keyword, ",")
  555. }
  556. }
  557. if cluename == "" {
  558. cluename = phone
  559. }
  560. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  561. "userid": userId,
  562. "uid": uId,
  563. "seatNumber": "",
  564. "position_id": 0,
  565. "is_assign": 0,
  566. "comeintime": nowTime,
  567. "createtime": nowTime,
  568. "updatetime": nowTime,
  569. "cluename": cluename,
  570. "top_cluetype": top_cluetype,
  571. "sub_cluetype": sub_cluetype,
  572. "trailstatus": "01",
  573. "name": name,
  574. "phone": phone,
  575. "position": position,
  576. "department": common.If(sourceCode == "app_xzcyh", departments, department),
  577. "industry": industry,
  578. "follow_project_area": follow_project_area,
  579. "role": role,
  580. "comeinsource_private": 2,
  581. "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
  582. "company_nature": isGroup,
  583. "company_verification": isCommerce,
  584. "remark": remark,
  585. "FREEZE_TIME": nowTime,
  586. })
  587. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  588. "clue_id": clueId,
  589. "position_id": positionId,
  590. "change_type": "创建线索",
  591. "new_value": "系统自动创建",
  592. "createtime": nowTime,
  593. "BCPCID": common.GetRandom(32),
  594. "operator_id": -1,
  595. })
  596. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  597. "clue_id": clueId,
  598. "position_id": positionId,
  599. "change_field": "position_id",
  600. "change_type": "所属人变更",
  601. "old_value": "",
  602. "new_value": "/",
  603. "createtime": nowTime,
  604. "BCPCID": common.GetRandom(32),
  605. "operator_id": -1,
  606. })
  607. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  608. "clue_id": clueId,
  609. "position_id": positionId,
  610. "change_field": "trailstatus",
  611. "change_type": "基本信息变更",
  612. "old_value": "商机线索",
  613. "new_value": "新增",
  614. "createtime": nowTime,
  615. "BCPCID": common.GetRandom(32),
  616. "operator_id": -1,
  617. })
  618. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1
  619. }) {
  620. log.Println("线索分配成功")
  621. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  622. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  623. "status": 1,
  624. "is_delete": 1,
  625. "createtime": nowTime,
  626. "updatetime": nowTime,
  627. "phone": phone,
  628. "baseinfo_id": uId,
  629. "SOURCE": source,
  630. })
  631. }
  632. return true
  633. } else {
  634. log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  635. return false
  636. }
  637. }
  638. func WorkUpdateClue(data map[string]interface{}, saleData []map[string]interface{}, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, pIsOk bool, demand string) bool {
  639. log.Println("工单线索修改前", data)
  640. nowTime := time.Now().Format("2006-01-02 15:04:05")
  641. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  642. trailstatus := common.ObjToString(data["trailstatus"]) //data都为原线索数据
  643. trailstatusTime := common.ObjToString(data["trailstatus_time"])
  644. var trailstatusTimes time.Time
  645. if trailstatusTime != "" {
  646. trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
  647. }
  648. clueId := common.Int64All(data["id"])
  649. is_assign := common.IntAll(data["is_assign"])
  650. oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
  651. old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
  652. oldTaskTime = common.ObjToString(data["tasktime"])
  653. is_task = common.IntAll(data["is_task"])
  654. taskstatus = common.IntAll(data["taskstatus"])
  655. BCPCID := common.GetRandom(32)
  656. old_name := common.ObjToString(data["name"])
  657. old_position := common.ObjToString(data["position"])
  658. old_department := common.ObjToString(data["department"])
  659. old_follow_project_area := common.ObjToString(data["follow_project_area"])
  660. old_role := common.ObjToString(data["role"])
  661. old_cluename := common.ObjToString(data["cluename"])
  662. old_top_cluetype := common.ObjToString(data["top_cluetype"])
  663. old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
  664. old_topname, old_subname := "", ""
  665. is_transfer := common.IntAll(data["is_transfer"])
  666. if taskstatus == 1 || is_task == 0 {
  667. taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
  668. } else {
  669. if is_task == 1 {
  670. if oldTaskTime != "" { //以最新的任务时间为主
  671. t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
  672. if err == nil && time.Now().Before(t1) {
  673. taskTime = nowTime
  674. } else {
  675. taskTime = oldTaskTime
  676. }
  677. }
  678. }
  679. }
  680. count, counts, t := 0, 0, time.Now() //加入任务车判断节假日
  681. for {
  682. count++
  683. currentTime := t.AddDate(0, 0, -count)
  684. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  685. isok := false
  686. for k, v := range DateMap {
  687. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  688. isok = true
  689. }
  690. }
  691. if isok {
  692. counts++
  693. }
  694. } else {
  695. isok := true
  696. for k, v := range DateMap {
  697. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  698. isok = false
  699. }
  700. }
  701. if isok {
  702. counts++
  703. }
  704. }
  705. if counts >= 2 {
  706. break
  707. }
  708. }
  709. //两天之前不包含节假日,有没有跟进记录
  710. recordCount := TiDb.CountBySql(`select count(1) from dwd_f_crm_trail_content where clue_id = ? and createtime > ?`, clueId, t.AddDate(0, 0, -count).Format(date.Date_Full_Layout))
  711. //
  712. clueUpdateData := map[string]interface{}{
  713. "updatetime": nowTime,
  714. "top_cluetype": top_cluetype,
  715. "sub_cluetype": sub_cluetype,
  716. "userid": userId,
  717. "comeinsource_private": 2,
  718. "tasksource": gconv.String(common.If(gconv.Int64(data["position_id"]) == 0, "线索自动分配", "线索来源自动更新")) + "-" + topname + "-" + subname,
  719. "company_nature": isGroup,
  720. "company_verification": isCommerce,
  721. }
  722. old_remark := common.ObjToString(data["remark"])
  723. if old_remark != "" {
  724. remark = old_remark + ";" + remark //备注不能替换,只能往上拼接
  725. }
  726. clueUpdateData["remark"] = remark
  727. old_demand := common.ObjToString(data["customer_demand"])
  728. if demand != old_demand {
  729. clueUpdateData["customer_demand"] = demand
  730. }
  731. if cluename != "" {
  732. clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
  733. }
  734. if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
  735. if name != "" {
  736. clueUpdateData["name"] = name
  737. }
  738. if position != "" {
  739. clueUpdateData["position"] = position
  740. }
  741. if department != "" {
  742. clueUpdateData["department"] = department
  743. }
  744. if follow_project_area != "" {
  745. clueUpdateData["follow_project_area"] = follow_project_area
  746. }
  747. if role != "" {
  748. clueUpdateData["role"] = role
  749. }
  750. // clueUpdateData["industry"] = industry
  751. }
  752. if sourceCode == "app_xzcyh" { //这个留资单独处理一下
  753. if departments != "" {
  754. clueUpdateData["department"] = departments
  755. department = departments
  756. }
  757. business_scope := common.ObjToString(data["business_scope"])
  758. keywords := ""
  759. if len(keyword) > 0 && keyword[0] != "" {
  760. keywords = strings.Join(keyword, ",")
  761. if business_scope != "" {
  762. clueUpdateData["business_scope"] = business_scope + "," + keywords
  763. } else {
  764. clueUpdateData["business_scope"] = keywords
  765. }
  766. }
  767. }
  768. if old_position_id != 0 {
  769. for _, v := range saleData {
  770. if old_position_id == common.Int64All(v["position_id"]) {
  771. oldsaleName = common.ObjToString(v["name"])
  772. }
  773. }
  774. }
  775. if trailstatus == "00" || old_position_id == 0 || is_assign != 1 || (pIsOk && old_cluename != cluename && cluename != "") {
  776. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  777. } else {
  778. clueUpdateData["seatNumber"] = seatNumber
  779. clueUpdateData["position_id"] = positionId
  780. clueUpdateData["comeintime"] = nowTime
  781. clueUpdateData["is_assign"] = 1
  782. clueUpdateData["comeinsource_open"] = nil
  783. clueUpdateData["level_open"] = nil
  784. clueUpdateData["clue_level"] = nil
  785. }
  786. } else if is_assign == 1 && positionId != old_position_id {
  787. clueUpdateData["seatNumber"] = seatNumber
  788. clueUpdateData["position_id"] = positionId
  789. clueUpdateData["comeintime"] = nowTime
  790. clueUpdateData["is_assign"] = 1
  791. clueUpdateData["comeinsource_open"] = nil
  792. clueUpdateData["level_open"] = nil
  793. clueUpdateData["clue_level"] = nil
  794. } else { //不属于上边情况就还是属于原来的销售,不更换销售
  795. clueUpdateData["seatNumber"] = old_seatNumber
  796. clueUpdateData["position_id"] = old_position_id
  797. clueUpdateData["is_assign"] = 1
  798. clueUpdateData["comeinsource_open"] = nil
  799. clueUpdateData["level_open"] = nil
  800. clueUpdateData["clue_level"] = nil
  801. }
  802. if trailstatus != "08" && is_assign == 0 { //在公海的成交客户
  803. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  804. } else {
  805. clueUpdateData["trailstatus"] = "01"
  806. clueUpdateData["trailstatus_time"] = nowTime
  807. }
  808. }
  809. is_ok := false
  810. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  811. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  812. } else {
  813. if item == "eventReg" {
  814. eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
  815. if eData != nil && len(*eData) > 0 {
  816. for _, e := range *eData {
  817. new_value := common.ObjToString(e["new_value"])
  818. if strings.Contains(new_value, subname) {
  819. is_ok = true
  820. }
  821. }
  822. }
  823. if !is_ok {
  824. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  825. if taskTime == "" {
  826. taskTime = nowTime
  827. }
  828. clueUpdateData["task_time"] = nowTime
  829. clueUpdateData["tasktime"] = taskTime
  830. clueUpdateData["taskstatus"] = 0
  831. }
  832. if is_transfer != 1 {
  833. clueUpdateData["is_task"] = 1
  834. }
  835. }
  836. } else {
  837. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  838. if taskTime == "" {
  839. taskTime = nowTime
  840. }
  841. clueUpdateData["task_time"] = nowTime
  842. clueUpdateData["tasktime"] = taskTime
  843. clueUpdateData["taskstatus"] = 0
  844. }
  845. if is_transfer != 1 {
  846. clueUpdateData["is_task"] = 1
  847. }
  848. }
  849. }
  850. }
  851. ok, updateId14 := true, true
  852. updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
  853. updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
  854. updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
  855. if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
  856. //线索
  857. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  858. //变更记录
  859. if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
  860. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  861. } else if old_position_id == 0 {
  862. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  863. "clue_id": clueId,
  864. "position_id": positionId,
  865. "change_field": "position_id",
  866. "change_type": "所属人变更",
  867. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  868. "new_value": common.If(saleName != "", saleName, "/"),
  869. "createtime": nowTime,
  870. "BCPCID": common.GetRandom(32),
  871. "operator_id": -1,
  872. })
  873. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  874. "clue_id": clueId,
  875. "position_id": positionId,
  876. "change_field": "trailstatus",
  877. "change_type": "基本信息变更",
  878. "old_value": CodeTrail[trailstatus],
  879. "new_value": "新增",
  880. "createtime": nowTime,
  881. "BCPCID": common.GetRandom(32),
  882. "operator_id": -1,
  883. })
  884. } else {
  885. if pIsOk && old_cluename != cluename && cluename != "" {
  886. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  887. "clue_id": clueId,
  888. "position_id": positionId,
  889. "change_field": "position_id",
  890. "change_type": "所属人变更",
  891. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  892. "new_value": common.If(saleName != "", saleName, "/"),
  893. "createtime": nowTime,
  894. "BCPCID": common.GetRandom(32),
  895. "operator_id": -1,
  896. })
  897. } else {
  898. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  899. "clue_id": clueId,
  900. "position_id": positionId,
  901. "change_field": "position_id",
  902. "change_type": "所属人变更",
  903. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  904. "new_value": common.If(saleName != "", saleName, "/"),
  905. "createtime": nowTime,
  906. "BCPCID": common.GetRandom(32),
  907. "operator_id": -1,
  908. })
  909. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  910. "clue_id": clueId,
  911. "position_id": common.Int64All(data["position_id"]),
  912. "change_field": "trailstatus",
  913. "change_type": "基本信息变更",
  914. "old_value": "无意向客户",
  915. "new_value": "流失",
  916. "createtime": nowTime,
  917. "BCPCID": common.GetRandom(32),
  918. "operator_id": -1,
  919. })
  920. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  921. "clue_id": clueId,
  922. "position_id": positionId,
  923. "change_field": "trailstatus",
  924. "change_type": "基本信息变更",
  925. "old_value": "商机线索",
  926. "new_value": "新增",
  927. "createtime": nowTime,
  928. "BCPCID": common.GetRandom(32),
  929. "operator_id": -1,
  930. })
  931. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  932. "clue_id": clueId,
  933. "position_id": positionId,
  934. "change_field": "trailstatus",
  935. "change_type": "基本信息变更",
  936. "old_value": "无意向客户",
  937. "new_value": "商机线索",
  938. "createtime": nowTime,
  939. "BCPCID": common.GetRandom(32),
  940. "operator_id": -1,
  941. })
  942. }
  943. }
  944. } else if trailstatus != "08" && is_assign == 0 {
  945. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  946. } else {
  947. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  948. "clue_id": clueId,
  949. "position_id": positionId,
  950. "change_field": "position_id",
  951. "change_type": "所属人变更",
  952. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  953. "new_value": common.If(saleName != "", saleName, "/"),
  954. "createtime": nowTime,
  955. "BCPCID": common.GetRandom(32),
  956. "operator_id": -1,
  957. })
  958. if trailstatus != "01" {
  959. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  960. "clue_id": clueId,
  961. "position_id": positionId,
  962. "change_field": "trailstatus",
  963. "change_type": "基本信息变更",
  964. "old_value": CodeTrail[trailstatus],
  965. "new_value": "商机线索",
  966. "createtime": nowTime,
  967. "BCPCID": common.GetRandom(32),
  968. "operator_id": -1,
  969. })
  970. }
  971. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  972. "clue_id": clueId,
  973. "position_id": positionId,
  974. "change_field": "trailstatus",
  975. "change_type": "基本信息变更",
  976. "old_value": "商机线索",
  977. "new_value": "新增",
  978. "createtime": nowTime,
  979. "BCPCID": common.GetRandom(32),
  980. "operator_id": -1,
  981. })
  982. }
  983. } else if (pIsOk && old_cluename == cluename && cluename != "") && is_assign == 1 && positionId != old_position_id {
  984. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  985. "clue_id": clueId,
  986. "position_id": positionId,
  987. "change_field": "position_id",
  988. "change_type": "所属人变更",
  989. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  990. "new_value": common.If(saleName != "", saleName, "/"),
  991. "createtime": nowTime,
  992. "BCPCID": common.GetRandom(32),
  993. "operator_id": -1,
  994. })
  995. }
  996. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  997. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  998. } else {
  999. if old_sub_cluetype != sub_cluetype && !is_ok {
  1000. if is_transfer != 1 {
  1001. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1002. "clue_id": clueId,
  1003. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))),
  1004. "change_type": "加入任务车",
  1005. "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
  1006. "createtime": nowTime,
  1007. "BCPCID": common.GetRandom(32),
  1008. "operator_id": -1,
  1009. })
  1010. } else {
  1011. cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
  1012. if cdata != nil && len(*cdata) > 0 {
  1013. kcpositionId := common.Int64All((*cdata)["position_id"])
  1014. tasksource := common.ObjToString((*cdata)["tasksource"])
  1015. taskstatus := common.IntAll((*cdata)["taskstatus"])
  1016. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1017. "clue_id": clueId,
  1018. "position_id": kcpositionId,
  1019. "change_type": "加入任务车",
  1020. "new_value": "线上主动留资",
  1021. "createtime": nowTime,
  1022. "BCPCID": common.GetRandom(32),
  1023. "operator_id": -1,
  1024. })
  1025. if taskstatus == 1 {
  1026. tasksource = "10"
  1027. } else {
  1028. if tasksource != "" && !strings.Contains(tasksource, "10") {
  1029. if !strings.Contains(tasksource, "10") {
  1030. tasksource += ",10"
  1031. }
  1032. } else {
  1033. tasksource = "10"
  1034. }
  1035. }
  1036. updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  1037. "tasksource": tasksource,
  1038. "tasktime": nowTime,
  1039. "is_task": 1,
  1040. "taskstatus": 0,
  1041. })
  1042. }
  1043. }
  1044. }
  1045. }
  1046. }
  1047. if old_top_cluetype != "" {
  1048. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  1049. if pcodeData != nil && len(*pcodeData) > 0 {
  1050. old_topname = common.ObjToString((*pcodeData)["name"])
  1051. }
  1052. }
  1053. if old_sub_cluetype != "" {
  1054. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  1055. if pcodeData != nil && len(*pcodeData) > 0 {
  1056. old_subname = common.ObjToString((*pcodeData)["name"])
  1057. }
  1058. }
  1059. if item != "orders" {
  1060. if old_name != name && name != "" {
  1061. updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1062. "clue_id": clueId,
  1063. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1064. "change_field": "name",
  1065. "change_type": "基本信息变更",
  1066. "old_value": common.If(old_name != "", old_name, "/"),
  1067. "new_value": common.If(name != "", name, "/"),
  1068. "createtime": nowTime,
  1069. "BCPCID": BCPCID,
  1070. "operator_id": -1,
  1071. })
  1072. }
  1073. if old_position != position && position != "" {
  1074. updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1075. "clue_id": clueId,
  1076. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1077. "change_field": "position",
  1078. "change_type": "基本信息变更",
  1079. "old_value": common.If(old_position != "", old_position, "/"),
  1080. "new_value": common.If(position != "", position, "/"),
  1081. "createtime": nowTime,
  1082. "BCPCID": BCPCID,
  1083. "operator_id": -1,
  1084. })
  1085. }
  1086. if old_department != department && department != "" {
  1087. updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1088. "clue_id": clueId,
  1089. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1090. "change_field": "department",
  1091. "change_type": "基本信息变更",
  1092. "old_value": common.If(old_department != "", old_department, "/"),
  1093. "new_value": common.If(department != "", department, "/"),
  1094. "createtime": nowTime,
  1095. "BCPCID": BCPCID,
  1096. "operator_id": -1,
  1097. })
  1098. }
  1099. if old_role != role && role != "" {
  1100. updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))), "change_field": "role", "change_type": "基本信息变更", "old_value": common.If(old_role != "", old_role, "/"), "new_value": common.If(role != "", role, "/"), "createtime": nowTime, "BCPCID": BCPCID, "operator_id": -1})
  1101. }
  1102. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  1103. old_area, old_area_arr := "", []string{}
  1104. new_area, new_area_arr := "", []string{}
  1105. for _, v := range strings.Split(old_follow_project_area, ",") {
  1106. old_area_arr = append(old_area_arr, CodeArea[v])
  1107. }
  1108. for _, v := range strings.Split(follow_project_area, ",") {
  1109. new_area_arr = append(new_area_arr, CodeArea[v])
  1110. }
  1111. old_area = strings.Join(old_area_arr, ",")
  1112. new_area = strings.Join(new_area_arr, ",")
  1113. updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1114. "clue_id": clueId,
  1115. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1116. "change_field": "follow_project_area",
  1117. "change_type": "基本信息变更",
  1118. "old_value": common.If(old_area != "", old_area, "/"),
  1119. "new_value": common.If(new_area != "", new_area, "/"),
  1120. "createtime": nowTime,
  1121. "BCPCID": BCPCID,
  1122. "operator_id": -1,
  1123. })
  1124. }
  1125. if old_cluename != cluename && cluename != "" {
  1126. updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1127. "clue_id": clueId,
  1128. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1129. "change_field": "cluename",
  1130. "change_type": "基本信息变更",
  1131. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  1132. "new_value": common.If(cluename != "", cluename, "/"),
  1133. "createtime": nowTime,
  1134. "BCPCID": BCPCID,
  1135. "operator_id": -1,
  1136. })
  1137. }
  1138. }
  1139. if old_top_cluetype != top_cluetype {
  1140. updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1141. "clue_id": clueId,
  1142. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1143. "change_field": "top_cluetype",
  1144. "change_type": "基本信息变更",
  1145. "old_value": common.If(old_topname != "", old_topname, "/"),
  1146. "new_value": common.If(topname != "", topname, "/"),
  1147. "createtime": nowTime,
  1148. "BCPCID": BCPCID,
  1149. "operator_id": -1,
  1150. })
  1151. }
  1152. if old_sub_cluetype != sub_cluetype {
  1153. updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1154. "clue_id": clueId,
  1155. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))))),
  1156. "change_field": "sub_cluetype",
  1157. "change_type": "基本信息变更",
  1158. "old_value": common.If(old_subname != "", old_subname, "/"),
  1159. "new_value": common.If(subname != "", subname, "/"),
  1160. "createtime": nowTime,
  1161. "BCPCID": BCPCID,
  1162. "operator_id": -1,
  1163. })
  1164. }
  1165. return ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 && updateId4 > -1 && updateId5 > -1 && updateId6 > -1 && updateId7 > -1 && updateId8 > -1 && updateId9 > -1 && updateId10 > -1 && updateId11 > -1 && updateId12 > -1 && updateId13 > -1 && updateId14
  1166. }) {
  1167. log.Println("线索更新成功")
  1168. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  1169. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  1170. "status": 1,
  1171. "is_delete": 1,
  1172. "createtime": nowTime,
  1173. "updatetime": nowTime,
  1174. "phone": phone,
  1175. "baseinfo_id": uId,
  1176. "SOURCE": source,
  1177. })
  1178. }
  1179. if old_position_id == 0 {
  1180. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
  1181. }
  1182. return true
  1183. } else {
  1184. log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  1185. return false
  1186. }
  1187. }
  1188. func UpdateClue(data map[string]interface{}, saleData []map[string]interface{}, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, pIsOk bool) bool {
  1189. log.Println("线索修改前", data)
  1190. nowTime := time.Now().Format("2006-01-02 15:04:05")
  1191. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  1192. trailstatus := common.ObjToString(data["trailstatus"]) //data都为原线索数据
  1193. trailstatusTime := common.ObjToString(data["trailstatus_time"])
  1194. var trailstatusTimes time.Time
  1195. if trailstatusTime != "" {
  1196. trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
  1197. }
  1198. clueId := common.Int64All(data["id"])
  1199. is_assign := common.IntAll(data["is_assign"])
  1200. oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
  1201. old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
  1202. oldTaskTime = common.ObjToString(data["tasktime"])
  1203. is_task = common.IntAll(data["is_task"])
  1204. taskstatus = common.IntAll(data["taskstatus"])
  1205. BCPCID := common.GetRandom(32)
  1206. old_name := common.ObjToString(data["name"])
  1207. old_position := common.ObjToString(data["position"])
  1208. old_department := common.ObjToString(data["department"])
  1209. old_follow_project_area := common.ObjToString(data["follow_project_area"])
  1210. old_role := common.ObjToString(data["role"])
  1211. old_cluename := common.ObjToString(data["cluename"])
  1212. old_top_cluetype := common.ObjToString(data["top_cluetype"])
  1213. old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
  1214. old_topname, old_subname := "", ""
  1215. is_transfer := common.IntAll(data["is_transfer"])
  1216. if taskstatus == 1 || is_task == 0 {
  1217. taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
  1218. } else {
  1219. if is_task == 1 {
  1220. if oldTaskTime != "" { //以最新的任务时间为主
  1221. t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
  1222. if err == nil && time.Now().Before(t1) {
  1223. taskTime = nowTime
  1224. } else {
  1225. taskTime = oldTaskTime
  1226. }
  1227. }
  1228. }
  1229. }
  1230. count, counts, t := 0, 0, time.Now() //加入任务车判断节假日
  1231. for {
  1232. count++
  1233. currentTime := t.AddDate(0, 0, -count)
  1234. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  1235. isok := false
  1236. for k, v := range DateMap {
  1237. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  1238. isok = true
  1239. }
  1240. }
  1241. if isok {
  1242. counts++
  1243. }
  1244. } else {
  1245. isok := true
  1246. for k, v := range DateMap {
  1247. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  1248. isok = false
  1249. }
  1250. }
  1251. if isok {
  1252. counts++
  1253. }
  1254. }
  1255. if counts >= 2 {
  1256. break
  1257. }
  1258. }
  1259. //两天之前不包含节假日,有没有跟进记录
  1260. recordCount := TiDb.CountBySql(`select count(1) from dwd_f_crm_trail_content where clue_id = ? and createtime > ?`, clueId, t.AddDate(0, 0, -count).Format(date.Date_Full_Layout))
  1261. //
  1262. clueUpdateData := map[string]interface{}{
  1263. "updatetime": nowTime,
  1264. "top_cluetype": top_cluetype,
  1265. "sub_cluetype": sub_cluetype,
  1266. "userid": userId,
  1267. "comeinsource_private": 2,
  1268. "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
  1269. "company_nature": isGroup,
  1270. "company_verification": isCommerce,
  1271. }
  1272. old_remark := common.ObjToString(data["remark"])
  1273. if old_remark != "" {
  1274. remark = old_remark + ";" + remark //备注不能替换,只能往上拼接
  1275. }
  1276. clueUpdateData["remark"] = remark
  1277. if cluename != "" {
  1278. clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
  1279. }
  1280. if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
  1281. if name != "" {
  1282. clueUpdateData["name"] = name
  1283. }
  1284. if position != "" {
  1285. clueUpdateData["position"] = position
  1286. }
  1287. if department != "" {
  1288. clueUpdateData["department"] = department
  1289. }
  1290. if follow_project_area != "" {
  1291. clueUpdateData["follow_project_area"] = follow_project_area
  1292. }
  1293. if role != "" {
  1294. clueUpdateData["role"] = role
  1295. }
  1296. // clueUpdateData["industry"] = industry
  1297. }
  1298. if sourceCode == "app_xzcyh" { //这个留资单独处理一下
  1299. if departments != "" {
  1300. clueUpdateData["department"] = departments
  1301. department = departments
  1302. }
  1303. business_scope := common.ObjToString(data["business_scope"])
  1304. keywords := ""
  1305. if len(keyword) > 0 && keyword[0] != "" {
  1306. keywords = strings.Join(keyword, ",")
  1307. if business_scope != "" {
  1308. clueUpdateData["business_scope"] = business_scope + "," + keywords
  1309. } else {
  1310. clueUpdateData["business_scope"] = keywords
  1311. }
  1312. }
  1313. }
  1314. if trailstatus == "00" || old_position_id == 0 || is_assign != 1 || (pIsOk && old_cluename != cluename && cluename != "") {
  1315. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1316. } else {
  1317. clueUpdateData["seatNumber"] = seatNumber
  1318. clueUpdateData["position_id"] = positionId
  1319. clueUpdateData["comeintime"] = nowTime
  1320. clueUpdateData["is_assign"] = 1
  1321. clueUpdateData["comeinsource_open"] = nil
  1322. clueUpdateData["level_open"] = nil
  1323. clueUpdateData["clue_level"] = nil
  1324. for _, v := range saleData {
  1325. if common.Int64All(data["position_id"]) == common.Int64All(v["position_id"]) {
  1326. oldsaleName = common.ObjToString(v["name"])
  1327. }
  1328. }
  1329. }
  1330. } else { //不属于上边情况就还是属于原来的销售,不更换销售
  1331. clueUpdateData["seatNumber"] = old_seatNumber
  1332. clueUpdateData["position_id"] = old_position_id
  1333. clueUpdateData["is_assign"] = 1
  1334. clueUpdateData["comeinsource_open"] = nil
  1335. clueUpdateData["level_open"] = nil
  1336. clueUpdateData["clue_level"] = nil
  1337. }
  1338. if trailstatus != "08" && is_assign == 0 { //在公海的成交客户
  1339. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1340. } else {
  1341. clueUpdateData["trailstatus"] = "01"
  1342. clueUpdateData["trailstatus_time"] = nowTime
  1343. }
  1344. }
  1345. is_ok := false
  1346. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  1347. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1348. } else {
  1349. if item == "eventReg" {
  1350. eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
  1351. if eData != nil && len(*eData) > 0 {
  1352. for _, e := range *eData {
  1353. new_value := common.ObjToString(e["new_value"])
  1354. if strings.Contains(new_value, subname) {
  1355. is_ok = true
  1356. }
  1357. }
  1358. }
  1359. if !is_ok {
  1360. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  1361. if taskTime == "" {
  1362. taskTime = nowTime
  1363. }
  1364. clueUpdateData["task_time"] = nowTime
  1365. clueUpdateData["tasktime"] = taskTime
  1366. clueUpdateData["taskstatus"] = 0
  1367. }
  1368. if is_transfer != 1 {
  1369. clueUpdateData["is_task"] = 1
  1370. }
  1371. }
  1372. } else {
  1373. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  1374. if taskTime == "" {
  1375. taskTime = nowTime
  1376. }
  1377. clueUpdateData["task_time"] = nowTime
  1378. clueUpdateData["tasktime"] = taskTime
  1379. clueUpdateData["taskstatus"] = 0
  1380. }
  1381. if is_transfer != 1 {
  1382. clueUpdateData["is_task"] = 1
  1383. }
  1384. }
  1385. }
  1386. }
  1387. ok, updateId14 := true, true
  1388. updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
  1389. updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
  1390. updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
  1391. if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
  1392. //线索
  1393. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  1394. //变更记录
  1395. if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
  1396. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1397. } else if old_position_id == 0 {
  1398. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1399. "clue_id": clueId,
  1400. "position_id": positionId,
  1401. "change_field": "position_id",
  1402. "change_type": "所属人变更",
  1403. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1404. "new_value": common.If(saleName != "", saleName, "/"),
  1405. "createtime": nowTime,
  1406. "BCPCID": common.GetRandom(32),
  1407. "operator_id": -1,
  1408. })
  1409. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1410. "clue_id": clueId,
  1411. "position_id": positionId,
  1412. "change_field": "trailstatus",
  1413. "change_type": "基本信息变更",
  1414. "old_value": CodeTrail[trailstatus],
  1415. "new_value": "新增",
  1416. "createtime": nowTime,
  1417. "BCPCID": common.GetRandom(32),
  1418. "operator_id": -1,
  1419. })
  1420. } else {
  1421. if pIsOk && old_cluename != cluename && cluename != "" {
  1422. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1423. "clue_id": clueId,
  1424. "position_id": positionId,
  1425. "change_field": "position_id",
  1426. "change_type": "所属人变更",
  1427. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1428. "new_value": common.If(saleName != "", saleName, "/"),
  1429. "createtime": nowTime,
  1430. "BCPCID": common.GetRandom(32),
  1431. "operator_id": -1,
  1432. })
  1433. } else {
  1434. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1435. "clue_id": clueId,
  1436. "position_id": positionId,
  1437. "change_field": "position_id",
  1438. "change_type": "所属人变更",
  1439. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1440. "new_value": common.If(saleName != "", saleName, "/"),
  1441. "createtime": nowTime,
  1442. "BCPCID": common.GetRandom(32),
  1443. "operator_id": -1,
  1444. })
  1445. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1446. "clue_id": clueId,
  1447. "position_id": common.Int64All(data["position_id"]),
  1448. "change_field": "trailstatus",
  1449. "change_type": "基本信息变更",
  1450. "old_value": "无意向客户",
  1451. "new_value": "流失",
  1452. "createtime": nowTime,
  1453. "BCPCID": common.GetRandom(32),
  1454. "operator_id": -1,
  1455. })
  1456. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1457. "clue_id": clueId,
  1458. "position_id": positionId,
  1459. "change_field": "trailstatus",
  1460. "change_type": "基本信息变更",
  1461. "old_value": "商机线索",
  1462. "new_value": "新增",
  1463. "createtime": nowTime,
  1464. "BCPCID": common.GetRandom(32),
  1465. "operator_id": -1,
  1466. })
  1467. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1468. "clue_id": clueId,
  1469. "position_id": positionId,
  1470. "change_field": "trailstatus",
  1471. "change_type": "基本信息变更",
  1472. "old_value": "无意向客户",
  1473. "new_value": "商机线索",
  1474. "createtime": nowTime,
  1475. "BCPCID": common.GetRandom(32),
  1476. "operator_id": -1,
  1477. })
  1478. }
  1479. }
  1480. } else if trailstatus != "08" && is_assign == 0 {
  1481. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1482. } else {
  1483. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1484. "clue_id": clueId,
  1485. "position_id": positionId,
  1486. "change_field": "position_id",
  1487. "change_type": "所属人变更",
  1488. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1489. "new_value": common.If(saleName != "", saleName, "/"),
  1490. "createtime": nowTime,
  1491. "BCPCID": common.GetRandom(32),
  1492. "operator_id": -1,
  1493. })
  1494. if trailstatus != "01" {
  1495. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1496. "clue_id": clueId,
  1497. "position_id": positionId,
  1498. "change_field": "trailstatus",
  1499. "change_type": "基本信息变更",
  1500. "old_value": CodeTrail[trailstatus],
  1501. "new_value": "商机线索",
  1502. "createtime": nowTime,
  1503. "BCPCID": common.GetRandom(32),
  1504. "operator_id": -1,
  1505. })
  1506. }
  1507. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1508. "clue_id": clueId,
  1509. "position_id": positionId,
  1510. "change_field": "trailstatus",
  1511. "change_type": "基本信息变更",
  1512. "old_value": "商机线索",
  1513. "new_value": "新增",
  1514. "createtime": nowTime,
  1515. "BCPCID": common.GetRandom(32),
  1516. "operator_id": -1,
  1517. })
  1518. }
  1519. }
  1520. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  1521. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1522. } else {
  1523. if old_sub_cluetype != sub_cluetype && !is_ok {
  1524. if is_transfer != 1 {
  1525. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1526. "clue_id": clueId,
  1527. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  1528. "change_type": "加入任务车",
  1529. "new_value": gconv.String(common.If(gconv.Int64(data["position_id"]) == 0, "线索自动分配", "线索来源自动更新")) + "-" + topname + "-" + subname,
  1530. "createtime": nowTime,
  1531. "BCPCID": common.GetRandom(32),
  1532. "operator_id": -1,
  1533. })
  1534. } else {
  1535. cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
  1536. if cdata != nil && len(*cdata) > 0 {
  1537. kcpositionId := common.Int64All((*cdata)["position_id"])
  1538. tasksource := common.ObjToString((*cdata)["tasksource"])
  1539. taskstatus := common.IntAll((*cdata)["taskstatus"])
  1540. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1541. "clue_id": clueId,
  1542. "position_id": kcpositionId,
  1543. "change_type": "加入任务车",
  1544. "new_value": "线上主动留资",
  1545. "createtime": nowTime,
  1546. "BCPCID": common.GetRandom(32),
  1547. "operator_id": -1,
  1548. })
  1549. if taskstatus == 1 {
  1550. tasksource = "10"
  1551. } else {
  1552. if tasksource != "" && !strings.Contains(tasksource, "10") {
  1553. if !strings.Contains(tasksource, "10") {
  1554. tasksource += ",10"
  1555. }
  1556. } else {
  1557. tasksource = "10"
  1558. }
  1559. }
  1560. updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  1561. "tasksource": tasksource,
  1562. "tasktime": nowTime,
  1563. "is_task": 1,
  1564. "taskstatus": 0,
  1565. })
  1566. }
  1567. }
  1568. }
  1569. }
  1570. }
  1571. if old_top_cluetype != "" {
  1572. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  1573. if pcodeData != nil && len(*pcodeData) > 0 {
  1574. old_topname = common.ObjToString((*pcodeData)["name"])
  1575. }
  1576. }
  1577. if old_sub_cluetype != "" {
  1578. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  1579. if pcodeData != nil && len(*pcodeData) > 0 {
  1580. old_subname = common.ObjToString((*pcodeData)["name"])
  1581. }
  1582. }
  1583. if item != "orders" {
  1584. if old_name != name && name != "" {
  1585. updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1586. "clue_id": clueId,
  1587. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1588. "change_field": "name",
  1589. "change_type": "基本信息变更",
  1590. "old_value": common.If(old_name != "", old_name, "/"),
  1591. "new_value": common.If(name != "", name, "/"),
  1592. "createtime": nowTime,
  1593. "BCPCID": BCPCID,
  1594. "operator_id": -1,
  1595. })
  1596. }
  1597. if old_position != position && position != "" {
  1598. updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1599. "clue_id": clueId,
  1600. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1601. "change_field": "position",
  1602. "change_type": "基本信息变更",
  1603. "old_value": common.If(old_position != "", old_position, "/"),
  1604. "new_value": common.If(position != "", position, "/"),
  1605. "createtime": nowTime,
  1606. "BCPCID": BCPCID,
  1607. "operator_id": -1,
  1608. })
  1609. }
  1610. if old_department != department && department != "" {
  1611. updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1612. "clue_id": clueId,
  1613. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1614. "change_field": "department",
  1615. "change_type": "基本信息变更",
  1616. "old_value": common.If(old_department != "", old_department, "/"),
  1617. "new_value": common.If(department != "", department, "/"),
  1618. "createtime": nowTime,
  1619. "BCPCID": BCPCID,
  1620. "operator_id": -1,
  1621. })
  1622. }
  1623. if old_role != role && role != "" {
  1624. updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))), "change_field": "role", "change_type": "基本信息变更", "old_value": common.If(old_role != "", old_role, "/"), "new_value": common.If(role != "", role, "/"), "createtime": nowTime, "BCPCID": BCPCID, "operator_id": -1})
  1625. }
  1626. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  1627. old_area, old_area_arr := "", []string{}
  1628. new_area, new_area_arr := "", []string{}
  1629. for _, v := range strings.Split(old_follow_project_area, ",") {
  1630. old_area_arr = append(old_area_arr, CodeArea[v])
  1631. }
  1632. for _, v := range strings.Split(follow_project_area, ",") {
  1633. new_area_arr = append(new_area_arr, CodeArea[v])
  1634. }
  1635. old_area = strings.Join(old_area_arr, ",")
  1636. new_area = strings.Join(new_area_arr, ",")
  1637. updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1638. "clue_id": clueId,
  1639. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1640. "change_field": "follow_project_area",
  1641. "change_type": "基本信息变更",
  1642. "old_value": common.If(old_area != "", old_area, "/"),
  1643. "new_value": common.If(new_area != "", new_area, "/"),
  1644. "createtime": nowTime,
  1645. "BCPCID": BCPCID,
  1646. "operator_id": -1,
  1647. })
  1648. }
  1649. if old_cluename != cluename && cluename != "" {
  1650. updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1651. "clue_id": clueId,
  1652. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1653. "change_field": "cluename",
  1654. "change_type": "基本信息变更",
  1655. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  1656. "new_value": common.If(cluename != "", cluename, "/"),
  1657. "createtime": nowTime,
  1658. "BCPCID": BCPCID,
  1659. "operator_id": -1,
  1660. })
  1661. }
  1662. }
  1663. if old_top_cluetype != top_cluetype {
  1664. updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1665. "clue_id": clueId,
  1666. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1667. "change_field": "top_cluetype",
  1668. "change_type": "基本信息变更",
  1669. "old_value": common.If(old_topname != "", old_topname, "/"),
  1670. "new_value": common.If(topname != "", topname, "/"),
  1671. "createtime": nowTime,
  1672. "BCPCID": BCPCID,
  1673. "operator_id": -1,
  1674. })
  1675. }
  1676. if old_sub_cluetype != sub_cluetype {
  1677. updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1678. "clue_id": clueId,
  1679. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1680. "change_field": "sub_cluetype",
  1681. "change_type": "基本信息变更",
  1682. "old_value": common.If(old_subname != "", old_subname, "/"),
  1683. "new_value": common.If(subname != "", subname, "/"),
  1684. "createtime": nowTime,
  1685. "BCPCID": BCPCID,
  1686. "operator_id": -1,
  1687. })
  1688. }
  1689. return ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 && updateId4 > -1 && updateId5 > -1 && updateId6 > -1 && updateId7 > -1 && updateId8 > -1 && updateId9 > -1 && updateId10 > -1 && updateId11 > -1 && updateId12 > -1 && updateId13 > -1 && updateId14
  1690. }) {
  1691. log.Println("线索更新成功")
  1692. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  1693. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  1694. "status": 1,
  1695. "is_delete": 1,
  1696. "createtime": nowTime,
  1697. "updatetime": nowTime,
  1698. "phone": phone,
  1699. "baseinfo_id": uId,
  1700. "SOURCE": source,
  1701. })
  1702. }
  1703. if old_position_id == 0 {
  1704. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
  1705. }
  1706. return true
  1707. } else {
  1708. log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  1709. return false
  1710. }
  1711. }