Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 实现了权益码相关接口 #31

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/common/counterName/counterName.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package counterName

// 定义要统计的字段

const (
// QrcodeScan 权益码扫描次数
QrcodeScan string = "qrcode_scan"
// Feedback 问题反馈数量
Feedback string = "feedback_scan"
// FeedbackHandle 处理问题反馈数量
FeedbackHandle string = "feedback_handle"
)
17 changes: 17 additions & 0 deletions app/common/feedbackType/feedbackType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package feedbackType

// FeedbackType
const (
Activities uint = iota // 校园活动
DiningAndShops // 食堂及商铺
Dormitories // 宿舍
Academic // 教学服务(选课、转专业等)
Facilities // 校园设施
Classrooms // 教室
Library // 图书馆
Transportation // 交通
Security // 安保
HealthCare // 医疗服务
Policies // 学院相关政策(如综测等)
Others // 其他服务
)
33 changes: 33 additions & 0 deletions app/controllers/qrcodeController/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package qrcodeController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type scanCountData struct {
ID uint `form:"id" binding:"required"`
}

// ScanCount 更新权益码扫码次数
func ScanCount(c *gin.Context) {
var data scanCountData
if err := c.ShouldBind(&data); err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}
if err := qrcodeService.AddScanCount(data.ID); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, nil)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}
utils.JsonSuccessResponse(c, nil)
}
43 changes: 43 additions & 0 deletions app/controllers/qrcodeController/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package qrcodeController

import (
"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)

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

// CreateQrcode 创建一个权益码
func CreateQrcode(c *gin.Context) {
var data createQrcodeData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

err = qrcodeService.SaveQrcode(models.Qrcode{
Status: true,
College: data.College,
Department: data.Department,
Description: data.Description,
FeedbackType: data.FeedbackType,
Location: data.Location,
})

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

utils.JsonSuccessResponse(c, nil)
}
46 changes: 46 additions & 0 deletions app/controllers/qrcodeController/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package qrcodeController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type deleteQrcodeData struct {
ID uint `json:"id"`
}

// DeleteQrcode 删除一个权益码
func DeleteQrcode(c *gin.Context) {
var data deleteQrcodeData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

// 判断权益码是否存在
_, err = qrcodeService.GetQrcodeById(data.ID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

// 删除权益码
err = qrcodeService.DeleteQrcodeById(data.ID)

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

utils.JsonSuccessResponse(c, nil)
}
82 changes: 82 additions & 0 deletions app/controllers/qrcodeController/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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"
"gorm.io/gorm"
)

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
err := c.ShouldBind(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

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

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, 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
}
63 changes: 63 additions & 0 deletions app/controllers/qrcodeController/getList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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"`
Status bool `json:"status"`
}

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,
filter.Status,
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,
})
}
59 changes: 59 additions & 0 deletions app/controllers/qrcodeController/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package qrcodeController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type updateQrcodeData struct {
ID uint `json:"id" binding:"required"`
College uint `json:"college" binding:"required"`
Department string `json:"department" binding:"required"`
Description string `json:"description"`
FeedbackType uint `json:"feedback_type" binding:"required"`
Location string `json:"location" binding:"required"`
Status bool `json:"status"`
}

// UpdateQrcode 更新权益码信息
func UpdateQrcode(c *gin.Context) {
var data updateQrcodeData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

qrcode, err := qrcodeService.GetQrcodeById(data.ID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

{ // 更新权益码信息
SugarMGP marked this conversation as resolved.
Show resolved Hide resolved
qrcode.College = data.College
qrcode.Department = data.Department
qrcode.Description = data.Description
qrcode.FeedbackType = data.FeedbackType
qrcode.Location = data.Location
qrcode.Status = data.Status
}

err = qrcodeService.SaveQrcode(qrcode)

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

utils.JsonSuccessResponse(c, nil)
}
9 changes: 9 additions & 0 deletions app/models/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

// Counter 用于记录每天相关字段的增量
type Counter struct {
ID uint
Day int64 // 基于时间戳计算当前天数 `time.Now()/86400`
Name string // 统计的字段名
Count int64 // 计数器
}
23 changes: 23 additions & 0 deletions app/models/qrcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package models

import (
"database/sql"
"time"
)

// Qrcode 权益码的结构体
type Qrcode struct {
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 `json:"scan_count"` // 扫描次数
FeedbackCount uint `json:"feedback_count"` // 反馈次数
}
Loading
Loading