feat:完善项目
This commit is contained in:
79
cron/tenant.go
Normal file
79
cron/tenant.go
Normal file
@ -0,0 +1,79 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"SciencesServer/app/common/model"
|
||||
"SciencesServer/app/service"
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/logger"
|
||||
"SciencesServer/serve/orm"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Tenant struct{}
|
||||
|
||||
type TenantHandle func() (string, func())
|
||||
|
||||
// ContractDeadline 合同处理
|
||||
func (this *Tenant) ContractDeadline() TenantHandle {
|
||||
return func() (string, func()) {
|
||||
return "0 0 0 * * ?", func() {
|
||||
now := time.Now()
|
||||
|
||||
mSysTenant := model.NewSysTenant()
|
||||
|
||||
out := make([]*model.SysTenant, 0)
|
||||
|
||||
err := model.Find(mSysTenant, &out, &model.ModelWhereOrder{Where: model.NewWhereIn("status", []model.SysTenantStatus{
|
||||
model.SysTenantStatusForNormal, model.SysTenantStatusForWellExpire,
|
||||
})}, &model.ModelWhereOrder{Where: model.NewWhereCondition("deadline", ">=", now.AddDate(0, 0, -7))})
|
||||
|
||||
if err != nil {
|
||||
logger.ErrorF("Cron Tenant ContractDeadline Error:%v", err)
|
||||
return
|
||||
}
|
||||
well := make([]uint64, 0)
|
||||
expired := make([]uint64, 0)
|
||||
expiredKey := make([]string, 0)
|
||||
|
||||
for _, v := range out {
|
||||
// 超过协议时间
|
||||
if v.Deadline.After(now) {
|
||||
expired = append(expired, v.ID)
|
||||
expiredKey = append(expiredKey, v.Key)
|
||||
continue
|
||||
}
|
||||
if v.Status == model.SysTenantStatusForNormal {
|
||||
well = append(well, v.ID)
|
||||
}
|
||||
}
|
||||
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||||
if len(well) > 0 {
|
||||
if err = model.UpdatesWhere(mSysTenant, map[string]interface{}{
|
||||
"status": model.SysTenantStatusForWellExpire, "updated_at": now,
|
||||
}, []*model.ModelWhere{model.NewWhereIn("id", well)}, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(expired) > 0 {
|
||||
if err = model.UpdatesWhere(mSysTenant, map[string]interface{}{
|
||||
"status": model.SysTenantStatusForExpired, "updated_at": now,
|
||||
}, []*model.ModelWhere{model.NewWhereIn("id", expired)}, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
service.Publish(config.EventForRedisListDestroy, config.RedisKeyForTenant, expiredKey)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
logger.ErrorF("Cron Tenant ContractDeadline Transaction Error:%v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewTenant() *Tenant {
|
||||
return &Tenant{}
|
||||
}
|
Reference in New Issue
Block a user