123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878 |
- 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 := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于60天后到期,请注意查看</p>"
- emailContents += "<table border='1'><tr><td width='200'>60天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
- var isSend bool
- msgContent := ``
- for _, v := range companyItems {
- endTime := v.EndDate
- if v.Status == "正式" {
- endTime = v.ContractEndDate
- }
- emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
- 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 += "</table></br>"
- 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 := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于30天后到期,请注意查看</p>"
- emailContents += "<table border='1'><tr><td width='200'>30天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
- var isSend bool
- msgContent := ``
- for _, v := range companyItems {
- endTime := v.EndDate
- if v.Status == "正式" {
- endTime = v.ContractEndDate
- }
- emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
- 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 += "</table></br>"
- 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 := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于15天后到期,请注意查看</p>"
- emailContents += "<table border='1'><tr><td width='200'>15天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
- for _, v := range companyItems {
- endTime := v.EndDate
- if v.Status == "正式" {
- endTime = v.ContractEndDate
- }
- emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
- 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 += "</table></br>"
- 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 := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于7天后到期,请注意查看</p>"
- emailContents += "<table border='1'><tr><td width='200'>7天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
- for _, v := range companyItems {
- endTime := v.EndDate
- if v.Status == "正式" {
- endTime = v.ContractEndDate
- }
- emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
- 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 += "</table></br>"
- 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 := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于今天后到期,请注意查看</p>"
- emailContents += "<table border='1'><tr><td width='200'>1天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
- for _, v := range companyItems {
- endTime := v.EndDate
- if v.Status == "正式" {
- endTime = v.ContractEndDate
- }
- emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
- 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 += "</table></br>"
- 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
- }
|