Jelajahi Sumber

客成发邮件处理

WH01243 6 bulan lalu
induk
melakukan
603f8f73d6
2 mengubah file dengan 413 tambahan dan 18 penghapusan
  1. 413 16
      clueSync/kc.go
  2. 0 2
      clueSync/main.go

+ 413 - 16
clueSync/kc.go

@@ -611,6 +611,7 @@ func refundAuto() {
 	findNowTime := time.Now().AddDate(0, 0, -db.ExpirationPeriod).Format(date.Date_Full_Layout)
 	nowTime := time.Now().Format(date.Date_Full_Layout)
 	mailData := map[string][]map[string]interface{}{}
+	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, "", "", ""
@@ -644,14 +645,14 @@ func refundAuto() {
 							"userName":     userName,
 							"remrk":        "1天后即将移交",
 							"reason":       "成交客户续费失败",
-					})
-					salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
-						"company_name": company_name,
-						"phone":        phone,
-						"userName":     userName,
-						"remrk":        "1天后即将移交",
-						"reason":       "成交客户续费失败",
-						"cluename":     cluename,
+						})
+						salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
+							"company_name": company_name,
+							"phone":        phone,
+							"userName":     userName,
+							"remrk":        "1天后即将移交",
+							"reason":       "成交客户续费失败",
+							"cluename":     cluename,
 						})
 						continue
 					}
@@ -762,14 +763,14 @@ func refundAuto() {
 									"remrk":        "1天后即将移交",
 									"reason":       "成交客户续费失败",
 								})
-									salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
-										"company_name": company_name,
-										"phone":        phone,
-										"userName":     userName,
-										"remrk":        "1天后即将移交",
-										"reason":       "成交客户续费失败",
-										"cluename":     cluename,
-									})
+								salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
+									"company_name": company_name,
+									"phone":        phone,
+									"userName":     userName,
+									"remrk":        "1天后即将移交",
+									"reason":       "成交客户续费失败",
+									"cluename":     cluename,
+								})
 								isOk = true
 							} else if time.Now().Unix()-vip_endtimes.Unix() < db.HandoverCycle*86400 {
 								isOk = true
@@ -929,9 +930,405 @@ func refundAuto() {
 	for i, v := range mailData {
 		ExitKcSend(i, v)
 	}
+	//移交电销销售提醒
+	for i, v := range salesEmailData {
+		ExitKcSaleSend(i, v)
+	}
 	log.Println("自动移交销售定时任务结束")
 }
 
+// 查找一部三部人员信息 返回人员信息 以及管理员信息
+func FindDeptAdmin(positionId int64) (map[string]interface{}, map[string]interface{}, map[string]interface{}) {
+	//先判断那个部门的
+	userData1 := TiDb.SelectBySql(`SELECT
+	a.position_id,
+	a.name,
+	b.seat_number,
+	b.role_id,
+	a.bi_pcode,
+	a.dept_name,
+	a.resign
+FROM
+			(SELECT *
+		FROM dwd_d_crm_department_level_succbi u1
+		WHERE id = (
+			SELECT MAX(id)
+			FROM dwd_d_crm_department_level_succbi u2
+			WHERE  u1.position_id = u2.position_id		
+		)
+		ORDER BY id desc) a
+		INNER JOIN (SELECT *
+			FROM dwd_f_crm_personnel_management u1
+			WHERE id = (
+				SELECT MAX(id)
+				FROM dwd_f_crm_personnel_management u2
+				WHERE u1.position_id = u2.position_id		
+			)
+			ORDER BY id desc) b ON  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,
+	a.resign
+FROM
+			(SELECT *
+		FROM dwd_d_crm_department_level_succbi u1
+		WHERE id = (
+			SELECT MAX(id)
+			FROM dwd_d_crm_department_level_succbi u2
+			WHERE  u1.position_id = u2.position_id		
+		)
+		ORDER BY id desc) a
+			INNER JOIN (SELECT *
+		FROM dwd_f_crm_personnel_management u1
+		WHERE id = (
+			SELECT MAX(id)
+			FROM dwd_f_crm_personnel_management u2
+			WHERE u1.position_id = u2.position_id		
+		)
+		ORDER BY id desc) b ON 
+	a.dept_name LIKE "%销售三部"
+	AND a.position_id = b.position_id`)
+	if userData3 == nil || len(*userData3) == 0 || userData1 == nil || len(*userData1) == 0 {
+		log.Println("找不到一部三部员工信息")
+		return nil, nil, nil
+	}
+	//一部高级管理员信息
+	seniorAdmin1 := map[string]interface{}{}
+	//三部高级管理员信息
+	seniorAdmin3 := map[string]interface{}{}
+	//一部管理员信息
+	admin1 := map[string]map[string]interface{}{}
+
+	//返回当前人信息
+	admin := map[string]interface{}{}
+	for _, v := range *userData3 {
+		v["type"] = 3
+		id := gconv.Int64(v["position_id"])
+		roleId := gconv.Int64(v["role_id"])
+		resign := gconv.Int64(v["resign"])
+		if id == positionId && resign == 0 {
+			admin = v
+		}
+		if roleId == 8 {
+			v["orderStatus"] = 2
+			seniorAdmin3 = v
+		}
+	}
+	for _, v := range *userData1 {
+		v["type"] = 1
+		id := gconv.Int64(v["position_id"])
+		roleId := gconv.Int64(v["role_id"])
+		dept_name := gconv.String(v["dept_name"])
+		resign := gconv.Int64(v["resign"])
+		if id == positionId && resign == 0 {
+			admin = v
+		}
+		if roleId == 8 {
+			v["orderStatus"] = 2
+			seniorAdmin1 = v
+		} else if roleId == 3 {
+			admin1[dept_name] = v
+		}
+	}
+	//查看当前人属于哪个部门
+	if admin == nil {
+		//当前人不属于电销部的
+		admin = *(TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{
+			"position_id": positionId,
+			"resign":      1,
+		}, "name", ""))
+	}
+	positiontype := gconv.Int64(admin["type"])
+	deptName := gconv.String(admin["dept_name"])
+	switch positiontype {
+	case 1:
+		return admin, admin1[deptName], seniorAdmin1
+	case 3:
+		return admin, nil, seniorAdmin3
+	default:
+		return admin, nil, nil
+	}
+}
+
+// 邮箱获取
+func GetMail(personName string) (string, string) {
+	data := Mysql.SelectBySql(`select  name,mail,img from   entniche_user  where  ent_id =?`, db.EntId)
+	if data == nil || len(*data) == 0 {
+		return "", ""
+	}
+	for _, m := range *data {
+		name := gconv.String(m["name"])
+		if name == personName {
+			//
+			return gconv.String(m["mail"]), gconv.String(m["img"])
+		}
+	}
+	return "", ""
+}
+
+// 退客成进销售 销售收邮件
+func ExitKcSaleSend(positionId int64, infoList []map[string]interface{}) {
+	person, admin, seniorAdmin := FindDeptAdmin(positionId)
+	personMail := ""
+	adminMail := ""
+	seniorAdminMail := ""
+	if person != nil {
+		personMail, _ = GetMail(gconv.String(person["name"]))
+	}
+	if admin != nil {
+		adminMail, _ = GetMail(gconv.String(admin["name"]))
+	}
+	if seniorAdmin != nil {
+		seniorAdminMail, _ = GetMail(gconv.String(seniorAdmin["name"]))
+	}
+	//退出客成 即将退出客成信息  销售信息编辑
+	gmail := &mail.GmailAuth{
+		SmtpHost: db.Mail.SmtpHost,
+		SmtpPort: db.Mail.SmtpPort,
+		User:     db.Mail.User,
+		Pwd:      db.Mail.Pwd,
+	}
+	//正文拼接
+	startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:</p><table><thead><tr><th>序号</th><th>线索名称</th><th>联系人</th><th>姓名</th><th>移交状态</th><th>移交销售原因</th></tr></thead><tbody>`
+	for i, v := range infoList {
+		startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["cluename"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["remrk"]), gconv.String(v["reason"]))
+	}
+	endStr := `</tbody></table><p></body></html>`
+	startStr += endStr
+	to := ""
+	cc := ""
+	if personMail != "" {
+		to = personMail
+		if adminMail != "" && seniorAdminMail != "" {
+			if adminMail != seniorAdminMail {
+				cc = fmt.Sprintf("%s|%s", adminMail, seniorAdminMail)
+			} else {
+				cc = adminMail
+			}
+		} else if adminMail != "" {
+			cc = adminMail
+		} else if seniorAdminMail != "" {
+			cc = seniorAdminMail
+		}
+	} else {
+		if adminMail != "" {
+			to = adminMail
+			if seniorAdminMail != "" {
+				cc = seniorAdminMail
+			}
+		} else {
+			cc = seniorAdminMail
+
+		}
+	}
+	if to != "" {
+		status := mail.GSendMail_q("剑鱼标讯", to, cc, "", "成交客户续费失败移交销售通知", startStr, "", "", gmail)
+		if status {
+			log.Println("客成发邮件 send mail success", startStr, to, cc)
+		}
+	}
+}
+
+// 客成人员变更发邮件
+func CustomerChange() {
+	personMap := map[int64]string{}
+	personData := TiDb.SelectBySql(`select  name,position_id  from  dwd_d_crm_department_level_succbi where  ent_id=? and  resign=0   and  position_id>0`, db.EntId)
+	if personData != nil && len(*personData) > 0 {
+		for _, v := range *personData {
+			personMap[gconv.Int64(v["position_id"])] = gconv.String(v["name"])
+		}
+	}
+	startTime := cfg.LastKcChangeTime
+	if startTime == "" {
+		startTime = time.Now().Format(time.DateTime)
+	}
+	customMap1 := map[string][]map[string]interface{}{}
+	customMap2 := map[string][]map[string]interface{}{}
+	saleMap := map[int64][]map[string]interface{}{}
+	saleNobodyCustomMapMap := map[int64][]map[string]interface{}{}
+	TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
+		for _, v := range *l {
+			startTime = gconv.String(v["createtime"])
+			data := CustomerChangeHandle(v)
+			oldPerson := gconv.String(data["oldPerson"])
+			newPerson := gconv.String(data["newPerson"])
+			positionId := gconv.Int64(data["positionId"])
+			operatorId := gconv.Int64(data["operatorId"])
+			if oldPerson == "/" {
+				data["oldPerson"] = "-"
+			}
+			if positionId > 0 {
+				data["positionName"] = personMap[positionId]
+			}
+			if operatorId > 0 {
+				data["operatorName"] = personMap[operatorId]
+			}
+			if (oldPerson == "" || oldPerson == "/") && newPerson != "" && newPerson != "/" && positionId > 0 {
+				//之前没有可成人员
+				saleNobodyCustomMapMap[positionId] = append(saleNobodyCustomMapMap[positionId], data)
+			} else if newPerson != "" && newPerson != "/" && oldPerson != "" && oldPerson != "/" && positionId > 0 {
+				//之前有客成
+				saleMap[positionId] = append(saleMap[positionId], data)
+			}
+			if newPerson != "" && newPerson != "/" {
+				customMap2[newPerson] = append(customMap2[newPerson], data)
+			}
+			if oldPerson != "" && oldPerson != "/" {
+				customMap1[oldPerson] = append(customMap1[oldPerson], data)
+			}
+		}
+		return true
+	}, `select operator_id, clue_id,position_id,old_value,new_value from   dwd_f_crm_clue_change_record  where    createtime>?  and change_type="客户成功经理"  and  operator_id>0  order by  createtime`, startTime)
+	if len(customMap1) > 0 {
+		ChangeSendKc(customMap1, 1)
+	}
+	if len(customMap2) > 0 {
+		ChangeSendKc(customMap1, 2)
+	}
+	if len(saleMap) > 0 {
+		ChangeSendSale(saleMap, 2)
+	}
+	if len(saleNobodyCustomMapMap) > 0 {
+		ChangeSendSale(saleNobodyCustomMapMap, 1)
+	}
+	cfg.LastKcChangeTime = startTime
+	common.WriteSysConfig(&cfg)
+}
+
+// 客户成功经理变更邮件提醒客成人员
+func ChangeSendKc(data map[string][]map[string]interface{}, personType int64) {
+	for name, v := range data {
+		to, _ := GetMail(name)
+		//正文拼接
+		startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p></p><table><thead><tr><th>序号</th><th>公司名称</th><th>联系人</th><th>姓名</th><th>变更前客户经理</th><th>变更后客户经理</th><th>操作人</th></tr></thead><tbody>`
+		for i, v := range v {
+			startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["companyName"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["oldPerson"]), gconv.String(v["newPerson"]), gconv.String(v["operatorName"]))
+		}
+		endStr := `</tbody></table><p></body></html>`
+		startStr += endStr
+		if to != "" {
+			status := mail.GSendMail_q("剑鱼标讯", to, "", "", gconv.String(common.If(personType == 1, "您有新的客户,请及时跟进处理", "您的客户已转至其他客户成功经理负责")), startStr, "", "", &Gmail)
+			if status {
+				log.Println("客成发邮件 send mail success", startStr, to, "")
+			}
+		}
+	}
+}
+
+func ChangeSendSale(data map[int64][]map[string]interface{}, personType int64) {
+	if personType == 1 {
+		//客户成功经理从空变成有人
+		for _, v := range data {
+			for _, v1 := range v {
+				to, img := GetMail(gconv.String(v1["newPerson"]))
+				startStr := `<style> *,body,html{margin:10px;font-family:tahoma,arial,'Hiragino Sans GB','Microsoft YaHei',宋体,ans-serif;font-size:16px;}p{margin:15px;font-size:18px;}table{background-color: rgb(244, 244, 249);padding:5px 15px;border:solid 1px #ddd;margin: 20px 0px 20px 50px;vertical-align:top;display:inline-block;}.tit{width:120px;}td{padding: 5px;}.clear{clear: both;}</style>
+				<p>销售人员"%s"成交的客户已成功移交至客成,公司名称:%s,姓名:%s,手机号:%s,负责客户成功经理:%s</p>
+				<p>请引导客户添加客户成功经理企业微信:</p><div class='clear'>%s</div>`
+				endStr := fmt.Sprintf(startStr, gconv.String(v1["positionName"]), gconv.String(v1["clueName"]), gconv.String(v1["userName"]), gconv.String(v1["phone"]), gconv.String(v1["newPerson"]), common.If(img != "", fmt.Sprintf(`<img width="300" height="450" src="%s">`, img), ""))
+				if to != "" {
+					status := mail.GSendMail_q("剑鱼标讯", to, "", "", "成交客户已成功移交客成", endStr, "", "", &Gmail)
+					if status {
+						log.Println("客成发邮件 send mail success", startStr, to, "")
+					}
+				}
+			}
+		}
+		return
+	}
+	//客户成功经理从A变成了B
+	for positionId, v := range data {
+		person, admin, seniorAdmin := FindDeptAdmin(positionId)
+		personMail := ""
+		adminMail := ""
+		seniorAdminMail := ""
+		if person != nil {
+			personMail, _ = GetMail(gconv.String(person["name"]))
+		}
+		if admin != nil {
+			adminMail, _ = GetMail(gconv.String(admin["name"]))
+		}
+		if seniorAdmin != nil {
+			seniorAdminMail, _ = GetMail(gconv.String(seniorAdmin["name"]))
+		}
+		to := ""
+		cc := ""
+		if personMail != "" {
+			to = personMail
+			if adminMail != "" && seniorAdminMail != "" {
+				cc = adminMail
+			} else if seniorAdminMail != "" {
+				cc = seniorAdminMail
+			}
+		} else {
+			if adminMail != "" {
+				to = adminMail
+				if seniorAdminMail != "" {
+					cc = seniorAdminMail
+				}
+			} else {
+				cc = seniorAdminMail
+
+			}
+		}
+		//正文拼接
+		startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:
+		</p><table><thead><tr><th>序号</th><th>线索名称</th><th>联系人</th><th>姓名</th><th>变更前客户经理</th><th>变更后客户经理</th><th>销售人员</th></tr></thead><tbody>`
+		for i, v := range v {
+			startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["clueName"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["oldPerson"]), gconv.String(v["newPerson"]), gconv.String(v["positionName"]))
+		}
+		endStr := `</tbody></table><p></body></html>`
+		startStr += endStr
+		if to != "" {
+			status := mail.GSendMail_q("剑鱼标讯", to, cc, "", "成交客户负责客户成功经理变更", startStr, "", "", &Gmail)
+			if status {
+				log.Println("客成发邮件 send mail success", startStr, to, "")
+			}
+		}
+	}
+}
+
+// 客成人员变更基本信息获取
+func CustomerChangeHandle(data map[string]interface{}) map[string]interface{} {
+	clueId := gconv.Int64(data["clue_id"])
+	oldPerson := gconv.String(data["old_value"])
+	newPerson := gconv.String(data["new_value"])
+	operatorId := gconv.Int64(data["operator_id"])
+	companyName := ""
+	phone := ""
+	userName := ""
+	clueName := ""
+	clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{
+		"id": clueId,
+	}, "", "")
+	if clueData == nil || len(*clueData) == 0 {
+		return nil
+	}
+	phone = common.ObjToString((*clueData)["phone"])
+	userName = gconv.String((*clueData)["name"])
+	positionId := gconv.Int64((*clueData)["position_id"])
+	clueName = gconv.String((*clueData)["cluename"])
+	customData := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{
+		"clue_id": clueId,
+	}, "", "")
+	if customData != nil || len(*customData) != 0 {
+		companyName = gconv.String((*customData)["company_name"])
+	}
+	return map[string]interface{}{
+		"oldPerson":   oldPerson,
+		"newPerson":   newPerson,
+		"operatorId":  operatorId,
+		"phone":       phone,
+		"userName":    userName,
+		"positionId":  positionId,
+		"clueName":    clueName,
+		"companyName": companyName,
+	}
+}
 func refundAutoHistory() {
 	data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1)
 	if data != nil {

+ 0 - 2
clueSync/main.go

@@ -153,8 +153,6 @@ func main() {
 	MgoQyxy = mongodb.NewMgoWithUser(db.MgoQyxy.Address, db.MgoQyxy.DbName, db.MgoQyxy.User, db.MgoQyxy.Password, db.MgoQyxy.DbSize)
 	InitArea()
 	InitProduct(db.ProductArr)
-	//kcSync()
-	//refundAuto() //客成移交销售
 	if *mode == 1 {
 		go ordersClue() //后台订单进线索
 		//一秒钟一次