-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
206 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package qrcodeController | ||
|
||
import ( | ||
"4u-go/app/apiException" | ||
"4u-go/app/services/qrcodeService" | ||
"4u-go/app/utils" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type filter struct { | ||
College []uint `json:"college"` | ||
FeedbackType []uint `json:"feedback_type"` | ||
} | ||
|
||
type getListData struct { | ||
Keyword string `json:"keyword"` | ||
Filter filter `json:"filter"` | ||
Page int `json:"page"` | ||
PageSize int `json:"page_size"` | ||
} | ||
|
||
type getListResponse struct { | ||
QrcodeList []qrcodeResp `json:"qrcode_list"` | ||
Total int64 `json:"total"` | ||
} | ||
|
||
// GetList 实现了权益码列表的分页获取, 搜索, 筛选 | ||
func GetList(c *gin.Context) { | ||
var data getListData | ||
err := c.ShouldBindJSON(&data) | ||
if err != nil { | ||
apiException.AbortWithException(c, apiException.ParamError, err) | ||
return | ||
} | ||
filter := data.Filter | ||
|
||
qrcodeListResp := make([]qrcodeResp, 0) | ||
|
||
qrcodeList, total, err := qrcodeService.GetList( | ||
filter.College, filter.FeedbackType, | ||
data.Keyword, data.Page, data.PageSize) | ||
if err != nil { | ||
apiException.AbortWithException(c, apiException.ParamError, err) | ||
return | ||
} | ||
|
||
for _, qrcode := range qrcodeList { | ||
resp, err := generateResp(qrcode) | ||
if err != nil { | ||
apiException.AbortWithException(c, apiException.ParamError, err) | ||
return | ||
} | ||
qrcodeListResp = append(qrcodeListResp, *resp) | ||
} | ||
|
||
utils.JsonSuccessResponse(c, getListResponse{ | ||
QrcodeList: qrcodeListResp, | ||
Total: total, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package qrcodeService | ||
|
||
import ( | ||
"4u-go/app/models" | ||
dbUtils "4u-go/app/utils/database" | ||
"4u-go/config/database" | ||
) | ||
|
||
// GetList 获取权益码信息列表的筛选,搜索,分页 | ||
func GetList( | ||
collegeFilter []uint, | ||
feedbackFilter []uint, | ||
keyword string, | ||
page int, pageSize int, | ||
) (qrcodeList []models.Qrcode, total int64, err error) { | ||
query := database.DB.Model(models.Qrcode{}) | ||
|
||
// 关键词搜索 | ||
if len(keyword) > 0 { | ||
query = query.Where("ID = ? "+ | ||
"OR department LIKE ? "+ | ||
"OR location LIKE ? "+ | ||
"OR description LIKE ?", keyword, "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%") | ||
} | ||
|
||
// 筛选`责任部门` | ||
if len(collegeFilter) > 0 { | ||
query = query.Scopes(dbUtils.Filter("college", collegeFilter)) | ||
} | ||
|
||
// 筛选`反馈类型` | ||
if len(feedbackFilter) > 0 { | ||
query = query.Scopes(dbUtils.Filter("feedback_type", feedbackFilter)) | ||
} | ||
|
||
// 分页查找 | ||
err = query.Count(&total). | ||
Scopes(dbUtils.Paginate(page, pageSize)). | ||
Find(&qrcodeList).Error | ||
|
||
return qrcodeList, total, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package database | ||
|
||
import "gorm.io/gorm" | ||
|
||
// Filter 自定义筛选插件 | ||
// Usage: db.Scopes(dbUtils.Filter("college", [1,2,3])) | ||
// Desc: 筛选出"college"字段为1,2,3其中之一的字段 | ||
// | ||
// Spec: 使用泛型以接收所有类型的切片 | ||
func Filter[T any](name string, choices []T) func(db *gorm.DB) *gorm.DB { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where(name+" IN (?)", choices) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package database | ||
|
||
import "gorm.io/gorm" | ||
|
||
// Paginate 自定义分页插件 | ||
// Usage: db.Scopes(dbUtils.Paginate(page, pageSize)) | ||
func Paginate(page int, size int) func(db *gorm.DB) *gorm.DB { | ||
return func(db *gorm.DB) *gorm.DB { | ||
pageSize := size | ||
if pageSize > 20 { | ||
pageSize = 20 | ||
} else if pageSize <= 0 { | ||
pageSize = 10 | ||
} | ||
offset := (page - 1) * pageSize | ||
return db.Offset(offset).Limit(pageSize) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters