cutego/modules/core/dao/cron_job_dao.go

116 lines
2.9 KiB
Go
Raw Normal View History

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"
2023-01-18 17:21:12 +08:00
"cutego/modules/core/dataobject"
2023-01-18 17:46:25 +08:00
"cutego/pkg/logging"
2022-03-01 13:50:13 +08:00
"cutego/pkg/page"
2023-01-18 17:09:49 +08:00
"cutego/refs"
2022-03-01 13:50:13 +08:00
"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 分页查询数据
2023-01-18 17:21:12 +08:00
func (d CronJobDao) SelectPage(query request.CronJobQuery) ([]dataobject.SysCronJob, int64) {
configs := make([]dataobject.SysCronJob, 0)
2023-01-18 17:09:49 +08:00
session := d.sql(refs.SqlDB.NewSession())
2022-03-01 13:50:13 +08:00
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 {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return nil, 0
}
return configs, total
}
// Insert 添加数据
2023-01-18 17:21:12 +08:00
func (d CronJobDao) Insert(config dataobject.SysCronJob) int64 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
insert, err := session.Insert(&config)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
session.Rollback()
return 0
}
session.Commit()
return insert
}
// SelectById 查询数据
2023-01-18 17:21:12 +08:00
func (d CronJobDao) SelectById(id int64) *dataobject.SysCronJob {
config := dataobject.SysCronJob{}
2023-01-18 17:09:49 +08:00
session := d.sql(refs.SqlDB.NewSession())
2022-03-01 13:50:13 +08:00
_, err := session.Where("job_id = ?", id).Get(&config)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return nil
}
return &config
}
// Update 修改数据
2023-01-18 17:21:12 +08:00
func (d CronJobDao) Update(config dataobject.SysCronJob) int64 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 17:43:41 +08:00
session.Begin()
update, err := session.Where("job_id = ?", config.JobId).Update(&config)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 17:43:41 +08:00
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 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
2023-01-18 17:21:12 +08:00
_, err := session.In("job_id", list).Delete(&dataobject.SysCronJob{})
2022-03-01 13:50:13 +08:00
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
session.Rollback()
return false
}
session.Commit()
return true
}
2023-01-18 14:53:01 +08:00
// SelectByFuncAlias 通过方法别名获取任务详情
2023-01-18 17:21:12 +08:00
func (d CronJobDao) SelectByFuncAlias(funcAlias string) *dataobject.SysCronJob {
config := dataobject.SysCronJob{}
2023-01-18 17:09:49 +08:00
session := d.sql(refs.SqlDB.NewSession())
2022-03-01 13:50:13 +08:00
_, err := session.Where("func_alias = ?", funcAlias).Get(&config)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return nil
}
return &config
}
2023-01-18 14:53:01 +08:00
// SelectAll 查找所有启用状态的任务
2023-01-18 17:21:12 +08:00
func (d CronJobDao) SelectAll() ([]dataobject.SysCronJob, int) {
configs := make([]dataobject.SysCronJob, 0)
2023-01-18 17:09:49 +08:00
session := d.sql(refs.SqlDB.NewSession())
2023-01-18 14:53:01 +08:00
err := session.Where("status = ?", 1).Find(&configs)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2023-01-18 14:53:01 +08:00
return nil, 0
}
return configs, len(configs)
}