12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397 |
- package main
- import (
- "app.yhyue.com/moapp/jybase/redis"
- "fmt"
- "log"
- "math"
- "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"
- )
- // 电销工单生成
- func rderAcceptance() {
- sql := fmt.Sprintf(`select * from order_acceptance where is_clue=2 and is_delete=1 order by propose_time `)
- //sql := fmt.Sprintf(`select * from order_acceptance where is_clue=2 and is_delete=1 and id = 333 order by propose_time `)
- data := WorkOrder.SelectBySql(sql)
- if data != nil && len(*data) > 0 {
- for _, v := range *data {
- acceptance_no := gconv.String(v["acceptance_no"])
- childrenArr := WorkOrder.Find("order_acceptance_children", map[string]interface{}{
- "acceptance_no": acceptance_no,
- }, "", "", -1, -1)
- if childrenArr != nil && len(*childrenArr) > 0 {
- company := ""
- phone := ""
- demand := ""
- name := ""
- product := ""
- for _, v1 := range *childrenArr {
- if gconv.String(v1["field_name"]) == "公司名称" {
- company = gconv.String(v1["field_value"])
- }
- if gconv.String(v1["field_name"]) == "联系方式num" {
- phone = gconv.String(v1["field_value"])
- }
- if gconv.String(v1["field_name"]) == "客户姓名" {
- name = gconv.String(v1["field_value"])
- }
- if gconv.String(v1["field_name"]) == "客户需求" {
- demand = gconv.String(v1["field_value"])
- }
- if gconv.String(v1["field_name"]) == "咨询产品" {
- product = gconv.String(v1["field_value"])
- }
- }
- if !WorkDataHandle(company, phone, demand, name, product, v) {
- log.Println("工单创建失败")
- }
- }
- }
- }
- }
- func WorkDataHandle(company, phone, demand, name, product string, acceptanceData map[string]interface{}) bool {
- uId := ""
- query := map[string]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
- }
- }
- if uId == "" {
- log.Println("用户信息不存在")
- return false
- }
- cluename := company
- if cluename == "" {
- cluename = phone //没有线索名,手机号代替
- }
- ok, data, saleData := false, map[string]interface{}{}, []map[string]interface{}{}
- isGroup, isCommerce := GetCompanyType(cluename)
- uCount, _ := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
- if uCount != nil && len(*uCount) > 0 {
- isUpdate := gconv.Int64((*uCount)["is_artificially_modified"])
- if isUpdate == 1 {
- cluename = ""
- }
- clueId := gconv.Int64((*uCount)["id"])
- positionId := gconv.Int64((*uCount)["position_id"])
- trailstatus := gconv.String((*uCount)["trailstatus"])
- IS_TRANSFER := gconv.Int64((*uCount)["IS_TRANSFER"])
- if IS_TRANSFER == 1 {
- //客成处理
- //客成
- //生成客成数据
- customerMap := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{
- "clue_id": clueId,
- }, "position_id,name", "")
- if customerMap != nil && len(*customerMap) > 0 {
- //UpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", company, name, gconv.String((*uCount)["name"]), phone, "", "", "", "", "", "", gconv.String((*uCount)["seat_number"]), "", gconv.Int64((*uCount)["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
- WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String((*uCount)["name"]), phone, "", "", "", "", "", "", gconv.String((*uCount)["seat_number"]), "", gconv.Int64((*uCount)["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
- data = map[string]interface{}{
- "type": "kc",
- "position_id": (*customerMap)["position_id"],
- "name": (*customerMap)["name"],
- }
- } else {
- log.Println("查询不到客成数据", clueId, uId)
- return false
- }
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": (*customerMap)["position_id"],
- "change_type": "加入任务车",
- "new_value": "咨询客服转客成",
- "createtime": time.Now().Format(date.Date_Full_Layout),
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- } else {
- if positionId == 0 {
- //线索变更
- ok, data, saleData = FindPosition(0, "", gconv.String(acceptanceData["creator_time"]))
- if !ok {
- log.Println(positionId, "用户查询失败")
- return false
- }
- WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, true, demand)
- } else {
- ok, data, saleData = FindPosition(positionId, trailstatus, gconv.String(acceptanceData["creator_time"]))
- if !ok {
- log.Println(positionId, "用户查询失败")
- return false
- }
- if gconv.Int64(data["position_id"]) == positionId {
- WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
- } else {
- WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, true, demand)
- }
- }
- }
- } else {
- //新增线索
- ok, data, _ = FindPosition(0, "", gconv.String(acceptanceData["creator_time"]))
- if !ok {
- return false
- }
- SaveClue("", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
- }
- //工单生成
- ok1 := AddOrderWork(acceptanceData, data, product, phone, company)
- log.Println("工单创建", ok1, gconv.String(acceptanceData["acceptance_no"]))
- return true
- }
- func AddOrderWork(acceptanceData map[string]interface{}, userData map[string]interface{}, product, phone, company string) bool {
- nowTime := time.Now().Format(date.Date_Full_Layout)
- work_order_no := fmt.Sprintf("GD%s%s", time.Now().Format(date.Date_yyyyMMdd), FindNumber("gd"))
- productArr := []string{}
- for _, v := range strings.Split(product, ",") {
- switch ProductMap[v] {
- case "dk":
- continue
- default:
- productArr = append(productArr, v)
- }
- }
- two_type := "dx"
- if gconv.String(userData["type"]) == "kc" {
- two_type = "kc"
- }
- personMap := GetPerson(gconv.String(userData["position_id"]))
- if personMap == nil || len(personMap) == 0 {
- return false
- }
- orderWorkMap := map[string]interface{}{
- "work_order_no": work_order_no,
- "acceptance_no": gconv.String(acceptanceData["acceptance_no"]),
- "type": strings.Join(productArr, ","),
- "status": common.If(gconv.Int64(userData["orderStatus"]) == 0, 2, 1),
- "initiator_name": gconv.String(acceptanceData["creator_name"]),
- "initiator_position_id": gconv.String(acceptanceData["initiator_position_id"]),
- "current_name": common.If(gconv.Int64(userData["orderStatus"]) == 0, nil, userData["name"]),
- "current_position_id": common.If(gconv.Int64(userData["orderStatus"]) == 0, nil, userData["position_id"]),
- "history_name": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["name"], nil),
- "history_postion_id": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["position_id"], nil),
- "is_delete": 1,
- "creator_name": gconv.String(acceptanceData["creator_name"]),
- "creator_position_id": gconv.String(acceptanceData["creator_position_id"]),
- "creator_time": nowTime,
- "two_type": two_type,
- "department_no": gconv.String(personMap["deptId"]),
- "department_name": gconv.String(personMap["deptName"]),
- "update_time": common.If(gconv.Int64(userData["orderStatus"]) == 0, nowTime, nil),
- }
- ok3 := WorkOrder.Insert("order_work", orderWorkMap)
- if ok3 > 0 {
- status := 2
- if WorkOrder.Count("order_work", map[string]interface{}{
- "status": 1,
- "acceptance_no": gconv.String(acceptanceData["acceptance_no"]),
- }) > 0 {
- status = 1
- }
- WorkOrder.Update("order_acceptance", map[string]interface{}{
- "id": acceptanceData["id"],
- }, map[string]interface{}{
- "is_clue": 3,
- "status": status,
- "over_time": common.If(status == 1, nil, nowTime),
- })
- //日志添加
- approvalRecordMap := map[string]interface{}{
- "work_order_no": work_order_no,
- "status": common.If(gconv.Int64(userData["orderStatus"]) == 0, 3, 1),
- "new_status": common.If(gconv.Int64(userData["orderStatus"]) == 0, 2, nil),
- "handle_name": userData["name"],
- "handle_position_id": userData["position_id"],
- "handle_dept_id": gconv.String(personMap["deptId"]),
- "handle_dept_name": gconv.String(personMap["deptName"]),
- "creator_name": gconv.String(acceptanceData["creator_name"]),
- "creator_position_id": gconv.String(acceptanceData["initiator_position_id"]),
- "is_delete": 1,
- "creator_time": nowTime,
- "handle_time": common.If(gconv.Int64(userData["orderStatus"]) == 0, nowTime, nil),
- "update_name": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["name"], nil),
- "update_position_id": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["position_id"], nil),
- "update_time": common.If(gconv.Int64(userData["orderStatus"]) == 0, nowTime, nil),
- }
- WorkOrder.Insert("approval_record", approvalRecordMap)
- WorkMail(personMap,
- strings.Join(productArr, ","),
- gconv.Int64(common.If(gconv.Int64(userData["orderStatus"]) == 0, 2, 1)),
- gconv.String(personMap["name"]),
- gconv.String(acceptanceData["creator_name"]),
- nowTime,
- work_order_no,
- phone,
- company)
- return true
- }
- return false
- }
- // 本级以及上级管理员查询
- func GetPerson(positionId string) map[string]interface{} {
- person := map[string]interface{}{}
- positionArrMap := Base.SelectBySql(fmt.Sprintf(`select a.phone,b.id,b.ent_id from
- base_user a
- INNER JOIN base_position b
- on b.id=%s and b.user_id=a.id and b.type=1`,
- positionId))
- if positionArrMap == nil || len(*positionArrMap) == 0 {
- return map[string]interface{}{}
- }
- phone := gconv.String((*positionArrMap)[0]["phone"])
- entId := gconv.String((*positionArrMap)[0]["ent_id"])
- entUserArrMap := Mysql.SelectBySql(fmt.Sprintf(`SELECT
- a.name as name,a.mail as mail,b.dept_id as deptId,a.phone ,c.name as deptName
- FROM
- entniche_user a
- INNER JOIN entniche_department_user b ON a.ent_id =%s
- AND a.phone IN %s
- and a.id=b.user_id
- inner join entniche_department c on b.dept_id=c.id
- `, entId, fmt.Sprintf("(%s)", phone)))
- //商机管理员
- if entUserArrMap == nil || len(*entUserArrMap) == 0 {
- return map[string]interface{}{}
- }
- deptId := gconv.Int64((*entUserArrMap)[0]["deptId"])
- person = map[string]interface{}{
- "name": gconv.String((*entUserArrMap)[0]["name"]),
- "mail": gconv.String((*entUserArrMap)[0]["mail"]),
- "phone": gconv.String((*entUserArrMap)[0]["phone"]),
- "deptId": gconv.String((*entUserArrMap)[0]["deptId"]),
- "deptName": gconv.String((*entUserArrMap)[0]["deptName"]),
- }
- //本部门管理员查询
- depthMap := Mysql.SelectBySql(`SELECT
- c.name as name,c.mail as mail
- FROM
- entniche_department_user a
- INNER JOIN entniche_user_role b ON a.dept_id = ?
- AND a.user_id = b.user_id
- AND b.role_id !=""
- INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
- if depthMap != nil && len(*depthMap) > 0 {
- person["deptPersonName"] = gconv.String((*depthMap)[0]["name"])
- person["deptPersonMail"] = gconv.String((*depthMap)[0]["mail"])
- }
- //商机管理员查询
- superiorDepthMap := Mysql.SelectBySql(`SELECT
- c.*
- FROM
- entniche_department d
- INNER JOIN entniche_department_user a ON d.id = ?
- AND d.pid = a.dept_id
- INNER JOIN entniche_user_role b ON a.user_id = b.user_id
- AND b.role_id !=""
- INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
- if superiorDepthMap != nil && len(*superiorDepthMap) > 0 {
- person["superiorDepthPersonName"] = gconv.String((*superiorDepthMap)[0]["name"])
- person["superiorDepthPersonMail"] = gconv.String((*superiorDepthMap)[0]["mail"])
- }
- return person
- }
- // 编号查询
- func FindNumber(moudle string) string {
- today := time.Now().Format("2006-01-02")
- yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
- key := fmt.Sprintf("%s_%s", today, moudle)
- yesterdayKey := fmt.Sprintf("%s_%s", yesterday, moudle)
- if ok, _ := redis.Exists("newother", yesterdayKey); ok {
- //删除之前数据
- redis.Del("newother", yesterdayKey)
- }
- count := redis.Incr("newother", key)
- log.Println("编号获取", moudle, fmt.Sprintf("%04d", count))
- return fmt.Sprintf("%04d", count)
- }
- // 人员查询
- func GetAllocation(proportion1, proportion3 float64, deptCount1, deptCount3 int64, administrators1, administrators3 map[string]interface{}, creatorTime string) map[string]interface{} {
- log.Println("分配比例查询", proportion1, proportion3, deptCount1, deptCount3)
- if deptCount1 == 0 {
- return administrators1
- }
- if deptCount3 == 0 {
- return administrators3
- }
- nowAllocationRatio := math.Round(gconv.Float64(deptCount1 / deptCount3))
- if cfg.AllocationRatio == 0 {
- //重新计算
- cfg.AllocationRatio = math.Round(proportion1 / proportion3)
- cfg.AllocationTime = creatorTime
- common.WriteSysConfig(&cfg)
- log.Println("新增计算比例", cfg.AllocationRatio)
- return administrators1
- } else {
- if nowAllocationRatio == cfg.AllocationRatio {
- //重新计算
- cfg.AllocationRatio = math.Round(proportion1 / proportion3)
- cfg.AllocationTime = creatorTime
- common.WriteSysConfig(&cfg)
- return administrators1
- }
- }
- if nowAllocationRatio > cfg.AllocationRatio {
- return administrators3
- } else {
- return administrators1
- }
- //达到比例也重新计算
- }
- func FindPosition(positionId int64, trailstatus, creatorTime string) (bool, map[string]interface{}, []map[string]interface{}) {
- allData := []map[string]interface{}{}
- //查询两个部门高级管理员
- //查询两个部门所有人员标识
- //电销三部
- userData1 := TiDb.SelectBySql(`SELECT
- a.position_id,
- a.name,
- b.seat_number,
- b.role_id,
- a.bi_pcode,
- a.dept_name
- FROM
- dwd_d_crm_department_level_succbi a
- INNER JOIN dwd_f_crm_personnel_management b ON a.resign = 0 and b.resign = 0
- AND a.dept_name LIKE "%电销部%"
- AND a.position_id = b.position_id`)
- userData3 := TiDb.SelectBySql(`SELECT
- a.position_id,
- a.name,
- b.seat_number,
- b.role_id,
- a.bi_pcode,
- a.dept_name
- FROM
- dwd_d_crm_department_level_succbi a
- INNER JOIN dwd_f_crm_personnel_management b ON a.resign = 0 and b.resign = 0
- AND a.resign = 0
- AND a.dept_name LIKE "%销售三部"
- AND a.position_id = b.position_id`)
- if userData3 == nil || len(*userData3) == 0 || userData1 == nil || len(*userData1) == 0 {
- return false, map[string]interface{}{}, allData
- }
- //一部管理员信息
- administrators1 := map[string]interface{}{}
- //三部管理员信息
- administrators3 := map[string]interface{}{}
- //返回当前人信息
- administrators := map[string]interface{}{}
- //电销一部
- deptNameMap1 := map[string]interface{}{}
- deptNameMap3 := map[string]interface{}{}
- proportion1 := float64(0)
- proportion3 := float64(0)
- for _, v := range *userData3 {
- v["type"] = 3
- id := gconv.Int64(v["position_id"])
- roleId := gconv.Int64(v["role_id"])
- if id == positionId {
- administrators = v
- }
- if roleId == 8 {
- v["orderStatus"] = 2
- administrators3 = v
- } else if roleId == 3 {
- proportion3 += 0.5
- } else {
- proportion3 += 1
- }
- deptNameMap3[gconv.String(v["bi_pcode"])] = true
- allData = append(allData, v)
- }
- for _, v := range *userData1 {
- v["type"] = 1
- id := gconv.Int64(v["position_id"])
- roleId := gconv.Int64(v["role_id"])
- if id == positionId {
- administrators = v
- }
- if roleId == 8 {
- v["orderStatus"] = 2
- administrators1 = v
- } else if roleId == 3 {
- proportion1 += 0.5
- } else {
- proportion1 += 1
- }
- deptNameMap1[gconv.String(v["bi_pcode"])] = true
- allData = append(allData, v)
- }
- if positionId != 0 {
- if administrators == nil || len(administrators) == 0 {
- return false, map[string]interface{}{}, allData
- }
- positiontype := gconv.Int64(administrators["type"])
- if trailstatus == "01" || trailstatus == "03" || trailstatus == "04" {
- switch positiontype {
- case 1:
- //找他上级
- administrators = administrators1
- case 3:
- //找他上级
- administrators = administrators3
- }
- return true, administrators, allData
- } else {
- return true, administrators, allData
- }
- }
- //按照比例分配选择一部还是三部
- //一部查询
- deptCount1 := CalculateProportion(deptNameMap1)
- //三部查询
- deptCount3 := CalculateProportion(deptNameMap3)
- return true, GetAllocation(proportion1, proportion3, deptCount1, deptCount3, administrators1, administrators3, creatorTime), allData
- log.Println("电销信息获取失败", positionId)
- return false, map[string]interface{}{}, allData
- }
- func CalculateProportion(deptNameMap map[string]interface{}) int64 {
- deptNameStr := ""
- for k := range deptNameMap {
- if deptNameStr == "" {
- deptNameStr = k
- } else {
- deptNameStr = fmt.Sprintf("%s,%s", deptNameStr, k)
- }
- }
- if deptNameStr == "" {
- return int64(0)
- }
- count := int64(0)
- sql := fmt.Sprintf(`SELECT
- sum(1) as count
- FROM
- order_work a
- INNER JOIN approval_record b ON a.creator_time >= "%s"
- AND FIND_IN_SET( a.department_no , "%s")
- AND a.work_order_no = b.work_order_no
- and (b.new_status!= 1 or b.new_status is null )
- `, cfg.AllocationTime, deptNameStr)
- data := WorkOrder.SelectBySql(sql)
- if data != nil && len(*data) > 0 {
- count = gconv.Int64((*data)[0]["count"])
- }
- return count
- }
- // 未支付订单 30分钟一次
- func orders() {
- //一个小时未支付进入线索 A
- log.Println("未支付订单定时任务开始")
- lastOrderId := cfg.LastOrderId
- selectTimeStart := time.Unix(time.Now().Unix()-7200, 0).Format(date.Date_Full_Layout)
- selectTimeEnd := time.Unix(time.Now().Unix()-3600, 0).Format(date.Date_Full_Layout)
- sql := fmt.Sprintf(`select * from dataexport_order where 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 {
- order_status := common.IntAll(v["order_status"])
- is_backstage_order := common.IntAll(v["is_backstage_order"])
- product_type_str1 := `"大会员","VIP订阅","数据流量包","历史数据"`
- product_type := common.ObjToString(v["product_type"])
- if order_status == 0 && is_backstage_order == 0 && strings.Contains(product_type_str1, product_type) {
- ok1, ok2 := FormatData(v, "orders")
- if !ok1 {
- common.WriteSysConfig(&cfg)
- log.Println("线索卡点", "orders", v, selectTimeEnd, selectTimeStart)
- break
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "orders", v, selectTimeEnd, selectTimeStart)
- common.WriteSysConfig(&cfg)
- break
- }
- }
- }
- cfg.LastOrderId = common.IntAll(v["id"])
- }
- }
- common.WriteSysConfig(&cfg)
- log.Println("未支付订单定时任务结束")
- }
- func readClue() {
- log.Println("读表进线索定时任务开始")
- lastReadClueTime := cfg.LastReadClueTime
- sql := fmt.Sprintf(`select * from clue_info where updatetime > "%s" order by updatetime asc`, fmt.Sprint(lastReadClueTime))
- data := TiDb.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- ok1, ok2 := FormatData(v, "readClue")
- if !ok1 {
- common.WriteSysConfig(&cfg)
- log.Println("线索卡点", "readClue", v, lastReadClueTime)
- break
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "readClue", v, lastReadClueTime)
- common.WriteSysConfig(&cfg)
- break
- }
- }
- cfg.LastReadClueTime = common.ObjToString(v["updatetime"])
- }
- }
- common.WriteSysConfig(&cfg)
- log.Println("读表进线索定时任务结束")
- }
- // 新注册用户 5分钟一次
- func users() {
- //判断节假日
- runOk := getRunOk()
- if !runOk {
- log.Println("不是工作日,任务暂停")
- return
- }
- //新用户注册后5分钟内进入线索 C
- log.Println("新注册用户定时任务开始")
- selectTimeEnd := cfg.LastUserId
- sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where createtime > "%s" and source = "0101" and status != 2 order by createtime asc`, selectTimeEnd)
- data := TiDb.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- for k, v := range *data {
- //判断用户是否有小程序切使用过剑鱼其他产品
- s_platform := gconv.String(v["s_platform"])
- login_positionid := gconv.Int64(v["login_positionid"])
- createtime := common.ObjToString(v["createtime"])
- if s_platform == "xcx" && login_positionid == 0 {
- log.Println(v, "用户是否有小程序且未使用过剑鱼其他产品")
- } else {
- ok1, ok2 := FormatData(v, "users")
- if !ok1 {
- log.Println("线索卡点", "users", v, selectTimeEnd)
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "users", v, selectTimeEnd)
- }
- }
- }
- if k == len(*data)-1 {
- cfg.LastUserId = createtime
- }
- }
- }
- common.WriteSysConfig(&cfg)
- selectXcxTimeEnd := cfg.LastXcxUserId
- xcxSql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where s_platform ="xcx" and updatetime> "%s" and source = "0101" and status != 2 and clue_operate_status!=1 order by createtime asc`, selectXcxTimeEnd)
- xcxData := TiDb.SelectBySql(xcxSql)
- if xcxData != nil && *xcxData != nil && len(*xcxData) > 0 {
- for k, v := range *xcxData {
- //判断用户是否有小程序切使用过剑鱼其他产品
- s_platform := gconv.String(v["s_platform"])
- login_positionid := gconv.Int64(v["login_positionid"])
- updatetime := common.ObjToString(v["updatetime"])
- if s_platform == "xcx" && login_positionid == 0 {
- log.Println(gconv.String(v["id"]), "用户是否有小程序且未使用过剑鱼其他产品")
- } else {
- ok1, ok2 := FormatData(v, "xcxusers")
- if !ok1 {
- common.WriteSysConfig(&cfg)
- log.Println("小程序用户分配线索卡点", "xcxusers", v, selectXcxTimeEnd)
- break
- } else {
- if !ok2 {
- log.Println("小程序用户分配已达上限", "xcxusers", v, selectXcxTimeEnd)
- common.WriteSysConfig(&cfg)
- break
- }
- }
- TiDb.Update("dwd_f_userbase_baseinfo", map[string]interface{}{
- "id": gconv.Int64(v["id"]),
- }, map[string]interface{}{
- "clue_operate_status": 1,
- })
- }
- if k == len(*xcxData)-1 {
- cfg.LastXcxUserId = updatetime
- }
- }
- }
- common.WriteSysConfig(&cfg)
- log.Println("新注册用户定时任务结束")
- }
- // 留资 5分钟一次
- func saleLeads() {
- //判断节假日
- runOk := getRunOk()
- if !runOk {
- 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)}
- }
- log.Println("query :", query)
- iter := session.DB(db.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter()
- thisData := map[string]interface{}{}
- for {
- if !iter.Next(&thisData) {
- break
- }
- sourceCode := common.ObjToString(thisData["source"])
- if sourceCode == "" {
- log.Println("留资没有source", thisData)
- continue
- }
- //
- filterArr := []string{"-pc", "-app", "-wx", "-h5"}
- sourceMap := map[string]string{}
- saleSource := TiDb.SelectBySql(`SELECT source,name FROM d_saleleads_code WHERE (department LIKE '%大客户%' or department LIKE '%市场组%' or department LIKE '%咨询组%') AND is_delete = 1`)
- if saleSource != nil && len(*saleSource) > 0 {
- for _, v := range *saleSource {
- source := common.ObjToString(v["source"])
- name := common.ObjToString(v["name"])
- for _, s := range filterArr {
- name = strings.ReplaceAll(name, s, "")
- }
- sourceMap[source] = name
- }
- }
- if sourceMap[sourceCode] != "" {
- continue
- }
- ok1, ok2 := FormatData(thisData, "saleLeads")
- if !ok1 {
- log.Println("线索卡点", "saleLeads", thisData, lastId)
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "saleLeads", thisData, lastId)
- }
- }
- cfg.LastId = mongodb.BsonIdToSId(thisData["_id"])
- }
- common.WriteSysConfig(&cfg)
- 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 != "0105" and source != "0104" and source != "0103" and source != "0102"`, 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"])
- //判断用户是否有小程序切使用过剑鱼其他产品
- s_platform := gconv.String(v["s_platform"])
- login_positionid := gconv.Int64(v["login_positionid"])
- if s_platform == "xcx" && login_positionid == 0 {
- log.Println(phone, uId, userId, "用户是否有小程序且未使用过剑鱼其他产品")
- continue
- }
- registedate := common.ObjToString(v["l_registedate"])
- name := common.ObjToString(v["name"])
- nowTime := time.Now().Format(date.Date_Full_Layout)
- source := common.ObjToString(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 {
- contactsData2 := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where baseinfo_id = ? and is_delete = 1", uId)
- if contactsData2 != nil && len(*contactsData2) > 0 {
- log.Println("userbase uid不为空 新增通讯录", uId)
- 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 {
- registedates, _ := time.Parse(date.Date_Full_Layout, registedate)
- count := TiDb.CountBySql("select count(1) as count from dwd_f_crm_clue_info where uid = ?", uId)
- log.Println("userbase uid 线索数量 ", count)
- log.Println("userbase uid 注册时间 ", registedates)
- if time.Now().Unix()-registedates.Unix() > int64(db.RegTimes)*86400 {
- if count == 0 {
- clueId := int64(0)
- sql := fmt.Sprintf(`select * from freeClubSign where mogUserId="%s" and sub_again_date > "%s" `, userId, selectTimeEnd)
- data := BiService.SelectBySql(sql)
- if len(*data) > 0 && userId != "" {
- 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": "532",
- "sub_cluetype": "670",
- "trailstatus": "01",
- "name": name,
- "phone": phone,
- "comeintime_open": nowTime,
- "comeinsource_open": 1,
- "FREEZE_TIME": nowTime,
- })
- } else {
- 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": "532",
- "sub_cluetype": "475",
- "trailstatus": "01",
- "name": name,
- "phone": phone,
- "comeintime_open": nowTime,
- "comeinsource_open": 1,
- "FREEZE_TIME": nowTime,
- })
- }
- if clueId > 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,
- })
- TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": -1,
- "change_type": "创建线索",
- "new_value": "系统自动创建",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- }
- } 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 {
- ok1, ok2 := FormatData(v, "users")
- if !ok1 {
- common.WriteSysConfig(&cfg)
- log.Println("线索卡点", "userbase uid", v, uId)
- break
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "userbase uid", v, uId)
- common.WriteSysConfig(&cfg)
- break
- }
- }
- } 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定时任务结束")
- }
- func getRunOk() bool {
- currentTime, runOk := time.Now(), false
- if currentTime.Weekday() == time.Sunday {
- 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
- }
- }
- return runOk
- }
- 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{}, sourceCode string, sourceId int64) (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 == "message" {
- pcode = "532"
- code = "477"
- level = "B"
- topname = "其他"
- subname = "机器人客服主动咨询"
- } else if item == "readClue" {
- level = "A"
- topname = "超级订阅临期用户"
- pcode = "614"
- if sourceId == 1 {
- code = "615"
- subname = "60天后到期"
- } else if sourceId == 2 {
- code = "616"
- subname = "45天后到期"
- } else if sourceId == 3 {
- code = "617"
- subname = "15天后到期"
- } else {
- code = "618"
- subname = "7天后到期"
- }
- } else if item == "xcxusers" {
- level = "C"
- 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"])
- }
- }
- }
- } else if item == "allocation" {
- pcode = "532"
- code = "671"
- level = "D"
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- topname = common.ObjToString((*pcodeData)["name"])
- }
- pcodeData = TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": code}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- subname = common.ObjToString((*pcodeData)["name"])
- }
- } else if item == "rebind" {
- pcode = "532"
- code = "670"
- level = "D"
- pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- topname = common.ObjToString((*pcodeData)["name"])
- }
- pcodeData = TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": code}, "", "")
- if pcodeData != nil && len(*pcodeData) > 0 {
- subname = common.ObjToString((*pcodeData)["name"])
- }
- } else {
- 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, cluename, phone string, isGroup, isCommerce int) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}, isOk, isFreeze bool, noticePositionId int64) {
- isOk = false
- isFreeze = false
- if TiDb.Count("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone, "is_assign": 1}) == 0 { //线索没销售进入,有销售走分配次数最少的逻辑
- if isGroup == 0 && isCommerce == 1 && cluename != "" { //非集团在工商库线索名不为空
- //cdata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "", "", -1, -1)
- cdata := TiDb.SelectBySql(`select position_id,MAX(trail_time) as trail_time,max(last_ring_time) as last_ring_time,seatNumber,count(id) as count from dwd_f_crm_clue_info where cluename=? and is_assign =1 GROUP BY position_id `, cluename)
- if cdata != nil && len(*cdata) > 0 { //找到了公司有人在跟进
- isOk = true
- pdata := TiDb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)
- if pdata == nil {
- positionId = 0
- seatNumber = ""
- saleName = ""
- return
- }
- saleData = *pdata
- cdataNew := []map[string]interface{}{}
- if len(*cdata) > 1 {
- //可能有多个人跟进
- personMap := map[int64]bool{}
- upperLimitPersonMap := map[int64]bool{}
- upperLimitCdataNew := []map[string]interface{}{}
- upperLimitCount := 0
- isFull := false
- for _, m := range *cdata {
- positionid := gconv.Int64(m["position_id"])
- for _, v := range *pdata {
- resign := common.IntAll(v["resign"])
- assign_type := common.IntAll(v["assign_type"])
- role_id := common.IntAll(v["role_id"])
- if positionid == common.Int64All(v["position_id"]) {
- if resign == 0 && (assign_type == 1 || role_id == 8) {
- if !FindUpperLimit(gconv.String(positionid), mode, true) {
- personMap[positionid] = true
- m["saleName"] = common.ObjToString(v["name"])
- cdataNew = append(cdataNew, m)
- } else {
- upperLimitCount++
- upperLimitPersonMap[positionid] = true
- m["saleName"] = common.ObjToString(v["name"])
- upperLimitCdataNew = append(upperLimitCdataNew, m)
- }
- }
- }
- }
- if len(cdataNew) == 0 && len(upperLimitCdataNew) > 0 {
- //没有达上限的人数为0 全部都是达上限的
- personMap = upperLimitPersonMap
- cdataNew = upperLimitCdataNew
- isFull = true
- }
- }
- //查询是否都有没有离职
- if len(personMap) != 0 && len(cdataNew) > 0 {
- layout := "2006-01-02 15:04:05"
- //有人没有离职
- data := map[string]interface{}{}
- trailTime := int64(0)
- for _, m := range cdataNew {
- currentTime := int64(0)
- if gconv.String(m["trail_time"]) == "" {
- continue
- }
- t, _ := time.Parse(layout, gconv.String(m["trail_time"]))
- currentTime = t.Unix()
- if currentTime > trailTime {
- trailTime = currentTime
- data = m
- }
- }
- if trailTime == 0 {
- //需要查看通话记录
- lastRingTime := int64(0)
- for _, m := range cdataNew {
- currentTime := int64(0)
- if gconv.String(m["last_ring_time"]) == "" {
- continue
- }
- t, _ := time.Parse(layout, gconv.String(m["last_ring_time"]))
- currentTime = t.Unix()
- if currentTime > lastRingTime {
- lastRingTime = currentTime
- data = m
- }
- }
- if lastRingTime != 0 {
- positionId = common.Int64All(data["position_id"])
- noticePositionId = positionId
- seatNumber = common.ObjToString(data["seatNumber"])
- saleName = common.ObjToString(data["saleName"])
- if isFull {
- isFreeze = true
- noticePositionId = positionId
- positionId = 0
- seatNumber = ""
- saleName = ""
- }
- return
- } else {
- count := 0
- //线索数量判断
- for i, v := range cdataNew {
- if i == 0 {
- count = gconv.Int(v["count"])
- data = v
- } else {
- if count < gconv.Int(v["count"]) {
- count = gconv.Int(v["count"])
- data = v
- }
- }
- }
- positionId = common.Int64All(data["position_id"])
- noticePositionId = positionId
- saleName = common.ObjToString(data["saleName"])
- seatNumber = common.ObjToString(data["seatNumber"])
- if isFull {
- isFreeze = true
- noticePositionId = positionId
- positionId = 0
- seatNumber = ""
- saleName = ""
- }
- return
- }
- } else {
- positionId = common.Int64All(data["position_id"])
- noticePositionId = positionId
- seatNumber = common.ObjToString(data["seatNumber"])
- saleName = common.ObjToString(data["saleName"])
- if isFull {
- isFreeze = true
- noticePositionId = positionId
- positionId = 0
- seatNumber = ""
- saleName = ""
- }
- return
- }
- }
- } else {
- //只有一人跟进
- //(1)该销售人员未离职,则继续分配给该销售人员(保持现状);
- //该销售人员已离职,则随机分配
- positionId = common.Int64All((*cdata)[0]["position_id"])
- noticePositionId = positionId
- seatNumber = common.ObjToString((*cdata)[0]["seatNumber"])
- for _, v := range *pdata {
- resign := common.IntAll(v["resign"])
- assign_type := common.IntAll(v["assign_type"])
- role_id := common.IntAll(v["role_id"])
- if positionId == common.Int64All(v["position_id"]) {
- if resign == 0 && (assign_type == 1 || role_id == 8) {
- if FindUpperLimit(gconv.String(positionId), mode, true) {
- isFreeze = true
- positionId = 0
- seatNumber = ""
- saleName = ""
- break
- } else {
- saleName = common.ObjToString(v["name"])
- return
- }
- }
- }
- }
- }
- }
- }
- }
- positionId = 0
- seatNumber = ""
- saleName = ""
- query := `select * from dwd_f_crm_personnel_management where assign_type = 1 and`
- if mode == "A" {
- query += ` assign_level like "%A%"`
- } else if mode == "B" {
- query += ` assign_level like "%B%"`
- } else if mode == "D" {
- query += ` assign_level like "%D%"`
- } else {
- query += ` assign_level like "%C%"`
- }
- 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.Int64All(v["position_id"]) == common.Int64All(vv["position_id"]) {
- if common.IntAll(v["resign"]) == 0 {
- vv["status"] = 1
- } else {
- vv["status"] = 2
- }
- isOk = true
- }
- }
- if !isOk { //有新员工直接分给新员工
- positionId = common.Int64All(v["position_id"])
- saleName = common.ObjToString(v["name"])
- log.Println("新员工, ", positionId, saleName)
- rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
- TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
- "position_id": positionId,
- "clue_level": mode,
- "count": common.Int64All((*rData)["count"]),
- })
- break
- }
- }
- if positionId == 0 {
- res := int64(0)
- countres := 0
- for _, v := range *countData {
- if common.IntAll(v["status"]) == 1 {
- if FindUpperLimit(gconv.String(v["position_id"]), mode, false) {
- continue
- }
- if countres == 0 {
- res = common.Int64All(v["count"])
- positionId = common.Int64All(v["position_id"])
- } else {
- if common.Int64All(v["count"]) <= res {
- res = common.Int64All(v["count"])
- positionId = common.Int64All(v["position_id"])
- }
- }
- countres++
- }
- }
- log.Println(444, res)
- }
- } else {
- for _, kv := range *data {
- positionId1 := gconv.String(kv["position_id"])
- if !FindUpperLimit(positionId1, "", false) {
- positionId = common.Int64All(kv["position_id"])
- saleName = common.ObjToString(kv["name"])
- rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
- if rData != nil && len(*rData) > 0 {
- TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
- "position_id": positionId,
- "clue_level": mode,
- "count": common.Int64All((*rData)["count"]),
- })
- } else {
- TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
- "position_id": positionId,
- "clue_level": mode,
- "count": 0,
- })
- }
- break
- }
- }
- }
- for _, v := range *data {
- if positionId == common.Int64All(v["position_id"]) {
- seatNumber = common.ObjToString(v["seat_number"])
- saleName = common.ObjToString(v["name"])
- }
- }
- }
- 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
- }
- func GetCompanyType(companyName string) (int, int) {
- //是否是集团
- isGroup, isCommerce := 0, 0
- if c := TiDb.CountBySql(`select count(1) from group_company_name where company_name=?`, companyName); c > 0 {
- isGroup = 1
- }
- //是否在工商库
- if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": companyName, "company_type": map[string]interface{}{"$ne": "个体工商户"}}); c > 0 {
- isCommerce = 1
- }
- return isGroup, isCommerce
- }
- // 查询是否达上限
- func FindUpperLimit(positionId string, level string, isAdd bool) bool {
- if positionId == "" {
- return false
- }
- isFull := TiDb.CountBySql(`select count(1) from dwd_f_crm_clue_info where position_id=? and is_assign=1 and is_transfer != 1 `, positionId) >= db.AllocationCap
- if isFull && isAdd && level != "" {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
- }
- return isFull
- }
- func getSeatNumberPositionId(seatNumber string) (positionId int64) {
- saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "")
- if saleData != nil && len(*saleData) > 0 {
- positionId = common.Int64All((*saleData)["position_id"])
- }
- return
- }
- // 重新关注用户处理
- func rebind() {
- log.Println("重新关注用户处理开始")
- //判断节假日
- runOk := getRunOk()
- if !runOk {
- log.Println("不是工作日,任务暂停")
- return
- }
- rebindTimeEnd := cfg.RebindTime
- sql := fmt.Sprintf(`select * from freeClubSign where sub_again_date > "%s" order by sub_again_date asc`, rebindTimeEnd)
- data := BiService.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- rebindTimeEnd = common.ObjToString(v["sub_again_date"])
- registedates, _ := time.Parse(date.Date_Full_Layout, gconv.String(v["register_time"]))
- if time.Now().Unix()-registedates.Unix() > int64(db.RegTimes)*86400 {
- ok1, ok2 := FormatData(v, "rebind")
- if !ok1 {
- log.Println("线索卡点", "allocation", v, rebindTimeEnd)
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "allocation", v, rebindTimeEnd)
- }
- }
- }
- }
- cfg.RebindTime = rebindTimeEnd
- common.WriteSysConfig(&cfg)
- }
- log.Println("重新关注用户处理结束")
- }
- // 活跃用户处理
- func activeUsers() {
- log.Println("活跃用户处理开始")
- //判断节假日
- runOk := getRunOk()
- if !runOk {
- log.Println("不是工作日,任务暂停")
- return
- }
- //活跃用户查询
- activeTimeEnd := cfg.ActiveTime
- sql := fmt.Sprintf(`select * from freeClubSign where act_again_date > "%s" order by act_again_date asc`, activeTimeEnd)
- data := BiService.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- activeTimeEnd = common.ObjToString(v["act_again_date"])
- ok1, ok2 := FormatData(v, "allocation")
- log.Println(v, "allocation", ok1, ok2)
- if !ok1 {
- log.Println("线索卡点", "allocation", v, activeTimeEnd)
- } else {
- if !ok2 {
- log.Println("用户分配已达上限", "allocation", v, activeTimeEnd)
- }
- }
- }
- cfg.ActiveTime = activeTimeEnd
- common.WriteSysConfig(&cfg)
- }
- log.Println("活跃用户处理结束")
- }
|