Przeglądaj źródła

Merge branch 'master' into feature/v1.5.31

yuelujie 3 miesięcy temu
rodzic
commit
23039e01b6
1 zmienionych plików z 125 dodań i 63 usunięć
  1. 125 63
      clueSync/kc.go

+ 125 - 63
clueSync/kc.go

@@ -701,7 +701,8 @@ func refundAuto() {
 	salesEmailData := map[int64][]map[string]interface{}{}
 	TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
 		for _, v := range *l {
-			saleId, cluename, company_nature, company_verification, uid, phone, userName := int64(0), "", 0, 0, "", "", ""
+			cluename, company_nature, company_verification, uid, phone, userName, oldSaleId, oldsaleName := "", 0, 0, "", "", "", int64(0), ""
+			saleId, newSeatNumber, newPerson := int64(0), "", ""
 			clueId := common.Int64All(v["clue_id"])
 			company_name := gconv.String(v["company_name"])
 			name := common.ObjToString(v["name"])
@@ -709,7 +710,12 @@ func refundAuto() {
 			isRenewalProtection := common.IntAll(v["is_renewal_protection"])
 			clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "name,phone,company_nature,company_verification,cluename,userid,position_id,uid", "")
 			if clueData != nil && len(*clueData) > 0 {
-				saleId, _ = FindPositionIdClueId(clueId)
+				saleId, newPerson, newSeatNumber = FindPositionIdClueId(clueId)
+				oldSaleId = common.Int64All((*clueData)["position_id"])
+				oldPersonData := FindPersonOne(oldSaleId)
+				if oldSaleId != 0 {
+					oldsaleName = gconv.String(oldPersonData["name"])
+				}
 				cluename = common.ObjToString((*clueData)["cluename"])
 				userName = gconv.String((*clueData)["name"])
 				company_nature = common.IntAll((*clueData)["company_nature"])
@@ -735,20 +741,12 @@ func refundAuto() {
 							"remrk":        "3天后即将移交",
 							"reason":       "成交客户续费失败",
 						})
-						/*salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
-							"company_name": company_name,
-							"phone":        phone,
-							"userName":     userName,
-							"remrk":        "1天后即将移交",
-							"reason":       "成交客户续费失败",
-							"cluename":     cluename,
-						})*/
 						continue
 					}
 				}
 
 			}
-			isFull := FindUpperLimit(gconv.String(saleId), "", false)
+			isFull := 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 `, uid)
 			if myOrders != nil {
@@ -861,11 +859,7 @@ func refundAuto() {
 						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)
@@ -873,51 +867,49 @@ func refundAuto() {
 										isOks := false
 										for _, vv := range *adata {
 											position_ids := common.Int64All(vv["position_id"])
-											seatNumber := common.ObjToString(vv["seatNumber"])
+											newSeatNumber = 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)
-												}
+												newPerson = gconv.String((*saleDatas)["name"])
 												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 len(*sdata) == 0 {
+												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 = 8`, 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)
-												}
+												newSeatNumber = common.ObjToString((*sdata)[0]["seat_number"])
+												newPerson = gconv.String((*sdata)[0]["name"])
 											}
 										}
 									}
 								} 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)
+									sdata := &[]map[string]interface{}{}
+									//查看当前是否属于一部三部
+									userData := FindPersonOne(saleId)
+									if userData != nil {
+										deptName := gconv.String(userData["dept_name"])
+										if strings.Contains(deptName, "一部") {
+											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 len(*sdata) == 0 {
+												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 = 8`, saleId)
+											}
+										} else if strings.Contains(deptName, "三部") {
+											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 = 8`, 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)
-										}
+										newSeatNumber = common.ObjToString((*sdata)[0]["seat_number"])
+										newPerson = common.ObjToString((*sdata)[0]["name"])
 									}
 								}
 							}
-						} 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)
-							}
 						}
 						if kcposition_id > 0 {
 							mailData[name] = append(mailData[name], map[string]interface{}{
@@ -928,6 +920,7 @@ func refundAuto() {
 								"reason":       "成交客户续费失败",
 							})
 						}
+						isFull = FindUpperLimit(gconv.String(saleId), "", false)
 						if saleId > 0 {
 							salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
 								"company_name": company_name,
@@ -937,6 +930,50 @@ func refundAuto() {
 								"reason":       "成交客户续费失败",
 								"cluename":     cluename,
 							})
+							if saleId != oldSaleId {
+								//新增个人员变更
+								TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
+									"clue_id":      clueId,
+									"position_id":  saleId,
+									"change_field": "position_id",
+									"change_type":  "所属人变更",
+									"old_value":    common.If(oldsaleName != "", oldsaleName, "/"),
+									"new_value":    common.If(newPerson != "", newPerson, "/"),
+									"createtime":   nowTime,
+									"BCPCID":       common.GetRandom(32),
+									"operator_id":  -1,
+								})
+								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, newSeatNumber, 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,is_assign=1 WHERE id = ?`, nowTime, saleId, newSeatNumber, 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,is_assign=1 WHERE id = ?`, nowTime, clueId)
+								}
+							}
+						} else if saleId == 0 {
+							//找不到人
+							TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=0,seatNumber="",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=0 WHERE id = ?`, nowTime, nowTime, clueId)
+							if oldSaleId != 0 {
+								//新增个人员变更
+								TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
+									"clue_id":      clueId,
+									"position_id":  saleId,
+									"change_field": "position_id",
+									"change_type":  "所属人变更",
+									"old_value":    common.If(oldsaleName != "", oldsaleName, "/"),
+									"new_value":    common.If(newPerson != "", newPerson, "/"),
+									"createtime":   nowTime,
+									"BCPCID":       common.GetRandom(32),
+									"operator_id":  -1,
+									"SHUOMING":     "业绩归属查不到人",
+								})
+								//再做一个记录变更
+							}
 						}
 						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
 							"clue_id":      clueId,
@@ -949,15 +986,17 @@ func refundAuto() {
 							"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,
-						})
+						if saleId > 0 {
+							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),
@@ -1013,7 +1052,7 @@ func refundAuto() {
 		}
 		return true
 	}, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where is_transfer = 0`)
-	//}, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where clue_id =1473919`)
+	//}, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where clue_id =1189082`)
 
 	//移交电销提醒
 	for i, v := range mailData {
@@ -1351,7 +1390,7 @@ func CustomerChangeHandle(data map[string]interface{}) map[string]interface{} {
 	phone = common.ObjToString((*clueData)["phone"])
 	userName = gconv.String((*clueData)["name"])
 	//positionId := gconv.Int64((*clueData)["position_id"])
-	positionId, _ := FindPositionIdClueId(clueId)
+	positionId, _, _ := FindPositionIdClueId(clueId)
 	clueName = gconv.String((*clueData)["cluename"])
 	customData := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{
 		"clue_id": clueId,
@@ -1370,13 +1409,15 @@ func CustomerChangeHandle(data map[string]interface{}) map[string]interface{} {
 		"companyName": companyName,
 	}
 }
-func FindPositionIdClueId(clueId int64) (int64, string) {
+
+// 查找订单实际业绩归属
+func FindPositionIdClueId(clueId int64) (int64, string, string) {
 	querySql := `
 	SELECT
-		e.username as salesperson ,c.position_id
+		e.username as salesperson ,c.position_id,f.dept_name,c.seat_number
 	FROM
 		dwd_f_crm_clue_info a
-		INNER JOIN dwd_f_userbase_order_info b ON a.id = ?
+INNER JOIN dwd_f_userbase_order_info b ON a.id = ?
 		AND a.uid = b.uid 
 		AND (
 			b.payable_money > 0 
@@ -1388,17 +1429,24 @@ func FindPositionIdClueId(clueId int64) (int64, string) {
 		AND ( b.refund_status != 1 OR b.refund_status IS NULL ) 
 		AND b.vip_starttime < "2099-01-01" 
 		AND b.product_type IN ( "大会员", "企业商机管理" ) 
-		AND ( b.order_change = 0 OR ( b.order_change = 0 AND DATEDIFF( b.vip_endtime, b.vip_starttime )> 90 ) ) 
+		AND ( b.order_change != 0 or  b.order_change  is  NULL OR ( b.order_change = 0 AND DATEDIFF( b.vip_endtime, b.vip_starttime )> 90 ) ) 
 		inner JOIN  jianyu.order_sale_record d on  b.order_code=d.ordercode and  d.state=1 
 		INNER JOIN cadmin.admin_user e   on  d.saler_Id = e.id
 		INNER JOIN   dwd_f_crm_personnel_management c   on c.ent_id =?  and    e.username=c.name 
+		inner  join dwd_d_crm_department_level_succbi  f  on   c.position_id=f.position_id
 	ORDER BY
-		b.vip_endtime desc ,b.payable_money DESC limit  1`
+		b.vip_endtime desc ,b.payable_money DESC `
 	clueData := TiDb.SelectBySql(querySql, clueId, db.EntId)
 	if clueData == nil || len(*clueData) == 0 {
-		return 0, ""
+		return 0, "", ""
 	}
-	return gconv.Int64((*clueData)[0]["position_id"]), gconv.String((*clueData)[0]["salesperson"])
+	for _, m := range *clueData {
+		deptName := gconv.String(m["dept_name"])
+		if strings.Contains(deptName, "一部") || strings.Contains(deptName, "三部") {
+			return gconv.Int64(m["position_id"]), gconv.String(m["salesperson"]), gconv.String(m["seat_number"])
+		}
+	}
+	return 0, "", ""
 }
 func refundAutoHistory() {
 	data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1)
@@ -1479,9 +1527,7 @@ func PhoneToPositionId(phone string) int64 {
 }
 func kcClue(userId, uId, seatNumber, cluename, top_cluetype, sub_cluetype,
 	name, phone, position, sourceCode, industry, follow_project_area,
-	role, item, subname, topname, remark, demand, department, departments, saleName, source string,
-	positionId int64, isGroup, isCommerce int,
-	keywords []string) bool {
+	role, item, subname, topname, remark, demand, department, departments, saleName, source string, positionId int64, isGroup, isCommerce int, keywords []string) bool {
 	clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
 	nowTime := time.Now().Format("2006-01-02 15:04:05")
 	nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
@@ -1754,7 +1800,7 @@ func ExitKcSend(personName string, infoList []map[string]interface{}) {
 
 // 客成邮件   订单信息初始化
 func KcOrderFormat(orderCode string, bigmemberService, combo map[int64]string) *OrderInfo {
-	orderData := Mysql.SelectBySql(`SELECT a.id,a.order_code,a.pay_money,a.user_phone,a.vip_starttime,a.vip_endtime,a.product_type,a.filter,a.user_id,a.ent_id,a.company_name,a.create_person,SUM(b.return_money) AS return_money 
+	orderData := Mysql.SelectBySql(`SELECT a.id,a.order_code,a.pay_money,a.user_phone,a.vip_starttime,a.vip_endtime,a.product_type,a.filter,a.user_id,a.ent_id,a.company_name,a.create_person,SUM(b.return_money) AS return_money
 		FROM jianyu.dataexport_order a
 	LEFT JOIN return_money_record b ON (a.order_code=b.order_code)
 	WHERE a.order_code=? AND a.order_status=1 AND (a.product_type='大会员' OR a.product_type='大会员-子账号' or  a.product_type = "企业商机管理")
@@ -1857,3 +1903,19 @@ func SaleMail(positionId int64) string {
 	}
 	return to
 }
+func FindPersonOne(positionId int64) map[string]interface{} {
+	personData := TiDb.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]
+}