job.go 93 KB

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