123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- package company_contract
- import (
- "context"
- "errors"
- "fmt"
- "hongze/hongze_task/models"
- "hongze/hongze_task/models/company"
- "hongze/hongze_task/models/company_approval"
- "hongze/hongze_task/models/company_contract"
- "hongze/hongze_task/services/alarm_msg"
- "hongze/hongze_task/services/cygx"
- "hongze/hongze_task/utils"
- "strings"
- "time"
- )
- // HandleCompanyContract 合同处理
- func HandleCompanyContract(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("合同处理 ErrMsg:"+err.Error(), 3)
- }
- }()
- today := utils.GetToday(utils.FormatDate)
- total, list, err := company_contract.GetStartContractListByStartDate(today)
- if err != nil {
- fmt.Println("HandleCompanyContract Err:" + err.Error())
- utils.FileLog.Info("HandleCompanyContract Err:%s" + err.Error())
- return
- }
- fmt.Printf("总共%d条数据待处理\n", total)
- errorList := make([]string, 0)
- for _, v := range list {
- //fmt.Println(k, v.CompanyId)
- //fmt.Println(v)
- companyApproval, err := company_approval.GetCompanyApprovalByCompanyContractId(v.CompanyContractId)
- if err != nil {
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";err:", err))
- continue
- }
- if companyApproval == nil {
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";找不到对应的审批单"))
- continue
- }
- //客户产品信息
- companyProduct, err := models.GetCompanyProduct(v.CompanyId, v.ProductId)
- if err != nil {
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";寻找客户产品时异常", ";Err:"+err.Error()))
- continue
- }
- if companyProduct == nil {
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";寻找客户产品时异常2", ";Err:"+err.Error()))
- continue
- }
- //companyProduct.EndDate
- //格式化客户当前产品结束日期
- //companyProductEndDate, err := time.Parse(utils.FormatDate, companyProduct.EndDate)
- //if err != nil {
- // errorList = append(errorList,fmt.Sprint("合同id:",v.CompanyContractId,";格式化客户当前产品结束时间时异常 err:",err))
- // continue
- //}
- //格式化合同结束日期
- //contractEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
- //if err != nil {
- // errorList = append(errorList,fmt.Sprint("合同id:",v.CompanyContractId,";格式化合同结束日期时异常 err:",err))
- // continue
- //}
- //如果 合同结束日期 小于 客户当前产品结束日期,那么不执行合同内容
- //if contractEndDate.Before(companyProductEndDate) {
- // errorList = append(errorList,fmt.Sprint("合同id:",v.CompanyContractId,";合同结束日期 小于 客户当前产品结束日期,不执行该合同内容"))
- // continue
- //}
- //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新
- switch companyApproval.ApplyMethod {
- case 1:
- e := company.TryOutToFormal(v.CompanyId, v.ProductId, utils.AdminId, v.CompanyContractId, v.StartDate, v.EndDate, utils.RealName, v.ProductName, v.PackageType, v.RaiPackageType)
- if e != nil {
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";TryOutToFormal Err: ", e.Error()))
- }
- case 5, 6:
- // 续约异常客户记录
- tmpErr := contactHandleCompanyRenewalRecord(v, today)
- if tmpErr != nil {
- errorList = append(errorList, tmpErr.Error())
- }
- e := company.ApplyServiceUpdate(v.CompanyId, v.ProductId, utils.AdminId, v.CompanyContractId, v.StartDate, v.EndDate, utils.RealName, v.ProductName, v.PackageType, v.RaiPackageType)
- if e != nil {
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";ApplyServiceUpdate Err: ", e.Error()))
- }
- default:
- errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";审批单类型异常"))
- continue
- }
- {
- updateSource := ``
- switch companyApproval.ApplyMethod {
- case 1: //试用转正式
- updateSource = `turn_positive`
- case 2: //冻结->试用
- updateSource = "thaw"
- case 3: //试用延期
- updateSource = "delay"
- case 4: //原销售申请领取流失客户
- updateSource = "apply_receive"
- case 5: //正式客户申请续约
- updateSource = "service_update"
- case 6: //正式客户新增补充协议
- updateSource = "add_agreement"
- }
- tmpStartDate, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
- tmpEndDate, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
- companyProductUpdateLog := &models.CompanyProductUpdateLog{
- Id: 0,
- CompanyId: companyProduct.CompanyId,
- ProductId: companyProduct.ProductId,
- Status: companyProduct.Status,
- SellerId: companyProduct.SellerId,
- SellerName: companyProduct.SellerName,
- Source: updateSource,
- IsFormal: companyProduct.IsFormal, //是否已经转正式,0是没有转正式,1是已经转过正式
- StartDate: tmpStartDate,
- EndDate: tmpEndDate,
- CreateTime: time.Now(),
- }
- go models.AddCompanyProductUpdateLog(companyProductUpdateLog)
- }
- //研选审批通过的时候研选扣点更新
- {
- cygx.YanXuanCompanyApproval(v.CompanyId)
- cygx.ActivitySpecialCompanyApproval(v.CompanyId, v.CompanyContractId, companyProduct.CompanyName) //审批通过的时候专项调研次数更新
- //cygx.HandleAllocationCompanyContractByYanXuan(v.CompanyContractId) //如果合同只有研选的时候,自动处理派点
- cygx.HandleCompanyContractPackageDifference(v.CompanyContractId) //更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
- cygx.HandleCompanyContractPermissionContractType(v.CompanyContractId) //更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
- }
- }
- //错误信息,批量返回错误信息
- if len(errorList) > 0 {
- err = errors.New(strings.Join(errorList, ";"))
- }
- return
- }
- // contactHandleCompanyRenewalRecord
- // @Description: 合同处理完成后的续约异常记录
- // @author: Roc
- // @datetime 2023-12-07 14:24:44
- // @param contractInfo *company_contract.CompanyContract
- // @param day string
- // @return err error
- func contactHandleCompanyRenewalRecord(contractInfo *company_contract.CompanyContract, day string) (err error) {
- // 判断合同类型是否是续约合同,如果不是的话,就不往下走了
- if contractInfo.ContractType != `续约合同` {
- return
- }
- // 获取早于当前合同结束日期的上一份合同
- lastContract, tmpErr := company_contract.GetLastContractListByEndDate(contractInfo.CompanyId, contractInfo.ProductId, contractInfo.EndDate)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";通过最近一份合同的日期获取早于该合同的最晚一份合同失败,ERR:", tmpErr))
- return
- }
- // 校验 上一份合同的结束日期 与 今天 相隔的天数
- betweenDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, day, lastContract.EndDate)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";计算两个日期相差的天数失败,ERR:", tmpErr))
- return
- }
- source := 2 // 正常续约
- // 如果间隔时间超过60天,那么标记为超时续约
- if betweenDay > 60 {
- source = 3 // 超时续约
- }
- // 如果间隔时间超过60天,那么标记为超时续约
- companyProductItem, tmpErr := models.GetCompanyProductItemByCompanyId(contractInfo.CompanyId, contractInfo.ProductId)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";GetCompanyProductItemByCompanyId失败,ERR:", tmpErr))
- return
- }
- var shareSellerId int
- var shareSellerName string
- if companyProductItem.IsShare == 1 {
- shareSellerId = companyProductItem.ShareSellerId
- shareSellerName = companyProductItem.ShareSeller
- }
- item := &models.CompanyRenewalRecord{
- Id: 0,
- CompanyId: contractInfo.CompanyId,
- ProductId: contractInfo.ProductId,
- Source: source,
- SellerId: companyProductItem.SellerId,
- SellerName: companyProductItem.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- tmpErr = item.Add()
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";添加续约异常记录失败,ERR:", tmpErr))
- }
- return
- }
- // HandleCompanyRenewalRecord
- // @Description: 处理续约客户的问题
- // @author: Roc
- // @datetime 2023-12-07 10:56:24
- // @param cont context.Context
- // @return err error
- func HandleCompanyRenewalRecord(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("合同处理 ErrMsg:"+err.Error(), 3)
- }
- }()
- // 需求是60个自然后未续约的客户,所以要减去61天
- endDate := time.Now().AddDate(0, 0, -61).Format(utils.FormatDate)
- total, list, err := company_contract.GetContractListByEndDate(endDate)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
- return
- }
- fmt.Printf("总共%d条数据待处理\n", total)
- if total <= 0 {
- return
- }
- companyIdList := make([]int, 0)
- for _, companyInfo := range list {
- companyIdList = append(companyIdList, companyInfo.CompanyId)
- }
- companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
- return
- }
- addList := make([]*models.CompanyRenewalRecord, 0)
- for _, v := range companyList {
- if v.Status != "正式" {
- var shareSellerId int
- var shareSellerName string
- if v.IsShare == 1 {
- shareSellerId = v.ShareSellerId
- shareSellerName = v.ShareSeller
- }
- addList = append(addList, &models.CompanyRenewalRecord{
- Id: 0,
- CompanyId: v.CompanyId,
- ProductId: v.ProductId,
- Source: 1,
- SellerId: v.SellerId,
- SellerName: v.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- if len(addList) > 0 {
- err = models.MultiAddCompanyRenewalRecord(addList)
- }
- return
- }
- // handleCompanyRenewalRecord
- // @Description: 处理合同到期后还未续约的记录
- // @author: Roc
- // @datetime 2024-02-21 13:41:48
- // @param endDate string
- // @param source int
- // @return err error
- func handleCompanyRenewalRecord(endDate string, source int) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同到期后还未续约的记录异常 ErrMsg:%s", endDate, source, err.Error()), 3)
- }
- }()
- // 根据日期获取截止日期是该日期的合同列表
- total, list, err := company_contract.GetContractListByEndDate(endDate)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
- return
- }
- fmt.Printf("总共%d条数据待处理\n", total)
- if total <= 0 {
- return
- }
- companyIdList := make([]int, 0)
- for _, companyInfo := range list {
- companyIdList = append(companyIdList, companyInfo.CompanyId)
- }
- companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
- return
- }
- addList := make([]*models.CompanyRenewalRecord, 0)
- for _, v := range companyList {
- if v.Status != "正式" {
- var shareSellerId int
- var shareSellerName string
- if v.IsShare == 1 {
- shareSellerId = v.ShareSellerId
- shareSellerName = v.ShareSeller
- }
- addList = append(addList, &models.CompanyRenewalRecord{
- Id: 0,
- CompanyId: v.CompanyId,
- ProductId: v.ProductId,
- Source: source,
- SellerId: v.SellerId,
- SellerName: v.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- if len(addList) > 0 {
- err = models.MultiAddCompanyRenewalRecord(addList)
- }
- return
- }
- // handleCompanyRenewalRecord
- // @Description: 处理合同即将到期(还未过期)后还未续约的记录
- // @author: Roc
- // @datetime 2024-02-21 13:41:48
- // @param endDate string
- // @param source int
- // @return err error
- func handleCompanyExpiringRenewalRecord(contractDate string, source int) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同即将到期(还未过期)后还未续约的记录异常 ErrMsg:%s", contractDate, source, err.Error()), 3)
- }
- }()
- // 根据日期获取截止日期是该日期的合同列表
- total, list, err := company_contract.GetContractListByEndDate(contractDate)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
- return
- }
- fmt.Printf("总共%d条数据待处理\n", total)
- if total <= 0 {
- return
- }
- companyIdList := make([]int, 0)
- for _, companyInfo := range list {
- companyIdList = append(companyIdList, companyInfo.CompanyId)
- }
- // 已经续约了的客户
- afterContractCompanyIdMap := make(map[int]int)
- {
- // 根据客户id列表和上一份合同的结束日期,获取后续的合同
- _, afterContractList, tmpErr := company_contract.GetAfterCompanyContractListByCompanyIdListAndEndDate(contractDate, companyIdList)
- if tmpErr != nil {
- err = tmpErr
- utils.FileLog.Info("company_contract.GetAfterCompanyContractListByCompanyIdListAndEndDate Err:%s" + err.Error())
- return
- }
- for _, afterContract := range afterContractList {
- afterContractCompanyIdMap[afterContract.CompanyId] = afterContract.CompanyId
- }
- }
- // 客户列表
- companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
- return
- }
- addList := make([]*models.CompanyRenewalRecord, 0)
- for _, v := range companyList {
- _, ok := afterContractCompanyIdMap[v.CompanyId]
- // 已经续约了,不处理
- if ok {
- continue
- }
- // 未续约客户,去记录
- var shareSellerId int
- var shareSellerName string
- if v.IsShare == 1 {
- shareSellerId = v.ShareSellerId
- shareSellerName = v.ShareSeller
- }
- addList = append(addList, &models.CompanyRenewalRecord{
- Id: 0,
- CompanyId: v.CompanyId,
- ProductId: v.ProductId,
- Source: source,
- SellerId: v.SellerId,
- SellerName: v.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- if len(addList) > 0 {
- err = models.MultiAddCompanyRenewalRecord(addList)
- }
- return
- }
- func HandleCompanyRenewalRecordV2(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("合同处理 ErrMsg:"+err.Error(), 3)
- }
- }()
- // 1:续约异常客户;4:合同到期后一个月未续约客户;5:合同到期未续约客户;6:合同到期前一个月还未续约的客户;7:合同到期前两个月还未续约客户;8:合同到期前3个月还未续约客户;9:合同到期前4个月还未续约客户'
- // 下面的日期都减去1天,是为了加上当日
- // 1:需求是60个自然日后未续约的客户
- {
- endDate := time.Now().AddDate(0, 0, -60-1).Format(utils.FormatDate)
- handleCompanyRenewalRecord(endDate, 1)
- }
- // 4:需求是30个自然日后未续约的客户
- {
- endDate := time.Now().AddDate(0, 0, -30-1).Format(utils.FormatDate)
- handleCompanyRenewalRecord(endDate, 4)
- }
- // 5:需求是到期日后未续约的客户
- {
- endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
- handleCompanyRenewalRecord(endDate, 5)
- }
- // 即将到期还未到期的统计
- // 合同到期前一个月还未续约的客户;
- {
- endDate := time.Now().AddDate(0, 0, 30-1).Format(utils.FormatDate)
- handleCompanyExpiringRenewalRecord(endDate, 6)
- }
- // 7:合同到期前两个月还未续约客户;
- {
- endDate := time.Now().AddDate(0, 0, 60-1).Format(utils.FormatDate)
- handleCompanyExpiringRenewalRecord(endDate, 7)
- }
- // 8:合同到期前3个月还未续约客户;
- {
- endDate := time.Now().AddDate(0, 0, 90-1).Format(utils.FormatDate)
- handleCompanyExpiringRenewalRecord(endDate, 8)
- }
- // 9:合同到期前4个月还未续约客户
- {
- endDate := time.Now().AddDate(0, 0, 120-1).Format(utils.FormatDate)
- handleCompanyExpiringRenewalRecord(endDate, 9)
- }
- return
- }
- // 续约异常客户数据修复
- func FixData() {
- //firstTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local)
- firstTime := time.Date(2024, 2, 28, 0, 0, 0, 0, time.Local)
- endTime := time.Date(2024, 2, 28, 1, 0, 0, 0, time.Local)
- for currTime := firstTime; currTime.Before(endTime); currTime = currTime.AddDate(0, 0, 1) {
- currDate := currTime.Format(utils.FormatDate)
- fmt.Printf("结束%s的数据修复\n", currDate)
- // 4:需求是30个自然日后未续约的客户
- {
- handleFixCompanyRenewalRecord(currTime.AddDate(0, 0, -30-1).Format(utils.FormatDate), currTime, 4)
- }
- // 5:需求是到期日后未续约的客户
- {
- handleFixCompanyRenewalRecord(currTime.AddDate(0, 0, -1).Format(utils.FormatDate), currTime, 5)
- }
- //即将到期还未到期的统计
- //合同到期前一个月还未续约的客户;
- {
- handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 30-1).Format(utils.FormatDate), currTime, 6)
- }
- // 7:合同到期前两个月还未续约客户;
- {
- handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 60-1).Format(utils.FormatDate), currTime, 7)
- }
- // 8:合同到期前3个月还未续约客户;
- {
- handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 90-1).Format(utils.FormatDate), currTime, 8)
- }
- // 9:合同到期前4个月还未续约客户
- {
- handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 120-1).Format(utils.FormatDate), currTime, 9)
- }
- }
- fmt.Println("end fix")
- }
- // handleFixCompanyRenewalRecord
- // @Description: 修复合同到期后N个月还未续约的客户
- // @author: Roc
- // @datetime 2024-02-21 15:29:29
- // @param endDate string
- // @param source int
- // @return err error
- func handleFixCompanyRenewalRecord(endDate string, currTime time.Time, source int) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同到期后还未续约的记录异常 ErrMsg:%s", currTime.Format(utils.FormatDate), source, err.Error()), 3)
- }
- }()
- // 根据日期获取截止日期是该日期的合同列表
- total, list, err := company_contract.GetContractListByEndDate(endDate)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
- return
- }
- fmt.Printf("总共%d条数据待处理\n", total)
- if total <= 0 {
- return
- }
- companyIdList := make([]int, 0)
- for _, companyInfo := range list {
- companyIdList = append(companyIdList, companyInfo.CompanyId)
- }
- // 已经续约了的合同列表
- afterContractCompanyIdMap := make(map[int]int)
- {
- // 根据客户id列表和上一份合同的结束日期,获取已经续约了的合同列表
- _, renewalList, tmpErr := company_contract.GetContractListByEndDateAndModifyTime(endDate, endDate, companyIdList)
- if tmpErr != nil {
- err = tmpErr
- utils.FileLog.Info("HandleCompanyRenewalRecord GetContractListByEndDateAndModifyTime Err:%s" + err.Error())
- return
- }
- for _, afterContract := range renewalList {
- afterContractCompanyIdMap[afterContract.CompanyId] = afterContract.CompanyId
- }
- }
- // 获取客户列表
- companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
- return
- }
- addList := make([]*models.CompanyRenewalRecord, 0)
- for _, v := range companyList {
- _, ok := afterContractCompanyIdMap[v.CompanyId]
- // 已经续约了,不处理
- if ok {
- continue
- }
- var shareSellerId int
- var shareSellerName string
- if v.IsShare == 1 {
- shareSellerId = v.ShareSellerId
- shareSellerName = v.ShareSeller
- }
- addList = append(addList, &models.CompanyRenewalRecord{
- Id: 0,
- CompanyId: v.CompanyId,
- ProductId: v.ProductId,
- Source: source,
- SellerId: v.SellerId,
- SellerName: v.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: currTime,
- ModifyTime: currTime,
- })
- }
- if len(addList) > 0 {
- err = models.MultiAddCompanyRenewalRecord(addList)
- }
- return
- }
- // handleFixCompanyExpiringRenewalRecord
- // @Description: 修复合同到期前N个月还未续约的客户
- // @author: Roc
- // @datetime 2024-02-21 15:29:29
- // @param endDate string
- // @param source int
- // @return err error
- func handleFixCompanyExpiringRenewalRecord(endDate string, currTime time.Time, source int) (err error) {
- currDate := currTime.Format(utils.FormatDate)
- defer func() {
- if err != nil {
- //fmt.Println("合同处理 ErrMsg:"+err.Error())
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同到期后还未续约的记录异常 ErrMsg:%s", currDate, source, err.Error()), 3)
- }
- }()
- // 根据日期获取截止日期是该日期的合同列表
- total, list, err := company_contract.GetContractListByEndDate(endDate)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
- return
- }
- fmt.Printf("总共%d条数据待处理\n", total)
- if total <= 0 {
- return
- }
- companyIdList := make([]int, 0)
- for _, companyInfo := range list {
- companyIdList = append(companyIdList, companyInfo.CompanyId)
- }
- // 已经续约了的合同列表
- afterContractCompanyIdMap := make(map[int]int)
- {
- // 根据客户id列表和上一份合同的结束日期,获取已经续约了的合同列表
- _, renewalList, tmpErr := company_contract.GetContractListByEndDateAndModifyTime(endDate, currDate, companyIdList)
- if tmpErr != nil {
- err = tmpErr
- utils.FileLog.Info("HandleCompanyRenewalRecord GetContractListByEndDateAndModifyTime Err:%s" + err.Error())
- return
- }
- for _, afterContract := range renewalList {
- afterContractCompanyIdMap[afterContract.CompanyId] = afterContract.CompanyId
- }
- }
- // 获取客户列表
- companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
- if err != nil {
- utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
- return
- }
- addList := make([]*models.CompanyRenewalRecord, 0)
- for _, v := range companyList {
- _, ok := afterContractCompanyIdMap[v.CompanyId]
- // 已经续约了,不处理
- if ok {
- continue
- }
- var shareSellerId int
- var shareSellerName string
- if v.IsShare == 1 {
- shareSellerId = v.ShareSellerId
- shareSellerName = v.ShareSeller
- }
- addList = append(addList, &models.CompanyRenewalRecord{
- Id: 0,
- CompanyId: v.CompanyId,
- ProductId: v.ProductId,
- Source: source,
- SellerId: v.SellerId,
- SellerName: v.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: currTime,
- ModifyTime: currTime,
- })
- }
- if len(addList) > 0 {
- err = models.MultiAddCompanyRenewalRecord(addList)
- }
- return
- }
|