package services import ( "context" "fmt" "hongze/hongze_task/models" "hongze/hongze_task/models/eta_business" "hongze/hongze_task/services/alarm_msg" "hongze/hongze_task/utils" "time" ) // EtaBusinessExpiredRemind ETA商家到期提醒 func EtaBusinessExpiredRemind(cont context.Context) (err error) { // 频次: 1-1天; 2-7天; 3-15天; 4-30天; 5-60天 frequencyArr := []int{1, 2, 3, 4, 5} for _, f := range frequencyArr { _ = HandleEtaBusinessExpiredRemind(f) time.Sleep(15 * time.Second) } return } // HandleEtaBusinessExpiredRemind ETA商家到期提醒 func HandleEtaBusinessExpiredRemind(frequency int) (err error) { defer func() { if err != nil { tips := fmt.Sprintf("ETA商家到期提醒失败, frequency: %d, Err: %s", frequency, err.Error()) utils.FileLog.Info("%s", tips) go alarm_msg.SendAlarmMsg(tips, 3) } }() // 频次对应天数 dayMap := map[int]int{1: 1, 2: 7, 3: 15, 4: 30, 5: 60} days := dayMap[frequency] if days <= 0 { err = fmt.Errorf("提醒频次有误") return } // 获取当前合约N后过期的ETA商家 monthLater := time.Now().Local().AddDate(0, 0, days) expiredTime := monthLater.Format(utils.FormatDate) businesses := make([]*eta_business.EtaBusiness, 0) { businessOb := new(eta_business.EtaBusiness) cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.ExpiredTime) pars := make([]interface{}, 0) pars = append(pars, expiredTime) list, e := businessOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { err = fmt.Errorf("获取商家列表失败, Err: " + e.Error()) return } businesses = list } if len(businesses) == 0 { return } // 获取销售 sellers, e := models.GetSellersOpenId() if e != nil { err = fmt.Errorf("获取销售信息失败, Err: " + e.Error()) return } // 以销售为单位 sellerBusinessMap := make(map[int][]*eta_business.EtaBusiness) for _, v := range businesses { if sellerBusinessMap[v.SellerId] == nil { sellerBusinessMap[v.SellerId] = make([]*eta_business.EtaBusiness, 0) } sellerBusinessMap[v.SellerId] = append(sellerBusinessMap[v.SellerId], v) } sellerMap := make(map[int]*models.Sellers) for _, s := range sellers { sellerMap[s.AdminId] = s } // 推送邮件和公众号 remindRecords := make([]*eta_business.EtaBusinessRemindRecord, 0) for k, v := range sellerBusinessMap { seller := sellerMap[k] if seller == nil { continue } if seller.Email == "" && seller.OpenId == "" { continue } if len(v) == 0 { continue } uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), frequency, utils.GetRandDigit(5)) contentRemark := "" contentsEmail := fmt.Sprintf(`

您有【%d】ETA客户将于%d天后到期,请注意查看

`, len(v), days) contentsEmail += fmt.Sprintf(``, days) for _, bz := range v { row := fmt.Sprintf(``, bz.BusinessName, bz.ExpiredTime.Format(utils.FormatDate), bz.SellerName, "ETA") contentsEmail += row contentRemark += fmt.Sprintf("【%s】", bz.BusinessName) // 数据入库 remindRecords = append(remindRecords, &eta_business.EtaBusinessRemindRecord{ Type: frequency, SellerId: seller.AdminId, SellerName: seller.RealName, EtaBusinessId: bz.EtaBusinessId, BusinessName: bz.BusinessName, EndDate: bz.ExpiredTime.Format(utils.FormatDate), UniqueCode: uniqueCode, CreateTime: time.Now(), }) } contentsEmail += "
%d天后到期客户名称到期日期销售人员客户类型
%s%s%s%s
" // 邮件 if seller.Email != "" { msg := fmt.Sprintf("到期前%d天提醒", days) utils.SendEmailByHongze(msg, contentsEmail, seller.Email, "", "") } // 公众号, first和remark已经无效了 if seller.OpenId != "" { first := fmt.Sprintf(`您有【%d】 客户将于%d天后到期,请注意查看`, len(v), days) keyword1 := fmt.Sprintf(`【%d】ETA到期前%d天提醒,点击查看`, len(v), days) keyword2 := expiredTime remark := contentRemark openIdList := make([]*models.OpenIdList, 0) openIdItem := new(models.OpenIdList) openIdItem.OpenId = seller.OpenId openIdList = append(openIdList, openIdItem) _ = SendWxMsgWithEtaBusinessRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList) } // 数据入库 if len(remindRecords) > 0 { recordOb := new(eta_business.EtaBusinessRemindRecord) if e = recordOb.CreateMulti(remindRecords); e != nil { err = fmt.Errorf("批量新增ETA商家提醒记录失败, Err: %s", e.Error()) return } } } return } // EtaBusinessUpdateStatus 每日更新ETA商家签约状态 func EtaBusinessUpdateStatus(cont context.Context) (err error) { defer func() { if err != nil { tips := "ETA商家签约状态更新: EtaBusinessUpdateStatus, Err: " + err.Error() utils.FileLog.Info("%s", tips) go alarm_msg.SendAlarmMsg(tips, 3) } }() // 获取所有商家 businesses := make([]*eta_business.EtaBusiness, 0) { ob := new(eta_business.EtaBusiness) cond := `` pars := make([]interface{}, 0) list, e := ob.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { err = fmt.Errorf("获取商家列表失败, Err: " + e.Error()) return } businesses = list } if len(businesses) == 0 { return } // 获取所有商家签约 contracts := make([]*eta_business.EtaBusinessContract, 0) { ob := new(eta_business.EtaBusinessContract) cond := `` pars := make([]interface{}, 0) list, e := ob.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { err = fmt.Errorf("获取商家合同列表失败, Err: " + e.Error()) return } contracts = list } if len(contracts) == 0 { return } businessContracts := make(map[int][]*eta_business.EtaBusinessContract) // 商家对应的所有签约 for _, c := range contracts { if businessContracts[c.EtaBusinessId] == nil { businessContracts[c.EtaBusinessId] = make([]*eta_business.EtaBusinessContract, 0) } businessContracts[c.EtaBusinessId] = append(businessContracts[c.EtaBusinessId], c) } // 遍历判断商家当前合约的时间, 更新签约状态 updateBusiness := make([]*eta_business.EtaBusiness, 0) strToday := time.Now().Format(utils.FormatDate) today, _ := time.ParseInLocation(utils.FormatDate, strToday, time.Local) for _, b := range businesses { cs := businessContracts[b.EtaBusinessId] // 待签约 if cs == nil || (cs != nil && len(cs) == 0) { //if b.SigningStatus != eta_business.EtaBusinessSigningStatusTerminate { // b.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate // b.ModifyTime = time.Now().Local() // updateBusiness = append(updateBusiness, b) //} continue } // 有签约 using := false // 是否在任一存续期内 for _, c := range cs { // 当前合约 if today.Equal(c.SigningTime) || today.Equal(c.ExpiredTime) || (today.After(c.SigningTime) && today.Before(c.ExpiredTime)) { b.ContractId = c.EtaBusinessContractId b.SigningTime = c.SigningTime b.ExpiredTime = c.ExpiredTime b.ModifyTime = time.Now().Local() // 是否为首次签约 if c.IsFirst == 1 { b.SigningStatus = eta_business.EtaBusinessSigningStatusFirst } else { b.SigningStatus = eta_business.EtaBusinessSigningStatusContinue } updateBusiness = append(updateBusiness, b) using = true break } } // 不存在任一合同期内 if !using { b.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate updateBusiness = append(updateBusiness, b) } } // 更新签约状态 if len(updateBusiness) > 0 { ob := new(eta_business.EtaBusiness) cols := []string{"ContractId", "SigningStatus", "SigningTime", "ExpiredTime", "ModifyTime"} if e := ob.UpdateMulti(updateBusiness, cols); e != nil { err = fmt.Errorf("批量更新签约状态失败, Err: %s", e.Error()) return } } return }