-
Notifications
You must be signed in to change notification settings - Fork 4
/
fileindex.go
147 lines (120 loc) · 3.3 KB
/
fileindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/uxff/flexdrive/pkg/dao"
"github.com/uxff/flexdrive/pkg/dao/base"
"github.com/uxff/flexdrive/pkg/log"
"github.com/uxff/flexdrive/pkg/utils/paginator"
)
func init() {
}
type FileIndexListRequest struct {
CreateStart string `form:"createStart"`
CreateEnd string `form:"createEnd"`
Name string `form:"fileName"`
FileHash string `form:"fileHash"`
NodeId int `form:"nodeId"`
Status int `form:"status"`
Page int `form:"page"`
PageSize int `form:"pagesize"`
}
func (r *FileIndexListRequest) ToCondition() (condition map[string]interface{}) {
condition = make(map[string]interface{})
if r.CreateStart != "" {
condition["created>=?"] = r.CreateStart
}
if r.CreateEnd != "" {
condition["created<=?"] = r.CreateEnd
}
if r.Name != "" {
condition["fileName like ?"] = "%" + r.Name + "%"
}
if r.FileHash != "" {
condition["fileHash = ?"] = r.FileHash
}
if r.NodeId != 0 {
condition["nodeId = ?"] = r.NodeId
}
log.Debugf("r=%+v tocondition:%+v", r, condition)
return condition
}
// 接口返回的元素
type FileIndexItem struct {
dao.FileIndex
}
func NewFileIndexItemFromEnt(fileIndexEnt *dao.FileIndex) *FileIndexItem {
return &FileIndexItem{
FileIndex: *fileIndexEnt,
}
}
func FileIndexList(c *gin.Context) {
requestId := c.GetString(CtxKeyRequestId)
// 请求参数校验
req := &FileIndexListRequest{}
err := c.ShouldBindQuery(req)
if err != nil {
StdErrResponse(c, ErrInvalidParam)
return
}
// 列表查询
list := make([]*dao.FileIndex, 0)
total, err := base.ListAndCountByCondition(&dao.FileIndex{}, req.ToCondition(), req.Page, req.PageSize, "", &list)
if err != nil {
log.Trace(requestId).Errorf("list failed:%v", err)
StdErrResponse(c, ErrInternal)
return
}
// 从数据库结构转换成返回结构
resItems := make([]*FileIndexItem, 0)
for _, v := range list {
resItems = append(resItems, NewFileIndexItemFromEnt(v))
}
c.HTML(http.StatusOK, "fileindex/list.tpl", gin.H{
"LoginInfo": getLoginInfo(c),
"IsLogin": isLoginIn(c),
"total": total,
"page": req.Page,
"pagesize": req.PageSize,
"list": resItems,
"reqParam": req,
"paginator": paginator.NewPaginator(c.Request, 10, int64(total)),
})
}
func FileIndexEnable(c *gin.Context) {
fileIndexId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
if fileIndexId <= 0 {
StdErrResponse(c, ErrInvalidParam)
return
}
enable, _ := strconv.ParseInt(c.Param("enable"), 10, 64)
//loginInfo := getLoginInfo(c)
fileIndexEnt, err := dao.GetFileIndexById(int(fileIndexId))
//_, err := base.GetByCol("id", mid, fileIndexEnt)
// exist, err := base.GetByCol("mid", mid, fileIndexEnt)
if err != nil {
log.Errorf("db error:%v", err)
StdErrResponse(c, ErrInternal)
return
}
if fileIndexEnt == nil {
StdErrResponse(c, ErrMgrNotExist)
return
}
if enable == 1 {
// 启用
fileIndexEnt.Status = base.StatusNormal
} else if enable == 9 {
// 停用
fileIndexEnt.Status = base.StatusDeleted
}
_, err = base.UpdateByCol("id", fileIndexId, fileIndexEnt, []string{"status"})
if err != nil {
log.Errorf("db error:%v", err)
StdErrResponse(c, ErrInternal)
return
}
//StdResponse(c, ErrSuccess, nil)
c.Redirect(http.StatusMovedPermanently, RouteFileIndexList)
}