2022-03-01 13:50:13 +08:00
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
2023-01-18 15:40:27 +08:00
|
|
|
"cutego/modules/core/api/v1/request"
|
|
|
|
"cutego/modules/core/entity"
|
2022-03-01 13:50:13 +08:00
|
|
|
"cutego/pkg/common"
|
|
|
|
"cutego/pkg/page"
|
|
|
|
"github.com/druidcaesa/gotool"
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CronJobDao struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d CronJobDao) sql(session *xorm.Session) *xorm.Session {
|
|
|
|
return session.Table("sys_cron_job")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectPage 分页查询数据
|
|
|
|
func (d CronJobDao) SelectPage(query request.CronJobQuery) ([]entity.SysCronJob, int64) {
|
|
|
|
configs := make([]entity.SysCronJob, 0)
|
|
|
|
session := d.sql(SqlDB.NewSession())
|
|
|
|
if gotool.StrUtils.HasNotEmpty(query.JobName) {
|
|
|
|
session.And("job_name like concat('%', ?, '%')", query.JobName)
|
|
|
|
}
|
2022-03-01 17:43:41 +08:00
|
|
|
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
|
|
|
session.And("status = ?", query.Status)
|
|
|
|
}
|
2022-03-01 13:50:13 +08:00
|
|
|
total, _ := page.GetTotal(session.Clone())
|
|
|
|
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&configs)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
return configs, total
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert 添加数据
|
|
|
|
func (d CronJobDao) Insert(config entity.SysCronJob) int64 {
|
|
|
|
session := SqlDB.NewSession()
|
|
|
|
session.Begin()
|
|
|
|
insert, err := session.Insert(&config)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
session.Rollback()
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
session.Commit()
|
|
|
|
return insert
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectById 查询数据
|
|
|
|
func (d CronJobDao) SelectById(id int64) *entity.SysCronJob {
|
|
|
|
config := entity.SysCronJob{}
|
|
|
|
session := d.sql(SqlDB.NewSession())
|
|
|
|
_, err := session.Where("job_id = ?", id).Get(&config)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update 修改数据
|
|
|
|
func (d CronJobDao) Update(config entity.SysCronJob) int64 {
|
2022-03-01 17:43:41 +08:00
|
|
|
session := SqlDB.NewSession()
|
|
|
|
session.Begin()
|
|
|
|
update, err := session.Where("job_id = ?", config.JobId).Update(&config)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
session.Rollback()
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
session.Commit()
|
|
|
|
return update
|
2022-03-01 13:50:13 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 14:53:01 +08:00
|
|
|
// Delete 删除数据
|
2022-03-01 13:50:13 +08:00
|
|
|
func (d CronJobDao) Delete(list []int64) bool {
|
|
|
|
session := SqlDB.NewSession()
|
|
|
|
session.Begin()
|
2022-03-01 17:43:41 +08:00
|
|
|
_, err := session.In("job_id", list).Delete(&entity.SysCronJob{})
|
2022-03-01 13:50:13 +08:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
session.Rollback()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
session.Commit()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:53:01 +08:00
|
|
|
// SelectByFuncAlias 通过方法别名获取任务详情
|
2022-03-01 13:50:13 +08:00
|
|
|
func (d CronJobDao) SelectByFuncAlias(funcAlias string) *entity.SysCronJob {
|
|
|
|
config := entity.SysCronJob{}
|
|
|
|
session := d.sql(SqlDB.NewSession())
|
|
|
|
_, err := session.Where("func_alias = ?", funcAlias).Get(&config)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &config
|
|
|
|
}
|
2023-01-18 14:53:01 +08:00
|
|
|
|
|
|
|
// SelectAll 查找所有启用状态的任务
|
|
|
|
func (d CronJobDao) SelectAll() ([]entity.SysCronJob, int) {
|
|
|
|
configs := make([]entity.SysCronJob, 0)
|
|
|
|
session := d.sql(SqlDB.NewSession())
|
|
|
|
err := session.Where("status = ?", 1).Find(&configs)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorLog(err)
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
return configs, len(configs)
|
|
|
|
}
|