job.go 94 KB

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