修改curd基本写法
This commit is contained in:
1 parent
55496078f0
commit
ca98fd873c
3 files changed
+41
-82
No files matched your search
+21
-21
@@ -43,10 +43,10 @@ func GetSysJob(c *gin.Context) {
|
|||||||
err := c.BindUri(&v)
|
err := c.BindUri(&v)
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
data.JobId, _ = tools.StringToInt(v.Id)
|
data.JobId, _ = tools.StringToInt(v.Id)
|
||||||
result, err := data.Get()
|
err = data.Get(data.JobId)
|
||||||
tools.HasError(err, "抱歉未找到相关信息", -1)
|
tools.HasError(err, "抱歉未找到相关信息", -1)
|
||||||
|
|
||||||
app.OK(c, result, "")
|
app.OK(c, data, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func InsertSysJob(c *gin.Context) {
|
func InsertSysJob(c *gin.Context) {
|
||||||
@@ -54,9 +54,9 @@ func InsertSysJob(c *gin.Context) {
|
|||||||
err := c.ShouldBindJSON(&data)
|
err := c.ShouldBindJSON(&data)
|
||||||
data.CreateBy = tools.GetUserIdStr(c)
|
data.CreateBy = tools.GetUserIdStr(c)
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
result, err := data.Create()
|
err = data.Create()
|
||||||
tools.HasError(err, "", -1)
|
tools.HasError(err, "", -1)
|
||||||
app.OK(c, result, "")
|
app.OK(c, data, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateSysJob(c *gin.Context) {
|
func UpdateSysJob(c *gin.Context) {
|
||||||
@@ -64,10 +64,10 @@ func UpdateSysJob(c *gin.Context) {
|
|||||||
err := c.ShouldBindJSON(&data)
|
err := c.ShouldBindJSON(&data)
|
||||||
tools.HasError(err, "数据解析失败", -1)
|
tools.HasError(err, "数据解析失败", -1)
|
||||||
data.UpdateBy = tools.GetUserIdStr(c)
|
data.UpdateBy = tools.GetUserIdStr(c)
|
||||||
result, err := data.Update(data.JobId)
|
_, err = data.Update(data.JobId)
|
||||||
tools.HasError(err, "", -1)
|
tools.HasError(err, "", -1)
|
||||||
|
|
||||||
app.OK(c, result, "")
|
app.OK(c, data, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteSysJob(c *gin.Context) {
|
func DeleteSysJob(c *gin.Context) {
|
||||||
@@ -78,7 +78,7 @@ func DeleteSysJob(c *gin.Context) {
|
|||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
data.UpdateBy = tools.GetUserIdStr(c)
|
data.UpdateBy = tools.GetUserIdStr(c)
|
||||||
IDS := tools.IdsStrToIdsIntGroupStr(v.Id)
|
IDS := tools.IdsStrToIdsIntGroupStr(v.Id)
|
||||||
_, err = data.BatchDelete(IDS)
|
err = data.BatchDelete(IDS)
|
||||||
tools.HasError(err, msg.DeletedFail, 500)
|
tools.HasError(err, msg.DeletedFail, 500)
|
||||||
app.OK(c, nil, msg.DeletedSuccess)
|
app.OK(c, nil, msg.DeletedSuccess)
|
||||||
}
|
}
|
||||||
@@ -89,14 +89,14 @@ func RemoveJob(c *gin.Context) {
|
|||||||
err := c.BindUri(&v)
|
err := c.BindUri(&v)
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
data.JobId, _ = tools.StringToInt(v.Id)
|
data.JobId, _ = tools.StringToInt(v.Id)
|
||||||
result, err := data.Get()
|
err = data.Get(data.JobId)
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
cn := jobs.Remove(result.EntryId)
|
cn := jobs.Remove(data.EntryId)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case res := <-cn:
|
case res := <-cn:
|
||||||
if res {
|
if res {
|
||||||
_, _ = data.RemoveEntryID(result.EntryId)
|
_, _ = data.RemoveEntryID(data.EntryId)
|
||||||
app.OK(c, nil, msg.DeletedSuccess)
|
app.OK(c, nil, msg.DeletedSuccess)
|
||||||
}
|
}
|
||||||
case <-time.After(time.Second * 1):
|
case <-time.After(time.Second * 1):
|
||||||
@@ -111,22 +111,22 @@ func StartJob(c *gin.Context) {
|
|||||||
err := c.BindUri(&v)
|
err := c.BindUri(&v)
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
data.JobId, _ = tools.StringToInt(v.Id)
|
data.JobId, _ = tools.StringToInt(v.Id)
|
||||||
result, err := data.Get()
|
err = data.Get(data.JobId)
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
if result.JobType == 1 {
|
if data.JobType == 1 {
|
||||||
var j = &jobs.HttpJob{}
|
var j = &jobs.HttpJob{}
|
||||||
j.InvokeTarget = result.InvokeTarget
|
j.InvokeTarget = data.InvokeTarget
|
||||||
j.CronExpression = result.CronExpression
|
j.CronExpression = data.CronExpression
|
||||||
j.JobId = result.JobId
|
j.JobId = data.JobId
|
||||||
j.Name = result.JobName
|
j.Name = data.JobName
|
||||||
data.EntryId, err = jobs.AddJob(j)
|
data.EntryId, err = jobs.AddJob(j)
|
||||||
} else {
|
} else {
|
||||||
var j = &jobs.ExecJob{}
|
var j = &jobs.ExecJob{}
|
||||||
j.InvokeTarget = result.InvokeTarget
|
j.InvokeTarget = data.InvokeTarget
|
||||||
j.CronExpression = result.CronExpression
|
j.CronExpression = data.CronExpression
|
||||||
j.JobId = result.JobId
|
j.JobId = data.JobId
|
||||||
j.Name = result.JobName
|
j.Name = data.JobName
|
||||||
j.Args = result.Args
|
j.Args = data.Args
|
||||||
data.EntryId, err = jobs.AddJob(j)
|
data.EntryId, err = jobs.AddJob(j)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-59
@@ -28,50 +28,13 @@ func (SysJob) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建SysJob
|
// 创建SysJob
|
||||||
func (e *SysJob) Create() (SysJob, error) {
|
func (e *SysJob) Create() (err error) {
|
||||||
var doc SysJob
|
return orm.Eloquent.Table(e.TableName()).Create(e).Error
|
||||||
result := orm.Eloquent.Table(e.TableName()).Create(&e)
|
|
||||||
if result.Error != nil {
|
|
||||||
err := result.Error
|
|
||||||
return doc, err
|
|
||||||
}
|
|
||||||
doc = *e
|
|
||||||
return doc, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取SysJob
|
// 获取SysJob
|
||||||
func (e *SysJob) Get() (SysJob, error) {
|
func (e *SysJob) Get(id interface{}) (err error) {
|
||||||
var doc SysJob
|
return orm.Eloquent.Table(e.TableName()).First(e, id).Error
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取SysJob带分页
|
// 获取SysJob带分页
|
||||||
@@ -109,17 +72,9 @@ func (e *SysJob) GetList() ([]SysJob, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新SysJob
|
// 更新SysJob
|
||||||
func (e *SysJob) Update(id int) (update SysJob, err error) {
|
func (e *SysJob) Update(id interface{}) (rowsAffected int64, err error) {
|
||||||
if err = orm.Eloquent.Table(e.TableName()).Where("job_id = ?", id).First(&update).Error; err != nil {
|
result := orm.Eloquent.Table(e.TableName()).Where(id).Updates(&e)
|
||||||
return
|
return result.RowsAffected, result.Error
|
||||||
}
|
|
||||||
|
|
||||||
//参数1:是要修改的数据
|
|
||||||
//参数2:是修改的数据
|
|
||||||
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *SysJob) RemoveAllEntryID() (update SysJob, err error) {
|
func (e *SysJob) RemoveAllEntryID() (update SysJob, err error) {
|
||||||
@@ -144,7 +99,7 @@ func (e *SysJob) RemoveEntryID(entryID int) (update SysJob, err error) {
|
|||||||
|
|
||||||
// 删除SysJob
|
// 删除SysJob
|
||||||
func (e *SysJob) Delete(id int) (success bool, err error) {
|
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
|
success = false
|
||||||
return
|
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) {
|
func (e *SysJob) BatchDelete(id []int) error {
|
||||||
if err = orm.Eloquent.Table(e.TableName()).Where("job_id in (?)", id).Delete(&SysJob{}).Error; err != nil {
|
return orm.Eloquent.Table(e.TableName()).Where(id).Delete(&SysJob{}).Error
|
||||||
return
|
|
||||||
}
|
|
||||||
Result = true
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
@@ -175,11 +175,19 @@ func (e *SysTables) Update() (update SysTables, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *SysTables) Delete() (success bool, 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
|
success = false
|
||||||
return
|
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
|
success = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user