增加删除N个月前的任务日志API

pull/21/merge
ouqiang 2017-05-10 16:09:10 +08:00
parent 0dfa998d11
commit 0bc4070762
4 changed files with 29 additions and 6 deletions

View File

@ -43,7 +43,7 @@
4. 浏览器访问 http://localhost:5920
### 源码安装
1. `go`语言版本1.7+
2. `go get -d https://github.com/ouqiang/gocron`
2. `go get -d github.com/ouqiang/gocron`
3. 编译 `go build`
4. 启动、访问方式同上

View File

@ -73,8 +73,14 @@ func (taskLog *TaskLog) List(params CommonMap) ([]TaskLog, error) {
}
// 清空表
func (TaskLog *TaskLog) Clear() (int64, error) {
return Db.Where("1=1").Delete(TaskLog);
func (taskLog *TaskLog) Clear() (int64, error) {
return Db.Where("1=1").Delete(taskLog);
}
// 删除N个月前的日志
func (taskLog *TaskLog) Remove(id int) (int64, error) {
t := time.Now().AddDate(0, -id, 0)
return Db.Where("start_time <= ?", t.Format(DefaultTimeFormat)).Delete(taskLog)
}
func (taskLog *TaskLog) Total(params CommonMap) (int64, error) {

View File

@ -89,6 +89,7 @@ func Register(m *macaron.Macaron) {
// API
m.Group("/api/v1", func() {
m.Route("/tasklog/update-status", "GET,POST", tasklog.UpdateStatus)
m.Post("/tasklog/remove/:id", tasklog.Remove)
});
// 404错误

View File

@ -1,5 +1,7 @@
package tasklog
// 任务日志
import (
"gopkg.in/macaron.v1"
"github.com/ouqiang/gocron/models"
@ -11,9 +13,6 @@ import (
"github.com/ouqiang/gocron/routers/base"
)
// @author qiang.ou<qingqianludao@gmail.com>
// @date 2017/4/7-21:18
func Index(ctx *macaron.Context) {
logModel := new(models.TaskLog)
queryParams := parseQueryParams(ctx)
@ -49,12 +48,29 @@ func Clear(ctx *macaron.Context) string {
return json.Success(utils.SuccessContent, nil)
}
// 删除N个月前的日志
func Remove(ctx *macaron.Context) string {
month := ctx.ParamsInt(":id")
json := utils.JsonResponse{}
if month < 1 || month > 12 {
return json.CommonFailure("参数取值范围1-12")
}
taskLogModel := new(models.TaskLog)
_, err := taskLogModel.Remove(month)
if err != nil {
return json.CommonFailure("删除失败", err)
}
return json.Success("删除成功", nil)
}
// 更新任务状态
func UpdateStatus(ctx *macaron.Context) string {
id := ctx.QueryTrim("id")
status := ctx.QueryInt("status")
result := ctx.QueryTrim("result")
json := utils.JsonResponse{}
if id == "" {
return json.CommonFailure("任务ID不能为空")
}