job.go 93 KB

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