From ca98fd873cae66fb6c7f0f9684f51e0740e502b6 Mon Sep 17 00:00:00 2001 From: linwenxiang <991154416@qq.com> Date: Sat, 29 Aug 2020 18:54:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9curd=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/sysjob/sysjob.go | 46 +++++++++++++------------- models/sysjob.go | 69 ++++++--------------------------------- models/tools/systables.go | 12 +++++-- 3 files changed, 43 insertions(+), 84 deletions(-) diff --git a/apis/sysjob/sysjob.go b/apis/sysjob/sysjob.go index 18e495da..9fc97cfe 100644 --- a/apis/sysjob/sysjob.go +++ b/apis/sysjob/sysjob.go @@ -20,10 +20,10 @@ func GetSysJobList(c *gin.Context) { var pageIndex = 1 if size := c.Request.FormValue("pageSize"); size != "" { - pageSize,err = tools.StringToInt(size) + pageSize, err = tools.StringToInt(size) } if index := c.Request.FormValue("pageIndex"); index != "" { - pageIndex,err = tools.StringToInt(index) + pageIndex, err = tools.StringToInt(index) } var v dto.SysJobSearch @@ -43,10 +43,10 @@ func GetSysJob(c *gin.Context) { err := c.BindUri(&v) tools.HasError(err, "", 500) data.JobId, _ = tools.StringToInt(v.Id) - result, err := data.Get() + err = data.Get(data.JobId) tools.HasError(err, "抱歉未找到相关信息", -1) - app.OK(c, result, "") + app.OK(c, data, "") } func InsertSysJob(c *gin.Context) { @@ -54,9 +54,9 @@ func InsertSysJob(c *gin.Context) { err := c.ShouldBindJSON(&data) data.CreateBy = tools.GetUserIdStr(c) tools.HasError(err, "", 500) - result, err := data.Create() + err = data.Create() tools.HasError(err, "", -1) - app.OK(c, result, "") + app.OK(c, data, "") } func UpdateSysJob(c *gin.Context) { @@ -64,10 +64,10 @@ func UpdateSysJob(c *gin.Context) { err := c.ShouldBindJSON(&data) tools.HasError(err, "数据解析失败", -1) data.UpdateBy = tools.GetUserIdStr(c) - result, err := data.Update(data.JobId) + _, err = data.Update(data.JobId) tools.HasError(err, "", -1) - app.OK(c, result, "") + app.OK(c, data, "") } func DeleteSysJob(c *gin.Context) { @@ -78,7 +78,7 @@ func DeleteSysJob(c *gin.Context) { tools.HasError(err, "", 500) data.UpdateBy = tools.GetUserIdStr(c) IDS := tools.IdsStrToIdsIntGroupStr(v.Id) - _, err = data.BatchDelete(IDS) + err = data.BatchDelete(IDS) tools.HasError(err, msg.DeletedFail, 500) app.OK(c, nil, msg.DeletedSuccess) } @@ -89,14 +89,14 @@ func RemoveJob(c *gin.Context) { err := c.BindUri(&v) tools.HasError(err, "", 500) data.JobId, _ = tools.StringToInt(v.Id) - result, err := data.Get() + err = data.Get(data.JobId) tools.HasError(err, "", 500) - cn := jobs.Remove(result.EntryId) + cn := jobs.Remove(data.EntryId) select { case res := <-cn: if res { - _, _ = data.RemoveEntryID(result.EntryId) + _, _ = data.RemoveEntryID(data.EntryId) app.OK(c, nil, msg.DeletedSuccess) } case <-time.After(time.Second * 1): @@ -111,22 +111,22 @@ func StartJob(c *gin.Context) { err := c.BindUri(&v) tools.HasError(err, "", 500) data.JobId, _ = tools.StringToInt(v.Id) - result, err := data.Get() + err = data.Get(data.JobId) tools.HasError(err, "", 500) - if result.JobType == 1 { + if data.JobType == 1 { var j = &jobs.HttpJob{} - j.InvokeTarget = result.InvokeTarget - j.CronExpression = result.CronExpression - j.JobId = result.JobId - j.Name = result.JobName + j.InvokeTarget = data.InvokeTarget + j.CronExpression = data.CronExpression + j.JobId = data.JobId + j.Name = data.JobName data.EntryId, err = jobs.AddJob(j) } else { var j = &jobs.ExecJob{} - j.InvokeTarget = result.InvokeTarget - j.CronExpression = result.CronExpression - j.JobId = result.JobId - j.Name = result.JobName - j.Args = result.Args + j.InvokeTarget = data.InvokeTarget + j.CronExpression = data.CronExpression + j.JobId = data.JobId + j.Name = data.JobName + j.Args = data.Args data.EntryId, err = jobs.AddJob(j) } diff --git a/models/sysjob.go b/models/sysjob.go index 6d2b203c..0100d615 100644 --- a/models/sysjob.go +++ b/models/sysjob.go @@ -28,50 +28,13 @@ func (SysJob) TableName() string { } // 创建SysJob -func (e *SysJob) Create() (SysJob, error) { - var doc SysJob - result := orm.Eloquent.Table(e.TableName()).Create(&e) - if result.Error != nil { - err := result.Error - return doc, err - } - doc = *e - return doc, nil +func (e *SysJob) Create() (err error) { + return orm.Eloquent.Table(e.TableName()).Create(e).Error } // 获取SysJob -func (e *SysJob) Get() (SysJob, error) { - var doc SysJob - table := orm.Eloquent.Table(e.TableName()) - - if e.JobId != 0 { - table = table.Where("job_id = ?", e.JobId) - } - - if e.JobName != "" { - table = table.Where("job_name like ?", "%"+e.JobName+"%") - } - - if e.JobGroup != "" { - table = table.Where("job_group = ?", e.JobGroup) - } - - if e.CronExpression != "" { - table = table.Where("cron_expression = ?", e.CronExpression) - } - - if e.InvokeTarget != "" { - table = table.Where("invoke_target = ?", e.InvokeTarget) - } - - if e.Status != 0 { - table = table.Where("status = ?", e.Status) - } - - if err := table.First(&doc).Error; err != nil { - return doc, err - } - return doc, nil +func (e *SysJob) Get(id interface{}) (err error) { + return orm.Eloquent.Table(e.TableName()).First(e, id).Error } // 获取SysJob带分页 @@ -109,17 +72,9 @@ func (e *SysJob) GetList() ([]SysJob, error) { } // 更新SysJob -func (e *SysJob) Update(id int) (update SysJob, err error) { - if err = orm.Eloquent.Table(e.TableName()).Where("job_id = ?", id).First(&update).Error; err != nil { - return - } - - //参数1:是要修改的数据 - //参数2:是修改的数据 - if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil { - return - } - return +func (e *SysJob) Update(id interface{}) (rowsAffected int64, err error) { + result := orm.Eloquent.Table(e.TableName()).Where(id).Updates(&e) + return result.RowsAffected, result.Error } func (e *SysJob) RemoveAllEntryID() (update SysJob, err error) { @@ -144,7 +99,7 @@ func (e *SysJob) RemoveEntryID(entryID int) (update SysJob, err error) { // 删除SysJob func (e *SysJob) Delete(id int) (success bool, err error) { - if err = orm.Eloquent.Table(e.TableName()).Where("job_id = ?", id).Delete(&SysJob{}).Error; err != nil { + if err = orm.Eloquent.Table(e.TableName()).Where(id).Delete(&SysJob{}).Error; err != nil { success = false return } @@ -153,10 +108,6 @@ func (e *SysJob) Delete(id int) (success bool, err error) { } //批量删除 -func (e *SysJob) BatchDelete(id []int) (Result bool, err error) { - if err = orm.Eloquent.Table(e.TableName()).Where("job_id in (?)", id).Delete(&SysJob{}).Error; err != nil { - return - } - Result = true - return +func (e *SysJob) BatchDelete(id []int) error { + return orm.Eloquent.Table(e.TableName()).Where(id).Delete(&SysJob{}).Error } diff --git a/models/tools/systables.go b/models/tools/systables.go index af73bff5..372855ff 100644 --- a/models/tools/systables.go +++ b/models/tools/systables.go @@ -175,11 +175,19 @@ func (e *SysTables) Update() (update SysTables, err error) { } func (e *SysTables) Delete() (success bool, err error) { - if err = orm.Eloquent.Table("sys_tables").Delete(SysTables{}, "table_id = ?", e.TableId).Error; err != nil { + tx := orm.Eloquent.Begin() + defer func() { + if err != nil { + tx.Rollback() + } else { + tx.Commit() + } + }() + if err = tx.Table("sys_tables").Delete(SysTables{}, "table_id = ?", e.TableId).Error; err != nil { success = false return } - if err = orm.Eloquent.Table("sys_columns").Delete(SysColumns{}, "table_id = ?", e.TableId).Error; err != nil { + if err = tx.Table("sys_columns").Delete(SysColumns{}, "table_id = ?", e.TableId).Error; err != nil { success = false return }