job.go 101 KB

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