123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- 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(`<div><p>您有【%d】ETA客户将于%d天后到期,请注意查看</p>`, len(v), days)
- contentsEmail += fmt.Sprintf(`<table border='1'><tr><td width='200'>%d天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>`, days)
- for _, bz := range v {
- row := fmt.Sprintf(`<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, 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 += "</table></div>"
- // 邮件
- 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
- }
|