123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- package main
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/mongodb"
- "database/sql"
- "errors"
- "fmt"
- "github.com/gogf/gf/v2/util/gconv"
- "go.mongodb.org/mongo-driver/bson"
- "log"
- "time"
- )
- // 子账号及免费账号移交电销
- func SubAndFreeAccountToDx(cusId, changeType int64) {
- //根据主账号客成id查询需要移交子账号、免费账号线索
- customerData := TiDb.SelectBySql("SELECT a.product_access,a.id,a.ent_id,b.uid,a.account_type,a.primary_id,b.id as clue_id,a.name as kcName,a.position_id FROM `dwd_f_csm_customer_info` a LEFT JOIN dwd_f_crm_clue_info b ON a.clue_id = b.id WHERE a.primary_id = ? AND a.is_transfer = 0", cusId)
- if customerData == nil || len(*customerData) == 0 {
- return
- }
- for _, val := range *customerData {
- //这个过程需要判断每个用户是否有其他身份下的主账号在客成中
- clueId := common.Int64All(val["clue_id"])
- uid := common.InterfaceToStr(val["uid"])
- currentEntId := common.Int64All(val["ent_id"])
- accountType := common.IntAll(val["account_type"]) //原来在客成的 账号类型 1:主账号 2:子账号 3:企业组织架构免费账号 4:非集团公司免费账号
- oldKcName := common.InterfaceToStr(val["name"])
- userPowerMap, err := GetPersonIdentityPower(uid, accountType, currentEntId, "")
- if err != nil {
- log.Printf("查询企业下子账号、员工其他身份下是否有会员权益失败: uid:%s\n", uid)
- continue
- }
- if userPowerMap == nil { //移出客成,进入公海
- changeReason := ""
- kcPositionId := common.Int64All(val["position_id"])
- switch changeType {
- case 1: //权益到期
- changeReason = "成交客户续约失败,关联线索移交销售"
- case 2: //退款
- changeReason = "成交客户申请退款,关联线索移交销售"
- case 3: //
- productName := GetProductName(gconv.Int64(val["customerData"]))
- changeReason = fmt.Sprintf("%s权限被企业删除", productName)
- }
- kcToOpenSeaUpdate(clueId, kcPositionId, changeType, changeReason)
- } else { //该用户在其他身份下有会员权益
- entId := common.Int64All(userPowerMap["entId"])
- mainCusData := common.ObjToMap(userPowerMap["customer"]) //主账号信息
- mainCompanyName := common.InterfaceToStr((*mainCusData)["company_name"])
- isGroup, isCommerce := GetCompanyTypeByClueId(mainCompanyName, clueId)
- newAccountType := common.IntAll(userPowerMap["newAccountType"])
- SubAndFreeAccountUpdate(*mainCusData, entId, clueId, newAccountType, isGroup, isCommerce, oldKcName)
- }
- }
- log.Printf("客户id:%d 下的子账号、免费账号移交完成", cusId)
- }
- func GetPersonIdentityPower(uid string, accountType int, currentEntId int64, cluename string) (map[string]interface{}, error) {
- //查询到该用户下得所有身份
- userEntData := TiDb.SelectBySql("SELECT ent_id,position_id,company_name,type,ent_user_id,userid,phone FROM data_service.user_system"+
- " WHERE uid = ? AND status = 1", uid)
- if userEntData == nil || len(*userEntData) == 0 {
- return nil, errors.New("为查询到用户身份信息")
- }
- allMemberType := []map[string]interface{}{}
- if accountType == 2 {
- log.Println("大会员子账号")
- }
- switch accountType {
- case 1, 2, 51, 52:
- for _, v := range *userEntData {
- newType := 0
- entId := common.Int64All(v["ent_id"])
- if entId == currentEntId && accountType != 51 {
- continue
- }
- userPhone := common.InterfaceToStr(v["phone"])
- entUserId := common.Int64All(v["ent_user_id"])
- if entId == 0 && common.IntAll(v["type"]) == 0 { //个人身份
- //判断个人身份是否存在个人大会员或者是子账号
- //identityCus := map[string]interface{}{}
- userId := common.InterfaceToStr(v["userid"])
- if userId == "" {
- continue
- }
- userData, ok := Mgo.FindOne("user", bson.M{"s_member_mainid": bson.M{"$exists": true}, "i_member_sub_status": 1, "i_member_status": bson.M{"$gt": 0}, "_id": mongodb.StringTOBsonId(userId)})
- if userData != nil && len(*userData) > 0 && ok {
- memberSubStatus := common.IntAll((*userData)["i_member_sub_status"])
- memberMainid := common.InterfaceToStr((*userData)["s_member_mainid"])
- if memberSubStatus == 1 && memberMainid != "" { //子账号
- newType = 2
- }
- if newType == 2 { //判断主账号是否在客成
- mainId := userId
- if newType == 2 {
- mainId = memberMainid
- }
- data := map[string]interface{}{
- "entId": entId,
- "newAccountType": newType,
- }
- userCus := TiDb.FindOne("dwd_f_csm_customer_info", bson.M{"ent_id": mainId, "is_transfer": 0, "account_type": 1}, "", "")
- if userCus == nil || len(*userCus) == 0 {
- log.Printf("查询主账号客户信息失败: userId:%s\n", mainId)
- continue
- }
- data["customer"] = userCus
- allMemberType = append(allMemberType, data)
- }
- }
- } else if entId != 0 && common.IntAll(v["type"]) == 1 { //企业身份
- //需要查询此人在不在企业下边
- if Mysql.Count("entniche_user", map[string]interface{}{
- "ent_id": entId,
- "id": entUserId,
- }) == 0 {
- continue
- }
- //判断该身份下时候是大会员企业管理管,或者是企业员工(是否有权益)
- //entName := common.InterfaceToStr(v["company_name"])
- entPowerData := Mysql.SelectBySql("SELECT a.wait_empower_id,b.phone,b.name as entName FROM `entniche_power` a LEFT JOIN entniche_info b ON a.ent_id = b.id WHERE a.ent_id = ? AND a.ent_user_id = ? AND a.status = 1", entId, entUserId)
- if entPowerData != nil && len(*entPowerData) > 0 {
- //entName = common.InterfaceToStr((*entPowerData)[0]["entName"])
- newType = 2
- }
- if newType == 0 {
- //是否有商机管理权益
- entchineData := Mysql.SelectBySql("SELECT a.power,b.name as entName FROM `entniche_user` a LEFT JOIN entniche_info b ON a.ent_id = b.id WHERE a.ent_id = ? AND a.power = 1 AND a.phone = ?", entId, userPhone)
- if entchineData != nil && len(*entchineData) > 0 {
- //entName = common.InterfaceToStr((*entchineData)[0]["entName"])
- newType = 2
- } else {
- newType = 3
- }
- }
- data := map[string]interface{}{
- "entId": entId,
- "newAccountType": newType,
- //"customer": string(jsonBytes)
- }
- //查询该身份企业是否在客成
- entCusData := TiDb.FindOne("dwd_f_csm_customer_info", bson.M{"ent_id": entId, "is_transfer": 0, "account_type": 1}, "", "")
- if entCusData == nil || len(*entCusData) == 0 {
- log.Printf("查询主账号客户信息失败: entId:%d\n", entId)
- continue
- }
- data["customer"] = entCusData
- allMemberType = append(allMemberType, data)
- }
- //被踢出企业 以后 归集表还没有处理
- if newType == 0 && (accountType == 51 || accountType == 52) && !isGroupCompany(cluename) {
- //是否是集团
- if cluename != "" {
- entCusData := TiDb.FindOne("dwd_f_csm_customer_info", bson.M{"company_name": cluename, "is_transfer": 0, "account_type": 1}, "", "")
- if entCusData == nil || len(*entCusData) == 0 {
- log.Printf("查询主账号客户信息失败: entId:%d\n", entId)
- continue
- }
- //查询该身份企业是否在客成
- data := map[string]interface{}{
- "ent_id": gconv.String((*entCusData)["ent_id"]),
- "newAccountType": 4,
- }
- data["customer"] = entCusData
- allMemberType = append(allMemberType, data)
- }
- }
- }
- case 3, 4: //说明改用身份下没有更高级的账号类型,只能是 3-4 4-3
- for _, vl := range *userEntData {
- entId := common.Int64All(vl["ent_id"])
- newType := 0
- if entId == 0 && common.IntAll(vl["type"]) == 0 { //个人身份
- continue
- }
- if entId == currentEntId {
- continue
- }
- entCusData := TiDb.FindOne("dwd_f_csm_customer_info", bson.M{"ent_id": entId, "is_transfer": 0, "account_type": 1}, "", "")
- if entCusData == nil || len(*entCusData) == 0 {
- log.Printf("查询主账号客户信息失败: entId:%d\n", entId)
- continue
- }
- //是否是集团
- if common.InterfaceToStr(vl["company_name"]) != "" {
- if c := TiDb.CountBySql(`select count(1) from group_company_name where company_name=?`, common.InterfaceToStr(vl["company_name"])); c > 0 {
- newType = 3
- } else {
- newType = 4
- }
- //查询该身份企业是否在客成
- if newType != 0 {
- data := map[string]interface{}{
- "entId": entId,
- "newAccountType": newType,
- }
- data["customer"] = entCusData
- allMemberType = append(allMemberType, data)
- }
- }
- }
- }
- if len(allMemberType) > 0 {
- minEntry := findMinAccountType(allMemberType)
- return minEntry, nil
- }
- return nil, nil
- }
- func findMinAccountType(data []map[string]interface{}) (minItem map[string]interface{}) {
- minVal := 0
- found := false
- for _, item := range data {
- if val, ok := item["newAccountType"].(int); ok {
- if !found || val < minVal {
- minVal = val
- minItem = item
- found = true
- }
- }
- }
- return
- }
- // 子账号、免费账号移交到电销公海
- func kcToOpenSeaUpdate(clueId, kcpositionId, changeType int64, changeReason string) { //changeType 1:续约失败 2:退款 3:权限被企业删除
- nowTime := time.Now().Format(date.Date_Full_Layout)
- ok := TiDb.ExecTx("退出客成系统,进入电销公海", func(tx *sql.Tx) bool {
- updateClueMap := map[string]interface{}{
- "is_assign": 0,
- "updatetime": nowTime,
- "comeintime_open": nowTime,
- "position_id": 0,
- "seatNumber": nil,
- "is_transfer": 0,
- }
- if changeType == 3 { //权限被企业删除 销售线索来源更新为“其他-产品权限被企业删除”
- updateClueMap["top_cluetype"] = "532"
- updateClueMap["sub_cluetype"] = "949"
- }
- ok1 := TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClueMap)
- ok2 := TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
- "is_transfer": 1,
- "account_type": nil,
- "primary_id": nil,
- })
- in1 := TiDb.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": changeReason,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- in2 := TiDb.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": changeReason,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- })
- return ok1 && ok2 && in1 > 0 && in2 > 0
- })
- if !ok {
- log.Printf("子账号或免费账号移交销售失败:clueId:%d\n", clueId)
- }
- }
- // 从客成划转到另外一个客成
- func SubAndFreeAccountUpdate(data map[string]interface{}, entId, clueId int64, newAccountType, isGroup, isCommerce int, oldKcName string) {
- mainCusId := common.Int64All(data["id"])
- cusCompanyName := common.InterfaceToStr(data["company_name"])
- nowTime := time.Now().Format(date.Date_Full_Layout)
- kcPositionId := common.Int64All(data["position_id"])
- newKcName := common.InterfaceToStr(data["name"])
- ok := TiDb.ExecTx("更新线索、客成信息", func(tx *sql.Tx) bool {
- ok1 := TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
- "ent_id": entId,
- "company_name": cusCompanyName,
- "account_type": newAccountType,
- "primary_id": mainCusId,
- "transfertime": nowTime,
- //"service_starttime": data["service_starttime"],
- //"service_endtime": data["service_endtime"],
- "position_id": kcPositionId,
- "name": newKcName,
- "is_admin": 0,
- "is_renewal_protection": 0,
- "product_access": data["product_access"],
- "buy_subject": data["buy_subject"],
- })
- ok2 := TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
- "updatetime": nowTime,
- "cluename": cusCompanyName,
- "company_nature": isGroup,
- "company_verification": isCommerce,
- })
- AddClueChange(clueId, kcPositionId, "position_id", "客户成功经理", gconv.String(data["name"]), newKcName, nowTime, common.GetRandom(32), "", "")
- AddClueChange(clueId, kcPositionId, "", "成交客户移交", "", "移交至客户成功组", nowTime, common.GetRandom(32), "客成切换", "")
- return ok1 && ok2
- })
- if !ok {
- log.Printf("子账号或免费账号移交销售失败:clueId:%d\n", clueId)
- }
- }
- func ChangeClueNameToKc(uid string, clueName string) error {
- //查询需要移交的主账号客成信息
- mainCusData := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"company_name": clueName, "is_transfer": 0, "account_type": 1}, "", "")
- if mainCusData == nil || len(*mainCusData) == 0 {
- log.Printf("该线索名称没有主账号在客成,不需要移交,线索名称:%s", clueName)
- return nil
- }
- mainEntId := common.Int64All((*mainCusData)["ent_id"])
- mainKcPositionId := common.Int64All((*mainCusData)["position_id"])
- mainKcName := common.InterfaceToStr((*mainCusData)["name"])
- //判断线索是否在客成
- cusData := map[string]interface{}{}
- data := TiDb.SelectBySql("SELECT a.cluename,a.id,b.id as cusId,b.is_transfer FROM dwd_f_crm_clue_info a LEFT JOIN dwd_f_csm_customer_info b ON a.id = b.clue_id WHERE a.uid = ? AND b.is_transfer = 0", uid)
- if data != nil && len(*data) > 0 {
- cusData = (*data)[0]
- }
- cusId := common.Int64All(cusData["cusId"])
- clueId := common.Int64All(cusData["id"])
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- //判断之前该线索是否移交过客成
- if cusId != 0 { //该线索曾经移交过客成
- if common.IntAll(cusData["is_transfer"]) == 0 {
- return nil
- } else {
- up1 := TiDb.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": mainEntId,
- "company_name": clueName,
- "position_id": mainKcPositionId,
- "name": mainKcName,
- "is_admin": 0,
- "primary_id": cusId,
- "account_type": 4,
- "is_task": 0,
- })
- in := TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": mainKcPositionId,
- "change_type": "成交客户移交",
- "new_value": "移交至客户成功部",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- "change_reason": "留资公司名称与移交客成主账号线索名称一致",
- })
- up2 := TiDb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime})
- if up1 && up2 && in > 0 {
- return nil
- }
- return errors.New("更新客成信息出错")
- }
- } else {
- saveMap := map[string]interface{}{
- "clue_id": clueId,
- "transfertime": nowTime,
- "position_id": mainKcPositionId,
- "name": mainKcName,
- "ent_id": mainEntId,
- "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 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})
- updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": clueId,
- "position_id": mainKcPositionId,
- "change_type": "成交客户移交",
- "new_value": "移交至客户成功部",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": -1,
- "change_reason": "留资公司名称与移交客成主账号线索名称一致",
- })
- return cId > -1 && ok && updateId1 > -1
- }) {
- return nil
- }
- return errors.New("移交客成失败")
- }
- }
|