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