-
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.
Merge pull request #31 from zjutjh/MangoGovo/dev
feat: 实现了权益码相关接口
- Loading branch information
Showing
21 changed files
with
585 additions
and
1 deletion.
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
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" | ||
) |
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,17 @@ | ||
package feedbackType | ||
|
||
// FeedbackType | ||
const ( | ||
Activities uint = iota // 校园活动 | ||
DiningAndShops // 食堂及商铺 | ||
Dormitories // 宿舍 | ||
Academic // 教学服务(选课、转专业等) | ||
Facilities // 校园设施 | ||
Classrooms // 教室 | ||
Library // 图书馆 | ||
Transportation // 交通 | ||
Security // 安保 | ||
HealthCare // 医疗服务 | ||
Policies // 学院相关政策(如综测等) | ||
Others // 其他服务 | ||
) |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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 | ||
} |
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,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, | ||
}) | ||
} |
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,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 | ||
} | ||
|
||
{ // 更新权益码信息 | ||
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) | ||
} |
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,9 @@ | ||
package models | ||
|
||
// Counter 用于记录每天相关字段的增量 | ||
type Counter struct { | ||
ID uint | ||
Day int64 // 基于时间戳计算当前天数 `time.Now()/86400` | ||
Name string // 统计的字段名 | ||
Count int64 // 计数器 | ||
} |
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,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"` // 反馈次数 | ||
} |
Oops, something went wrong.