package services
import (
"context"
"fmt"
"hongze/hongze_task/models"
"hongze/hongze_task/services/company"
"hongze/hongze_task/services/cygx"
"hongze/hongze_task/utils"
"strconv"
"strings"
"time"
)
// 客户自动冻结->试用两个月结束后,进入冻结
func CompanyFreeze(cont context.Context) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "试用两个月结束后,进入冻结 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
}
}()
endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
items, err := models.GetCompanyNeedFreeze(endDate)
if err != nil {
fmt.Println("GetCompanyNeedFreeze Err:" + err.Error())
utils.FileLog.Info("GetCompanyNeedFreeze Err:%s" + err.Error())
return
}
for k, v := range items {
fmt.Println(k, v.CompanyId)
count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
if err != nil {
return err
}
if count > 0 {
continue
}
//试用转冻结
companyReportPermissionList, err := models.CompanyFreeze(v.CompanyId, v.ProductId)
if err != nil {
utils.FileLog.Info("CompanyFreeze Err:%s" + err.Error())
return err
}
//新增操作记录
{
remark := "试用转冻结"
operation := "freeze"
approveContent := ""
go AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
//新增 客户产品权限 试用 转 冻结 的日志
for _, v := range companyReportPermissionList {
_ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
}
}
}
return
}
// 客户自动流失->冻结超3个月未处理
func CompanyLoss(cont context.Context) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "自动流失->冻结超3个月未处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
}
}()
endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
items, err := models.GetCompanyNeedLoss(endDate)
if err != nil {
fmt.Println("GetCompanyNeedLoss Err:" + err.Error())
utils.FileLog.Info("GetCompanyNeedLoss Err:%s" + err.Error())
return
}
for k, v := range items {
fmt.Println(k, v.CompanyId)
count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
if err != nil {
return err
}
if count > 0 {
continue
}
err = models.CompanyLoss(v.CompanyId, v.ProductId)
if err != nil {
utils.FileLog.Info("CompanyLoss Err:%s" + err.Error())
return err
}
//新增操作记录
{
remark := "冻结转流失"
operation := "loss"
approveContent := "流失"
AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
}
}
return
}
// 正式客户自动试用->合同到期未续约转试用
func CompanyTryOut(cont context.Context) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "正式客户自动试用->合同到期未续约转试用 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
}
}()
endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
items, err := models.GetCompanyNeedTryOut(endDate)
if err != nil {
fmt.Println("GetCompanyNeedTryOut Err:" + err.Error())
utils.FileLog.Info("GetCompanyNeedTryOut Err:%s" + err.Error())
return
}
for k, v := range items {
count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
if err != nil {
return err
}
if count > 0 {
continue
}
fmt.Println(k, v.CompanyId)
// 更新专项调研点数,需要在状态变更之前
if v.ProductId == 2 {
cygx.ActivitySpecialCompanyTryOutReduce(v.CompanyId)
}
//客户正式转试用
companyReportPermissionList, err := models.CompanyTryOut(v.CompanyId, v.ProductId)
if err != nil {
utils.FileLog.Info("CompanyLoss Err:%s" + err.Error())
return err
}
//新增操作记录
{
remark := "正式转试用"
operation := "try_out"
approveContent := "试用"
AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
//新增 客户产品权限 正式 转 试用 的日志
for _, v := range companyReportPermissionList {
_ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
}
}
{
//正式转试用,删除不续约归因内容
company.DeleteCompanNoRenewedAscribe(v.CompanyId, v.ProductId)
}
//正式转试用定时任务更新研选扣点
if v.ProductId == 2 {
cygx.YanXuanCompanyCompanyTryOut(v.CompanyId)
}
}
return
}
func AddCompanyOperationRecord(companyId, sysUserId, productId, approveAdminId int, companyName, productName, sysUserRealName, remark, operation, approveContent, approveUserRealName, approveRemark, status string) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "services:AddCompanyOperationRecord;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
record := new(models.CompanyOperationRecord)
record.CompanyId = companyId
record.CompanyName = companyName
record.SysUserId = sysUserId
record.SysRealName = sysUserRealName
record.Remark = remark
record.Operation = operation
record.CreateTime = time.Now()
record.ProductId = productId
record.ProductName = productName
record.ApproveUserId = approveAdminId
record.ApproveRealName = approveUserRealName
record.ApproveContent = approveContent
record.ApproveRemark = approveRemark
record.Status = status
_, err = models.AddCompanyOperationRecord(record)
return
}
// 客户老数据同步
func CompanyOldDataSync() {
companyItems, err := models.GetCompanyOldDataSync()
if err != nil {
fmt.Println("GetCompanyOldDataSync Err:" + err.Error())
return
}
productId := 1
productName := "ficc"
for k, v := range companyItems {
fmt.Println(k, v.CompanyName, v.CompanyId)
productItem := new(models.CompanyProduct)
productItem.CompanyId = v.CompanyId
productItem.ProductId = productId
productItem.ProductName = productName
productItem.CompanyName = v.CompanyName
//客户标签,1:付费客户,2:试用客户,3:流失客户,4:潜在客户,5:永续
//'试用','永续','冻结','流失','正式','潜在'
status := ""
if v.CompanyType == 1 {
status = "正式"
} else if v.CompanyType == 2 {
status = "试用"
} else if v.CompanyType == 3 {
status = "流失"
} else if v.CompanyType == 4 {
status = "潜在"
} else if v.CompanyType == 5 {
status = "永续"
} else {
fmt.Println("无效的客户状态:"+status, v.CompanyType)
return
}
if status == "" {
fmt.Println("无效的客户状态:"+status, v.CompanyType)
return
}
productItem.Status = status
//获取销售
seller, err := models.GetAdminByAdminId(v.SellsId)
if err != nil && err.Error() != utils.ErrNoRow() {
fmt.Println("获取销售失败,Err:"+err.Error(), v.SellsId)
return
}
var adminId, groupId, departmentId int
var sellerRealName string
if seller == nil {
fmt.Println("销售不存在:", v.SellsId)
//return
} else {
adminId = seller.AdminId
sellerRealName = seller.RealName
groupId = seller.GroupId
departmentId = seller.DepartmentId
}
productItem.SellerId = adminId
productItem.SellerName = sellerRealName
productItem.GroupId = groupId
productItem.DepartmentId = departmentId
productItem.CreateTime = time.Now()
productItem.ModifyTime = time.Now()
productItem.StartDate = v.StartDate
productItem.EndDate = v.EndDate
productItem.LoseReason = v.LoseReason
productItem.LossTime = v.LossTime
_, err = models.AddCompanyProduct(productItem)
if err != nil {
fmt.Println("新增客户产品失败,Err:" + err.Error())
return
}
//权限处理
err = models.ModifyCompanyOldPermission(v.StartDate, v.EndDate, productName, v.CompanyId, productId)
if err != nil {
fmt.Println("客户权限处理失败,Err:" + err.Error())
return
}
if k > 500 {
return
}
}
}
// CompanyRemind 正式/试用客户--到期提醒
func CompanyRemind(cont context.Context) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
//获取销售
sellers, err := models.GetSellersOpenId()
for k, v := range sellers {
fmt.Println(k, v.AdminId, v.Mobile)
CompanyRemind60Day(v)
time.Sleep(5 * time.Second)
CompanyRemind30Day(v)
time.Sleep(5 * time.Second)
CompanyRemind15Day(v)
time.Sleep(5 * time.Second)
CompanyRemind7Day(v)
time.Sleep(5 * time.Second)
CompanyRemind1Day(v)
time.Sleep(5 * time.Second)
}
return
}
// CompanyRemind60Day 60天后到期客户
func CompanyRemind60Day(seller *models.Sellers) {
var err error
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
remindType := 5
uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
//remindEndDate := "2020-12-31" //time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
remindEndDate := time.Now().AddDate(0, 0, 60).Format(utils.FormatDate)
companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
if err != nil {
return
}
emailContents := "
您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于60天后到期,请注意查看
"
emailContents += "
60天后到期客户名称 | 到期日期 | 销售人员 | 客户类型 |
"
var isSend bool
msgContent := ``
for _, v := range companyItems {
endTime := v.EndDate
if v.Status == "正式" {
endTime = v.ContractEndDate
}
emailContents += `` + v.CompanyName + ` | ` + endTime + ` | ` + seller.RealName + ` | ` + v.Status + ` |
`
msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
isSend = true
// 数据入库
companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
//CompanyRemindRecordId: 0,
Type: remindType,
SellerId: seller.AdminId,
SellerName: seller.RealName,
CompanyId: v.CompanyId,
CompanyName: v.CompanyName,
Status: v.Status,
EndDate: endTime,
UniqueCode: uniqueCode,
CreateTime: time.Now(),
})
}
emailContents += "
"
if isSend {
if seller.Email != "" {
utils.SendEmailByHongze("到期前60天提醒", emailContents, seller.Email, "", "")
}
if seller.OpenId != "" {
first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于60天后到期,请注意查看"
//keyword1 := "到期前30天提醒"
keyword1 := fmt.Sprintf(`【%d】客户到期前60天提醒,点击查看`, len(companyItems))
keyword2 := remindEndDate
remark := msgContent
openIdList := make([]*models.OpenIdList, 0)
openIdItem := new(models.OpenIdList)
openIdItem.OpenId = seller.OpenId
openIdList = append(openIdList, openIdItem)
SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
}
}
// 数据入库
if len(companyRemindRecordList) > 0 {
models.AddMultiCompanyRemindRecord(companyRemindRecordList)
}
}
// CompanyRemind30Day 30天后到期客户
func CompanyRemind30Day(seller *models.Sellers) {
var err error
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
remindType := 4
uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
//remindEndDate := "2020-12-31" //time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
remindEndDate := time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
if err != nil {
return
}
emailContents := "
您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于30天后到期,请注意查看
"
emailContents += "
30天后到期客户名称 | 到期日期 | 销售人员 | 客户类型 |
"
var isSend bool
msgContent := ``
for _, v := range companyItems {
endTime := v.EndDate
if v.Status == "正式" {
endTime = v.ContractEndDate
}
emailContents += `` + v.CompanyName + ` | ` + endTime + ` | ` + seller.RealName + ` | ` + v.Status + ` |
`
msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
isSend = true
// 数据入库
companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
//CompanyRemindRecordId: 0,
Type: remindType,
SellerId: seller.AdminId,
SellerName: seller.RealName,
CompanyId: v.CompanyId,
CompanyName: v.CompanyName,
Status: v.Status,
EndDate: endTime,
UniqueCode: uniqueCode,
CreateTime: time.Now(),
})
}
emailContents += "
"
if isSend {
if seller.Email != "" {
utils.SendEmailByHongze("到期前30天提醒", emailContents, seller.Email, "", "")
}
if seller.OpenId != "" {
first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于30天后到期,请注意查看"
//keyword1 := "到期前30天提醒"
keyword1 := fmt.Sprintf(`【%d】客户到期前30天提醒,点击查看`, len(companyItems))
keyword2 := remindEndDate
remark := msgContent
openIdList := make([]*models.OpenIdList, 0)
openIdItem := new(models.OpenIdList)
openIdItem.OpenId = seller.OpenId
openIdList = append(openIdList, openIdItem)
SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
}
}
// 数据入库
if len(companyRemindRecordList) > 0 {
models.AddMultiCompanyRemindRecord(companyRemindRecordList)
}
}
// CompanyRemind15Day 15天后到期客户
func CompanyRemind15Day(seller *models.Sellers) {
var err error
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
remindType := 3
uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
remindEndDate := time.Now().AddDate(0, 0, 15).Format(utils.FormatDate)
companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
if err != nil {
return
}
var isSend bool
msgContent := ``
emailContents := "
您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于15天后到期,请注意查看
"
emailContents += "
15天后到期客户名称 | 到期日期 | 销售人员 | 客户类型 |
"
for _, v := range companyItems {
endTime := v.EndDate
if v.Status == "正式" {
endTime = v.ContractEndDate
}
emailContents += `` + v.CompanyName + ` | ` + endTime + ` | ` + seller.RealName + ` | ` + v.Status + ` |
`
msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
isSend = true
// 数据入库
companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
//CompanyRemindRecordId: 0,
Type: remindType,
SellerId: seller.AdminId,
SellerName: seller.RealName,
CompanyId: v.CompanyId,
CompanyName: v.CompanyName,
Status: v.Status,
EndDate: endTime,
UniqueCode: uniqueCode,
CreateTime: time.Now(),
})
}
emailContents += "
"
if isSend {
if seller.Email != "" {
utils.SendEmailByHongze("到期前15天提醒", emailContents, seller.Email, "", "")
}
if seller.OpenId != "" {
first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于15天后到期,请注意查看"
//keyword1 := "到期前15天提醒"
keyword1 := fmt.Sprintf(`【%d】客户到期前15天提醒,点击查看`, len(companyItems))
keyword2 := remindEndDate
remark := msgContent
openIdList := make([]*models.OpenIdList, 0)
openIdItem := new(models.OpenIdList)
openIdItem.OpenId = seller.OpenId
openIdList = append(openIdList, openIdItem)
SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
}
}
// 数据入库
if len(companyRemindRecordList) > 0 {
models.AddMultiCompanyRemindRecord(companyRemindRecordList)
}
}
// CompanyRemind7Day 7天后到期客户名称
func CompanyRemind7Day(seller *models.Sellers) {
var err error
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
remindType := 2
uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
remindEndDate := time.Now().AddDate(0, 0, 7).Format(utils.FormatDate)
companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
if err != nil {
return
}
var isSend bool
msgContent := ``
emailContents := "
您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于7天后到期,请注意查看
"
emailContents += "
7天后到期客户名称 | 到期日期 | 销售人员 | 客户类型 |
"
for _, v := range companyItems {
endTime := v.EndDate
if v.Status == "正式" {
endTime = v.ContractEndDate
}
emailContents += `` + v.CompanyName + ` | ` + endTime + ` | ` + seller.RealName + ` | ` + v.Status + ` |
`
msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
isSend = true
// 数据入库
companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
//CompanyRemindRecordId: 0,
Type: remindType,
SellerId: seller.AdminId,
SellerName: seller.RealName,
CompanyId: v.CompanyId,
CompanyName: v.CompanyName,
Status: v.Status,
EndDate: endTime,
UniqueCode: uniqueCode,
CreateTime: time.Now(),
})
}
emailContents += "
"
if isSend {
if seller.Email != "" {
utils.SendEmailByHongze("到期前7天提醒", emailContents, seller.Email, "", "")
}
if seller.OpenId != "" {
first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于7天后到期,请注意查看"
//keyword1 := "到期前7天提醒"
keyword1 := fmt.Sprintf(`【%d】客户到期前7天提醒,点击查看`, len(companyItems))
keyword2 := remindEndDate
remark := msgContent
openIdList := make([]*models.OpenIdList, 0)
openIdItem := new(models.OpenIdList)
openIdItem.OpenId = seller.OpenId
openIdList = append(openIdList, openIdItem)
SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
}
}
// 数据入库
if len(companyRemindRecordList) > 0 {
models.AddMultiCompanyRemindRecord(companyRemindRecordList)
}
}
// CompanyRemind1Day到期当天提醒
func CompanyRemind1Day(seller *models.Sellers) {
var err error
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
}
}()
companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
remindType := 1
uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
remindEndDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
if err != nil {
return
}
var isSend bool
msgContent := ``
emailContents := "
您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于今天后到期,请注意查看
"
emailContents += "
1天后到期客户名称 | 到期日期 | 销售人员 | 客户类型 |
"
for _, v := range companyItems {
endTime := v.EndDate
if v.Status == "正式" {
endTime = v.ContractEndDate
}
emailContents += `` + v.CompanyName + ` | ` + endTime + ` | ` + seller.RealName + ` | ` + v.Status + ` |
`
msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
isSend = true
// 数据入库
companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
//CompanyRemindRecordId: 0,
Type: remindType,
SellerId: seller.AdminId,
SellerName: seller.RealName,
CompanyId: v.CompanyId,
CompanyName: v.CompanyName,
Status: v.Status,
EndDate: endTime,
UniqueCode: uniqueCode,
CreateTime: time.Now(),
})
}
emailContents += "
"
if isSend {
if seller.Email != "" {
utils.SendEmailByHongze("到期前1天提醒", emailContents, seller.Email, "", "")
}
if seller.OpenId != "" {
first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于1天后到期,请注意查看"
//keyword1 := "到期前1天提醒"
keyword1 := fmt.Sprintf(`【%d】客户到期前1天提醒,点击查看`, len(companyItems))
keyword2 := remindEndDate
remark := msgContent
openIdList := make([]*models.OpenIdList, 0)
openIdItem := new(models.OpenIdList)
openIdItem.OpenId = seller.OpenId
openIdList = append(openIdList, openIdItem)
SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
}
}
// 数据入库
if len(companyRemindRecordList) > 0 {
models.AddMultiCompanyRemindRecord(companyRemindRecordList)
}
}
// StaticCompanyTryDay 定时任务每天更新客户试用总天数
func StaticCompanyTryDay(cont context.Context) (err error) {
errList := make([]string, 0)
defer func() {
if len(errList) > 0 {
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "定时任务每天更新客户试用总天数 ErrMsg:"+strings.Join(errList, "\n"), utils.EmailSendToUsers)
}
}()
list, err := models.GetCompanyProductTryOutUpdateNoStopGroup()
if err != nil {
fmt.Println("获取客户变更数据失败:", err)
return
}
for _, v := range list {
isAdd := false //是否要增加一天,默认不加
permissionList, tmpErr := models.GetCompanyReportPermissionByStatus(v.CompanyId, v.ProductId, "试用")
if tmpErr != nil {
errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";寻找对应的试用品种失败,Err:", tmpErr))
continue
}
currPermissionIdList := make([]int, 0) //当前试用的品种
for _, permission := range permissionList {
currPermissionIdList = append(currPermissionIdList, permission.ChartPermissionId)
}
//获取所有未停止的记录列表
logList, tmpErr := models.GetCompanyProductTryOutUpdateNoStopListByEndDate(v.CompanyId, v.ProductId)
if tmpErr != nil {
errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";寻找客户的试用日志失败,Err:", tmpErr))
continue
}
logPermissionIdList := make([]int, 0) //当前日志中的试用的品种
for _, log := range logList {
//获取所有未停止的品种试用记录列表
logPermissionList, tmpErr := models.GetCompanyProductTryOutPermissionUpdateNoStopListByEndDate(log.Id)
if tmpErr != nil {
errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";logId:", v.Id, ";寻找客户的试用品种日志失败,Err:", tmpErr))
continue
}
lenLogPermissionList := len(logPermissionList) //当前日志存在试用的品种数量
stopPermission := 0 //当前日志需要停止的品种数量
currTime := time.Now() //当前时间
for _, logPermission := range logPermissionList {
if utils.InArrayByInt(logPermissionIdList, logPermission.ChartPermissionId) {
// 如果已经被其他记录使用了,那么就将当前记录给标记停止
logPermission.IsStop = 1
// 如果当前时间晚于结束日期,那么实际结束日期就是今天之前一天,否则就是当天
if currTime.After(logPermission.EndDate) {
logPermission.RealEndDate = currTime.AddDate(0, 0, -1)
} else {
logPermission.RealEndDate = time.Now()
}
logPermission.Update([]string{"IsStop", "RealEndDate"})
stopPermission++
continue
} else if !utils.InArrayByInt(currPermissionIdList, logPermission.ChartPermissionId) {
// 如果该品种不在当前客户的品种里面,那么也要将当前记录给标记停止
logPermission.IsStop = 1
// 如果当前时间晚于结束日期,那么实际结束日期就是今天之前一天,否则就是当天
if currTime.After(logPermission.EndDate) {
logPermission.RealEndDate = currTime.AddDate(0, 0, -1)
} else {
logPermission.RealEndDate = time.Now()
}
logPermission.Update([]string{"IsStop", "RealEndDate"})
stopPermission++
continue
}
// 说明当天还是试用状态,需要变更实际结束日期
logPermission.RealEndDate = currTime
logPermission.Update([]string{"RealEndDate"})
// 剩下的说明还处于试用状态,需要添加1天试用期,且需要把该品种加入到当前日志中的试用的品种列表
isAdd = true
logPermissionIdList = append(logPermissionIdList, logPermission.ChartPermissionId)
}
//如果当前日志存在试用的品种数量 == 当前日志需要停止的品种数量
// 那么当前日志也是处于停用状态
if lenLogPermissionList == stopPermission {
log.IsStop = 1
// 如果当前时间晚于结束日期,那么实际结束日期就是今天之前一天,否则就是当天
if currTime.After(log.EndDate) {
log.RealEndDate = currTime.AddDate(0, 0, -1)
} else {
log.RealEndDate = time.Now()
}
log.Update([]string{"IsStop", "RealEndDate"})
}
}
// 如果需要添加,那么将该客户品种添加1天
if isAdd {
}
// 更新客户产品的试用天数
tmpErr = models.AddCompanyProductTryOutDayTotal(v.CompanyId, v.ProductId)
if tmpErr != nil {
errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";logId:", v.Id, ";更新客户产品的试用天数,Err:", tmpErr))
continue
}
}
return
}
// 权益正式客户自动转X类试用(永续)->合同到期未续约转X类试用(永续)
func CompanyTryOutXClassRai(cont context.Context) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "正式客户自动试用->合同到期未续约转试用 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
}
}()
endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
items, err := models.GetCompanyNeedTryOutXClassRai(endDate)
if err != nil {
fmt.Println("GetCompanyNeedTryOutRai Err:" + err.Error())
utils.FileLog.Info("GetCompanyNeedTryOutRai Err:%s" + err.Error())
return
}
for k, v := range items {
count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
if err != nil {
return err
}
if count > 0 {
continue
}
fmt.Println(k, v.CompanyId)
// 更新专项调研点数,需要在状态变更之前
if v.ProductId == 2 {
cygx.ActivitySpecialCompanyTryOutReduce(v.CompanyId)
}
//客户正式转试用
companyReportPermissionList, err := models.CompanyTryOutXClassRai(v.CompanyId, v.ProductId)
if err != nil {
utils.FileLog.Info("CompanyTryOutXClassRai Err:%s" + err.Error())
return err
}
//新增操作记录
{
remark := "正式转X类试用"
operation := "try_out_x_class"
approveContent := "X类试用"
AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
//新增 客户产品权限 正式 转 试用 的日志
for _, v := range companyReportPermissionList {
_ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
}
}
{
//正式转试用,删除不续约归因内容
company.DeleteCompanNoRenewedAscribe(v.CompanyId, v.ProductId)
}
//正式转试用定时任务更新研选扣点
{
cygx.YanXuanCompanyCompanyTryOut(v.CompanyId)
}
}
return
}
// 客户自动冻结->X类试用(永续)三个月结束后,进入冻结
func CompanyFreezeXClassRai(cont context.Context) (err error) {
defer func() {
if err != nil {
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "X类试用(永续)三个月结束后,进入冻结ErrMsg:"+err.Error(), utils.EmailSendToUsers)
}
}()
endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
items, err := models.GetCompanyNeedFreezeXClassRai(endDate)
if err != nil {
fmt.Println("GetCompanyNeedFreezeXClassRai Err:" + err.Error())
utils.FileLog.Info("GetCompanyNeedFreezeXClassRai Err:%s" + err.Error())
return
}
fmt.Println(len(items))
//return
for k, v := range items {
fmt.Println(k, v.CompanyId)
count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
if err != nil {
return err
}
if count > 0 {
continue
}
//试用转冻结
companyReportPermissionList, err := models.CompanyFreezeXClassRai(v.CompanyId, v.ProductId)
if err != nil {
utils.FileLog.Info("CompanyFreeze Err:%s" + err.Error())
return err
}
//新增操作记录
{
remark := "X类试用转冻结"
operation := "x_class_freeze"
approveContent := ""
go AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
//新增 客户产品权限 试用 转 冻结 的日志
for _, v := range companyReportPermissionList {
_ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
}
}
}
return
}