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" ) // 未支付订单 30分钟一次 func orders() { //一个小时未支付进入线索 A log.Println("未支付订单定时任务开始") lastOrderId := cfg.LastOrderId selectTimeStart := time.Unix(time.Now().Unix()-7200, 0).Format("2006-01-02 15:04:05") selectTimeEnd := time.Unix(time.Now().Unix()-3600, 0).Format("2006-01-02 15:04:05") sql := fmt.Sprintf(`select * from dataexport_order where order_status = 0 and product_type in ("大会员","VIP订阅","数据流量包","历史数据") and create_time <= "%s" and create_time >= "%s" and id > %s`, selectTimeEnd, selectTimeStart, fmt.Sprint(lastOrderId)) data := Mysql.SelectBySql(sql) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { // FormatData(v, "orders") // if k == len(*data)-1 { // lastOrderId = common.IntAll(v["id"]) // } ok1, _ := FormatData(v, "orders") if !ok1 { common.WriteSysConfig(&cfg) break } cfg.LastOrderId = common.IntAll(v["id"]) } } common.WriteSysConfig(&cfg) log.Println("未支付订单定时任务结束") } // 新注册用户 5分钟一次 func users() { //判断节假日 currentTime, runOk := time.Now(), false 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 { runOk = true } } else { isok := true for k, v := range DateMap { if currentTime.Format(date.Date_Short_Layout) == k && v == 1 { isok = false } } if isok { runOk = true } } if !runOk { log.Println("不是工作日,任务暂停") return } else { if currentTime.Hour() == 17 && currentTime.Minute() > 30 { log.Println("不是工作日,任务暂停") return } } //新用户注册后5分钟内进入线索 C log.Println("新注册用户定时任务开始") selectTimeEnd := time.Unix(time.Now().Unix()-300, 0).Format("2006-01-02 15:04:05") sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where createtime > "%s" and source = 1`, selectTimeEnd) data := TiDb.SelectBySql(sql) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { FormatData(v, "users") } } log.Println("新注册用户定时任务结束") } func userbase() { log.Println("userbase定时任务开始") selectTimeEnd := time.Unix(time.Now().Unix()-1800, 0).Format("2006-01-02 15:04:05") sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where updatetime > "%s" and source > 0`, selectTimeEnd) data := TiDb.SelectBySql(sql) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { phone := common.ObjToString(v["phone"]) uId := common.ObjToString(v["uid"]) userId := common.ObjToString(v["userid"]) registedate := common.ObjToString(v["l_registedate"]) name := common.ObjToString(v["name"]) nowTime := time.Now().Format(date.Date_Full_Layout) source := common.IntAll(v["source"]) if phone != "" { contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone) if contactsData == nil || len(*contactsData) == 0 { registedates, _ := time.Parse(date.Date_Full_Layout, registedate) count := TiDb.CountBySql("select count(1) as count from dwd_f_crm_clue_info where phone = ?", phone) if time.Now().Unix()-registedates.Unix() > 7*86400 { if count == 0 { FormatData(v, "users") } else { 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, }) } } else { if count == 0 { clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{ "userid": userId, "uid": uId, "is_assign": 0, "comeintime": nowTime, "createtime": nowTime, "updatetime": nowTime, "cluename": phone, "top_cluetype": "", "sub_cluetype": "", "trailstatus": "01", "name": name, "phone": phone, }) if clueId > 0 { TiDb.Insert("dwd_f_crm_open_sea", map[string]interface{}{ "clue_id": clueId, "comeintime": nowTime, "comeinsource": 2, }) } } else { 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, }) } } } } } } log.Println("userbase定时任务结束") } // 留资 5分钟一次 func saleLeads() { //判断节假日 currentTime, runOk := time.Now(), false 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 { runOk = true } } else { isok := true for k, v := range DateMap { if currentTime.Format(date.Date_Short_Layout) == k && v == 1 { isok = false } } if isok { runOk = true } } if !runOk { log.Println("不是工作日,任务暂停") return } else { if currentTime.Hour() == 17 && currentTime.Minute() > 30 { log.Println("不是工作日,任务暂停") return } } //留资后5分钟内进入线索 //分为免费留资和付费留资 付费B 免费C log.Println("用户留资定时任务开始") session := Mgo.GetMgoConn() lastId := cfg.LastId defer func() { Mgo.DestoryMongoConn(session) }() query := map[string]interface{}{} if lastId != "" { query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId(lastId)} } // query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("63f6049a4c172a1fd44ed3c8")} log.Println("query :", query) iter := session.DB(cfg.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter() thisData := map[string]interface{}{} for { if !iter.Next(&thisData) { break } ok1, _ := FormatData(thisData, "saleLeads") if !ok1 { common.WriteSysConfig(&cfg) break } cfg.LastId = mongodb.BsonIdToSId(thisData["_id"]) // FormatData(thisData, "saleLeads") } common.WriteSysConfig(&cfg) log.Println("用户留资定时任务结束") } func FormatData(data map[string]interface{}, item string) (bool, bool) { userId, uId, positionId, source, cluename, phone := common.ObjToString(data["user_id"]), "", "", 0, "", "" role, industry, department, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", "" query, topname, subname := map[string]interface{}{}, "", "" 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.IntAll((*userInfo)["source"]) } //cluename --> company_name cluename = common.ObjToString(data["company_name"]) phone = common.ObjToString(data["user_phone"]) } else if item == "users" { 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.IntAll((*userInfo)["source"]) // userId = common.ObjToString((*userInfo)["userid"]) } } } 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"]) position = common.ObjToString(data["position"]) name = common.ObjToString(data["name"]) // sourceCode = common.ObjToString(data["source"]) 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.IntAll((*userInfo)["source"]) userId = common.ObjToString((*userInfo)["userid"]) } } //线索名称为空用手机号代替 if cluename == "" { cluename = phone } //域外用户和内部用户和没有手机号,不存线索 if source == 5 || source == 6 || phone == "" { log.Println("线索分配失败,线索过滤!!", item, source, phone, userId) return true, false } 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"]) position = common.ObjToString(sdata["position"]) name = common.ObjToString(sdata["name"]) } } //top_cluetype top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data) //follow_project_area --> follow_project_monitor follow_project_area = getAreaCode(userId) //seatNumber position_id log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area) if top_cluetype == "" || sub_cluetype == "" || level == "" { log.Println("线索分配失败,线索过滤top_cluetype!!", item, uId, phone, userId) return true, true } position_id, seatNumber, saleName, saleData := autoDraw(level) log.Println("data -------", position_id, seatNumber, saleName) if position_id > 0 && seatNumber != "" { // if TiDb.Count("dwd_f_crm_private_sea", map[string]interface{}{"position_id": position_id}) >= cfg.CountLimit { // log.Println("线索分配失败,私海线索数据超过限制", item, position_id, seatNumber, uId, userId, phone) // TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, level) // return false, false // // position_id, seatNumber, saleName, saleData = autoDraw(level) // } uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true if uCount != nil && len(*uCount) > 0 { oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id, source) } else { oks = SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id, source) } if oks { TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, level) } else { log.Println("线索分配失败!!", item, position_id, seatNumber, uId, userId, phone) return false, false } } return true, true } func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64, source int) 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, seaId, uodateId1, uodateId2, uodateId3, uodateId4 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0) // BCPCID := common.GetRandom(32) if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool { //线索 clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{ "userid": userId, "uid": uId, "seatNumber": seatNumber, "position_id": positionId, "is_assign": 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": department, "industry": industry, "follow_project_area": follow_project_area, "role": role, }) //私海 --> 任务车 seaId = TiDb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{ "clue_id": clueId, "seatNumber": seatNumber, "position_id": positionId, "comeintime": nowTime, "comeinsource": 2, "is_task": 1, "task_time": nowTime, "tasktime": common.If(item == "users", nowTimes, nowTime), "taskstatus": 0, "tasksource": "线索自动分配" + "-" + topname + "-" + subname, }) //变更记录 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, }) return clueId > -1 && seaId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -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, seaId, uodateId1, uodateId2, uodateId3, uodateId4, " 用户信息 ", 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, industry, follow_project_area, role, seatNumber string, positionId int64, source int) 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") //trailstatus = 无意向 为变更线索状态 //没有变更所属人查任务车,有车不变 trailstatus := common.ObjToString(data["trailstatus"]) 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"]) //已在任务车的判断任务时间 privateData := TiDb.FindOne("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, "", "") if privateData != nil && len(*privateData) > 0 { oldTaskTime = common.ObjToString((*privateData)["tasktime"]) is_task = common.IntAll(data["is_task"]) taskstatus = common.IntAll(data["taskstatus"]) } if taskstatus == 1 || is_task == 0 { taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes)) } 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{}{ "is_assign": 1, "updatetime": nowTime, "top_cluetype": top_cluetype, "sub_cluetype": sub_cluetype, "userid": userId, } seaUpdateData := map[string]interface{}{ "comeinsource": 2, "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname, } insertSeaData := map[string]interface{}{ "clue_id": clueId, "comeintime": nowTime, "comeinsource": 2, "is_task": 1, "task_time": nowTime, "tasktime": taskTime, "taskstatus": 0, "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname, } if cluename != "" { clueUpdateData["cluename"] = cluename } if item != "orders" && item != "users" { clueUpdateData["name"] = name clueUpdateData["position"] = position clueUpdateData["department"] = department clueUpdateData["industry"] = industry clueUpdateData["follow_project_area"] = follow_project_area clueUpdateData["role"] = role } if trailstatus == "00" || old_position_id == 0 || is_assign != 1 { clueUpdateData["seatNumber"] = seatNumber clueUpdateData["position_id"] = positionId clueUpdateData["comeintime"] = nowTime seaUpdateData["seatNumber"] = seatNumber seaUpdateData["position_id"] = positionId seaUpdateData["comeintime"] = nowTime insertSeaData["seatNumber"] = seatNumber insertSeaData["position_id"] = positionId for _, v := range saleData { if common.ObjToString(data["seatNumber"]) == common.ObjToString(v["seatNumber"]) { oldsaleName = common.ObjToString(v["name"]) } } } else { insertSeaData["seatNumber"] = old_seatNumber insertSeaData["position_id"] = old_position_id } if trailstatus != "08" && is_assign == 0 { clueUpdateData["trailstatus"] = "01" } if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 { seaUpdateData["is_task"] = 1 seaUpdateData["task_time"] = nowTime seaUpdateData["tasktime"] = taskTime seaUpdateData["taskstatus"] = 0 } ok, ok1, ok2, seaId := true, true, true, int64(0) 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 is_assign == 1 { ok1 = TiDb.UpdateByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, seaUpdateData) } else { ok2 = TiDb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId}) seaId = TiDb.InsertByTx(tx, "dwd_f_crm_private_sea", insertSeaData) } //变更记录 if (trailstatus == "00" || old_position_id == 0) && is_assign == 1 { 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 == "00" { 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 old_position_id == 0 { 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 common.Int64All(data["position_id"]) > 0 { 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, }) } } else if trailstatus != "08" && is_assign == 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, }) if trailstatus != "01" { 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": CodeTrail[trailstatus], "new_value": "商机线索", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) } } if trailstatus != "08" && recordCount <= 0 { 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, }) } 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 := "", "" 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" || 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" || 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" || 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" || 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 { 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" || 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" || 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" || 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" || 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 && ok1 && seaId > -1 && 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 }) { 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("线索更新失败!!!", ok, ok1, ok2, seaId, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, " 用户信息 ", item, position, seatNumber, uId, userId, phone) return false } } func getAreaCode(userId string) (code string) { followData := Base.Find("follow_project_monitor", map[string]interface{}{"s_userid": userId}, "", "", -1, -1) sidArr := []string{} if followData != nil && len(*followData) > 0 { for _, v := range *followData { infoId := common.ObjToString(v["s_id"]) sidArr = append(sidArr, infoId) } } if len(sidArr) > 0 { query := `{"query": {"bool": {"must": [{"terms": {"_id": ["%s"]}}],"must_not": [],"should": []}}}` query = fmt.Sprintf(query, strings.Join(sidArr, `","`)) biddingData := Es.Get("bidding", "bidding", query) if biddingData != nil && len(*biddingData) > 0 { codeMap := map[string]string{} codeArr := []string{} for _, v := range *biddingData { area := common.ObjToString(v["area"]) address := common.ObjToString(v["city"]) if address == "" { address = area } codeMap[address] = AreaCode[address] } if len(codeMap) > 0 { for _, v := range codeMap { codeArr = append(codeArr, v) } } if len(codeArr) > 0 { code = strings.Join(codeArr, ",") } } } log.Println("code ", code) return } func getClueType(item string, data map[string]interface{}) (pcode, code, level, topname, subname string) { if item == "orders" { productType := common.ObjToString(data["product_type"]) pcode = "1" level = "A" topname = "提交订单未支付" if productType == "VIP订阅" { code = "6" subname = "超级订阅" } else if productType == "大会员" { code = "7" subname = "大会员" } else if productType == "数据流量包" { code = "8" subname = "数据流量包" } else if productType == "历史数据" { code = "9" subname = "数据自助导出" } } else if item == "users" { pcode = "4" code = "154" level = "C" topname = "新增注册" subname = "新增注册用户" } else if item == "saleLeads" { sourceCode := common.ObjToString(data["source"]) if sourceCode != "" { codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"source": sourceCode}, "", "") if codeData != nil && len(*codeData) > 0 { pcode = common.ObjToString((*codeData)["pcode"]) code = common.ObjToString((*codeData)["code"]) level = common.ObjToString((*codeData)["clue_level"]) subname = common.ObjToString((*codeData)["name"]) pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "") if pcodeData != nil && len(*pcodeData) > 0 { topname = common.ObjToString((*pcodeData)["name"]) } } } } return } // 获取自动分配的人 func autoDraw(mode string) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}) { query := `select * from jy_salesperson_info where` if mode == "A" || mode == "B" { query += ` is_complete = 1` } else { query += ` is_complete = 1 or is_complete = 0` } data := TiDb.SelectBySql(query) if data != nil && len(*data) > 0 { saleData = *data sql := "select * from dwd_f_crm_clue_autodraw_record where clue_level = ?" countData := TiDb.SelectBySql(sql, mode) if countData != nil && len(*countData) > 0 { for _, v := range *data { //判断是否有新员工 isOk := false for _, vv := range *countData { if common.ObjToString(v["seatNumber"]) == common.ObjToString(vv["seatNumber"]) { if common.IntAll(v["status"]) == 0 { vv["status"] = 1 } else { vv["status"] = 2 } isOk = true } } //有新员工直接分给新员工 if !isOk { seatNumber = common.ObjToString(v["seatNumber"]) saleName = common.ObjToString(v["name"]) rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count asc") TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{ "seatNumber": seatNumber, "clue_level": mode, "count": common.Int64All((*rData)["count"]), }) break } } if seatNumber == "" { res := int64(0) countres := 0 for _, v := range *countData { if common.IntAll(v["status"]) == 1 { if countres == 0 { res = common.Int64All(v["count"]) seatNumber = common.ObjToString(v["seatNumber"]) } else { if common.Int64All(v["count"]) <= res { res = common.Int64All(v["count"]) seatNumber = common.ObjToString(v["seatNumber"]) } } countres++ } // if k == 0 { // res = common.Int64All(v["count"]) // seatNumber = common.ObjToString(v["seatNumber"]) // } else { // if common.Int64All(v["count"]) <= res { // res = common.Int64All(v["count"]) // seatNumber = common.ObjToString(v["seatNumber"]) // } // } } } } else { seatNumber = common.ObjToString((*data)[0]["seatNumber"]) saleName = common.ObjToString((*data)[0]["name"]) rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count asc") if rData != nil && len(*rData) > 0 { TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{ "seatNumber": seatNumber, "clue_level": mode, "count": common.Int64All((*rData)["count"]), }) } else { TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{ "seatNumber": seatNumber, "clue_level": mode, "count": 0, }) } } for _, v := range *data { if seatNumber == common.ObjToString(v["seatNumber"]) { positionId = getPositionId(common.ObjToString(v["phone"])) saleName = common.ObjToString(v["name"]) } } // if positionId > 0 { // TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, mode) // } } return } func getPositionId(phone string) (positionId int64) { userData, ok := Mgo.FindOne("user", map[string]interface{}{"s_phone": phone}) if ok && userData != nil && len(*userData) > 0 { userId := common.Int64All((*userData)["base_user_id"]) positionData := Base.FindOne("base_position", map[string]interface{}{"type": 1, "ent_id": 25917, "user_id": userId}, "", "") //TODO ent_id if positionData != nil && len(*positionData) > 0 { positionId = common.Int64All((*positionData)["id"]) } } return }