Browse Source

定时任务自动检查过期服务

huangyan 9 months ago
parent
commit
ddea2d92b1
9 changed files with 80 additions and 8 deletions
  1. 39 0
      app/model/apply.go
  2. 2 0
      app/model/applyCap.go
  3. 1 0
      app/model/capabilities.go
  4. 1 0
      app/services/apply.go
  5. 10 2
      configs/config.yaml
  6. 5 1
      global/setting.go
  7. 1 0
      go.mod
  8. 2 0
      go.sum
  9. 19 5
      main.go

+ 39 - 0
app/model/apply.go

@@ -253,6 +253,22 @@ func (a Apply) AddApply(apply Apply) e.Rescode {
 		apply.BackgroundImageObscure = new(float32)
 		*apply.BackgroundImageObscure = 0.5
 	}
+	if apply.BackgroundImagePc == nil {
+		apply.BackgroundImagePc = new(string)
+		*apply.BackgroundImagePc = global.IconSetting.BackgroundImagePc
+	}
+	if apply.BackgroundImageMobile == nil {
+		apply.BackgroundImageMobile = new(string)
+		*apply.BackgroundImageMobile = global.IconSetting.BackgroundImageMobile
+	}
+	if apply.StartupDiagramPc == nil {
+		apply.StartupDiagramPc = new(string)
+		*apply.StartupDiagramPc = global.IconSetting.StartupDiagramPc
+	}
+	if apply.StartupDiagramMobile == nil {
+		apply.StartupDiagramMobile = new(string)
+		*apply.StartupDiagramMobile = global.IconSetting.StartupDiagramMobile
+	}
 	if apply.TopicPC == nil {
 		apply.TopicPC = new(int)
 		*apply.TopicPC = 1
@@ -261,6 +277,7 @@ func (a Apply) AddApply(apply Apply) e.Rescode {
 		apply.TopicMobile = new(int)
 		*apply.TopicMobile = 1
 	}
+
 	tx := global.DBLink.Create(&apply)
 	if tx.Error != nil {
 		errMsg := tx.Error.Error()
@@ -365,3 +382,25 @@ func (a Apply) CollectionList(params unity.QueryPageParams, apply Apply, phone s
 	}
 	return result, total, nil
 }
+
+// Check 检查是否过期
+func (a Apply) Check() bool {
+	//TODO implement me
+	//检查过期时间大于当前时间的应用并且更新状态为3--过期
+	var applys []Apply
+	now := utils.Time(time.Now())
+	tx := global.DBLink.Table(a.TableName()).Where("certification_time < ?", now).Where("state != ?", 3).Find(&applys)
+	if tx.Error != nil {
+		return false
+	}
+	for i, _ := range applys {
+		update := tx.Where("certification_time = ?", applys[i].CertificationTime).Update("state", 3)
+		if update.Error != nil {
+			return false
+		}
+		if update.RowsAffected > 0 {
+			return true
+		}
+	}
+	return false
+}

+ 2 - 0
app/model/applyCap.go

@@ -14,6 +14,7 @@ type ApplyCap struct {
 	CapId        string `gorm:"type:varchar(50);" json:"cap_id" validate:"required"`
 	CapName      string `gorm:"type:varchar(50);" json:"cap_name" `
 	AppId        string `gorm:"type:varchar(50);" json:"app_id" validate:"required"`
+	Icon         string `gorm:"type:varchar(255);" json:"icon"`  // 图标
 	PcIsShow     int    `gorm:"type:int;" json:"pc_is_show"`     // 是否pc端显示 1 显示 2 不显示
 	MobileIsShow int    `gorm:"type:int;" json:"mobile_is_show"` // 是否移动端显示 1 显示 2 不显示
 	Sort         int    `gorm:"type:int;" json:"sort"`           // 排序
@@ -148,6 +149,7 @@ func (a ApplyCap) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
 	a.CapId = applycap.CapID
 	a.CapName = caps.CapName
 	a.AppId = applycap.AppID
+	a.Icon = caps.ICON
 	a.PcIsShow = 1
 	a.MobileIsShow = 1
 	var maxsort int

+ 1 - 0
app/model/capabilities.go

@@ -13,6 +13,7 @@ type Capabilities struct {
 	CapName        string  `gorm:"type:varchar(255);index:idx_name,unique" json:"cap_name"` // 应用名
 	CpaType        string  `gorm:"type:varchar(50);" json:"cap_type"`                       // 应用类型
 	Image          string  `gorm:"type:varchar(255);" json:"image"`                         // 应用图片
+	ICON           string  `gorm:"type:varchar(255);" json:"icon"`                          // 应用图标
 	CapId          string  `gorm:"type:varchar(50);unique" json:"cap_id"`                   // 应用id
 	CapDescription string  `gorm:"type:varchar(255);" json:"cap_description"`               // 应用描述
 	State          int     `gorm:"type:int;" json:"state"`                                  // 状态 1启用 2禁用

+ 1 - 0
app/services/apply.go

@@ -16,4 +16,5 @@ type Apply interface {
 	ApplyAddCap(applycap model.ApplyCapabilities) e.Rescode
 	UpDateApplyCap(applycap model.ApplyCapabilities) e.Rescode
 	CollectionList(params unity.QueryPageParams, apply model.Apply, queryCond string) (result []model.AppDto, total int64, err error)
+	Check() bool
 }

+ 10 - 2
configs/config.yaml

@@ -35,7 +35,7 @@ subMail:
   appid: "97173"
   signature: "f639a60e41ee0554921d89884f5ff87e"
 redis:
-  addr: "192.168.11.17:6379"
+  addr: "192.168.11.24:6379"
   password: ""
   db: 0
 nats:
@@ -50,4 +50,12 @@ qiniu:
   bucketName: "bzdcdn"
 #  应用默认图标
 Icon:
-  iconPath: "icon/icon.png"
+  iconPath: "icon/icon.png"
+#  pc端背景图
+  BackgroundImagePc: "https://bzdcdn.baozhida.cn//UpImage/17205770412596d822-1901-407e-a55e-b0dd3518c220.png"
+  #  移动端背景图
+  BackgroundImageMobile: "https://bzdcdn.baozhida.cn//UpImage/17205770293e545d32-bc2a-4acd-83c5-ac969fcaeb26.png"
+  #  pc端启动图
+  StartupDiagramPc: "https://bzdcdn.baozhida.cn//UpImage/17205770293e545d32-bc2a-4acd-83c5-ac969fcaeb26.png"
+  #  移动端启动图
+  StartupDiagramMobile: "https://bzdcdn.baozhida.cn//UpImage/17205770293e545d32-bc2a-4acd-83c5-ac969fcaeb26.png"

+ 5 - 1
global/setting.go

@@ -57,7 +57,11 @@ type Qiniu struct {
 	BucketName      string `json:"bucketName"`
 }
 type ICon struct {
-	IconPath string `json:"iconPath"`
+	IconPath              string `json:"iconPath"`
+	BackgroundImagePc     string `json:"BackgroundImagePc"`
+	BackgroundImageMobile string `json:"BackgroundImageMobile"`
+	StartupDiagramPc      string `json:"StartupDiagramPc"`
+	StartupDiagramMobile  string `json:"StartupDiagramMobile"`
 }
 
 var (

+ 1 - 0
go.mod

@@ -51,6 +51,7 @@ require (
 	github.com/nats-io/nkeys v0.4.7 // indirect
 	github.com/nats-io/nuid v1.0.1 // indirect
 	github.com/pelletier/go-toml/v2 v2.2.2 // indirect
+	github.com/robfig/cron/v3 v3.0.1 // indirect
 	github.com/sagikazarmark/locafero v0.4.0 // indirect
 	github.com/sagikazarmark/slog-shim v0.1.0 // indirect
 	github.com/sourcegraph/conc v0.3.0 // indirect

+ 2 - 0
go.sum

@@ -123,6 +123,8 @@ github.com/qiniu/dyn v1.3.0/go.mod h1:E8oERcm8TtwJiZvkQPbcAh0RL8jO1G0VXJMW3FAWdk
 github.com/qiniu/go-sdk/v7 v7.21.1 h1:D/IjVOlg5pTw0jeDjqTo6H5QM73Obb1AYfPOHmIFN+Q=
 github.com/qiniu/go-sdk/v7 v7.21.1/go.mod h1:8EM2awITynlem2VML2dXGHkMYP2UyECsGLOdp6yMpco=
 github.com/qiniu/x v1.10.5/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
+github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
+github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
 github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
 github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
 github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=

+ 19 - 5
main.go

@@ -2,7 +2,9 @@ package main
 
 import (
 	"context"
+	"github.com/robfig/cron/v3"
 	"project_management/app"
+	"project_management/app/model"
 	"project_management/database"
 	"project_management/global"
 	"project_management/nats"
@@ -10,6 +12,7 @@ import (
 )
 
 func init() {
+
 	err := global.SetupSetting()
 	if err != nil {
 		simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "配置文件读取失败")
@@ -20,15 +23,24 @@ func init() {
 	}
 	// 数据库迁移
 	database.Migrate(global.DBLink)
-	//本地化
-	//err = unity.InitializeValidator("zh")
-	//if err != nil {
-	//	simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "本地化失败")
-	//}
 	//nats
 	nats.SetupNats()
 	//redis
 	global.SetupRedisLink()
+	c := cron.New()
+	_, err = c.AddFunc("0 0 0 * * ?", func() {
+		check := model.Apply{}.Check()
+		if !check {
+			simple_zap.WithCtx(context.Background()).Sugar().Warn("项目初始化失败")
+			return
+		}
+	})
+	if err != nil {
+		simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "项目初始化失败")
+		return
+	}
+	c.Start()
+	select {}
 }
 
 // @title 项目管理
@@ -40,8 +52,10 @@ func init() {
 //go:generate swag init --parseDependency --parseDepth=6
 func main() {
 	err := app.InitRouter()
+
 	if err != nil {
 		simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "项目初始化失败")
 		return
 	}
+
 }