package service import ( "github.com/jinzhu/gorm" "go-common/app/job/main/up/dao/upcrm" "go-common/app/job/main/up/model/signmodel" "go-common/app/service/main/upcredit/mathutil" "go-common/library/log" "time" ) // change a lot state for sign up // state, 签约状态,正常或过期 // due_wan, 签约状态即将过期 // pay_expire_state,付费状态 //CheckStateJob 检查sign_up中的状态 func (s *Service) CheckStateJob(date time.Time) { log.Info("start run state job, date=%s", date) s.checkState(date) log.Info("finish run state job, date=%s", date) } func (s *Service) checkState(date time.Time) { // 更新签约状态: var crmdb = s.crmdb.GetDb() // 签约30天内到期 var signExpireList []*signmodel.SignUp var signDueList []*signmodel.SignUp var limit, offset = 200, 0 var count = limit var err error var signEndDate = date.AddDate(0, 0, 30) for count == limit { var items []*signmodel.SignUp // 把endDate在[-60, +30]范围内的用户全都找出来,过一遍 err = crmdb.Offset(offset).Limit(limit).Where("end_date?", signEndDate, date.AddDate(0, -4, 0)).Find(&items).Error if err != nil { log.Error("fail to get sign ups, err=%v", err) break } count = len(items) offset += count for _, v := range items { if v.EndDate.Time().Before(date) && v.State == signmodel.SignStateOnSign { // 过期 signExpireList = append(signExpireList, v) } else if !(v.EndDate.Time().Before(date) || v.EndDate.Time().After(signEndDate)) && v.DueWarn <= signmodel.DueWarnNoWarn { // 快到期 signDueList = append(signDueList, v) } } } var expireIDList []uint32 for _, v := range signExpireList { expireIDList = append(expireIDList, v.ID) } err = updateListWithLimit(crmdb, "state", signmodel.SignStateExpire, expireIDList, limit) if err != nil { log.Error("fail to update sign state, err=%v", err) return } var dueIDList []uint32 for _, v := range signDueList { dueIDList = append(dueIDList, v.ID) } err = updateListWithLimit(crmdb, "due_warn", signmodel.DueWarnWarn, dueIDList, limit) if err != nil { log.Error("fail to update sign due_warn, err=%v", err) return } // 付款7天内到期 offset = 0 count = limit var payDueDate = date.AddDate(0, 0, 7) var payExpireMap = map[uint32]int8{} for count == limit { var items []*signmodel.SignPay // 把endDate在[-max, +7]范围内的用户全都找出来,过一遍 err = crmdb.Offset(offset).Limit(limit).Where("due_date