package service import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "bp.jydev.jianyu360.cn/BaseService/biService/entity" "database/sql" "errors" "github.com/gogf/gf/v2/util/gconv" "go.mongodb.org/mongo-driver/bson" "log" "strings" "time" ) func RelatedCompanyIsCustom(companyName string) bool { if companyName == "" { return false } return entity.JyBiTidb.CountBySql("SELECT count(1) FROM dwd_f_csm_customer_info WHERE company_name = ? and account_type= 1 and is_transfer = 0", companyName) > 0 } // 修改线索名称 func ClueTransfer(clueId, changeType, kcType, mainClueId int64, clueName string) (status int, err error) { if changeType == 1 { //电销中修改线索名称 status, err = DxUpdateClueName(clueId, clueName) } else { //客成修改名称 if kcType == 1 { //回电销 status, err = ClueTransferDx(clueId, mainClueId) } else { //划转到关联客户客成 status, err = ClueTransferKc(clueId, clueName) } } return status, err } func DxUpdateClueName(clueId int64, clueName string) (status int, err error) { //查询关联客户客成信息 relatedData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"company_name": clueName, "account_type": 1, "is_transfer": 0}, "id,position_id,name,ent_id,company_name", "") if relatedData == nil || len(*relatedData) == 0 { return -1, errors.New("未查询到关联客户信息") } positionId := common.Int64All((*relatedData)["position_id"]) name := common.InterfaceToStr((*relatedData)["name"]) //客成经理名称 entId := common.Int64All((*relatedData)["ent_id"]) cusId := common.Int64All((*relatedData)["id"]) nowTime := time.Now().Format("2006-01-02 15:04:05") //判断之前该线索是否移交过客成 cusData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"clue_id": clueId}, "is_transfer", "") if cusData != nil && len(*cusData) > 0 { //该线索曾经移交过客成 if common.IntAll((*cusData)["is_transfer"]) == 0 { return 1, nil } else { up1 := entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{ "is_transfer": 0, "is_renewal_protection": 0, "transfertime": nowTime, "ent_id": entId, "company_name": clueName, "position_id": positionId, "name": name, "is_admin": 0, "primary_id": cusId, "account_type": 4, "is_task": 0, }) in := entity.JyBiTidb.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, "change_reason": "与主账号线索名称保持一致", }) up2 := entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime}) if up1 && up2 && in > 0 { return 1, nil } return -1, errors.New("更新客成信息出错") } } else { saveMap := map[string]interface{}{ "clue_id": clueId, "transfertime": nowTime, "position_id": positionId, "name": name, "ent_id": entId, "is_task": 0, "tasktime": nowTime, "taskstatus": 0, "tasksource": "1", "is_admin": 0, "relationship_building_way": 1, "inventory_way": 1, "training_way": 1, "is_pre_sales_training": 0, "service_stage": 1, "company_name": clueName, "primary_id": cusId, "account_type": 4, } cId, ok, updateId1 := int64(-1), false, int64(-1) if entity.JyBiTidb.ExecTx("保存客户", func(tx *sql.Tx) bool { cId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap) ok = entity.JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime}) updateId1 = entity.JyBiTidb.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, "change_reason": "与主账号线索名称保持一致", }) return cId > -1 && ok && updateId1 > -1 }) { return 1, nil } return -1, errors.New("移交客成失败") } } // 客成系统修改线索名称,移交电销 func ClueTransferDx(clueId, mainClueId int64) (status int, err error) { //查询原主账号电销人员 is_assign := 1 status = 1 positionId, kcPositionId, seatNumber, kcName, isFreeze := GetSalePerson(mainClueId) if isFreeze || positionId == 0 { is_assign = 0 } nowTime := time.Now().Format(date.Date_Full_Layout) ok := entity.JyBiTidb.ExecTx("退出客成系统,进入电销公海", func(tx *sql.Tx) bool { updateClueMap := map[string]interface{}{ "is_assign": is_assign, "updatetime": nowTime, "comeintime_open": nowTime, "position_id": positionId, "seatNumber": seatNumber, "is_transfer": 0, //"is_renewal_protection": 0, } ok1 := entity.JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClueMap) ok2 := entity.JyBiTidb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{ "is_transfer": 1, "position_id": 0, "name": "", "account_type": 0, "primary_id": "", }) in1 := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcPositionId > 0, kcPositionId, -1), "change_type": "退回公海", "new_value": "不再是非集团公司成交客户关联线索,移交销售", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) in2 := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcPositionId > 0, kcPositionId, -1), "change_type": "移交销售", "new_value": "不再是非集团公司成交客户关联线索,移交销售", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) in3 := entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": common.If(kcPositionId > 0, kcPositionId, -1), "change_field": "position_id", "change_type": "客户成功经理", "old_value": kcName, "new_value": "/", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, }) return ok1 && ok2 && in1 > 0 && in2 > 0 && in3 > 0 }) if !ok { log.Printf("子账号或免费账号移交销售失败:clueId:%d\n", clueId) status = -1 err = errors.New("子账号或免费账号移交销售失败") } return status, err } // 移交客成 func ClueTransferKc(clueId int64, clueName string) (int, error) { mainKcData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"company_name": clueName, "is_transfer": 0, "is_admin": 1}, "", "") if mainKcData == nil || len(*mainKcData) == 0 { return -1, errors.New("未查询到关联客户") } nowTime := time.Now().Format(date.Date_Full_Layout) cusId := common.Int64All((*mainKcData)["id"]) entId := common.Int64All((*mainKcData)["ent_id"]) companyName := common.InterfaceToStr((*mainKcData)["company_name"]) positionId := common.Int64All((*mainKcData)["position_id"]) name := common.Int64All((*mainKcData)["name"]) up1 := entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{ "is_transfer": 0, "is_renewal_protection": 0, "transfertime": nowTime, "ent_id": entId, "company_name": companyName, "position_id": positionId, "name": name, "is_admin": 0, "primary_id": cusId, "account_type": 4, "is_task": 0, }) in := entity.JyBiTidb.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, "change_reason": "与主账号线索名称保持一致", }) up2 := entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime}) if up1 && up2 && in > 0 { return 1, nil } return -1, errors.New("更新客成信息出错") } func GetSalePerson(mainClueId int64) (positionId, kcPosId int64, seatNumber, kcName string, isFreeze bool) { isFreeze = false clueData := entity.JyBiTidb.SelectBySql("SELECT a.position_id,a.seatNumber,b.position_id as kcPosId,b.name as kcName FROM dwd_f_crm_clue_info a LEFT JOIN dwd_f_csm_customer_info b ON a.id = b.clue_id WHERE a.id = ?", mainClueId) if clueData != nil && len(*clueData) > 0 { positionId = common.Int64All((*clueData)[0]["position_id"]) seatNumber = common.InterfaceToStr((*clueData)[0]["seatNumber"]) kcName = common.InterfaceToStr((*clueData)[0]["kcName"]) kcPosId = common.Int64All((*clueData)[0]["kcPosId"]) } pdata := entity.JyBiTidb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`) if pdata == nil { positionId = 0 seatNumber = "" return } saleData := []map[string]interface{}{} saleData = *pdata //(1)该销售人员未离职,则继续分配给该销售人员(保持现状); //该销售人员已离职,则随机分配 for _, v := range saleData { 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(positionId) { isFreeze = true positionId = 0 seatNumber = "" break } else { break } } else { //如果人离职了,放到离职销售人员所属部门的电销经理的私海里 sdata := &[]map[string]interface{}{} userData := FindPersonOne(positionId) if userData != nil { deptName := gconv.String(userData["dept_name"]) if strings.Contains(deptName, "一部") { sdata = entity.JyBiTidb.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`, positionId) if len(*sdata) == 0 { sdata = entity.JyBiTidb.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 = 8`, positionId) } } else if strings.Contains(deptName, "三部") { sdata = entity.JyBiTidb.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 = 8`, positionId) } } if sdata != nil && len(*sdata) > 0 { positionId = common.Int64All((*sdata)[0]["position_id"]) seatNumber = gconv.String((*sdata)[0]["seat_number"]) //newPerson = gconv.String((*sdata)[0]["name"]) } break } } } return } func FindPersonOne(positionId int64) map[string]interface{} { personData := entity.JyBiTidb.SelectBySql(`SELECT b.name, b.position_id, b.seat_number, b.role_id, a.dept_name FROM dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b ON a.position_id = ? AND a.position_id = b.position_id`, positionId) if personData == nil || len(*personData) == 0 { return nil } return (*personData)[0] }