job.go 89 KB

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