123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725 |
- package main
- import (
- "database/sql"
- "fmt"
- "log"
- "strings"
- "time"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/mongodb"
- )
- func FormatData(data map[string]interface{}, item string) (bool, bool) {
- userId, uId, positionId, source, cluename, phone, sourceCode, keyword := common.ObjToString(data["user_id"]), "", "", "", "", "", "", []string{}
- role, industry, department, departments, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", "", ""
- query, topname, subname, belong_to, sourceName, remark, sourceId := map[string]interface{}{}, "", "", "01", "", "", int64(0)
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- if item == "orders" { //订单
- if !mongodb.IsObjectIdHex(userId) {
- positionId = userId
- userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
- if userMapping != nil && len(*userMapping) > 0 {
- userId = common.ObjToString((*userMapping)["userid"])
- }
- }
- query["userid"] = userId
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"]) //用户来源
- belong_to = common.ObjToString((*userInfo)["belong_to"]) //用户归属
- }
- cluename = common.ObjToString(data["company_name"])
- phone = common.ObjToString(data["user_phone"])
- } else if item == "message" {
- userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"base_user_id": common.Int64All(data["own_id"])}, "", "")
- if userMapping != nil && len(*userMapping) > 0 {
- userId = common.ObjToString((*userMapping)["userid"])
- positionId = fmt.Sprint((*userMapping)["position_id"])
- }
- query["userid"] = userId
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"])
- belong_to = common.ObjToString((*userInfo)["belong_to"])
- phone = common.ObjToString((*userInfo)["phone"])
- if phone != "" {
- phoneMapping := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
- if phoneMapping != nil && len(*phoneMapping) > 0 {
- cluename = common.ObjToString((*phoneMapping)["cluename"])
- }
- }
- }
- } else if item == "users" || item == "xcxusers" { //注册用户
- userId = mongodb.BsonIdToSId(data["userid"])
- //新用户没有uid、source要等5分钟
- cluename = common.ObjToString(data["company_name"])
- phone = common.ObjToString(data["phone"])
- if phone != "" {
- contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
- if contactsData != nil && len(*contactsData) > 0 {
- if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
- uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
- query["uid"] = uId
- } else {
- query["userid"] = userId
- }
- } else {
- query["userid"] = userId
- }
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"])
- belong_to = common.ObjToString((*userInfo)["belong_to"])
- }
- if item == "xcxusers" {
- sourceCode = common.ObjToString(data["s_sourceid"])
- if sourceCode == "" {
- log.Println("留资没有source", phone)
- return true, true
- }
- }
- }
- } else if item == "saleLeads" { //留资
- userId = common.ObjToString(data["userid"])
- if !mongodb.IsObjectIdHex(userId) {
- positionId = userId
- userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
- if userMapping != nil && len(*userMapping) > 0 {
- userId = common.ObjToString((*userMapping)["userid"])
- }
- }
- cluename = common.ObjToString(data["company"])
- phone = common.ObjToString(data["phone"])
- role = common.ObjToString(data["companyType"])
- industry = common.ObjToString(data["industry"])
- department = common.ObjToString(data["branch"])
- departments = common.ObjToString(data["department"])
- position = common.ObjToString(data["position"])
- name = common.ObjToString(data["name"])
- sourceCode = common.ObjToString(data["source"])
- if sourceCode == "" {
- log.Println("留资没有source", phone)
- return true, true
- }
- remark = common.ObjToString(data["jyRemark"]) //荟聚线索备注
- keywordArr := data["keyword"]
- if keywordArr != nil {
- keyword = common.ObjArrToStringArr(data["keyword"].([]interface{}))
- }
- contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
- if contactsData != nil && len(*contactsData) > 0 {
- if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
- uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
- query["uid"] = uId
- } else {
- query["userid"] = userId
- }
- } else {
- query["userid"] = userId
- }
- if userId != "" || uId != "" {
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"])
- belong_to = common.ObjToString((*userInfo)["belong_to"])
- userId = common.ObjToString((*userInfo)["userid"])
- }
- } else {
- if sourceCode == "report_retention" || sourceCode == "marketing_retention" { //荟聚线索来源,不是剑鱼用户,需要等待用户归集
- log.Println("不是剑鱼用户留资,等待用户", phone)
- return false, false
- } else if remark != "" {
- log.Println("留资source过滤", phone, sourceCode)
- return true, true
- }
- }
- } else if item == "eventReg" { //渠道
- userId = common.ObjToString(data["userid"])
- cluename = common.ObjToString(data["company"])
- phone = common.ObjToString(data["sign_phone"])
- role = common.ObjToString(data["company_type"])
- department = common.ObjToString(data["branch"])
- position = common.ObjToString(data["position"])
- name = common.ObjToString(data["sign_name"])
- sourceCode = common.ObjToString(data["source_code"])
- sourceName = common.ObjToString(data["source_name"])
- log.Println(sourceName)
- contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
- if contactsData != nil && len(*contactsData) > 0 {
- if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
- uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
- query["uid"] = uId
- } else {
- query["userid"] = userId
- }
- } else {
- query["userid"] = userId
- }
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"])
- belong_to = common.ObjToString((*userInfo)["belong_to"])
- userId = common.ObjToString((*userInfo)["userid"])
- }
- if role == "集成商" || role == "设计院" {
- role = "其他-" + role
- }
- } else if item == "readClue" {
- userId = common.ObjToString(data["userId"])
- query["userid"] = userId
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"])
- belong_to = common.ObjToString((*userInfo)["belong_to"])
- }
- // cluename = common.ObjToString(data["companyName"])
- phone = common.ObjToString(data["phone"])
- sourceId = common.Int64All(data["sourceId"])
- }
- if cluename == "" && item != "message" && item != "orders" && item != "readClue" {
- cluename = phone //没有线索名,手机号代替
- }
- isGroup, isCommerce := GetCompanyType(cluename) //判断是否集团公司、工商库
- if source == "0104" || strings.HasPrefix(belong_to, "02") || source == "0102" || source == "0103" || phone == "" { //参照用户来源代码表
- //log.Println("线索分配失败,线索过滤!!", item, source, phone, userId)
- //saveHlyj(belong_to, item, phone, name, sourceName, cluename, position, nowTime, isGroup, isCommerce)
- return true, true
- }
- if uId == "" { //没有进用户归集,等待进入,结束任务,放在下次继续执行
- if isExists, _ := redis.Exists("bidx", "bidx_userId_"+userId); isExists {
- redisInt := redis.GetInt("bidx", "bidx_userId_"+userId)
- if redisInt > 4 {
- log.Println("线索分配失败,线索缺少信息,任务已执行超过1次", item, userId, phone)
- return true, true
- } else {
- redis.Incr("bidx", "bidx_userId_"+userId)
- }
- } else {
- redis.Put("bidx", "bidx_userId_"+userId, 1, 3600)
- }
- log.Println("线索分配失败,线索缺少信息", item, phone, userId)
- return false, false
- }
- if item == "orders" { //订单没信息再查一遍留资
- qid := positionId
- if qid == "" {
- qid = userId
- }
- saleLeadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"userid": qid}, map[string]interface{}{"_id": -1}, nil, false, 0, 1)
- if ok && saleLeadsData != nil && len(*saleLeadsData) > 0 {
- sdata := (*saleLeadsData)[0]
- role = common.ObjToString(sdata["companyType"])
- industry = common.ObjToString(sdata["industry"])
- department = common.ObjToString(sdata["branch"])
- departments = common.ObjToString(sdata["department"])
- position = common.ObjToString(sdata["position"])
- name = common.ObjToString(sdata["name"])
- }
- }
- top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data, sourceCode, sourceId) //查留资来源名字
- if topname == "市场活动" && item == "saleLeads" { //市场活动的不要
- log.Println("市场活动留资过滤 ", userId, phone)
- return true, true
- }
- if strings.HasPrefix(belong_to, "03") { //一切都好的不进
- isOk := saveEverything(userId, phone, item, subname, sourceCode)
- log.Println("渠道线索电销", userId, phone, item, subname, sourceCode)
- if !isOk {
- return true, true
- }
- }
- follow_project_area = getAreaCode(userId) //关注项目区域
- log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area, isGroup, isCommerce)
- if top_cluetype == "" || sub_cluetype == "" || level == "" {
- log.Println("线索分配失败,线索过滤top_cluetype!!", item, uId, phone, userId)
- return true, true
- }
- position_id, seatNumber, saleName, saleData, pIsOk, isFreeze, noticePositionId := autoDraw(level, cluename, phone, isGroup, isCommerce) //查询当前分配次数最少的,如果当前线索有销售,此次找出的不会+1
- log.Println("data -------", position_id, seatNumber, saleName, pIsOk, isFreeze, noticePositionId)
- if position_id > 0 && seatNumber != "" {
- uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
- if uCount != nil && len(*uCount) > 0 { //已存在,走更新
- batch_import := common.ObjToString((*uCount)["batch_import"])
- if batch_import != "" && item == "users" { //有导入批次号还是新用户,不执行
- return true, true
- }
- //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, "")
- 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)
- } else { //不存在走新增
- 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, "")
- if oks { //新增成功,销售分配次数+1
- TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, position_id, level)
- }
- }
- if !oks {
- log.Println("线索分配失败!!", item, position_id, seatNumber, uId, userId, phone)
- return false, false
- }
- } else {
- if isFreeze {
- //达上限放公海处理
- uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
- if uCount == nil || len(*uCount) == 0 {
- 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)
- log.Println(oks)
- } else {
- //留资线索更新
- if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
- BCPCID := common.GetRandom(32)
- clueId := common.Int64All((*uCount)["id"])
- old_name := common.ObjToString((*uCount)["name"])
- old_position := common.ObjToString((*uCount)["position"])
- old_department := common.ObjToString((*uCount)["department"])
- old_follow_project_area := common.ObjToString((*uCount)["follow_project_area"])
- old_role := common.ObjToString((*uCount)["role"])
- old_cluename := common.ObjToString((*uCount)["cluename"])
- old_top_cluetype := common.ObjToString((*uCount)["top_cluetype"])
- old_sub_cluetype := common.ObjToString((*uCount)["sub_cluetype"])
- clueUpdateData := map[string]interface{}{}
- if old_name != name && name != "" {
- clueUpdateData["name"] = name
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "name",
- "position_id": 0,
- "change_type": "基本信息变更",
- "old_value": common.If(old_name != "", old_name, "/"),
- "new_value": common.If(name != "", name, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1})
- }
- if old_position != position && position != "" {
- clueUpdateData["position"] = position
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "position",
- "position_id": 0,
- "change_type": "基本信息变更",
- "old_value": common.If(old_position != "", old_position, "/"),
- "new_value": common.If(position != "", position, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1})
- }
- if old_department != department && department != "" {
- clueUpdateData["department"] = department
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "department",
- "position_id": 0,
- "change_type": "基本信息变更",
- "old_value": common.If(old_department != "", old_department, "/"),
- "new_value": common.If(department != "", department, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1})
- }
- if old_follow_project_area != follow_project_area && follow_project_area != "" {
- clueUpdateData["follow_project_area"] = follow_project_area
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "follow_project_area",
- "position_id": 0,
- "change_type": "基本信息变更",
- "old_value": common.If(old_follow_project_area != "", old_follow_project_area, "/"),
- "new_value": common.If(follow_project_area != "", follow_project_area, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1})
- }
- if old_role != role && role != "" {
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "role",
- "change_type": "基本信息变更",
- "old_value": common.If(old_role != "", old_role, "/"),
- "new_value": common.If(role != "", role, "/"),
- "createtime": nowTime,
- "position_id": 0,
- "BCPCID": BCPCID,
- "operator_id": -1})
- clueUpdateData["role"] = role
- }
- if old_cluename != cluename && cluename != "" {
- clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "cluename",
- "change_type": "基本信息变更",
- "old_value": common.If(old_cluename != "", old_cluename, "/"),
- "new_value": common.If(cluename != "", cluename, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "position_id": 0,
- "operator_id": -1})
- }
- old_topname := ""
- old_subname := ""
- if old_top_cluetype != "" {
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- old_topname = common.ObjToString((*pcodeData)["name"])
- }
- }
- if old_sub_cluetype != "" {
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- old_subname = common.ObjToString((*pcodeData)["name"])
- }
- }
- if old_top_cluetype != top_cluetype && top_cluetype != "" {
- clueUpdateData["top_cluetype"] = top_cluetype //新的线索名不为空才替换
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "top_cluetype",
- "position_id": 0,
- "change_type": "基本信息变更",
- "old_value": common.If(old_topname != "", old_topname, "/"),
- "new_value": common.If(topname != "", topname, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1})
- }
- if old_sub_cluetype != sub_cluetype && sub_cluetype != "" {
- clueUpdateData["sub_cluetype"] = sub_cluetype //新的线索名不为空才替换
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "change_field": "sub_cluetype",
- "position_id": 0,
- "change_type": "基本信息变更",
- "old_value": common.If(old_subname != "", old_subname, "/"),
- "new_value": common.If(subname != "", subname, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1})
- }
- clueUpdateData["company_nature"] = isGroup
- clueUpdateData["company_verification"] = isCommerce
- TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
- }
- }
- AssFail(noticePositionId, cluename, phone)
- return true, true
- }
- return true, false
- }
- return true, true
- }
- 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 {
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
- clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
- if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
- keywords := ""
- if sourceCode == "app_xzcyh" {
- if len(keyword) > 0 && keyword[0] != "" {
- keywords = strings.Join(keyword, ",")
- }
- }
- if cluename == "" {
- cluename = phone
- }
- clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
- "userid": userId,
- "uid": uId,
- "seatNumber": seatNumber,
- "position_id": positionId,
- "is_assign": common.If(isFreeze, -3, 1),
- "comeintime": nowTime,
- "createtime": nowTime,
- "updatetime": nowTime,
- "cluename": cluename,
- "top_cluetype": top_cluetype,
- "sub_cluetype": sub_cluetype,
- "trailstatus": "01",
- "name": name,
- "phone": phone,
- "position": position,
- "department": common.If(sourceCode == "app_xzcyh", departments, department),
- "industry": industry,
- "follow_project_area": follow_project_area,
- "role": role,
- "comeinsource_private": 2,
- "is_task": 1,
- "task_time": nowTime,
- "tasktime": common.If(item == "users", nowTimes, nowTime),
- "taskstatus": 0,
- "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
- "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
- "company_nature": isGroup,
- "company_verification": isCommerce,
- "remark": remark,
- "customer_demand": demand,
- "FREEZE_TIME": nowTime,
- })
- uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_type": "创建线索",
- "new_value": "系统自动创建",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": "/",
- "new_value": saleName,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_type": "加入任务车",
- "new_value": "线索自动分配" + "-" + topname + "-" + subname,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "top_cluetype",
- "change_type": "基本信息变更",
- "old_value": "/",
- "new_value": topname,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "sub_cluetype",
- "change_type": "基本信息变更",
- "old_value": "/",
- "new_value": subname,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- //冻结处理
- return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1 && uodateId6 > -1
- }) {
- log.Println("线索分配成功")
- if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
- TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
- "status": 1,
- "is_delete": 1,
- "createtime": nowTime,
- "updatetime": nowTime,
- "phone": phone,
- "baseinfo_id": uId,
- "SOURCE": source,
- })
- }
- return true
- } else {
- log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
- return false
- }
- }
- 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 {
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- clueId, uodateId1, uodateId2, uodateId3 := int64(0), int64(0), int64(0), int64(0)
- if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
- keywords := ""
- if sourceCode == "app_xzcyh" {
- if len(keyword) > 0 && keyword[0] != "" {
- keywords = strings.Join(keyword, ",")
- }
- }
- if cluename == "" {
- cluename = phone
- }
- clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
- "userid": userId,
- "uid": uId,
- "seatNumber": "",
- "position_id": 0,
- "is_assign": 0,
- "comeintime": nowTime,
- "createtime": nowTime,
- "updatetime": nowTime,
- "cluename": cluename,
- "top_cluetype": top_cluetype,
- "sub_cluetype": sub_cluetype,
- "trailstatus": "01",
- "name": name,
- "phone": phone,
- "position": position,
- "department": common.If(sourceCode == "app_xzcyh", departments, department),
- "industry": industry,
- "follow_project_area": follow_project_area,
- "role": role,
- "comeinsource_private": 2,
- "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
- "company_nature": isGroup,
- "company_verification": isCommerce,
- "remark": remark,
- "FREEZE_TIME": nowTime,
- })
- uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_type": "创建线索",
- "new_value": "系统自动创建",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": "",
- "new_value": "/",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1
- }) {
- log.Println("线索分配成功")
- if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
- TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
- "status": 1,
- "is_delete": 1,
- "createtime": nowTime,
- "updatetime": nowTime,
- "phone": phone,
- "baseinfo_id": uId,
- "SOURCE": source,
- })
- }
- return true
- } else {
- log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
- return false
- }
- }
- 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 {
- log.Println("工单线索修改前", data)
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
- trailstatus := common.ObjToString(data["trailstatus"]) //data都为原线索数据
- trailstatusTime := common.ObjToString(data["trailstatus_time"])
- var trailstatusTimes time.Time
- if trailstatusTime != "" {
- trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
- }
- clueId := common.Int64All(data["id"])
- is_assign := common.IntAll(data["is_assign"])
- oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
- old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
- oldTaskTime = common.ObjToString(data["tasktime"])
- is_task = common.IntAll(data["is_task"])
- taskstatus = common.IntAll(data["taskstatus"])
- BCPCID := common.GetRandom(32)
- old_name := common.ObjToString(data["name"])
- old_position := common.ObjToString(data["position"])
- old_department := common.ObjToString(data["department"])
- old_follow_project_area := common.ObjToString(data["follow_project_area"])
- old_role := common.ObjToString(data["role"])
- old_cluename := common.ObjToString(data["cluename"])
- old_top_cluetype := common.ObjToString(data["top_cluetype"])
- old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
- old_topname, old_subname := "", ""
- is_transfer := common.IntAll(data["is_transfer"])
- if taskstatus == 1 || is_task == 0 {
- taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
- } else {
- if is_task == 1 {
- if oldTaskTime != "" { //以最新的任务时间为主
- t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
- if err == nil && time.Now().Before(t1) {
- taskTime = nowTime
- } else {
- taskTime = oldTaskTime
- }
- }
- }
- }
- count, counts, t := 0, 0, time.Now() //加入任务车判断节假日
- for {
- count++
- currentTime := t.AddDate(0, 0, -count)
- if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
- isok := false
- for k, v := range DateMap {
- if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
- isok = true
- }
- }
- if isok {
- counts++
- }
- } else {
- isok := true
- for k, v := range DateMap {
- if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
- isok = false
- }
- }
- if isok {
- counts++
- }
- }
- if counts >= 2 {
- break
- }
- }
- //两天之前不包含节假日,有没有跟进记录
- 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))
- //
- clueUpdateData := map[string]interface{}{
- "updatetime": nowTime,
- "top_cluetype": top_cluetype,
- "sub_cluetype": sub_cluetype,
- "userid": userId,
- "comeinsource_private": 2,
- "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
- "company_nature": isGroup,
- "company_verification": isCommerce,
- }
- old_remark := common.ObjToString(data["remark"])
- if old_remark != "" {
- remark = old_remark + ";" + remark //备注不能替换,只能往上拼接
- }
- clueUpdateData["remark"] = remark
- old_demand := common.ObjToString(data["customer_demand"])
- if demand != old_demand {
- clueUpdateData["customer_demand"] = demand
- }
- if cluename != "" {
- clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
- }
- if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
- if name != "" {
- clueUpdateData["name"] = name
- }
- if position != "" {
- clueUpdateData["position"] = position
- }
- if department != "" {
- clueUpdateData["department"] = department
- }
- if follow_project_area != "" {
- clueUpdateData["follow_project_area"] = follow_project_area
- }
- if role != "" {
- clueUpdateData["role"] = role
- }
- // clueUpdateData["industry"] = industry
- }
- if sourceCode == "app_xzcyh" { //这个留资单独处理一下
- if departments != "" {
- clueUpdateData["department"] = departments
- department = departments
- }
- business_scope := common.ObjToString(data["business_scope"])
- keywords := ""
- if len(keyword) > 0 && keyword[0] != "" {
- keywords = strings.Join(keyword, ",")
- if business_scope != "" {
- clueUpdateData["business_scope"] = business_scope + "," + keywords
- } else {
- clueUpdateData["business_scope"] = keywords
- }
- }
- }
- if old_position_id != 0 {
- for _, v := range saleData {
- if old_position_id == common.Int64All(v["position_id"]) {
- oldsaleName = common.ObjToString(v["name"])
- }
- }
- }
- if trailstatus == "00" || old_position_id == 0 || is_assign != 1 || (pIsOk && old_cluename != cluename && cluename != "") {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- clueUpdateData["seatNumber"] = seatNumber
- clueUpdateData["position_id"] = positionId
- clueUpdateData["comeintime"] = nowTime
- clueUpdateData["is_assign"] = 1
- clueUpdateData["comeinsource_open"] = nil
- clueUpdateData["level_open"] = nil
- clueUpdateData["clue_level"] = nil
- }
- } else if is_assign == 1 && positionId != old_position_id {
- clueUpdateData["seatNumber"] = seatNumber
- clueUpdateData["position_id"] = positionId
- clueUpdateData["comeintime"] = nowTime
- clueUpdateData["is_assign"] = 1
- clueUpdateData["comeinsource_open"] = nil
- clueUpdateData["level_open"] = nil
- clueUpdateData["clue_level"] = nil
- } else { //不属于上边情况就还是属于原来的销售,不更换销售
- clueUpdateData["seatNumber"] = old_seatNumber
- clueUpdateData["position_id"] = old_position_id
- clueUpdateData["is_assign"] = 1
- clueUpdateData["comeinsource_open"] = nil
- clueUpdateData["level_open"] = nil
- clueUpdateData["clue_level"] = nil
- }
- if trailstatus != "08" && is_assign == 0 { //在公海的成交客户
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- clueUpdateData["trailstatus"] = "01"
- clueUpdateData["trailstatus_time"] = nowTime
- }
- }
- is_ok := false
- if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- if item == "eventReg" {
- eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
- if eData != nil && len(*eData) > 0 {
- for _, e := range *eData {
- new_value := common.ObjToString(e["new_value"])
- if strings.Contains(new_value, subname) {
- is_ok = true
- }
- }
- }
- if !is_ok {
- if old_sub_cluetype != sub_cluetype || is_assign == 0 {
- if taskTime == "" {
- taskTime = nowTime
- }
- clueUpdateData["task_time"] = nowTime
- clueUpdateData["tasktime"] = taskTime
- clueUpdateData["taskstatus"] = 0
- }
- if is_transfer != 1 {
- clueUpdateData["is_task"] = 1
- }
- }
- } else {
- if old_sub_cluetype != sub_cluetype || is_assign == 0 {
- if taskTime == "" {
- taskTime = nowTime
- }
- clueUpdateData["task_time"] = nowTime
- clueUpdateData["tasktime"] = taskTime
- clueUpdateData["taskstatus"] = 0
- }
- if is_transfer != 1 {
- clueUpdateData["is_task"] = 1
- }
- }
- }
- }
- ok, updateId14 := true, true
- updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
- updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
- updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
- if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
- //线索
- ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
- //变更记录
- if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else if old_position_id == 0 {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- if pIsOk && old_cluename != cluename && cluename != "" {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": common.Int64All(data["position_id"]),
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "无意向客户",
- "new_value": "流失",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "无意向客户",
- "new_value": "商机线索",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- }
- } else if trailstatus != "08" && is_assign == 0 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- if trailstatus != "01" {
- updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "商机线索",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- } else if (pIsOk && old_cluename == cluename && cluename != "") && is_assign == 1 && positionId != old_position_id {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- if old_sub_cluetype != sub_cluetype && !is_ok {
- if is_transfer != 1 {
- updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))),
- "change_type": "加入任务车",
- "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
- if cdata != nil && len(*cdata) > 0 {
- kcpositionId := common.Int64All((*cdata)["position_id"])
- tasksource := common.ObjToString((*cdata)["tasksource"])
- taskstatus := common.IntAll((*cdata)["taskstatus"])
- updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": kcpositionId,
- "change_type": "加入任务车",
- "new_value": "线上主动留资",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- if taskstatus == 1 {
- tasksource = "10"
- } else {
- if tasksource != "" && !strings.Contains(tasksource, "10") {
- if !strings.Contains(tasksource, "10") {
- tasksource += ",10"
- }
- } else {
- tasksource = "10"
- }
- }
- updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
- "tasksource": tasksource,
- "tasktime": nowTime,
- "is_task": 1,
- "taskstatus": 0,
- })
- }
- }
- }
- }
- }
- if old_top_cluetype != "" {
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- old_topname = common.ObjToString((*pcodeData)["name"])
- }
- }
- if old_sub_cluetype != "" {
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- old_subname = common.ObjToString((*pcodeData)["name"])
- }
- }
- if item != "orders" {
- if old_name != name && name != "" {
- updateId6 = 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": "name",
- "change_type": "基本信息变更",
- "old_value": common.If(old_name != "", old_name, "/"),
- "new_value": common.If(name != "", name, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_position != position && position != "" {
- updateId7 = 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": "position",
- "change_type": "基本信息变更",
- "old_value": common.If(old_position != "", old_position, "/"),
- "new_value": common.If(position != "", position, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_department != department && department != "" {
- updateId8 = 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": "department",
- "change_type": "基本信息变更",
- "old_value": common.If(old_department != "", old_department, "/"),
- "new_value": common.If(department != "", department, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_role != role && role != "" {
- 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})
- }
- if old_follow_project_area != follow_project_area && follow_project_area != "" {
- old_area, old_area_arr := "", []string{}
- new_area, new_area_arr := "", []string{}
- for _, v := range strings.Split(old_follow_project_area, ",") {
- old_area_arr = append(old_area_arr, CodeArea[v])
- }
- for _, v := range strings.Split(follow_project_area, ",") {
- new_area_arr = append(new_area_arr, CodeArea[v])
- }
- old_area = strings.Join(old_area_arr, ",")
- new_area = strings.Join(new_area_arr, ",")
- updateId10 = 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": "follow_project_area",
- "change_type": "基本信息变更",
- "old_value": common.If(old_area != "", old_area, "/"),
- "new_value": common.If(new_area != "", new_area, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_cluename != cluename && cluename != "" {
- updateId11 = 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": "cluename",
- "change_type": "基本信息变更",
- "old_value": common.If(old_cluename != "", old_cluename, "/"),
- "new_value": common.If(cluename != "", cluename, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- }
- if old_top_cluetype != top_cluetype {
- updateId12 = 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": "top_cluetype",
- "change_type": "基本信息变更",
- "old_value": common.If(old_topname != "", old_topname, "/"),
- "new_value": common.If(topname != "", topname, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_sub_cluetype != sub_cluetype {
- updateId13 = 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.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))))),
- "change_field": "sub_cluetype",
- "change_type": "基本信息变更",
- "old_value": common.If(old_subname != "", old_subname, "/"),
- "new_value": common.If(subname != "", subname, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- 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
- }) {
- log.Println("线索更新成功")
- if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
- TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
- "status": 1,
- "is_delete": 1,
- "createtime": nowTime,
- "updatetime": nowTime,
- "phone": phone,
- "baseinfo_id": uId,
- "SOURCE": source,
- })
- }
- if old_position_id == 0 {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
- }
- return true
- } else {
- log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
- return false
- }
- }
- 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 {
- log.Println("线索修改前", data)
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
- trailstatus := common.ObjToString(data["trailstatus"]) //data都为原线索数据
- trailstatusTime := common.ObjToString(data["trailstatus_time"])
- var trailstatusTimes time.Time
- if trailstatusTime != "" {
- trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
- }
- clueId := common.Int64All(data["id"])
- is_assign := common.IntAll(data["is_assign"])
- oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
- old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
- oldTaskTime = common.ObjToString(data["tasktime"])
- is_task = common.IntAll(data["is_task"])
- taskstatus = common.IntAll(data["taskstatus"])
- BCPCID := common.GetRandom(32)
- old_name := common.ObjToString(data["name"])
- old_position := common.ObjToString(data["position"])
- old_department := common.ObjToString(data["department"])
- old_follow_project_area := common.ObjToString(data["follow_project_area"])
- old_role := common.ObjToString(data["role"])
- old_cluename := common.ObjToString(data["cluename"])
- old_top_cluetype := common.ObjToString(data["top_cluetype"])
- old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
- old_topname, old_subname := "", ""
- is_transfer := common.IntAll(data["is_transfer"])
- if taskstatus == 1 || is_task == 0 {
- taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
- } else {
- if is_task == 1 {
- if oldTaskTime != "" { //以最新的任务时间为主
- t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
- if err == nil && time.Now().Before(t1) {
- taskTime = nowTime
- } else {
- taskTime = oldTaskTime
- }
- }
- }
- }
- count, counts, t := 0, 0, time.Now() //加入任务车判断节假日
- for {
- count++
- currentTime := t.AddDate(0, 0, -count)
- if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
- isok := false
- for k, v := range DateMap {
- if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
- isok = true
- }
- }
- if isok {
- counts++
- }
- } else {
- isok := true
- for k, v := range DateMap {
- if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
- isok = false
- }
- }
- if isok {
- counts++
- }
- }
- if counts >= 2 {
- break
- }
- }
- //两天之前不包含节假日,有没有跟进记录
- 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))
- //
- clueUpdateData := map[string]interface{}{
- "updatetime": nowTime,
- "top_cluetype": top_cluetype,
- "sub_cluetype": sub_cluetype,
- "userid": userId,
- "comeinsource_private": 2,
- "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
- "company_nature": isGroup,
- "company_verification": isCommerce,
- }
- old_remark := common.ObjToString(data["remark"])
- if old_remark != "" {
- remark = old_remark + ";" + remark //备注不能替换,只能往上拼接
- }
- clueUpdateData["remark"] = remark
- if cluename != "" {
- clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
- }
- if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
- if name != "" {
- clueUpdateData["name"] = name
- }
- if position != "" {
- clueUpdateData["position"] = position
- }
- if department != "" {
- clueUpdateData["department"] = department
- }
- if follow_project_area != "" {
- clueUpdateData["follow_project_area"] = follow_project_area
- }
- if role != "" {
- clueUpdateData["role"] = role
- }
- // clueUpdateData["industry"] = industry
- }
- if sourceCode == "app_xzcyh" { //这个留资单独处理一下
- if departments != "" {
- clueUpdateData["department"] = departments
- department = departments
- }
- business_scope := common.ObjToString(data["business_scope"])
- keywords := ""
- if len(keyword) > 0 && keyword[0] != "" {
- keywords = strings.Join(keyword, ",")
- if business_scope != "" {
- clueUpdateData["business_scope"] = business_scope + "," + keywords
- } else {
- clueUpdateData["business_scope"] = keywords
- }
- }
- }
- if trailstatus == "00" || old_position_id == 0 || is_assign != 1 || (pIsOk && old_cluename != cluename && cluename != "") {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- clueUpdateData["seatNumber"] = seatNumber
- clueUpdateData["position_id"] = positionId
- clueUpdateData["comeintime"] = nowTime
- clueUpdateData["is_assign"] = 1
- clueUpdateData["comeinsource_open"] = nil
- clueUpdateData["level_open"] = nil
- clueUpdateData["clue_level"] = nil
- for _, v := range saleData {
- if common.Int64All(data["position_id"]) == common.Int64All(v["position_id"]) {
- oldsaleName = common.ObjToString(v["name"])
- }
- }
- }
- } else { //不属于上边情况就还是属于原来的销售,不更换销售
- clueUpdateData["seatNumber"] = old_seatNumber
- clueUpdateData["position_id"] = old_position_id
- clueUpdateData["is_assign"] = 1
- clueUpdateData["comeinsource_open"] = nil
- clueUpdateData["level_open"] = nil
- clueUpdateData["clue_level"] = nil
- }
- if trailstatus != "08" && is_assign == 0 { //在公海的成交客户
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- clueUpdateData["trailstatus"] = "01"
- clueUpdateData["trailstatus_time"] = nowTime
- }
- }
- is_ok := false
- if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- if item == "eventReg" {
- eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
- if eData != nil && len(*eData) > 0 {
- for _, e := range *eData {
- new_value := common.ObjToString(e["new_value"])
- if strings.Contains(new_value, subname) {
- is_ok = true
- }
- }
- }
- if !is_ok {
- if old_sub_cluetype != sub_cluetype || is_assign == 0 {
- if taskTime == "" {
- taskTime = nowTime
- }
- clueUpdateData["task_time"] = nowTime
- clueUpdateData["tasktime"] = taskTime
- clueUpdateData["taskstatus"] = 0
- }
- if is_transfer != 1 {
- clueUpdateData["is_task"] = 1
- }
- }
- } else {
- if old_sub_cluetype != sub_cluetype || is_assign == 0 {
- if taskTime == "" {
- taskTime = nowTime
- }
- clueUpdateData["task_time"] = nowTime
- clueUpdateData["tasktime"] = taskTime
- clueUpdateData["taskstatus"] = 0
- }
- if is_transfer != 1 {
- clueUpdateData["is_task"] = 1
- }
- }
- }
- }
- ok, updateId14 := true, true
- updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
- updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
- updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
- if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
- //线索
- ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
- //变更记录
- if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else if old_position_id == 0 {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- if pIsOk && old_cluename != cluename && cluename != "" {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": common.Int64All(data["position_id"]),
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "无意向客户",
- "new_value": "流失",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "无意向客户",
- "new_value": "商机线索",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- }
- } else if trailstatus != "08" && is_assign == 0 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
- "new_value": common.If(saleName != "", saleName, "/"),
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- if trailstatus != "01" {
- updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "商机线索",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- }
- if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
- if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
- } else {
- if old_sub_cluetype != sub_cluetype && !is_ok {
- if is_transfer != 1 {
- updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
- "change_type": "加入任务车",
- "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
- if cdata != nil && len(*cdata) > 0 {
- kcpositionId := common.Int64All((*cdata)["position_id"])
- tasksource := common.ObjToString((*cdata)["tasksource"])
- taskstatus := common.IntAll((*cdata)["taskstatus"])
- updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": kcpositionId,
- "change_type": "加入任务车",
- "new_value": "线上主动留资",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- if taskstatus == 1 {
- tasksource = "10"
- } else {
- if tasksource != "" && !strings.Contains(tasksource, "10") {
- if !strings.Contains(tasksource, "10") {
- tasksource += ",10"
- }
- } else {
- tasksource = "10"
- }
- }
- updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
- "tasksource": tasksource,
- "tasktime": nowTime,
- "is_task": 1,
- "taskstatus": 0,
- })
- }
- }
- }
- }
- }
- if old_top_cluetype != "" {
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- old_topname = common.ObjToString((*pcodeData)["name"])
- }
- }
- if old_sub_cluetype != "" {
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- old_subname = common.ObjToString((*pcodeData)["name"])
- }
- }
- if item != "orders" {
- if old_name != name && name != "" {
- updateId6 = 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": "name",
- "change_type": "基本信息变更",
- "old_value": common.If(old_name != "", old_name, "/"),
- "new_value": common.If(name != "", name, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_position != position && position != "" {
- updateId7 = 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": "position",
- "change_type": "基本信息变更",
- "old_value": common.If(old_position != "", old_position, "/"),
- "new_value": common.If(position != "", position, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_department != department && department != "" {
- updateId8 = 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": "department",
- "change_type": "基本信息变更",
- "old_value": common.If(old_department != "", old_department, "/"),
- "new_value": common.If(department != "", department, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_role != role && role != "" {
- 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})
- }
- if old_follow_project_area != follow_project_area && follow_project_area != "" {
- old_area, old_area_arr := "", []string{}
- new_area, new_area_arr := "", []string{}
- for _, v := range strings.Split(old_follow_project_area, ",") {
- old_area_arr = append(old_area_arr, CodeArea[v])
- }
- for _, v := range strings.Split(follow_project_area, ",") {
- new_area_arr = append(new_area_arr, CodeArea[v])
- }
- old_area = strings.Join(old_area_arr, ",")
- new_area = strings.Join(new_area_arr, ",")
- updateId10 = 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": "follow_project_area",
- "change_type": "基本信息变更",
- "old_value": common.If(old_area != "", old_area, "/"),
- "new_value": common.If(new_area != "", new_area, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_cluename != cluename && cluename != "" {
- updateId11 = 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": "cluename",
- "change_type": "基本信息变更",
- "old_value": common.If(old_cluename != "", old_cluename, "/"),
- "new_value": common.If(cluename != "", cluename, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- }
- if old_top_cluetype != top_cluetype {
- updateId12 = 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": "top_cluetype",
- "change_type": "基本信息变更",
- "old_value": common.If(old_topname != "", old_topname, "/"),
- "new_value": common.If(topname != "", topname, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- if old_sub_cluetype != sub_cluetype {
- updateId13 = 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": "sub_cluetype",
- "change_type": "基本信息变更",
- "old_value": common.If(old_subname != "", old_subname, "/"),
- "new_value": common.If(subname != "", subname, "/"),
- "createtime": nowTime,
- "BCPCID": BCPCID,
- "operator_id": -1,
- })
- }
- 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
- }) {
- log.Println("线索更新成功")
- if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
- TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
- "status": 1,
- "is_delete": 1,
- "createtime": nowTime,
- "updatetime": nowTime,
- "phone": phone,
- "baseinfo_id": uId,
- "SOURCE": source,
- })
- }
- if old_position_id == 0 {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
- }
- return true
- } else {
- log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
- return false
- }
- }
|