|
@@ -2,46 +2,75 @@ package service
|
|
|
|
|
|
import (
|
|
|
"app.yhyue.com/moapp/jybase/common"
|
|
|
+ "app.yhyue.com/moapp/jybase/date"
|
|
|
"bp.jydev.jianyu360.cn/BaseService/biService/entity"
|
|
|
"bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb"
|
|
|
+ "database/sql"
|
|
|
"errors"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
+ "log"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+type RelatedClue struct {
|
|
|
+ ClueId int64
|
|
|
+ CompanyName string
|
|
|
+ EntId int64
|
|
|
+ KcPositionId int64
|
|
|
+ KcName string
|
|
|
+ MainCusId int64
|
|
|
+}
|
|
|
+
|
|
|
func UpdateClueName(in *pb.UpdateCompanyNameReq) error {
|
|
|
newIsGroup, _ := CompanyType(in.NewCompanyName)
|
|
|
oldIsGroup, _ := CompanyType(in.OldCompanyName)
|
|
|
|
|
|
- mainData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": in.MainClueId, "is_transfer": 0, "primary_id": 1}, "", "")
|
|
|
+ mainData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": in.MainClueId, "is_transfer": 0, "account_type": 1}, "", "")
|
|
|
if mainData == nil || len(*mainData) == 0 {
|
|
|
return errors.New("主账号没有移交客成")
|
|
|
}
|
|
|
mainCusId := common.Int64All((*mainData)["id"])
|
|
|
+ mainKcPositionId := common.Int64All((*mainData)["position_id"])
|
|
|
+ mainKcName := common.InterfaceToStr((*mainData)["name"])
|
|
|
//把主账号、子账号和企业组织架构免费账号修改线索名称
|
|
|
//更新主账号线索、客成信息
|
|
|
+ nowTime := time.Now().Format("2006-01-02 15:04:05")
|
|
|
entity.JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": in.MainClueId}, map[string]interface{}{"cluename": in.NewCompanyName})
|
|
|
entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": in.MainClueId}, map[string]interface{}{"company_name": in.NewCompanyName, "ent_id": in.NewEntId})
|
|
|
+ entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
|
|
|
+ "clue_id": in.MainClueId,
|
|
|
+ "change_field": "cluename",
|
|
|
+ "change_type": "基本信息变更",
|
|
|
+ "old_value": common.If(in.OldCompanyName != "", in.OldCompanyName, "/"),
|
|
|
+ "new_value": common.If(in.NewCompanyName != "", in.NewCompanyName, "/"),
|
|
|
+ "createtime": nowTime,
|
|
|
+ "BCPCID": common.GetRandom(32),
|
|
|
+ "position_id": mainKcPositionId,
|
|
|
+ "operator_id": -1,
|
|
|
+ "change_reason": "主账号修改线索名称",
|
|
|
+ })
|
|
|
//子账号和企业组织架构免费账号修改线索名称
|
|
|
subAndFreeData := entity.JyBiTidb.SelectBySql("SELECT * FROM dwd_f_csm_customer_info WHERE primary_id = ? AND is_transfer = 0 AND (account_type=2 OR account_type=3)", mainCusId)
|
|
|
if subAndFreeData != nil && len(*subAndFreeData) > 0 {
|
|
|
- nowTime := time.Now().Format("2006-01-02 15:04:05")
|
|
|
for _, val := range *subAndFreeData {
|
|
|
//更新线索、客成信息
|
|
|
kcPositionId := common.Int64All(val["position_id"])
|
|
|
subClueId := common.Int64All(val["clue_id"]) //子账号或者企业组织架构免费账号线索id
|
|
|
- entity.JyBiTidb.UpdateOrDeleteBySql("UPDATE dwd_f_crm_clue_info SET cluename = ? WHERE id = ?", subClueId)
|
|
|
- entity.JyBiTidb.UpdateOrDeleteBySql("UPDATE dwd_f_csm_customer_info SET company_name = ?,ent_id=? WHERE clue_id = ?")
|
|
|
+ entity.JyBiTidb.UpdateOrDeleteBySql("UPDATE dwd_f_crm_clue_info SET cluename = ? WHERE id = ?", in.NewCompanyName, subClueId)
|
|
|
+ entity.JyBiTidb.UpdateOrDeleteBySql("UPDATE dwd_f_csm_customer_info SET company_name = ?,ent_id=? WHERE clue_id = ?", in.NewCompanyName, in.NewEntId, subClueId)
|
|
|
//插入变更记录
|
|
|
entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
|
|
|
- "clue_id": subClueId,
|
|
|
- "change_field": "cluename",
|
|
|
- "change_type": "基本信息变更",
|
|
|
- "old_value": common.If(in.OldCompanyName != "", in.OldCompanyName, "/"),
|
|
|
- "new_value": common.If(in.NewCompanyName != "", in.NewCompanyName, "/"),
|
|
|
- "createtime": nowTime,
|
|
|
- "BCPCID": common.GetRandom(32),
|
|
|
- "position_id": kcPositionId,
|
|
|
- "operator_id": -1})
|
|
|
+ "clue_id": subClueId,
|
|
|
+ "change_field": "cluename",
|
|
|
+ "change_type": "基本信息变更",
|
|
|
+ "old_value": common.If(in.OldCompanyName != "", in.OldCompanyName, "/"),
|
|
|
+ "new_value": common.If(in.NewCompanyName != "", in.NewCompanyName, "/"),
|
|
|
+ "createtime": nowTime,
|
|
|
+ "BCPCID": common.GetRandom(32),
|
|
|
+ "position_id": kcPositionId,
|
|
|
+ "operator_id": -1,
|
|
|
+ "change_reason": "与主账号线索名称保持一致",
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -49,32 +78,201 @@ func UpdateClueName(in *pb.UpdateCompanyNameReq) error {
|
|
|
if !oldIsGroup {
|
|
|
//查非集团免费账号
|
|
|
freeData := entity.JyBiTidb.SelectBySql("SELECT * FROM dwd_f_csm_customer_info WHERE primary_id = ? AND account_type = 4 AND is_transfer = 0", mainCusId)
|
|
|
- if freeData != nil && len(*freeData) > 0 {
|
|
|
+ if freeData != nil && len(*freeData) > 0 { //查企业下是否存在其他主账号存在客成
|
|
|
oldOtherCus := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"company_name": in.OldCompanyName, "is_transfer": 0, "primary_id": 1}, "", "")
|
|
|
if oldOtherCus != nil && len(*oldOtherCus) > 0 {
|
|
|
+ updateMap := map[string]interface{}{
|
|
|
+ "primary_id": (*oldOtherCus)["id"],
|
|
|
+ "company_name": (*oldOtherCus)["company_name"],
|
|
|
+ "ent_id": (*oldOtherCus)["ent_id"],
|
|
|
+ }
|
|
|
//把非集团免费账号移交另外的主账号下
|
|
|
+ queryMap := map[string]interface{}{
|
|
|
+ "primary_id": mainCusId,
|
|
|
+ "account_type": 4,
|
|
|
+ "is_transfer": 0,
|
|
|
+ }
|
|
|
+ entity.JyBiTidb.Update("dwd_f_csm_customer_info", queryMap, updateMap)
|
|
|
|
|
|
} else {
|
|
|
//把非集团免费账号移交电销
|
|
|
- /*data := entity.JyBiTidb.SelectBySql("SELECT * FROM dwd_f_csm_customer_info WHERE primary_id = ? AND is_transfer = 0", mainCusId)
|
|
|
- if data != nil && len(*data) > 0 {
|
|
|
- for _, val := range *data {
|
|
|
- //移交电销
|
|
|
- }
|
|
|
- }*/
|
|
|
+ for _, val := range *freeData {
|
|
|
+ //移交电销
|
|
|
+ clueId := common.Int64All(val["clue_id"])
|
|
|
+ kcPositionId := common.Int64All(val["position_id"])
|
|
|
+ kcName := common.InterfaceToStr(val["name"])
|
|
|
+ FreeClueToDx(clueId, kcPositionId, kcName)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if !oldIsGroup && !newIsGroup {
|
|
|
+ if !newIsGroup {
|
|
|
//把线索名称是新企业名称的线索移交客成
|
|
|
- }
|
|
|
-
|
|
|
- if oldIsGroup && !newIsGroup {
|
|
|
- //关联线索处理
|
|
|
+ //(2)产生1条“成交客户移交”的线索动态,备注:移交至客户成功部;
|
|
|
+ //(3)不加入任务车;
|
|
|
//(4)该线索的客户成功经理,由空调整为其关联客户的客户成功经理,并新增1条“客户成功经理变更”的线索动态;
|
|
|
//(5)“线索归属”标记为“客户成功部”。
|
|
|
+ newData := entity.JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": in.NewCompanyName, "is_transfer": 0}, "", "", -1, -1)
|
|
|
+ if newData != nil && len(*newData) > 0 {
|
|
|
+ for _, val := range *newData {
|
|
|
+ clueEntity := RelatedClue{
|
|
|
+ ClueId: common.Int64All(val["id"]),
|
|
|
+ CompanyName: common.InterfaceToStr(val["cluename"]),
|
|
|
+ EntId: in.NewEntId,
|
|
|
+ KcPositionId: mainKcPositionId,
|
|
|
+ KcName: mainKcName,
|
|
|
+ MainCusId: mainCusId,
|
|
|
+ }
|
|
|
+ clueEntity.RelatedClueToKc()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (rc RelatedClue) RelatedClueToKc() {
|
|
|
+ //判断之前该线索是否移交过客成
|
|
|
+ nowTime := time.Now().Format("2006-01-02 15:04:05")
|
|
|
+ cusData := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": rc.ClueId}, "is_transfer", "")
|
|
|
+ if cusData != nil && len(*cusData) > 0 { //该线索曾经移交过客成
|
|
|
+ entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": rc.ClueId}, bson.M{
|
|
|
+ "is_transfer": 0,
|
|
|
+ "is_renewal_protection": 0,
|
|
|
+ "transfertime": nowTime,
|
|
|
+ "ent_id": rc.EntId,
|
|
|
+ "company_name": rc.CompanyName,
|
|
|
+ "position_id": rc.KcPositionId,
|
|
|
+ "name": rc.KcName,
|
|
|
+ "is_admin": 0,
|
|
|
+ "primary_id": rc.MainCusId,
|
|
|
+ "account_type": 4,
|
|
|
+ "is_task": 0,
|
|
|
+ })
|
|
|
+ entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
|
|
|
+ "clue_id": rc.ClueId,
|
|
|
+ "position_id": rc.KcPositionId,
|
|
|
+ "change_type": "成交客户移交",
|
|
|
+ "new_value": "移交至客户成功部",
|
|
|
+ "createtime": nowTime,
|
|
|
+ "BCPCID": common.GetRandom(32),
|
|
|
+ "operator_id": -1,
|
|
|
+ "change_reason": "与主账号线索名称保持一致",
|
|
|
+ })
|
|
|
+ entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
|
|
|
+ "clue_id": rc.ClueId,
|
|
|
+ "position_id": common.If(rc.KcPositionId > 0, rc.KcPositionId, -1),
|
|
|
+ "change_field": "position_id",
|
|
|
+ "change_type": "客户成功经理",
|
|
|
+ "old_value": "/",
|
|
|
+ "new_value": rc.KcName,
|
|
|
+ "createtime": nowTime,
|
|
|
+ "BCPCID": common.GetRandom(32),
|
|
|
+ "operator_id": -1,
|
|
|
+ })
|
|
|
+ entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": rc.ClueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime})
|
|
|
+
|
|
|
+ } else {
|
|
|
+ saveMap := map[string]interface{}{
|
|
|
+ "clue_id": rc.ClueId,
|
|
|
+ "transfertime": nowTime,
|
|
|
+ "position_id": rc.KcPositionId,
|
|
|
+ "name": rc.KcName,
|
|
|
+ "ent_id": rc.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": rc.CompanyName,
|
|
|
+ "primary_id": rc.MainCusId,
|
|
|
+ "account_type": 4,
|
|
|
+ }
|
|
|
+ cId, ok := int64(-1), false
|
|
|
+ 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": rc.ClueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime})
|
|
|
+ ok1 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
|
|
|
+ "clue_id": rc.ClueId,
|
|
|
+ "position_id": rc.KcPositionId,
|
|
|
+ "change_type": "成交客户移交",
|
|
|
+ "new_value": "移交至客户成功部",
|
|
|
+ "createtime": nowTime,
|
|
|
+ "BCPCID": common.GetRandom(32),
|
|
|
+ "operator_id": -1,
|
|
|
+ "change_reason": "与主账号线索名称保持一致",
|
|
|
+ })
|
|
|
+ ok2 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
|
|
|
+ "clue_id": rc.ClueId,
|
|
|
+ "position_id": common.If(rc.KcPositionId > 0, rc.KcPositionId, -1),
|
|
|
+ "change_field": "position_id",
|
|
|
+ "change_type": "客户成功经理",
|
|
|
+ "old_value": "/",
|
|
|
+ "new_value": rc.KcName,
|
|
|
+ "createtime": nowTime,
|
|
|
+ "BCPCID": common.GetRandom(32),
|
|
|
+ "operator_id": -1,
|
|
|
+ })
|
|
|
+ return cId > -1 && ok && ok1 > -1 && ok2 > -1
|
|
|
+ }) {
|
|
|
+ log.Printf("非集团公司关联线索移交客成失败,线索id:%d", rc.ClueId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func FreeClueToDx(clueId, kcPositionId int64, kcName string) {
|
|
|
+ nowTime := time.Now().Format(date.Date_Full_Layout)
|
|
|
+ updateClueMap := map[string]interface{}{
|
|
|
+ "is_assign": 0,
|
|
|
+ "updatetime": nowTime,
|
|
|
+ "comeintime_open": nowTime,
|
|
|
+ "position_id": 0,
|
|
|
+ "seatNumber": "",
|
|
|
+ "is_transfer": 0,
|
|
|
+ }
|
|
|
+
|
|
|
+ entity.JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClueMap)
|
|
|
+ entity.JyBiTidb.Update("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": "",
|
|
|
+ })
|
|
|
+ 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,
|
|
|
+ })
|
|
|
+ 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,
|
|
|
+ })
|
|
|
+ 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,
|
|
|
+ "change_reason": "主账号修改线索名称,该线索原是非集团免费账号",
|
|
|
+ })
|
|
|
+}
|