package main import ( "database/sql" "encoding/json" "fmt" "log" "strings" "time" "github.com/gogf/gf/v2/util/gconv" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/mongodb" "app.yhyue.com/moapp/jybase/redis" ) func kcSync() { log.Println("客户成功系统移交定时任务开始") sql := `select * from dwd_f_userbase_order_info where payable_money > 0 and vip_endtime > "` + time.Now().Format(date.Date_Full_Layout) + `" and autoUpdate > "` + cfg.LastkcTime + `" order by autoUpdate asc` data := TiDb.SelectBySql(sql) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { product_type := common.ObjToString(v["product_type"]) data_spec := common.ObjToString(v["data_spec"]) starttime := common.ObjToString(v["vip_starttime"]) order_status := common.IntAll(v["order_status"]) refund_status := common.IntAll(v["refund_status"]) if (product_type == "大会员" || product_type == "企业商机管理") && order_status == 1 && data_spec != "dhy4" && !strings.HasPrefix(starttime, "2099") && refund_status != 1 { status := kcJob(v) if status == 0 { break } } cfg.LastkcTime = common.ObjToString(v["autoUpdate"]) } } common.WriteSysConfig(&cfg) log.Println("客户成功系统移交定时任务结束") } func kcJob(data map[string]interface{}) int { nowTime := time.Now().Format(date.Date_Full_Layout) uId, entId, clueId, saveMap, name, positionId, status := common.ObjToString(data["uid"]), fmt.Sprint(data["ent_id"]), int64(0), map[string]interface{}{}, "", int64(0), 1 clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "") if clueData != nil && len(*clueData) > 0 { clueId = common.Int64All((*clueData)["id"]) starttime := common.ObjToString(data["vip_starttime"]) endtime := common.ObjToString(data["vip_endtime"]) // user_role := common.IntAll(data["user_role"]) buy_subject := common.IntAll(data["buy_subject"]) product_type := common.ObjToString(data["product_type"]) data_spec := common.ObjToString(data["data_spec"]) userName := common.ObjToString((*clueData)["name"]) //phone := common.ObjToString((*clueData)["phone"]) product, company_name := 0, common.ObjToString(data["company_name"]) productMap := map[string]int{ "dhy6": 1, "dhy7": 2, "dhy3": 4, "dhy1": 5, "dhy2": 6, "dhy5": 7, "企业商机管理": 8, } if buy_subject == 1 || buy_subject == 0 { entId = common.ObjToString(data["userid"]) } if product_type == "企业商机管理" { product = productMap[product_type] powerData := TiDb.FindOne("dwd_f_data_equity_info", map[string]interface{}{"uid": uId, "product_type": "商机管理"}, "", "comeintime desc") if powerData != nil { starttime = common.ObjToString((*powerData)["starttime"]) endtime = common.ObjToString((*powerData)["endtime"]) } else { log.Println("客成移交权限未查到--", uId) return 0 } } else { if data_spec == "dhy6" { product = 1 filter := common.ObjToString(data["filter"]) filterMap := map[string]interface{}{} json.Unmarshal([]byte(filter), &filterMap) if len(filterMap) > 0 { areaCount := common.IntAll(filterMap["areaCount"]) if areaCount == 1 { product = 3 } } } else { product = productMap[data_spec] } } //同一公司名称(以客户详情-组织机构-公司名称)下的线索需分配给同1人 //合力亿捷线索同步 /*customerData := TiDbData.FindOne("customer", map[string]interface{}{"phone": phone}, "", "") if customerData != nil { log.Println("移交客成查询到合力亿捷") status999 := common.ObjToString((*customerData)["status999"]) //线索状态 source := common.ObjToString((*customerData)["source"]) //销售来源 customerNeeds := common.ObjToString((*customerData)["customerNeeds"]) //客户需求 wantGoods := common.ObjToString((*customerData)["wantGoods"]) //意向产品 job := common.ObjToString((*customerData)["job"]) //职位 createTime := common.ObjToString((*customerData)["createTime"]) //创建时间 lastUpdateTime := common.ObjToString((*customerData)["lastUpdateTime"]) //更新时间 area := common.ObjToString((*customerData)["area"]) //关注区域 customerBudget := common.ObjToString((*customerData)["customerBudget"]) //客户预算 isPolicymaker := common.ObjToString((*customerData)["customerBudget"]) //是否为决策人 belongToIndustry := common.ObjToString((*customerData)["belongToIndustry"]) //所属行业 seatNumber := common.ObjToString((*customerData)["empNo"]) //坐席号 statusMap := map[string]string{ "status0": "08", "status1": "07", "status2": "06", "status3": "05", "status4": "04", "status6": "00", "status5": "01", "spaceNumber": "02", "phoneDown": "02", } if status999 != "" { status999 = statusMap[status999] } else { status999 = "01" } top_cluetype := "172" sub_cluetype := "" cluetypeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"name": "其他-" + source}, "", "") if cluetypeData != nil { sub_cluetype = common.ObjToString((*cluetypeData)["code"]) } wantGoods = strings.ReplaceAll(wantGoods, "剑鱼大会员", "大会员") wantGoods = strings.ReplaceAll(wantGoods, "数据导出", "数据流量包") if isPolicymaker == "是" { isPolicymaker = "决策人" } else { isPolicymaker = "使用人" } areaCodeArr := []string{} areaCode := "" for _, v := range strings.Split(area, ",") { areaCodeArr = append(areaCodeArr, AreaCode[v]) } if len(areaCodeArr) > 0 { areaCode = strings.Join(areaCodeArr, ",") } SalePositionId := getSeatNumberPositionId(seatNumber) TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, map[string]interface{}{ "position_id": SalePositionId, "seatNumber": seatNumber, "is_assign": -2, "createtime": createTime, "updatetime": lastUpdateTime, "top_cluetype": top_cluetype, "sub_cluetype": sub_cluetype, "customer_demand": customerNeeds, "intended_products": wantGoods, "customer_budget": customerBudget, "contact_type": isPolicymaker, "position": job, "industry": belongToIndustry, "follow_project_area": areaCode, }) }*/ // entIds := common.IntAll(data["ent_id"]) positionId, name = cAutoDraw(entIds) log.Println("移交客成positionId", positionId, name) saveMap = map[string]interface{}{ "clue_id": clueId, "transfertime": nowTime, "position_id": positionId, "name": name, "service_starttime": starttime, "service_endtime": endtime, "ent_id": entId, "is_task": 1, "tasktime": nowTime, "taskstatus": 0, "tasksource": "1", "is_admin": 1, "product_access": product, "buy_subject": buy_subject, "relationship_building_way": 1, "inventory_way": 1, "training_way": 1, "is_pre_sales_training": 0, "service_stage": 1, "company_name": company_name, } if TiDb.Count("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}) > 0 { csmdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "") if csmdata != nil && len(*csmdata) > 0 { TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{ "is_transfer": 0, "is_renewal_protection": 0, "product_access": product, "buy_subject": buy_subject, "transfertime": nowTime, "service_starttime": starttime, "service_endtime": endtime, "ent_id": entId, "company_name": company_name, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": (*csmdata)["position_id"], "change_type": "成交客户移交", "new_value": "移交至客户成功组", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": (*csmdata)["position_id"], "change_field": "position_id", "change_type": "客户成功经理", "old_value": "/", "new_value": (*csmdata)["name"], "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName}) } } else { cId, ok, updateId1, updateId2, updateId3 := int64(-1), false, int64(-1), int64(-1), int64(-1) if TiDb.ExecTx("保存客户", func(tx *sql.Tx) bool { cId = TiDb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap) ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName}) updateId1 = 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, }) updateId2 = 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, }) updateId3 = 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": name, "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) return cId > -1 && ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 }) { TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, name) log.Println("保存客户成功") } else { log.Println("保存客户失败!!!", clueId, cId, ok, updateId1, updateId2, updateId3, " 用户信息 ", name, positionId, uId) } } return status } else { log.Println("客户未查到线索!!!", uId) if isExists, _ := redis.Exists("bidx", "bidx_uId_"+uId); isExists { redisInt := redis.GetInt("bidx", "bidx_uId_"+uId) if redisInt > 2 { return 1 } else { redis.Incr("bidx", "bidx_uId_"+uId) return 0 } } else { redis.Put("bidx", "bidx_uId_"+uId, 1, 3600) return 0 } } } func cAutoDraw(entId int) (positionId int64, name string) { query := `SELECT name,position_id FROM dwd_d_crm_department_level_succbi WHERE bi_pcode = (SELECT bi_code FROM dwd_d_crm_department_level_succbi WHERE name = "客户成功组" and resign=0) and resign = 0` data := TiDb.SelectBySql(query) if data != nil && len(*data) > 0 { if entId > 0 { entdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"ent_id": entId}, "", "") if entdata != nil && len(*entdata) > 0 { positionId = common.Int64All((*entdata)["position_id"]) name = common.ObjToString((*entdata)["name"]) return } } sql := `select a.name,a.count from dwd_f_csm_customer_autodraw_record a INNER JOIN dwd_d_crm_department_level_succbi b on (a.name = b.name) where b.resign = 0` countData := TiDb.SelectBySql(sql) if countData != nil && len(*countData) > 0 { for _, v := range *data { //判断是否有新员工 isOk := false for _, vv := range *countData { if common.ObjToString(v["name"]) == common.ObjToString(vv["name"]) { isOk = true } } //有新员工直接分给新员工 if !isOk { name = common.ObjToString(v["name"]) rData := TiDb.FindOne("dwd_f_csm_customer_autodraw_record", map[string]interface{}{}, "", "count desc") TiDb.Insert("dwd_f_csm_customer_autodraw_record", map[string]interface{}{ "name": name, "count": common.Int64All((*rData)["count"]), }) break } } res := int64(0) countres := 0 for _, v := range *countData { if countres == 0 { res = common.Int64All(v["count"]) name = common.ObjToString(v["name"]) } else { if common.Int64All(v["count"]) <= res { res = common.Int64All(v["count"]) name = common.ObjToString(v["name"]) } } countres++ } } for _, v := range *data { if name == common.ObjToString(v["name"]) { positionId = common.Int64All(v["position_id"]) } } } return } func kcAuto() { log.Println("客户成功系统自动进入任务车定时任务开始") nowTime := time.Now().Format(date.Date_Full_Layout) sql := `select * from dwd_f_csm_customer_info where ((relationship_building_way != 1 and inventory_way = 1) or (inventory_way != 1 and training_way = 1)) and is_task = 0` data := TiDb.SelectBySql(sql) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { relationship_building_way := common.IntAll(v["relationship_building_way"]) inventory_way := common.IntAll(v["inventory_way"]) training_way := common.IntAll(v["training_way"]) positionId := common.Int64All(v["position_id"]) clueId := common.Int64All(v["clue_id"]) tasksource, tasksources := common.ObjToString(v["tasksource"]), "" taskstatus := common.IntAll(v["taskstatus"]) if relationship_building_way != 1 && inventory_way == 1 { tasksources = "2" TiDb.Insert("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, }) } else if inventory_way != 1 && training_way == 1 { tasksources = "3" TiDb.Insert("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, }) } if taskstatus == 1 { tasksource = tasksources } else { if tasksource != "" { if !strings.Contains(tasksource, tasksources) { tasksource += "," + tasksources } } else { tasksource = tasksources } } TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{ "tasksource": tasksource, "tasktime": nowTime, "is_task": 1, "taskstatus": 0, }) } } log.Println("客户成功系统进入任务车定时任务结束") } func ordersClue() { log.Println("后台订单线索定时任务开始") lastOrderClueId := cfg.LastOrderClueId nowTime := time.Now().Format(date.Date_Full_Layout) sql := fmt.Sprintf(`select order_code,order_status,is_backstage_order,pay_money,saleDep,salesperson,user_phone,create_person,saleMoney,user_id,product_type,company_name,autoUpdate from dataexport_order where autoUpdate >= "%s" order by autoUpdate asc`, lastOrderClueId) data := Mysql.SelectBySql(sql) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { order_status := common.IntAll(v["order_status"]) is_backstage_order := common.IntAll(v["is_backstage_order"]) pay_money := common.IntAll(v["pay_money"]) orderCode := gconv.String(v["order_code"]) saleDep, _, salesperson := FindSaleRecord(orderCode) user_phone := common.ObjToString(v["user_phone"]) createperson := common.ObjToString(v["create_person"]) userId, uId, cluename, seatNumber, positionId, trailstatus, clueId := common.ObjToString(v["user_id"]), "", "", "", int64(0), "", int64(0) product_type_str2 := `"大会员","企业商机管理","VIP订阅"` product_type := common.ObjToString(v["product_type"]) if order_status == 1 && is_backstage_order == 1 && saleDep != "" && !strings.HasPrefix(user_phone, "9") && strings.Contains(product_type_str2, product_type) { //销售部 query := map[string]interface{}{} source := "" if product_type == "企业商机管理" { query["phone"] = user_phone userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "") if userInfo != nil && len(*userInfo) > 0 { uId = common.ObjToString((*userInfo)["uid"]) userId = common.ObjToString((*userInfo)["userid"]) source = common.ObjToString((*userInfo)["source"]) } else { log.Println("后台订单--未查询到 ", user_phone) break } } else { if !mongodb.IsObjectIdHex(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"]) } else { log.Println("后台订单--未查询到 ", user_phone) break } } 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"]) } else { log.Println("后台订单--未查询到 ", query, user_phone) break } } is_assign := 1 mailContent := "" cluename = common.ObjToString(v["company_name"]) mailContent = fmt.Sprintf("%s(%s)", cluename, user_phone) if cluename == "" { cluename = user_phone mailContent = user_phone } if pay_money > 0 { trailstatus = "08" } else { trailstatus = "01" } isGroup, isCommerce := GetCompanyType(cluename) //判断是否集团公司、工商库 clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "") if clueData == nil || len(*clueData) == 0 { log.Println("saleDep", saleDep) if saleDep == "销售部" { //撞单 log.Println("销售部订单", saleDep) if strings.Contains(salesperson, ",") { log.Println("销售部撞单 ", salesperson) if salesperson == "" { salesperson = createperson } } log.Println("销售部订单salesperson", salesperson) saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"name": salesperson}, "", "") if saleData != nil { seatNumber = common.ObjToString((*saleData)["seat_number"]) positionId = common.Int64All((*saleData)["position_id"]) } if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) { //线索需要冻结 is_assign = -3 } clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{ "userid": userId, "uid": uId, "seatNumber": seatNumber, "position_id": positionId, "is_assign": is_assign, "comeintime": nowTime, "createtime": nowTime, "updatetime": nowTime, "cluename": cluename, "top_cluetype": "4", "sub_cluetype": "154", "trailstatus": trailstatus, "name": user_phone, "phone": user_phone, "comeinsource_private": 2, "is_task": 0, "taskstatus": 0, "company_nature": isGroup, "company_verification": isCommerce, "FREEZE_TIME": nowTime, }) } else if saleDep == "市场部" { log.Println("市场部订单", saleDep) clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{ "userid": userId, "uid": uId, "is_assign": -2, "createtime": nowTime, "updatetime": nowTime, "cluename": cluename, "top_cluetype": "4", "sub_cluetype": "154", "trailstatus": trailstatus, "name": user_phone, "phone": user_phone, "is_task": 0, "taskstatus": 0, "company_nature": isGroup, "company_verification": isCommerce, "FREEZE_TIME": nowTime, }) } if clueId > 0 { TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(positionId > 0, positionId, -1), "change_type": "创建线索", "new_value": "系统自动创建", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) if is_assign == -3 { //新增冻结记录 TiDb.Insert("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, }) //移交失败发送消息 if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) { //发送邮件信息 OrderCreateFail(positionId, mailContent) } } } } } cfg.LastOrderClueId = common.ObjToString(v["autoUpdate"]) } } common.WriteSysConfig(&cfg) log.Println("后台订单线索定时任务结束") } func refundAuto() { log.Println("自动移交销售定时任务开始") nowTime := time.Now().Format(date.Date_Full_Layout) TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool { for _, v := range *l { saleId, cluename, company_nature, company_verification, uid, phone := int64(0), "", 0, 0, "", "" clueId := common.Int64All(v["clue_id"]) name := common.ObjToString(v["name"]) kcposition_id := common.Int64All(v["position_id"]) isRenewalProtection := common.IntAll(v["is_renewal_protection"]) clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "phone,company_nature,company_verification,cluename,userid,position_id,uid", "") if clueData != nil && len(*clueData) > 0 { saleId = common.Int64All((*clueData)["position_id"]) cluename = common.ObjToString((*clueData)["cluename"]) company_nature = common.IntAll((*clueData)["company_nature"]) company_verification = common.IntAll((*clueData)["company_verification"]) uid = common.ObjToString((*clueData)["uid"]) phone = common.ObjToString((*clueData)["phone"]) } isFull := FindUpperLimit(gconv.String(saleId), "", false) isAllRefund := false myOrders := TiDb.SelectBySql(`select refund_status from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and order_status = 1 and vip_endtime < ?`, uid, nowTime) if myOrders != nil { refundCount := 0 for _, v := range *myOrders { if common.IntAll(v["refund_status"]) == 1 { refundCount++ } } if len(*myOrders) > 0 && len(*myOrders) == refundCount { isAllRefund = true } } if isAllRefund { //全退款了 TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,is_assign=0,trailstatus="01",position_id=null,seatNumber=null,updatetime=?,comeintime_open=?,comeinsource_open=1,level_open=3,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null,is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcposition_id > 0, kcposition_id, -1), "change_type": "退回公海", "new_value": "成交客户申请退款", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcposition_id > 0, kcposition_id, -1), "change_field": "trailstatus", "change_type": "基本信息变更", "old_value": "成交客户", "new_value": "商机线索", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcposition_id > 0, kcposition_id, -1), "change_type": "客户成功经理", "change_field": "position_id", "old_value": name, "new_value": "/", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcposition_id > 0, kcposition_id, -1), "change_type": "移交销售", "new_value": "成交客户申请退款", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId) } else { order1 := TiDb.SelectBySql(`select id from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and vip_endtime > ? and order_status = 1 `, uid, nowTime) if order1 == nil || len(*order1) == 0 { isOk := false order3 := TiDb.SelectBySql(`select vip_endtime from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and vip_endtime < ? and order_status = 1 `, uid, nowTime) if order3 != nil && len(*order3) > 0 { for _, vv := range *order3 { if isRenewalProtection == 1 { vip_endtime := common.ObjToString(vv["vip_endtime"]) vip_endtimes, _ := time.ParseInLocation(date.Date_Full_Layout, vip_endtime, time.Local) if time.Now().Unix()-vip_endtimes.Unix() < 3*30*86400 { isOk = true } } } } if !isOk { //全到期了 if saleId > 0 { saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": saleId, "resign": 0}, "", "") if saleData != nil && len(*saleData) > 0 { if FindUpperLimit(gconv.String(saleId), "", false) { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, nowTime, nowTime, clueId) } else { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId) } } else { if company_nature == 0 && company_verification == 1 && cluename != "" { adata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "seatNumber,position_id", "", -1, -1) if adata != nil && len(*adata) > 0 { isOks := false for _, vv := range *adata { position_ids := common.Int64All(vv["position_id"]) seatNumber := common.ObjToString(vv["seatNumber"]) saleDatas := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": position_ids, "resign": 0}, "", "") if saleDatas != nil && len(*saleDatas) > 0 { saleId = position_ids if isFull { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId) } else { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId) } isOks = true break } } if !isOks { sdata := TiDb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) and b.role_id = 3`, saleId) if sdata != nil && len(*sdata) > 0 { saleId = common.Int64All((*sdata)[0]["position_id"]) seatNumber := common.ObjToString((*sdata)[0]["seat_number"]) if isFull { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId) } else { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId) } } } } } else { sdata := TiDb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) and b.role_id = 3`, saleId) if sdata != nil && len(*sdata) > 0 { saleId = common.Int64All((*sdata)[0]["position_id"]) seatNumber := common.ObjToString((*sdata)[0]["seat_number"]) if isFull { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId) } else { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId) } } } } } else { if isFull { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",top_cluetype="532",sub_cluetype="537",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, nowTime, clueId) } else { TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",top_cluetype="532",sub_cluetype="537",is_transfer=0 WHERE id = ?`, nowTime, clueId) } } TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcposition_id > 0, kcposition_id, -1), "change_type": "基本信息变更", "old_value": "成交客户", "change_field": "trailstatus", "new_value": "商机线索", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": saleId, "change_type": "加入任务车", "new_value": "线索自动分配-其他-成交客户续约失败", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcposition_id > 0, kcposition_id, -1), "change_type": "客户成功经理", "change_field": "position_id", "old_value": name, "new_value": "/", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": saleId, "change_type": "移交销售", "new_value": "成交客户续费失败,到期自动移交", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId) if isFull { //新增冻结记录 TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": saleId, "change_type": "线索冻结", "new_value": "销售人员私海已达上限", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) //发送邮件 mailContent := "" if cluename != "" { if phone != "" { if cluename == phone { mailContent = phone } else { mailContent = fmt.Sprintf("%s(%s)", cluename, phone) } } else { mailContent = cluename } } else { mailContent = phone } HandOverFail(saleId, mailContent) } } } } } return true }, `select ent_id,clue_id,position_id,name,is_renewal_protection from dwd_f_csm_customer_info where is_transfer = 0`) log.Println("自动移交销售定时任务结束") } func refundAutoHistory() { data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1) if data != nil { for _, v := range *data { seatNumber := common.ObjToString(v["seatNumber"]) adata := TiDb.Find("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "", -1, -1) if adata != nil { for _, vv := range *adata { if common.IntAll(vv["resign"]) == 0 { position_id := common.Int64All(vv["position_id"]) TiDb.Update("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"seatNumber": seatNumber}, map[string]interface{}{"position_id": position_id}) break } } } } } } func IsFreeze(source string, pay_money int, positionId, trailstatus string) bool { log.Println(source, pay_money == 0, FindUpperLimit(gconv.String( positionId), "", false), trailstatus) if source == "0104" && pay_money == 0 && FindUpperLimit(gconv.String( positionId), "", false) { return true } else if source == "0104" && pay_money > 0 && trailstatus == "08" && FindUpperLimit(gconv.String( positionId), "", false) { return true } return false } func FindSaleRecord(orderCode string) (dept string, positionId int64, position string) { recordList := Mysql.SelectBySql("select * from order_sale_record where ordercode=? ORDER BY state ,money ", orderCode) if recordList != nil && len(*recordList) > 0 { saler_dept := gconv.String((*recordList)[0]["saler_dept"]) if strings.Contains(saler_dept, "销售") { saler_dept = "销售部" } else if strings.Contains(saler_dept, "市场") { saler_dept = "市场部" } else { saler_dept = "" } return saler_dept, gconv.Int64((*recordList)[0]["saler_Id"]), gconv.String((*recordList)[0]["saler_name"]) } return "", int64(0), "" }