job.go 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/date"
  9. "app.yhyue.com/moapp/jybase/redis"
  10. "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/mongodb"
  12. )
  13. func FormatData(data map[string]interface{}, item string) (bool, bool) {
  14. userId, uId, positionId, source, cluename, phone, sourceCode, keyword := common.ObjToString(data["user_id"]), "", "", "", "", "", "", []string{}
  15. role, industry, department, departments, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", "", ""
  16. query, topname, subname, belong_to, sourceName, remark, sourceId := map[string]interface{}{}, "", "", "01", "", "", int64(0)
  17. nowTime := time.Now().Format("2006-01-02 15:04:05")
  18. if item == "orders" { //订单
  19. if !mongodb.IsObjectIdHex(userId) {
  20. positionId = userId
  21. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  22. if userMapping != nil && len(*userMapping) > 0 {
  23. userId = common.ObjToString((*userMapping)["userid"])
  24. }
  25. }
  26. query["userid"] = userId
  27. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  28. if userInfo != nil && len(*userInfo) > 0 {
  29. uId = common.ObjToString((*userInfo)["uid"])
  30. source = common.ObjToString((*userInfo)["source"]) //用户来源
  31. belong_to = common.ObjToString((*userInfo)["belong_to"]) //用户归属
  32. }
  33. cluename = common.ObjToString(data["company_name"])
  34. phone = common.ObjToString(data["user_phone"])
  35. } else if item == "message" {
  36. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"base_user_id": common.Int64All(data["own_id"])}, "", "")
  37. if userMapping != nil && len(*userMapping) > 0 {
  38. userId = common.ObjToString((*userMapping)["userid"])
  39. positionId = fmt.Sprint((*userMapping)["position_id"])
  40. }
  41. query["userid"] = userId
  42. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  43. if userInfo != nil && len(*userInfo) > 0 {
  44. uId = common.ObjToString((*userInfo)["uid"])
  45. source = common.ObjToString((*userInfo)["source"])
  46. belong_to = common.ObjToString((*userInfo)["belong_to"])
  47. phone = common.ObjToString((*userInfo)["phone"])
  48. if phone != "" {
  49. phoneMapping := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  50. if phoneMapping != nil && len(*phoneMapping) > 0 {
  51. cluename = common.ObjToString((*phoneMapping)["cluename"])
  52. }
  53. }
  54. }
  55. } else if item == "users" || item == "xcxusers" { //注册用户
  56. userId = mongodb.BsonIdToSId(data["userid"])
  57. //新用户没有uid、source要等5分钟
  58. cluename = common.ObjToString(data["company_name"])
  59. phone = common.ObjToString(data["phone"])
  60. if phone != "" {
  61. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  62. if contactsData != nil && len(*contactsData) > 0 {
  63. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  64. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  65. query["uid"] = uId
  66. } else {
  67. query["userid"] = userId
  68. }
  69. } else {
  70. query["userid"] = userId
  71. }
  72. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  73. if userInfo != nil && len(*userInfo) > 0 {
  74. uId = common.ObjToString((*userInfo)["uid"])
  75. source = common.ObjToString((*userInfo)["source"])
  76. belong_to = common.ObjToString((*userInfo)["belong_to"])
  77. }
  78. if item == "xcxusers" {
  79. sourceCode = common.ObjToString(data["s_sourceid"])
  80. if sourceCode == "" {
  81. log.Println("留资没有source", phone)
  82. return true, true
  83. }
  84. }
  85. }
  86. } else if item == "saleLeads" { //留资
  87. userId = common.ObjToString(data["userid"])
  88. if !mongodb.IsObjectIdHex(userId) {
  89. positionId = userId
  90. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  91. if userMapping != nil && len(*userMapping) > 0 {
  92. userId = common.ObjToString((*userMapping)["userid"])
  93. }
  94. }
  95. cluename = common.ObjToString(data["company"])
  96. phone = common.ObjToString(data["phone"])
  97. role = common.ObjToString(data["companyType"])
  98. industry = common.ObjToString(data["industry"])
  99. department = common.ObjToString(data["branch"])
  100. departments = common.ObjToString(data["department"])
  101. position = common.ObjToString(data["position"])
  102. name = common.ObjToString(data["name"])
  103. sourceCode = common.ObjToString(data["source"])
  104. if sourceCode == "" {
  105. log.Println("留资没有source", phone)
  106. return true, true
  107. }
  108. remark = common.ObjToString(data["jyRemark"]) //荟聚线索备注
  109. keywordArr := data["keyword"]
  110. if keywordArr != nil {
  111. keyword = common.ObjArrToStringArr(data["keyword"].([]interface{}))
  112. }
  113. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  114. if contactsData != nil && len(*contactsData) > 0 {
  115. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  116. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  117. query["uid"] = uId
  118. } else {
  119. query["userid"] = userId
  120. }
  121. } else {
  122. query["userid"] = userId
  123. }
  124. if userId != "" || uId != "" {
  125. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  126. if userInfo != nil && len(*userInfo) > 0 {
  127. uId = common.ObjToString((*userInfo)["uid"])
  128. source = common.ObjToString((*userInfo)["source"])
  129. belong_to = common.ObjToString((*userInfo)["belong_to"])
  130. userId = common.ObjToString((*userInfo)["userid"])
  131. }
  132. } else {
  133. if sourceCode == "report_retention" || sourceCode == "marketing_retention" { //荟聚线索来源,不是剑鱼用户,需要等待用户归集
  134. log.Println("不是剑鱼用户留资,等待用户", phone)
  135. return false, false
  136. } else if remark != "" {
  137. log.Println("留资source过滤", phone, sourceCode)
  138. return true, true
  139. }
  140. }
  141. } else if item == "eventReg" { //渠道
  142. userId = common.ObjToString(data["userid"])
  143. cluename = common.ObjToString(data["company"])
  144. phone = common.ObjToString(data["sign_phone"])
  145. role = common.ObjToString(data["company_type"])
  146. department = common.ObjToString(data["branch"])
  147. position = common.ObjToString(data["position"])
  148. name = common.ObjToString(data["sign_name"])
  149. sourceCode = common.ObjToString(data["source_code"])
  150. sourceName = common.ObjToString(data["source_name"])
  151. log.Println(sourceName)
  152. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  153. if contactsData != nil && len(*contactsData) > 0 {
  154. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  155. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  156. query["uid"] = uId
  157. } else {
  158. query["userid"] = userId
  159. }
  160. } else {
  161. query["userid"] = userId
  162. }
  163. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  164. if userInfo != nil && len(*userInfo) > 0 {
  165. uId = common.ObjToString((*userInfo)["uid"])
  166. source = common.ObjToString((*userInfo)["source"])
  167. belong_to = common.ObjToString((*userInfo)["belong_to"])
  168. userId = common.ObjToString((*userInfo)["userid"])
  169. }
  170. if role == "集成商" || role == "设计院" {
  171. role = "其他-" + role
  172. }
  173. } else if item == "readClue" {
  174. userId = common.ObjToString(data["userId"])
  175. query["userid"] = userId
  176. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  177. if userInfo != nil && len(*userInfo) > 0 {
  178. uId = common.ObjToString((*userInfo)["uid"])
  179. source = common.ObjToString((*userInfo)["source"])
  180. belong_to = common.ObjToString((*userInfo)["belong_to"])
  181. }
  182. // cluename = common.ObjToString(data["companyName"])
  183. phone = common.ObjToString(data["phone"])
  184. sourceId = common.Int64All(data["sourceId"])
  185. }
  186. if cluename == "" && item != "message" && item != "orders" && item != "readClue" {
  187. cluename = phone //没有线索名,手机号代替
  188. }
  189. isGroup, isCommerce := GetCompanyType(cluename) //判断是否集团公司、工商库
  190. if source == "0104" || strings.HasPrefix(belong_to, "02") || source == "0102" || source == "0103" || phone == "" { //参照用户来源代码表
  191. //log.Println("线索分配失败,线索过滤!!", item, source, phone, userId)
  192. //saveHlyj(belong_to, item, phone, name, sourceName, cluename, position, nowTime, isGroup, isCommerce)
  193. return true, true
  194. }
  195. if uId == "" { //没有进用户归集,等待进入,结束任务,放在下次继续执行
  196. if isExists, _ := redis.Exists("bidx", "bidx_userId_"+userId); isExists {
  197. redisInt := redis.GetInt("bidx", "bidx_userId_"+userId)
  198. if redisInt > 4 {
  199. log.Println("线索分配失败,线索缺少信息,任务已执行超过1次", item, userId, phone)
  200. return true, true
  201. } else {
  202. redis.Incr("bidx", "bidx_userId_"+userId)
  203. }
  204. } else {
  205. redis.Put("bidx", "bidx_userId_"+userId, 1, 3600)
  206. }
  207. log.Println("线索分配失败,线索缺少信息", item, phone, userId)
  208. return false, false
  209. }
  210. if item == "orders" { //订单没信息再查一遍留资
  211. qid := positionId
  212. if qid == "" {
  213. qid = userId
  214. }
  215. saleLeadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"userid": qid}, map[string]interface{}{"_id": -1}, nil, false, 0, 1)
  216. if ok && saleLeadsData != nil && len(*saleLeadsData) > 0 {
  217. sdata := (*saleLeadsData)[0]
  218. role = common.ObjToString(sdata["companyType"])
  219. industry = common.ObjToString(sdata["industry"])
  220. department = common.ObjToString(sdata["branch"])
  221. departments = common.ObjToString(sdata["department"])
  222. position = common.ObjToString(sdata["position"])
  223. name = common.ObjToString(sdata["name"])
  224. }
  225. }
  226. top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data, sourceCode, sourceId) //查留资来源名字
  227. if topname == "市场活动" && item == "saleLeads" { //市场活动的不要
  228. log.Println("市场活动留资过滤 ", userId, phone)
  229. return true, true
  230. }
  231. if strings.HasPrefix(belong_to, "03") { //一切都好的不进
  232. isOk := saveEverything(userId, phone, item, subname, sourceCode)
  233. log.Println("渠道线索电销", userId, phone, item, subname, sourceCode)
  234. if !isOk {
  235. return true, true
  236. }
  237. }
  238. follow_project_area = getAreaCode(userId) //关注项目区域
  239. log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area, isGroup, isCommerce)
  240. if top_cluetype == "" || sub_cluetype == "" || level == "" {
  241. log.Println("线索分配失败,线索过滤top_cluetype!!", item, uId, phone, userId)
  242. return true, true
  243. }
  244. position_id, seatNumber, saleName, saleData, pIsOk, isFreeze, noticePositionId := autoDraw(level, cluename, phone, isGroup, isCommerce) //查询当前分配次数最少的,如果当前线索有销售,此次找出的不会+1
  245. log.Println("data -------", position_id, seatNumber, saleName, pIsOk, isFreeze, noticePositionId)
  246. if position_id > 0 && seatNumber != "" {
  247. uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
  248. if uCount != nil && len(*uCount) > 0 { //已存在,走更新
  249. batch_import := common.ObjToString((*uCount)["batch_import"])
  250. if batch_import != "" && item == "users" { //有导入批次号还是新用户,不执行
  251. return true, true
  252. }
  253. //oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, pIsOk, "")
  254. oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, pIsOk)
  255. } else { //不存在走新增
  256. oks = SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, isFreeze, "")
  257. if oks { //新增成功,销售分配次数+1
  258. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, position_id, level)
  259. }
  260. }
  261. if !oks {
  262. log.Println("线索分配失败!!", item, position_id, seatNumber, uId, userId, phone)
  263. return false, false
  264. }
  265. } else {
  266. if isFreeze {
  267. //达上限放公海处理
  268. uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
  269. if uCount == nil || len(*uCount) == 0 {
  270. oks = SaveClue0(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, position_id, source, sourceCode, remark, keyword, belong_to, isGroup, isCommerce, isFreeze)
  271. log.Println(oks)
  272. } else {
  273. //留资线索更新
  274. if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
  275. BCPCID := common.GetRandom(32)
  276. clueId := common.Int64All((*uCount)["id"])
  277. old_name := common.ObjToString((*uCount)["name"])
  278. old_position := common.ObjToString((*uCount)["position"])
  279. old_department := common.ObjToString((*uCount)["department"])
  280. old_follow_project_area := common.ObjToString((*uCount)["follow_project_area"])
  281. old_role := common.ObjToString((*uCount)["role"])
  282. old_cluename := common.ObjToString((*uCount)["cluename"])
  283. old_top_cluetype := common.ObjToString((*uCount)["top_cluetype"])
  284. old_sub_cluetype := common.ObjToString((*uCount)["sub_cluetype"])
  285. clueUpdateData := map[string]interface{}{}
  286. if old_name != name && name != "" {
  287. clueUpdateData["name"] = name
  288. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  289. "clue_id": clueId,
  290. "change_field": "name",
  291. "position_id": 0,
  292. "change_type": "基本信息变更",
  293. "old_value": common.If(old_name != "", old_name, "/"),
  294. "new_value": common.If(name != "", name, "/"),
  295. "createtime": nowTime,
  296. "BCPCID": BCPCID,
  297. "operator_id": -1})
  298. }
  299. if old_position != position && position != "" {
  300. clueUpdateData["position"] = position
  301. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  302. "clue_id": clueId,
  303. "change_field": "position",
  304. "position_id": 0,
  305. "change_type": "基本信息变更",
  306. "old_value": common.If(old_position != "", old_position, "/"),
  307. "new_value": common.If(position != "", position, "/"),
  308. "createtime": nowTime,
  309. "BCPCID": BCPCID,
  310. "operator_id": -1})
  311. }
  312. if old_department != department && department != "" {
  313. clueUpdateData["department"] = department
  314. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  315. "clue_id": clueId,
  316. "change_field": "department",
  317. "position_id": 0,
  318. "change_type": "基本信息变更",
  319. "old_value": common.If(old_department != "", old_department, "/"),
  320. "new_value": common.If(department != "", department, "/"),
  321. "createtime": nowTime,
  322. "BCPCID": BCPCID,
  323. "operator_id": -1})
  324. }
  325. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  326. clueUpdateData["follow_project_area"] = follow_project_area
  327. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  328. "clue_id": clueId,
  329. "change_field": "follow_project_area",
  330. "position_id": 0,
  331. "change_type": "基本信息变更",
  332. "old_value": common.If(old_follow_project_area != "", old_follow_project_area, "/"),
  333. "new_value": common.If(follow_project_area != "", follow_project_area, "/"),
  334. "createtime": nowTime,
  335. "BCPCID": BCPCID,
  336. "operator_id": -1})
  337. }
  338. if old_role != role && role != "" {
  339. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  340. "clue_id": clueId,
  341. "change_field": "role",
  342. "change_type": "基本信息变更",
  343. "old_value": common.If(old_role != "", old_role, "/"),
  344. "new_value": common.If(role != "", role, "/"),
  345. "createtime": nowTime,
  346. "position_id": 0,
  347. "BCPCID": BCPCID,
  348. "operator_id": -1})
  349. clueUpdateData["role"] = role
  350. }
  351. if old_cluename != cluename && cluename != "" {
  352. clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
  353. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  354. "clue_id": clueId,
  355. "change_field": "cluename",
  356. "change_type": "基本信息变更",
  357. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  358. "new_value": common.If(cluename != "", cluename, "/"),
  359. "createtime": nowTime,
  360. "BCPCID": BCPCID,
  361. "position_id": 0,
  362. "operator_id": -1})
  363. }
  364. old_topname := ""
  365. old_subname := ""
  366. if old_top_cluetype != "" {
  367. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  368. if pcodeData != nil && len(*pcodeData) > 0 {
  369. old_topname = common.ObjToString((*pcodeData)["name"])
  370. }
  371. }
  372. if old_sub_cluetype != "" {
  373. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  374. if pcodeData != nil && len(*pcodeData) > 0 {
  375. old_subname = common.ObjToString((*pcodeData)["name"])
  376. }
  377. }
  378. if old_top_cluetype != top_cluetype && top_cluetype != "" {
  379. clueUpdateData["top_cluetype"] = top_cluetype //新的线索名不为空才替换
  380. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  381. "clue_id": clueId,
  382. "change_field": "top_cluetype",
  383. "position_id": 0,
  384. "change_type": "基本信息变更",
  385. "old_value": common.If(old_topname != "", old_topname, "/"),
  386. "new_value": common.If(topname != "", topname, "/"),
  387. "createtime": nowTime,
  388. "BCPCID": BCPCID,
  389. "operator_id": -1})
  390. }
  391. if old_sub_cluetype != sub_cluetype && sub_cluetype != "" {
  392. clueUpdateData["sub_cluetype"] = sub_cluetype //新的线索名不为空才替换
  393. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  394. "clue_id": clueId,
  395. "change_field": "sub_cluetype",
  396. "position_id": 0,
  397. "change_type": "基本信息变更",
  398. "old_value": common.If(old_subname != "", old_subname, "/"),
  399. "new_value": common.If(subname != "", subname, "/"),
  400. "createtime": nowTime,
  401. "BCPCID": BCPCID,
  402. "operator_id": -1})
  403. }
  404. clueUpdateData["company_nature"] = isGroup
  405. clueUpdateData["company_verification"] = isCommerce
  406. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  407. }
  408. }
  409. AssFail(noticePositionId, cluename, phone)
  410. return true, true
  411. }
  412. return true, false
  413. }
  414. return true, true
  415. }
  416. func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, isFreeze bool, demand string) bool {
  417. nowTime := time.Now().Format("2006-01-02 15:04:05")
  418. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  419. clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
  420. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  421. keywords := ""
  422. if sourceCode == "app_xzcyh" {
  423. if len(keyword) > 0 && keyword[0] != "" {
  424. keywords = strings.Join(keyword, ",")
  425. }
  426. }
  427. if cluename == "" {
  428. cluename = phone
  429. }
  430. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  431. "userid": userId,
  432. "uid": uId,
  433. "seatNumber": seatNumber,
  434. "position_id": positionId,
  435. "is_assign": common.If(isFreeze, -3, 1),
  436. "comeintime": nowTime,
  437. "createtime": nowTime,
  438. "updatetime": nowTime,
  439. "cluename": cluename,
  440. "top_cluetype": top_cluetype,
  441. "sub_cluetype": sub_cluetype,
  442. "trailstatus": "01",
  443. "name": name,
  444. "phone": phone,
  445. "position": position,
  446. "department": common.If(sourceCode == "app_xzcyh", departments, department),
  447. "industry": industry,
  448. "follow_project_area": follow_project_area,
  449. "role": role,
  450. "comeinsource_private": 2,
  451. "is_task": 1,
  452. "task_time": nowTime,
  453. "tasktime": common.If(item == "users", nowTimes, nowTime),
  454. "taskstatus": 0,
  455. "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
  456. "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
  457. "company_nature": isGroup,
  458. "company_verification": isCommerce,
  459. "remark": remark,
  460. "customer_demand": demand,
  461. "FREEZE_TIME": nowTime,
  462. })
  463. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  464. "clue_id": clueId,
  465. "position_id": positionId,
  466. "change_type": "创建线索",
  467. "new_value": "系统自动创建",
  468. "createtime": nowTime,
  469. "BCPCID": common.GetRandom(32),
  470. "operator_id": -1,
  471. })
  472. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  473. "clue_id": clueId,
  474. "position_id": positionId,
  475. "change_field": "position_id",
  476. "change_type": "所属人变更",
  477. "old_value": "/",
  478. "new_value": saleName,
  479. "createtime": nowTime,
  480. "BCPCID": common.GetRandom(32),
  481. "operator_id": -1,
  482. })
  483. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  484. "clue_id": clueId,
  485. "position_id": positionId,
  486. "change_field": "trailstatus",
  487. "change_type": "基本信息变更",
  488. "old_value": "商机线索",
  489. "new_value": "新增",
  490. "createtime": nowTime,
  491. "BCPCID": common.GetRandom(32),
  492. "operator_id": -1,
  493. })
  494. uodateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  495. "clue_id": clueId,
  496. "position_id": positionId,
  497. "change_type": "加入任务车",
  498. "new_value": "线索自动分配" + "-" + topname + "-" + subname,
  499. "createtime": nowTime,
  500. "BCPCID": common.GetRandom(32),
  501. "operator_id": -1,
  502. })
  503. uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  504. "clue_id": clueId,
  505. "position_id": positionId,
  506. "change_field": "top_cluetype",
  507. "change_type": "基本信息变更",
  508. "old_value": "/",
  509. "new_value": topname,
  510. "createtime": nowTime,
  511. "BCPCID": common.GetRandom(32),
  512. "operator_id": -1,
  513. })
  514. uodateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  515. "clue_id": clueId,
  516. "position_id": positionId,
  517. "change_field": "sub_cluetype",
  518. "change_type": "基本信息变更",
  519. "old_value": "/",
  520. "new_value": subname,
  521. "createtime": nowTime,
  522. "BCPCID": common.GetRandom(32),
  523. "operator_id": -1,
  524. })
  525. //冻结处理
  526. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1 && uodateId6 > -1
  527. }) {
  528. log.Println("线索分配成功")
  529. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  530. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  531. "status": 1,
  532. "is_delete": 1,
  533. "createtime": nowTime,
  534. "updatetime": nowTime,
  535. "phone": phone,
  536. "baseinfo_id": uId,
  537. "SOURCE": source,
  538. })
  539. }
  540. return true
  541. } else {
  542. log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  543. return false
  544. }
  545. }
  546. func SaveClue0(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, isFreeze bool) bool {
  547. nowTime := time.Now().Format("2006-01-02 15:04:05")
  548. clueId, uodateId1, uodateId2, uodateId3 := int64(0), int64(0), int64(0), int64(0)
  549. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  550. keywords := ""
  551. if sourceCode == "app_xzcyh" {
  552. if len(keyword) > 0 && keyword[0] != "" {
  553. keywords = strings.Join(keyword, ",")
  554. }
  555. }
  556. if cluename == "" {
  557. cluename = phone
  558. }
  559. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  560. "userid": userId,
  561. "uid": uId,
  562. "seatNumber": "",
  563. "position_id": 0,
  564. "is_assign": 0,
  565. "comeintime": nowTime,
  566. "createtime": nowTime,
  567. "updatetime": nowTime,
  568. "cluename": cluename,
  569. "top_cluetype": top_cluetype,
  570. "sub_cluetype": sub_cluetype,
  571. "trailstatus": "01",
  572. "name": name,
  573. "phone": phone,
  574. "position": position,
  575. "department": common.If(sourceCode == "app_xzcyh", departments, department),
  576. "industry": industry,
  577. "follow_project_area": follow_project_area,
  578. "role": role,
  579. "comeinsource_private": 2,
  580. "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
  581. "company_nature": isGroup,
  582. "company_verification": isCommerce,
  583. "remark": remark,
  584. "FREEZE_TIME": nowTime,
  585. })
  586. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  587. "clue_id": clueId,
  588. "position_id": positionId,
  589. "change_type": "创建线索",
  590. "new_value": "系统自动创建",
  591. "createtime": nowTime,
  592. "BCPCID": common.GetRandom(32),
  593. "operator_id": -1,
  594. })
  595. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  596. "clue_id": clueId,
  597. "position_id": positionId,
  598. "change_field": "position_id",
  599. "change_type": "所属人变更",
  600. "old_value": "",
  601. "new_value": "/",
  602. "createtime": nowTime,
  603. "BCPCID": common.GetRandom(32),
  604. "operator_id": -1,
  605. })
  606. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  607. "clue_id": clueId,
  608. "position_id": positionId,
  609. "change_field": "trailstatus",
  610. "change_type": "基本信息变更",
  611. "old_value": "商机线索",
  612. "new_value": "新增",
  613. "createtime": nowTime,
  614. "BCPCID": common.GetRandom(32),
  615. "operator_id": -1,
  616. })
  617. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1
  618. }) {
  619. log.Println("线索分配成功")
  620. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  621. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  622. "status": 1,
  623. "is_delete": 1,
  624. "createtime": nowTime,
  625. "updatetime": nowTime,
  626. "phone": phone,
  627. "baseinfo_id": uId,
  628. "SOURCE": source,
  629. })
  630. }
  631. return true
  632. } else {
  633. log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  634. return false
  635. }
  636. }
  637. func WorkUpdateClue(data map[string]interface{}, saleData []map[string]interface{}, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, pIsOk bool, demand string) bool {
  638. log.Println("工单线索修改前", data)
  639. nowTime := time.Now().Format("2006-01-02 15:04:05")
  640. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  641. trailstatus := common.ObjToString(data["trailstatus"]) //data都为原线索数据
  642. trailstatusTime := common.ObjToString(data["trailstatus_time"])
  643. var trailstatusTimes time.Time
  644. if trailstatusTime != "" {
  645. trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
  646. }
  647. clueId := common.Int64All(data["id"])
  648. is_assign := common.IntAll(data["is_assign"])
  649. oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
  650. old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
  651. oldTaskTime = common.ObjToString(data["tasktime"])
  652. is_task = common.IntAll(data["is_task"])
  653. taskstatus = common.IntAll(data["taskstatus"])
  654. BCPCID := common.GetRandom(32)
  655. old_name := common.ObjToString(data["name"])
  656. old_position := common.ObjToString(data["position"])
  657. old_department := common.ObjToString(data["department"])
  658. old_follow_project_area := common.ObjToString(data["follow_project_area"])
  659. old_role := common.ObjToString(data["role"])
  660. old_cluename := common.ObjToString(data["cluename"])
  661. old_top_cluetype := common.ObjToString(data["top_cluetype"])
  662. old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
  663. old_topname, old_subname := "", ""
  664. is_transfer := common.IntAll(data["is_transfer"])
  665. if taskstatus == 1 || is_task == 0 {
  666. taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
  667. } else {
  668. if is_task == 1 {
  669. if oldTaskTime != "" { //以最新的任务时间为主
  670. t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
  671. if err == nil && time.Now().Before(t1) {
  672. taskTime = nowTime
  673. } else {
  674. taskTime = oldTaskTime
  675. }
  676. }
  677. }
  678. }
  679. count, counts, t := 0, 0, time.Now() //加入任务车判断节假日
  680. for {
  681. count++
  682. currentTime := t.AddDate(0, 0, -count)
  683. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  684. isok := false
  685. for k, v := range DateMap {
  686. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  687. isok = true
  688. }
  689. }
  690. if isok {
  691. counts++
  692. }
  693. } else {
  694. isok := true
  695. for k, v := range DateMap {
  696. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  697. isok = false
  698. }
  699. }
  700. if isok {
  701. counts++
  702. }
  703. }
  704. if counts >= 2 {
  705. break
  706. }
  707. }
  708. //两天之前不包含节假日,有没有跟进记录
  709. 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))
  710. //
  711. clueUpdateData := map[string]interface{}{
  712. "updatetime": nowTime,
  713. "top_cluetype": top_cluetype,
  714. "sub_cluetype": sub_cluetype,
  715. "userid": userId,
  716. "comeinsource_private": 2,
  717. "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
  718. "company_nature": isGroup,
  719. "company_verification": isCommerce,
  720. }
  721. old_remark := common.ObjToString(data["remark"])
  722. if old_remark != "" {
  723. remark = old_remark + ";" + remark //备注不能替换,只能往上拼接
  724. }
  725. clueUpdateData["remark"] = remark
  726. old_demand := common.ObjToString(data["customer_demand"])
  727. if demand != old_demand {
  728. clueUpdateData["customer_demand"] = demand
  729. }
  730. if cluename != "" {
  731. clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
  732. }
  733. if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
  734. if name != "" {
  735. clueUpdateData["name"] = name
  736. }
  737. if position != "" {
  738. clueUpdateData["position"] = position
  739. }
  740. if department != "" {
  741. clueUpdateData["department"] = department
  742. }
  743. if follow_project_area != "" {
  744. clueUpdateData["follow_project_area"] = follow_project_area
  745. }
  746. if role != "" {
  747. clueUpdateData["role"] = role
  748. }
  749. // clueUpdateData["industry"] = industry
  750. }
  751. if sourceCode == "app_xzcyh" { //这个留资单独处理一下
  752. if departments != "" {
  753. clueUpdateData["department"] = departments
  754. department = departments
  755. }
  756. business_scope := common.ObjToString(data["business_scope"])
  757. keywords := ""
  758. if len(keyword) > 0 && keyword[0] != "" {
  759. keywords = strings.Join(keyword, ",")
  760. if business_scope != "" {
  761. clueUpdateData["business_scope"] = business_scope + "," + keywords
  762. } else {
  763. clueUpdateData["business_scope"] = keywords
  764. }
  765. }
  766. }
  767. if old_position_id != 0 {
  768. for _, v := range saleData {
  769. if old_position_id == common.Int64All(v["position_id"]) {
  770. oldsaleName = common.ObjToString(v["name"])
  771. }
  772. }
  773. }
  774. if trailstatus == "00" || old_position_id == 0 || is_assign != 1 || (pIsOk && old_cluename != cluename && cluename != "") {
  775. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  776. } else {
  777. clueUpdateData["seatNumber"] = seatNumber
  778. clueUpdateData["position_id"] = positionId
  779. clueUpdateData["comeintime"] = nowTime
  780. clueUpdateData["is_assign"] = 1
  781. clueUpdateData["comeinsource_open"] = nil
  782. clueUpdateData["level_open"] = nil
  783. clueUpdateData["clue_level"] = nil
  784. }
  785. } else if is_assign == 1 && positionId != old_position_id {
  786. clueUpdateData["seatNumber"] = seatNumber
  787. clueUpdateData["position_id"] = positionId
  788. clueUpdateData["comeintime"] = nowTime
  789. clueUpdateData["is_assign"] = 1
  790. clueUpdateData["comeinsource_open"] = nil
  791. clueUpdateData["level_open"] = nil
  792. clueUpdateData["clue_level"] = nil
  793. } else { //不属于上边情况就还是属于原来的销售,不更换销售
  794. clueUpdateData["seatNumber"] = old_seatNumber
  795. clueUpdateData["position_id"] = old_position_id
  796. clueUpdateData["is_assign"] = 1
  797. clueUpdateData["comeinsource_open"] = nil
  798. clueUpdateData["level_open"] = nil
  799. clueUpdateData["clue_level"] = nil
  800. }
  801. if trailstatus != "08" && is_assign == 0 { //在公海的成交客户
  802. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  803. } else {
  804. clueUpdateData["trailstatus"] = "01"
  805. clueUpdateData["trailstatus_time"] = nowTime
  806. }
  807. }
  808. is_ok := false
  809. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  810. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  811. } else {
  812. if item == "eventReg" {
  813. eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
  814. if eData != nil && len(*eData) > 0 {
  815. for _, e := range *eData {
  816. new_value := common.ObjToString(e["new_value"])
  817. if strings.Contains(new_value, subname) {
  818. is_ok = true
  819. }
  820. }
  821. }
  822. if !is_ok {
  823. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  824. if taskTime == "" {
  825. taskTime = nowTime
  826. }
  827. clueUpdateData["task_time"] = nowTime
  828. clueUpdateData["tasktime"] = taskTime
  829. clueUpdateData["taskstatus"] = 0
  830. }
  831. if is_transfer != 1 {
  832. clueUpdateData["is_task"] = 1
  833. }
  834. }
  835. } else {
  836. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  837. if taskTime == "" {
  838. taskTime = nowTime
  839. }
  840. clueUpdateData["task_time"] = nowTime
  841. clueUpdateData["tasktime"] = taskTime
  842. clueUpdateData["taskstatus"] = 0
  843. }
  844. if is_transfer != 1 {
  845. clueUpdateData["is_task"] = 1
  846. }
  847. }
  848. }
  849. }
  850. ok, updateId14 := true, true
  851. updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
  852. updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
  853. updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
  854. if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
  855. //线索
  856. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  857. //变更记录
  858. if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
  859. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  860. } else if old_position_id == 0 {
  861. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  862. "clue_id": clueId,
  863. "position_id": positionId,
  864. "change_field": "position_id",
  865. "change_type": "所属人变更",
  866. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  867. "new_value": common.If(saleName != "", saleName, "/"),
  868. "createtime": nowTime,
  869. "BCPCID": common.GetRandom(32),
  870. "operator_id": -1,
  871. })
  872. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  873. "clue_id": clueId,
  874. "position_id": positionId,
  875. "change_field": "trailstatus",
  876. "change_type": "基本信息变更",
  877. "old_value": CodeTrail[trailstatus],
  878. "new_value": "新增",
  879. "createtime": nowTime,
  880. "BCPCID": common.GetRandom(32),
  881. "operator_id": -1,
  882. })
  883. } else {
  884. if pIsOk && old_cluename != cluename && cluename != "" {
  885. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  886. "clue_id": clueId,
  887. "position_id": positionId,
  888. "change_field": "position_id",
  889. "change_type": "所属人变更",
  890. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  891. "new_value": common.If(saleName != "", saleName, "/"),
  892. "createtime": nowTime,
  893. "BCPCID": common.GetRandom(32),
  894. "operator_id": -1,
  895. })
  896. } else {
  897. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  898. "clue_id": clueId,
  899. "position_id": positionId,
  900. "change_field": "position_id",
  901. "change_type": "所属人变更",
  902. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  903. "new_value": common.If(saleName != "", saleName, "/"),
  904. "createtime": nowTime,
  905. "BCPCID": common.GetRandom(32),
  906. "operator_id": -1,
  907. })
  908. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  909. "clue_id": clueId,
  910. "position_id": common.Int64All(data["position_id"]),
  911. "change_field": "trailstatus",
  912. "change_type": "基本信息变更",
  913. "old_value": "无意向客户",
  914. "new_value": "流失",
  915. "createtime": nowTime,
  916. "BCPCID": common.GetRandom(32),
  917. "operator_id": -1,
  918. })
  919. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  920. "clue_id": clueId,
  921. "position_id": positionId,
  922. "change_field": "trailstatus",
  923. "change_type": "基本信息变更",
  924. "old_value": "商机线索",
  925. "new_value": "新增",
  926. "createtime": nowTime,
  927. "BCPCID": common.GetRandom(32),
  928. "operator_id": -1,
  929. })
  930. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  931. "clue_id": clueId,
  932. "position_id": positionId,
  933. "change_field": "trailstatus",
  934. "change_type": "基本信息变更",
  935. "old_value": "无意向客户",
  936. "new_value": "商机线索",
  937. "createtime": nowTime,
  938. "BCPCID": common.GetRandom(32),
  939. "operator_id": -1,
  940. })
  941. }
  942. }
  943. } else if trailstatus != "08" && is_assign == 0 {
  944. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  945. } else {
  946. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  947. "clue_id": clueId,
  948. "position_id": positionId,
  949. "change_field": "position_id",
  950. "change_type": "所属人变更",
  951. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  952. "new_value": common.If(saleName != "", saleName, "/"),
  953. "createtime": nowTime,
  954. "BCPCID": common.GetRandom(32),
  955. "operator_id": -1,
  956. })
  957. if trailstatus != "01" {
  958. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  959. "clue_id": clueId,
  960. "position_id": positionId,
  961. "change_field": "trailstatus",
  962. "change_type": "基本信息变更",
  963. "old_value": CodeTrail[trailstatus],
  964. "new_value": "商机线索",
  965. "createtime": nowTime,
  966. "BCPCID": common.GetRandom(32),
  967. "operator_id": -1,
  968. })
  969. }
  970. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  971. "clue_id": clueId,
  972. "position_id": positionId,
  973. "change_field": "trailstatus",
  974. "change_type": "基本信息变更",
  975. "old_value": "商机线索",
  976. "new_value": "新增",
  977. "createtime": nowTime,
  978. "BCPCID": common.GetRandom(32),
  979. "operator_id": -1,
  980. })
  981. }
  982. } else if (pIsOk && old_cluename == cluename && cluename != "") && is_assign == 1 && positionId != old_position_id {
  983. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  984. "clue_id": clueId,
  985. "position_id": positionId,
  986. "change_field": "position_id",
  987. "change_type": "所属人变更",
  988. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  989. "new_value": common.If(saleName != "", saleName, "/"),
  990. "createtime": nowTime,
  991. "BCPCID": common.GetRandom(32),
  992. "operator_id": -1,
  993. })
  994. }
  995. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  996. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  997. } else {
  998. if old_sub_cluetype != sub_cluetype && !is_ok {
  999. if is_transfer != 1 {
  1000. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1001. "clue_id": clueId,
  1002. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))),
  1003. "change_type": "加入任务车",
  1004. "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
  1005. "createtime": nowTime,
  1006. "BCPCID": common.GetRandom(32),
  1007. "operator_id": -1,
  1008. })
  1009. } else {
  1010. cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
  1011. if cdata != nil && len(*cdata) > 0 {
  1012. kcpositionId := common.Int64All((*cdata)["position_id"])
  1013. tasksource := common.ObjToString((*cdata)["tasksource"])
  1014. taskstatus := common.IntAll((*cdata)["taskstatus"])
  1015. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1016. "clue_id": clueId,
  1017. "position_id": kcpositionId,
  1018. "change_type": "加入任务车",
  1019. "new_value": "线上主动留资",
  1020. "createtime": nowTime,
  1021. "BCPCID": common.GetRandom(32),
  1022. "operator_id": -1,
  1023. })
  1024. if taskstatus == 1 {
  1025. tasksource = "10"
  1026. } else {
  1027. if tasksource != "" && !strings.Contains(tasksource, "10") {
  1028. if !strings.Contains(tasksource, "10") {
  1029. tasksource += ",10"
  1030. }
  1031. } else {
  1032. tasksource = "10"
  1033. }
  1034. }
  1035. updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  1036. "tasksource": tasksource,
  1037. "tasktime": nowTime,
  1038. "is_task": 1,
  1039. "taskstatus": 0,
  1040. })
  1041. }
  1042. }
  1043. }
  1044. }
  1045. }
  1046. if old_top_cluetype != "" {
  1047. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  1048. if pcodeData != nil && len(*pcodeData) > 0 {
  1049. old_topname = common.ObjToString((*pcodeData)["name"])
  1050. }
  1051. }
  1052. if old_sub_cluetype != "" {
  1053. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  1054. if pcodeData != nil && len(*pcodeData) > 0 {
  1055. old_subname = common.ObjToString((*pcodeData)["name"])
  1056. }
  1057. }
  1058. if item != "orders" {
  1059. if old_name != name && name != "" {
  1060. updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1061. "clue_id": clueId,
  1062. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1063. "change_field": "name",
  1064. "change_type": "基本信息变更",
  1065. "old_value": common.If(old_name != "", old_name, "/"),
  1066. "new_value": common.If(name != "", name, "/"),
  1067. "createtime": nowTime,
  1068. "BCPCID": BCPCID,
  1069. "operator_id": -1,
  1070. })
  1071. }
  1072. if old_position != position && position != "" {
  1073. updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1074. "clue_id": clueId,
  1075. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1076. "change_field": "position",
  1077. "change_type": "基本信息变更",
  1078. "old_value": common.If(old_position != "", old_position, "/"),
  1079. "new_value": common.If(position != "", position, "/"),
  1080. "createtime": nowTime,
  1081. "BCPCID": BCPCID,
  1082. "operator_id": -1,
  1083. })
  1084. }
  1085. if old_department != department && department != "" {
  1086. updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1087. "clue_id": clueId,
  1088. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1089. "change_field": "department",
  1090. "change_type": "基本信息变更",
  1091. "old_value": common.If(old_department != "", old_department, "/"),
  1092. "new_value": common.If(department != "", department, "/"),
  1093. "createtime": nowTime,
  1094. "BCPCID": BCPCID,
  1095. "operator_id": -1,
  1096. })
  1097. }
  1098. if old_role != role && role != "" {
  1099. updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))), "change_field": "role", "change_type": "基本信息变更", "old_value": common.If(old_role != "", old_role, "/"), "new_value": common.If(role != "", role, "/"), "createtime": nowTime, "BCPCID": BCPCID, "operator_id": -1})
  1100. }
  1101. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  1102. old_area, old_area_arr := "", []string{}
  1103. new_area, new_area_arr := "", []string{}
  1104. for _, v := range strings.Split(old_follow_project_area, ",") {
  1105. old_area_arr = append(old_area_arr, CodeArea[v])
  1106. }
  1107. for _, v := range strings.Split(follow_project_area, ",") {
  1108. new_area_arr = append(new_area_arr, CodeArea[v])
  1109. }
  1110. old_area = strings.Join(old_area_arr, ",")
  1111. new_area = strings.Join(new_area_arr, ",")
  1112. updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1113. "clue_id": clueId,
  1114. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1115. "change_field": "follow_project_area",
  1116. "change_type": "基本信息变更",
  1117. "old_value": common.If(old_area != "", old_area, "/"),
  1118. "new_value": common.If(new_area != "", new_area, "/"),
  1119. "createtime": nowTime,
  1120. "BCPCID": BCPCID,
  1121. "operator_id": -1,
  1122. })
  1123. }
  1124. if old_cluename != cluename && cluename != "" {
  1125. updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1126. "clue_id": clueId,
  1127. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1128. "change_field": "cluename",
  1129. "change_type": "基本信息变更",
  1130. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  1131. "new_value": common.If(cluename != "", cluename, "/"),
  1132. "createtime": nowTime,
  1133. "BCPCID": BCPCID,
  1134. "operator_id": -1,
  1135. })
  1136. }
  1137. }
  1138. if old_top_cluetype != top_cluetype {
  1139. updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1140. "clue_id": clueId,
  1141. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"])))),
  1142. "change_field": "top_cluetype",
  1143. "change_type": "基本信息变更",
  1144. "old_value": common.If(old_topname != "", old_topname, "/"),
  1145. "new_value": common.If(topname != "", topname, "/"),
  1146. "createtime": nowTime,
  1147. "BCPCID": BCPCID,
  1148. "operator_id": -1,
  1149. })
  1150. }
  1151. if old_sub_cluetype != sub_cluetype {
  1152. updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1153. "clue_id": clueId,
  1154. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))))),
  1155. "change_field": "sub_cluetype",
  1156. "change_type": "基本信息变更",
  1157. "old_value": common.If(old_subname != "", old_subname, "/"),
  1158. "new_value": common.If(subname != "", subname, "/"),
  1159. "createtime": nowTime,
  1160. "BCPCID": BCPCID,
  1161. "operator_id": -1,
  1162. })
  1163. }
  1164. 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
  1165. }) {
  1166. log.Println("线索更新成功")
  1167. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  1168. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  1169. "status": 1,
  1170. "is_delete": 1,
  1171. "createtime": nowTime,
  1172. "updatetime": nowTime,
  1173. "phone": phone,
  1174. "baseinfo_id": uId,
  1175. "SOURCE": source,
  1176. })
  1177. }
  1178. if old_position_id == 0 {
  1179. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
  1180. }
  1181. return true
  1182. } else {
  1183. log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  1184. return false
  1185. }
  1186. }
  1187. func UpdateClue(data map[string]interface{}, saleData []map[string]interface{}, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, level string, positionId int64, source, sourceCode, remark string, keyword []string, belong_to string, isGroup, isCommerce int, pIsOk bool) bool {
  1188. log.Println("线索修改前", data)
  1189. nowTime := time.Now().Format("2006-01-02 15:04:05")
  1190. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  1191. trailstatus := common.ObjToString(data["trailstatus"]) //data都为原线索数据
  1192. trailstatusTime := common.ObjToString(data["trailstatus_time"])
  1193. var trailstatusTimes time.Time
  1194. if trailstatusTime != "" {
  1195. trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
  1196. }
  1197. clueId := common.Int64All(data["id"])
  1198. is_assign := common.IntAll(data["is_assign"])
  1199. oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
  1200. old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
  1201. oldTaskTime = common.ObjToString(data["tasktime"])
  1202. is_task = common.IntAll(data["is_task"])
  1203. taskstatus = common.IntAll(data["taskstatus"])
  1204. BCPCID := common.GetRandom(32)
  1205. old_name := common.ObjToString(data["name"])
  1206. old_position := common.ObjToString(data["position"])
  1207. old_department := common.ObjToString(data["department"])
  1208. old_follow_project_area := common.ObjToString(data["follow_project_area"])
  1209. old_role := common.ObjToString(data["role"])
  1210. old_cluename := common.ObjToString(data["cluename"])
  1211. old_top_cluetype := common.ObjToString(data["top_cluetype"])
  1212. old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
  1213. old_topname, old_subname := "", ""
  1214. is_transfer := common.IntAll(data["is_transfer"])
  1215. if taskstatus == 1 || is_task == 0 {
  1216. taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
  1217. } else {
  1218. if is_task == 1 {
  1219. if oldTaskTime != "" { //以最新的任务时间为主
  1220. t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
  1221. if err == nil && time.Now().Before(t1) {
  1222. taskTime = nowTime
  1223. } else {
  1224. taskTime = oldTaskTime
  1225. }
  1226. }
  1227. }
  1228. }
  1229. count, counts, t := 0, 0, time.Now() //加入任务车判断节假日
  1230. for {
  1231. count++
  1232. currentTime := t.AddDate(0, 0, -count)
  1233. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  1234. isok := false
  1235. for k, v := range DateMap {
  1236. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  1237. isok = true
  1238. }
  1239. }
  1240. if isok {
  1241. counts++
  1242. }
  1243. } else {
  1244. isok := true
  1245. for k, v := range DateMap {
  1246. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  1247. isok = false
  1248. }
  1249. }
  1250. if isok {
  1251. counts++
  1252. }
  1253. }
  1254. if counts >= 2 {
  1255. break
  1256. }
  1257. }
  1258. //两天之前不包含节假日,有没有跟进记录
  1259. 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))
  1260. //
  1261. clueUpdateData := map[string]interface{}{
  1262. "updatetime": nowTime,
  1263. "top_cluetype": top_cluetype,
  1264. "sub_cluetype": sub_cluetype,
  1265. "userid": userId,
  1266. "comeinsource_private": 2,
  1267. "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
  1268. "company_nature": isGroup,
  1269. "company_verification": isCommerce,
  1270. }
  1271. old_remark := common.ObjToString(data["remark"])
  1272. if old_remark != "" {
  1273. remark = old_remark + ";" + remark //备注不能替换,只能往上拼接
  1274. }
  1275. clueUpdateData["remark"] = remark
  1276. if cluename != "" {
  1277. clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
  1278. }
  1279. if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
  1280. if name != "" {
  1281. clueUpdateData["name"] = name
  1282. }
  1283. if position != "" {
  1284. clueUpdateData["position"] = position
  1285. }
  1286. if department != "" {
  1287. clueUpdateData["department"] = department
  1288. }
  1289. if follow_project_area != "" {
  1290. clueUpdateData["follow_project_area"] = follow_project_area
  1291. }
  1292. if role != "" {
  1293. clueUpdateData["role"] = role
  1294. }
  1295. // clueUpdateData["industry"] = industry
  1296. }
  1297. if sourceCode == "app_xzcyh" { //这个留资单独处理一下
  1298. if departments != "" {
  1299. clueUpdateData["department"] = departments
  1300. department = departments
  1301. }
  1302. business_scope := common.ObjToString(data["business_scope"])
  1303. keywords := ""
  1304. if len(keyword) > 0 && keyword[0] != "" {
  1305. keywords = strings.Join(keyword, ",")
  1306. if business_scope != "" {
  1307. clueUpdateData["business_scope"] = business_scope + "," + keywords
  1308. } else {
  1309. clueUpdateData["business_scope"] = keywords
  1310. }
  1311. }
  1312. }
  1313. if trailstatus == "00" || old_position_id == 0 || is_assign != 1 || (pIsOk && old_cluename != cluename && cluename != "") {
  1314. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1315. } else {
  1316. clueUpdateData["seatNumber"] = seatNumber
  1317. clueUpdateData["position_id"] = positionId
  1318. clueUpdateData["comeintime"] = nowTime
  1319. clueUpdateData["is_assign"] = 1
  1320. clueUpdateData["comeinsource_open"] = nil
  1321. clueUpdateData["level_open"] = nil
  1322. clueUpdateData["clue_level"] = nil
  1323. for _, v := range saleData {
  1324. if common.Int64All(data["position_id"]) == common.Int64All(v["position_id"]) {
  1325. oldsaleName = common.ObjToString(v["name"])
  1326. }
  1327. }
  1328. }
  1329. } else { //不属于上边情况就还是属于原来的销售,不更换销售
  1330. clueUpdateData["seatNumber"] = old_seatNumber
  1331. clueUpdateData["position_id"] = old_position_id
  1332. clueUpdateData["is_assign"] = 1
  1333. clueUpdateData["comeinsource_open"] = nil
  1334. clueUpdateData["level_open"] = nil
  1335. clueUpdateData["clue_level"] = nil
  1336. }
  1337. if trailstatus != "08" && is_assign == 0 { //在公海的成交客户
  1338. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1339. } else {
  1340. clueUpdateData["trailstatus"] = "01"
  1341. clueUpdateData["trailstatus_time"] = nowTime
  1342. }
  1343. }
  1344. is_ok := false
  1345. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  1346. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1347. } else {
  1348. if item == "eventReg" {
  1349. eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
  1350. if eData != nil && len(*eData) > 0 {
  1351. for _, e := range *eData {
  1352. new_value := common.ObjToString(e["new_value"])
  1353. if strings.Contains(new_value, subname) {
  1354. is_ok = true
  1355. }
  1356. }
  1357. }
  1358. if !is_ok {
  1359. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  1360. if taskTime == "" {
  1361. taskTime = nowTime
  1362. }
  1363. clueUpdateData["task_time"] = nowTime
  1364. clueUpdateData["tasktime"] = taskTime
  1365. clueUpdateData["taskstatus"] = 0
  1366. }
  1367. if is_transfer != 1 {
  1368. clueUpdateData["is_task"] = 1
  1369. }
  1370. }
  1371. } else {
  1372. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  1373. if taskTime == "" {
  1374. taskTime = nowTime
  1375. }
  1376. clueUpdateData["task_time"] = nowTime
  1377. clueUpdateData["tasktime"] = taskTime
  1378. clueUpdateData["taskstatus"] = 0
  1379. }
  1380. if is_transfer != 1 {
  1381. clueUpdateData["is_task"] = 1
  1382. }
  1383. }
  1384. }
  1385. }
  1386. ok, updateId14 := true, true
  1387. updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
  1388. updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
  1389. updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
  1390. if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
  1391. //线索
  1392. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  1393. //变更记录
  1394. if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
  1395. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1396. } else if old_position_id == 0 {
  1397. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1398. "clue_id": clueId,
  1399. "position_id": positionId,
  1400. "change_field": "position_id",
  1401. "change_type": "所属人变更",
  1402. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1403. "new_value": common.If(saleName != "", saleName, "/"),
  1404. "createtime": nowTime,
  1405. "BCPCID": common.GetRandom(32),
  1406. "operator_id": -1,
  1407. })
  1408. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1409. "clue_id": clueId,
  1410. "position_id": positionId,
  1411. "change_field": "trailstatus",
  1412. "change_type": "基本信息变更",
  1413. "old_value": CodeTrail[trailstatus],
  1414. "new_value": "新增",
  1415. "createtime": nowTime,
  1416. "BCPCID": common.GetRandom(32),
  1417. "operator_id": -1,
  1418. })
  1419. } else {
  1420. if pIsOk && old_cluename != cluename && cluename != "" {
  1421. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1422. "clue_id": clueId,
  1423. "position_id": positionId,
  1424. "change_field": "position_id",
  1425. "change_type": "所属人变更",
  1426. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1427. "new_value": common.If(saleName != "", saleName, "/"),
  1428. "createtime": nowTime,
  1429. "BCPCID": common.GetRandom(32),
  1430. "operator_id": -1,
  1431. })
  1432. } else {
  1433. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1434. "clue_id": clueId,
  1435. "position_id": positionId,
  1436. "change_field": "position_id",
  1437. "change_type": "所属人变更",
  1438. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1439. "new_value": common.If(saleName != "", saleName, "/"),
  1440. "createtime": nowTime,
  1441. "BCPCID": common.GetRandom(32),
  1442. "operator_id": -1,
  1443. })
  1444. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1445. "clue_id": clueId,
  1446. "position_id": common.Int64All(data["position_id"]),
  1447. "change_field": "trailstatus",
  1448. "change_type": "基本信息变更",
  1449. "old_value": "无意向客户",
  1450. "new_value": "流失",
  1451. "createtime": nowTime,
  1452. "BCPCID": common.GetRandom(32),
  1453. "operator_id": -1,
  1454. })
  1455. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1456. "clue_id": clueId,
  1457. "position_id": positionId,
  1458. "change_field": "trailstatus",
  1459. "change_type": "基本信息变更",
  1460. "old_value": "商机线索",
  1461. "new_value": "新增",
  1462. "createtime": nowTime,
  1463. "BCPCID": common.GetRandom(32),
  1464. "operator_id": -1,
  1465. })
  1466. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1467. "clue_id": clueId,
  1468. "position_id": positionId,
  1469. "change_field": "trailstatus",
  1470. "change_type": "基本信息变更",
  1471. "old_value": "无意向客户",
  1472. "new_value": "商机线索",
  1473. "createtime": nowTime,
  1474. "BCPCID": common.GetRandom(32),
  1475. "operator_id": -1,
  1476. })
  1477. }
  1478. }
  1479. } else if trailstatus != "08" && is_assign == 0 {
  1480. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1481. } else {
  1482. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1483. "clue_id": clueId,
  1484. "position_id": positionId,
  1485. "change_field": "position_id",
  1486. "change_type": "所属人变更",
  1487. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  1488. "new_value": common.If(saleName != "", saleName, "/"),
  1489. "createtime": nowTime,
  1490. "BCPCID": common.GetRandom(32),
  1491. "operator_id": -1,
  1492. })
  1493. if trailstatus != "01" {
  1494. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1495. "clue_id": clueId,
  1496. "position_id": positionId,
  1497. "change_field": "trailstatus",
  1498. "change_type": "基本信息变更",
  1499. "old_value": CodeTrail[trailstatus],
  1500. "new_value": "商机线索",
  1501. "createtime": nowTime,
  1502. "BCPCID": common.GetRandom(32),
  1503. "operator_id": -1,
  1504. })
  1505. }
  1506. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1507. "clue_id": clueId,
  1508. "position_id": positionId,
  1509. "change_field": "trailstatus",
  1510. "change_type": "基本信息变更",
  1511. "old_value": "商机线索",
  1512. "new_value": "新增",
  1513. "createtime": nowTime,
  1514. "BCPCID": common.GetRandom(32),
  1515. "operator_id": -1,
  1516. })
  1517. }
  1518. }
  1519. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  1520. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  1521. } else {
  1522. if old_sub_cluetype != sub_cluetype && !is_ok {
  1523. if is_transfer != 1 {
  1524. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1525. "clue_id": clueId,
  1526. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  1527. "change_type": "加入任务车",
  1528. "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
  1529. "createtime": nowTime,
  1530. "BCPCID": common.GetRandom(32),
  1531. "operator_id": -1,
  1532. })
  1533. } else {
  1534. cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
  1535. if cdata != nil && len(*cdata) > 0 {
  1536. kcpositionId := common.Int64All((*cdata)["position_id"])
  1537. tasksource := common.ObjToString((*cdata)["tasksource"])
  1538. taskstatus := common.IntAll((*cdata)["taskstatus"])
  1539. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1540. "clue_id": clueId,
  1541. "position_id": kcpositionId,
  1542. "change_type": "加入任务车",
  1543. "new_value": "线上主动留资",
  1544. "createtime": nowTime,
  1545. "BCPCID": common.GetRandom(32),
  1546. "operator_id": -1,
  1547. })
  1548. if taskstatus == 1 {
  1549. tasksource = "10"
  1550. } else {
  1551. if tasksource != "" && !strings.Contains(tasksource, "10") {
  1552. if !strings.Contains(tasksource, "10") {
  1553. tasksource += ",10"
  1554. }
  1555. } else {
  1556. tasksource = "10"
  1557. }
  1558. }
  1559. updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  1560. "tasksource": tasksource,
  1561. "tasktime": nowTime,
  1562. "is_task": 1,
  1563. "taskstatus": 0,
  1564. })
  1565. }
  1566. }
  1567. }
  1568. }
  1569. }
  1570. if old_top_cluetype != "" {
  1571. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  1572. if pcodeData != nil && len(*pcodeData) > 0 {
  1573. old_topname = common.ObjToString((*pcodeData)["name"])
  1574. }
  1575. }
  1576. if old_sub_cluetype != "" {
  1577. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  1578. if pcodeData != nil && len(*pcodeData) > 0 {
  1579. old_subname = common.ObjToString((*pcodeData)["name"])
  1580. }
  1581. }
  1582. if item != "orders" {
  1583. if old_name != name && name != "" {
  1584. updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1585. "clue_id": clueId,
  1586. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1587. "change_field": "name",
  1588. "change_type": "基本信息变更",
  1589. "old_value": common.If(old_name != "", old_name, "/"),
  1590. "new_value": common.If(name != "", name, "/"),
  1591. "createtime": nowTime,
  1592. "BCPCID": BCPCID,
  1593. "operator_id": -1,
  1594. })
  1595. }
  1596. if old_position != position && position != "" {
  1597. updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1598. "clue_id": clueId,
  1599. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1600. "change_field": "position",
  1601. "change_type": "基本信息变更",
  1602. "old_value": common.If(old_position != "", old_position, "/"),
  1603. "new_value": common.If(position != "", position, "/"),
  1604. "createtime": nowTime,
  1605. "BCPCID": BCPCID,
  1606. "operator_id": -1,
  1607. })
  1608. }
  1609. if old_department != department && department != "" {
  1610. updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1611. "clue_id": clueId,
  1612. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1613. "change_field": "department",
  1614. "change_type": "基本信息变更",
  1615. "old_value": common.If(old_department != "", old_department, "/"),
  1616. "new_value": common.If(department != "", department, "/"),
  1617. "createtime": nowTime,
  1618. "BCPCID": BCPCID,
  1619. "operator_id": -1,
  1620. })
  1621. }
  1622. if old_role != role && role != "" {
  1623. updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))), "change_field": "role", "change_type": "基本信息变更", "old_value": common.If(old_role != "", old_role, "/"), "new_value": common.If(role != "", role, "/"), "createtime": nowTime, "BCPCID": BCPCID, "operator_id": -1})
  1624. }
  1625. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  1626. old_area, old_area_arr := "", []string{}
  1627. new_area, new_area_arr := "", []string{}
  1628. for _, v := range strings.Split(old_follow_project_area, ",") {
  1629. old_area_arr = append(old_area_arr, CodeArea[v])
  1630. }
  1631. for _, v := range strings.Split(follow_project_area, ",") {
  1632. new_area_arr = append(new_area_arr, CodeArea[v])
  1633. }
  1634. old_area = strings.Join(old_area_arr, ",")
  1635. new_area = strings.Join(new_area_arr, ",")
  1636. updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1637. "clue_id": clueId,
  1638. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1639. "change_field": "follow_project_area",
  1640. "change_type": "基本信息变更",
  1641. "old_value": common.If(old_area != "", old_area, "/"),
  1642. "new_value": common.If(new_area != "", new_area, "/"),
  1643. "createtime": nowTime,
  1644. "BCPCID": BCPCID,
  1645. "operator_id": -1,
  1646. })
  1647. }
  1648. if old_cluename != cluename && cluename != "" {
  1649. updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1650. "clue_id": clueId,
  1651. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1652. "change_field": "cluename",
  1653. "change_type": "基本信息变更",
  1654. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  1655. "new_value": common.If(cluename != "", cluename, "/"),
  1656. "createtime": nowTime,
  1657. "BCPCID": BCPCID,
  1658. "operator_id": -1,
  1659. })
  1660. }
  1661. }
  1662. if old_top_cluetype != top_cluetype {
  1663. updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1664. "clue_id": clueId,
  1665. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1666. "change_field": "top_cluetype",
  1667. "change_type": "基本信息变更",
  1668. "old_value": common.If(old_topname != "", old_topname, "/"),
  1669. "new_value": common.If(topname != "", topname, "/"),
  1670. "createtime": nowTime,
  1671. "BCPCID": BCPCID,
  1672. "operator_id": -1,
  1673. })
  1674. }
  1675. if old_sub_cluetype != sub_cluetype {
  1676. updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1677. "clue_id": clueId,
  1678. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1679. "change_field": "sub_cluetype",
  1680. "change_type": "基本信息变更",
  1681. "old_value": common.If(old_subname != "", old_subname, "/"),
  1682. "new_value": common.If(subname != "", subname, "/"),
  1683. "createtime": nowTime,
  1684. "BCPCID": BCPCID,
  1685. "operator_id": -1,
  1686. })
  1687. }
  1688. 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
  1689. }) {
  1690. log.Println("线索更新成功")
  1691. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  1692. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  1693. "status": 1,
  1694. "is_delete": 1,
  1695. "createtime": nowTime,
  1696. "updatetime": nowTime,
  1697. "phone": phone,
  1698. "baseinfo_id": uId,
  1699. "SOURCE": source,
  1700. })
  1701. }
  1702. if old_position_id == 0 {
  1703. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
  1704. }
  1705. return true
  1706. } else {
  1707. log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  1708. return false
  1709. }
  1710. }