62 Star 380 Fork 123

admpub / nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
exportLog.go 4.05 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-05-10 23:35 . v2.0.0
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package collector
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/handler"
"github.com/webx-top/db"
"github.com/webx-top/echo"
)
func ExportLog(ctx echo.Context) error {
exportID := ctx.Formx(`exportId`).Uint()
totalRows := ctx.Formx(`rows`).Int()
m := &dbschema.CollectorExportLog{}
page, size, totalRows, p := handler.PagingWithPagination(ctx)
cond := db.Cond{}
var export *dbschema.CollectorExport
var err error
if exportID > 0 {
export = &dbschema.CollectorExport{}
err = export.Get(nil, `id`, exportID)
cond[`export_id`] = exportID
}
cnt, err2 := m.List(nil, func(r db.Result) db.Result {
return r.OrderBy(`-id`)
}, page, size, cond)
if err2 != nil {
err = err2
}
if totalRows <= 0 {
totalRows = int(cnt())
p.SetRows(totalRows)
}
ctx.Set(`listData`, m.Objects())
ctx.Set(`pagination`, p)
if export == nil {
export = &dbschema.CollectorExport{}
}
ctx.Set(`export`, export)
ret := handler.Err(ctx, err)
ctx.Set(`activeURL`, `/collector/export`)
return ctx.Render(`collector/export_log`, ret)
}
func renderLogViewData(ctx echo.Context, m *dbschema.CollectorExportLog, err error) error {
ctx.Set(`data`, m)
ctx.Set(`activeURL`, `/collector/export`)
var export *dbschema.CollectorExport
if m.ExportId > 0 {
export = &dbschema.CollectorExport{}
err = export.Get(nil, `id`, m.ExportId)
}
ctx.Set(`export`, export)
return ctx.Render(`collector/export_log_view`, handler.Err(ctx, err))
}
func ExportLogView(ctx echo.Context) error {
id := ctx.Paramx(`id`).Uint()
m := &dbschema.CollectorExportLog{}
err := m.Get(nil, `id`, id)
if err != nil {
handler.SendFail(ctx, err.Error())
return ctx.Redirect(handler.URLFor(`/collector/export_log`))
}
return renderLogViewData(ctx, m, err)
}
func ExportLogDelete(ctx echo.Context) error {
id := ctx.Formx(`id`).Uint()
exportId := ctx.Formx(`exportId`).Uint()
m := &dbschema.CollectorExportLog{}
var (
cond db.Cond
err error
n int
)
if id > 0 {
cond = db.Cond{`id`: id}
} else {
ago := ctx.Form(`ago`)
if len(ago) < 2 {
handler.SendFail(ctx, ctx.T(`missing param`))
goto END
}
switch ago[len(ago)-1] {
case 'd': //删除几天前的。例如:7d
n, err = strconv.Atoi(strings.TrimSuffix(ago, `d`))
if err != nil {
handler.SendFail(ctx, err.Error()+`:`+ago)
goto END
}
cond = db.Cond{`created`: db.Lt(time.Now().AddDate(0, 0, -n).Unix())}
case 'm': //删除几个月前的。例如:1m
n, err = strconv.Atoi(strings.TrimSuffix(ago, `m`))
if err != nil {
handler.SendFail(ctx, err.Error()+`:`+ago)
goto END
}
cond = db.Cond{`created`: db.Lt(time.Now().AddDate(0, -n, 0).Unix())}
case 'y': //删除几年前的。例如:1y
n, err = strconv.Atoi(strings.TrimSuffix(ago, `y`))
if err != nil {
handler.SendFail(ctx, err.Error()+`:`+ago)
goto END
}
cond = db.Cond{`created`: db.Lt(time.Now().AddDate(-n, 0, 0).Unix())}
default:
handler.SendFail(ctx, ctx.T(`invalid param`))
goto END
}
if exportId > 0 {
cond[`export_id`] = exportId
}
}
err = m.Delete(nil, cond)
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
} else {
handler.SendFail(ctx, err.Error())
}
END:
return ctx.Redirect(handler.URLFor(`/collector/export_log?exportId=`) + fmt.Sprint(exportId))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.2

搜索帮助

344bd9b3 5694891 D2dac590 5694891