Skip to content

Commit

Permalink
feat: 实现了权益码列表的分页获取, 搜索, 筛选
Browse files Browse the repository at this point in the history
  • Loading branch information
MangoGovo committed Dec 8, 2024
1 parent da0a55e commit fce9fc0
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 25 deletions.
6 changes: 3 additions & 3 deletions app/controllers/qrcodeController/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

type createQrcodeData struct {
College uint `json:"college" binding:"required"`
College uint `json:"college"`
Department string `json:"department" binding:"required"`
Description string `json:"description" binding:"required"`
FeedbackType uint `json:"feedback_type" binding:"required"`
Description string `json:"description"`
FeedbackType uint `json:"feedback_type"`
Location string `json:"location" binding:"required"`
}

Expand Down
49 changes: 46 additions & 3 deletions app/controllers/qrcodeController/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package qrcodeController

import (
"errors"
"time"

"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/collegeService"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
Expand All @@ -14,6 +17,20 @@ type getQrcodeData struct {
ID uint `form:"id" binding:"required"`
}

type qrcodeResp struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
FeedbackType uint `json:"feedback_type"` // 反馈类型
College models.College `json:"college"` // 责任部门
Department string `json:"department"` // 负责单位
Location string `json:"location"` // 投放位置
Status bool `json:"status"` // 状态(是否启用)
Description string `json:"description"` // 备注

ScanCount uint `json:"scan_count"` // 扫描次数
FeedbackCount uint `json:"feedback_count"` // 反馈次数
}

// GetQrcode 获取权益码信息
func GetQrcode(c *gin.Context) {
var data getQrcodeData
Expand All @@ -25,15 +42,41 @@ func GetQrcode(c *gin.Context) {

qrcode, err := qrcodeService.GetQrcodeById(data.ID)

if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

resp, err := generateResp(qrcode)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, qrcode)
utils.JsonSuccessResponse(c, resp)
}

func generateResp(qrcode models.Qrcode) (*qrcodeResp, error) {
college, err := collegeService.GetCollegeById(qrcode.College)
if err != nil {
return nil, err
}

return &qrcodeResp{
ID: qrcode.ID,
CreatedAt: qrcode.CreatedAt,
FeedbackType: qrcode.FeedbackType,
College: college,
Department: qrcode.Department,
Location: qrcode.Location,
ScanCount: qrcode.ScanCount,
Status: qrcode.Status,

FeedbackCount: qrcode.FeedbackCount,
Description: qrcode.Description,
}, nil
}
60 changes: 60 additions & 0 deletions app/controllers/qrcodeController/getList.go
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,
})
}
12 changes: 5 additions & 7 deletions app/controllers/qrcodeController/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ func UpdateQrcode(c *gin.Context) {
}

qrcode, err := qrcodeService.GetQrcodeById(data.ID)

if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
return
}

if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

Expand Down
26 changes: 16 additions & 10 deletions app/models/qrcode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import "gorm.io/gorm"
import (
"database/sql"
"time"
)

// FeedbackType
const (
Expand All @@ -20,14 +23,17 @@ const (

// Qrcode 权益码的结构体
type Qrcode struct {
gorm.Model
FeedbackType uint // 反馈类型
College uint // 责任部门
Department string // 负责单位
Location string // 投放位置
Status bool // 状态(是否启用)
Description string // 备注
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"-"`
DeletedAt sql.NullTime `json:"-" gorm:"index"`
FeedbackType uint `json:"feedback_type"` // 反馈类型
College uint `json:"college"` // 责任部门
Department string `json:"department"` // 负责单位
Location string `json:"location"` // 投放位置
Status bool `json:"status"` // 状态(是否启用)
Description string `json:"description"` // 备注

ScanCount uint // 扫描次数
FeedbackCount uint // 反馈次数
ScanCount uint `json:"scan_count"` // 扫描次数
FeedbackCount uint `json:"feedback_count"` // 反馈次数
}
42 changes: 42 additions & 0 deletions app/services/qrcodeService/getList.go
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
}
14 changes: 14 additions & 0 deletions app/utils/database/filter.go
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)
}
}
18 changes: 18 additions & 0 deletions app/utils/database/paginate.go
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)
}
}
4 changes: 2 additions & 2 deletions config/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func Init(r *gin.Engine) {
{
adminQrcode.PUT("/status", qrcodeController.ToggleStatus)
adminQrcode.POST("", qrcodeController.CreateQrcode)
adminQrcode.DELETE("", qrcodeController.DeleteQrcode)
adminQrcode.DELETE("", midwares.CheckSuperAdmin, qrcodeController.DeleteQrcode)
adminQrcode.GET("", qrcodeController.GetQrcode)
adminQrcode.GET("/list", qrcodeController.GetList)
adminQrcode.POST("/list", qrcodeController.GetList)
adminQrcode.PUT("", qrcodeController.UpdateQrcode)
}
}
Expand Down

0 comments on commit fce9fc0

Please sign in to comment.