Commit 0f0fc0aa by 朱继来

设置限制跟进时间

parent 5d4de97c
package handle
import (
"crm-server/configs"
"crm-server/internal/dao"
"crm-server/internal/model"
"crm-server/internal/service"
"github.com/ichunt2019/logger"
"strconv"
"time"
)
/*
根据lie_salesman表,获取sys_limit=1的所有数据,添加限制跟进时间;(过滤线下销售、已下单的不处理)
*/
// CMS user_info
type UserInfo struct {
UserId int `db:"userId"`
DepartmentId int `db:"department_id"`
PositionId int `db:"position_id"`
}
// CMS user_department
type UserDepartment struct {
DepartmentId int `db:"department_id"`
DepartmentName string `db:"department_name"`
ParentId int `db:"parent_id"`
}
// 更新限制跟进时间
func UpdateLimitFollowTime() {
// 1、获取所有同步字段为1的数据
row, err := dao.GetDb().Queryx("select user_id,sale_id,assign_time from lie_salesman where sys_limit = 1")
if err != nil {
logger.Info("UpdateLimitFollowTime method error: ", err.Error())
return
}
// 2、遍历数据
for row.Next() {
var sales model.Salesman
err = row.StructScan(&sales)
// 2-1、检查是否为线下销售
is_offline_sales := IsOfflineSales(sales.Sale_Id)
if is_offline_sales { // 属于线下销售,则跳出本次循环
continue
}
// 2-2、计算限制时间
limit_follow_time := CalLimitFollowTime(sales.User_Id, sales.Assign_Time)
// 2-3、更新限制时间、同步字段-1
curr_time := time.Now().Unix()
_, err := dao.GetDb().Exec("update lie_salesman set sys_limit = ?, limit_follow_time = ?, update_time = ? where user_id = ?", -1, limit_follow_time, curr_time, sales.User_Id)
if err != nil {
logger.Info("更新限制时间、同步字段失败,CRM用户ID:"+ strconv.Itoa(sales.User_Id) +"原因:" + err.Error())
service.DingAlert(configs.Ding_crm_task_token, "任务告警:更新限制时间、同步字段失败,CRM用户ID:"+ strconv.Itoa(sales.User_Id) +"原因:" + err.Error(), "")
return
}
service.DingAlert(configs.Ding_crm_task_token, "任务告警:更新限制时间、同步字段成功,CRM用户ID:"+ strconv.Itoa(sales.User_Id), "")
}
}
// 检查是否属于线下销售
func IsOfflineSales(sale_id int) bool {
var user_info UserInfo
err := dao.GetCmsDb().Get(&user_info, "select userId,department_id,position_id from user_info where userId = ?", sale_id)
if err != nil {
logger.Info("获取CMS用户信息失败,userId:%d,错误原因:%v", sale_id, err.Error())
return false
}
offline_sales_department_ids := GetOfflineSaleDepartment()
_, ok := SliceFind(offline_sales_department_ids, user_info.DepartmentId)
return ok
}
// Find获取一个切片并在其中查找元素。如果找到它,它将返回它的索引,否则它将返回-1和一个错误的bool。
func SliceFind(slice []int, val int) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
// 获取线下销售所有部门 (线下销售部 40)
func GetOfflineSaleDepartment() []int {
var offline_sales_department_ids []int
offline_sales_department_ids = append(offline_sales_department_ids, 40)
var user_department []*UserDepartment
dao.GetCmsDb().Select(&user_department, "select department_id,department_name,parent_id from user_department where parent_id = 40")
for _, v := range user_department {
offline_sales_department_ids = append(offline_sales_department_ids, v.DepartmentId)
}
return offline_sales_department_ids
}
// 计算跟进时间
func CalLimitFollowTime(user_id int, assign_time int) int {
// 判断用户是否下单
is_order := IsOrder(user_id)
if is_order { // 已下单
return 0
}
// 未下单用户限制跟进时间为30天
var limit_follow_time int
limit_follow_time = assign_time + 30 * 86400
return limit_follow_time
}
// 判断用户是否下单
func IsOrder(user_id int) bool {
var user_extend model.UserExtend
// id,user_id,outter_uid,no_create_order,latest_order_time,completed_order_nums,model_nums,total_order_amount,amount_paid,contact,contact_info,create_time
dao.GetDb().Get(&user_extend, "select * from lie_user_extend where user_id = ?", user_id)
if user_extend.No_Create_Order == 2 { // 已下单
return true
}
return false
}
package handle
import (
"crm-server/configs"
"crm-server/internal/dao"
"crm-server/internal/model"
"crm-server/internal/service"
"fmt"
"github.com/ichunt2019/logger"
"strconv"
"time"
)
/*
1. 获取存在限制跟进时间字段的数据;lie_salesman
2. 判断用户是否下单;lie_user_extend
3. 已下单的取消限制跟进时间;
4. 未下单的对比限制跟进时间和当前时间, < 7天 放入即将流失用户表,<=0 释放用户,添加释放时间,放入总用户池表
*/
func SetFree() {
// 1、获取所有限制跟进时间不为0的数据
row, err := dao.GetDb().Queryx("select id,user_id,sale_id,assign_time,update_time,sys_limit,limit_follow_time from lie_salesman where limit_follow_time != 0")
if err != nil {
logger.Info("SetFree method error:", err.Error())
return
}
// 2、遍历数据
for row.Next() {
var sales model.Salesman
err = row.StructScan(&sales)
is_order := IsOrder(sales.User_Id)
if is_order {
SetLimitFollowTime(sales.User_Id)
} else {
CompareTime(sales.User_Id, sales.LimitFollowTime)
}
}
}
// 设置限制跟进时间为0
func SetLimitFollowTime(user_id int) {
curr_time := time.Now().Unix()
_, err := dao.GetDb().Exec("update lie_salesman set limit_follow_time = ?, update_time = ? where user_id = ?", 0, curr_time, user_id)
if err != nil {
logger.Info("设置限制时间为0失败,CRM用户ID:"+ strconv.Itoa(user_id) +"原因:" + err.Error())
service.DingAlert(configs.Ding_crm_token, "设置限制时间为0失败,CRM用户ID:"+ strconv.Itoa(user_id) +"原因:" + err.Error(), "")
return
}
}
// 对比时间
func CompareTime(user_id, limit_follow_time int) {
curr_time := time.Now().Unix()
// 转化为int64
s_limit_follow_time := strconv.Itoa(limit_follow_time)
limit_follow_time_64, _ := strconv.ParseInt(s_limit_follow_time,10,64)
diff := limit_follow_time_64 - curr_time
// 获取当前用户信息
var user_info model.Users
dao.GetDb().Get(&user_info, "select user_id,outter_uid,is_free,free_time,is_churn from lie_user where user_id = ?", user_id)
if diff <= 0 { // 释放用户
if user_info.Is_free == 1 {
return
}
_, err := dao.GetDb().Exec("update lie_user set is_free = ?, free_time = ?, is_churn = ? where user_id = ?", 1, curr_time, 0, user_id)
if err != nil {
logger.Info("释放用户失败,CRM用户ID:"+ strconv.Itoa(user_id) +"原因:" + err.Error())
service.DingAlert(configs.Ding_crm_task_token, "任务告警:释放用户失败,CRM用户ID:"+ strconv.Itoa(user_id) +"原因:" + err.Error(), "")
return
}
// 限制跟进时间标为0
dao.GetDb().Exec("update lie_salesman set limit_follow_time = ?, update_time = ? where user_id = ?", 0, curr_time, user_id)
// 添加释放日志
dao.GetDb().Exec("insert into lie_action_log (user_id, type, event, remark, create_time) values (?, ?, ?, ?, ?)", user_id, 2, "释放用户", "", curr_time)
service.DingAlert(configs.Ding_crm_task_token, "任务告警:释放用户成功,CRM用户ID:"+ strconv.Itoa(user_id), "")
} else if diff <= 7 * 86400 { // 设置为即将流失用户
if user_info.Is_churn == 1 {
fmt.Println("111")
return
}
_, err := dao.GetDb().Exec("update lie_user set is_churn = ? where user_id = ?", 1, user_id)
if err != nil {
logger.Info("设置为即将流失用户失败,CRM用户ID:"+ strconv.Itoa(user_id) +"原因:" + err.Error())
service.DingAlert(configs.Ding_crm_task_token, "任务告警:设置为即将流失用户失败,CRM用户ID:"+ strconv.Itoa(user_id) +"原因:" + err.Error(), "")
return
}
service.DingAlert(configs.Ding_crm_task_token, "任务告警:设置为即将流失用户成功,CRM用户ID:"+ strconv.Itoa(user_id), "")
}
}
package main
import (
"crm-server/cmd/follow/handle"
"flag"
"fmt"
"github.com/ichunt2019/logger"
"time"
)
// 日志目录
var LogDir string
// 解析命令行参数
func initArgs() {
flag.StringVar(&LogDir, "logDir", "", "日志目录")
flag.Parse()
}
func main () {
initArgs()
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"/follow"
logConfig["log_chan_size"] = "100"
logger.InitLogger("file",logConfig)
logger.Init()
// 定时执行
ticker := time.NewTicker(time.Second * 5)
//ticker := time.NewTicker(time.Minute * 30) // 第一次执行时间30分钟,后续2分钟
for {
select {
case <- ticker.C:
fmt.Println("ticker...")
handle.UpdateLimitFollowTime()
handle.SetFree()
}
}
}
...@@ -20,7 +20,6 @@ var onceLiexinBom sync.Once ...@@ -20,7 +20,6 @@ var onceLiexinBom sync.Once
func GetDb()(*sqlx.DB) { func GetDb()(*sqlx.DB) {
once.Do(func() { once.Do(func() {
dbConfig := configs.GetDBOne() dbConfig := configs.GetDBOne()
db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table)) db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table))
......
package model package model
type Salesman struct { type Salesman struct {
Id int `json:"id"` Id int `json:"id" db:"id"`
User_Id int `json:"user_id"` User_Id int `json:"user_id" db:"user_id"`
Sale_Id int `json:"sale_id"` Sale_Id int `json:"sale_id" db:"sale_id"`
Assign_Time int `json:"assign_time"` Assign_Time int `json:"assign_time" db:"assign_time"`
Update_Time int `json:"update_time"` Update_Time int `json:"update_time" db:"update_time"`
SysLimit int `json:"sys_limit" db:"sys_limit"`
LimitFollowTime int `json:"limit_follow_time" db:"limit_follow_time"`
} }
......
package model
type UserExtend struct {
Id int `db:"id"`
User_Id int `db:"user_id"`
Outter_Uid int `db:"outter_uid"`
No_Create_Order int `db:"no_create_order"`
Latest_Order_Time int `db:"latest_order_time"`
Completed_Order_Nums int `db:"completed_order_nums"`
Model_Nums int `db:"model_nums"`
Total_Order_Amount float64 `db:"total_order_amount"`
Amount_Paid float64 `db:"amount_paid"`
Contact string `db:"contact"`
Contact_Info string `db:"contact_info"`
Create_Time int `db:"create_time"`
}
...@@ -2,5 +2,9 @@ package model ...@@ -2,5 +2,9 @@ package model
// Kratos hello kratos. // Kratos hello kratos.
type Users struct { type Users struct {
User_id int64 `json:"user_id"` User_id int64 `json:"user_id" db:"user_id"`
Outter_uid int64 `json:"outter_uid" db:"outter_uid"`
Is_free int64 `json:"is_free" db:"is_free"`
Free_time int64 `json:"free_time" db:"free_time"`
Is_churn int64 `json:"is_churn" db:"is_churn"`
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment