diff --git a/app/common/counterName/counterName.go b/app/common/counterName/counterName.go new file mode 100644 index 0000000..0e0192d --- /dev/null +++ b/app/common/counterName/counterName.go @@ -0,0 +1,12 @@ +package counterName + +// 定义要统计的字段 + +const ( + // QrcodeScan 权益码扫描次数 + QrcodeScan string = "qrcode_scan" + // Feedback 问题反馈数量 + Feedback string = "feedback_scan" + // FeedbackHandle 处理问题反馈数量 + FeedbackHandle string = "feedback_handle" +) diff --git a/app/common/feedbackType/feedbackType.go b/app/common/feedbackType/feedbackType.go new file mode 100644 index 0000000..be4ea95 --- /dev/null +++ b/app/common/feedbackType/feedbackType.go @@ -0,0 +1,17 @@ +package feedbackType + +// FeedbackType +const ( + Activities uint = iota // 校园活动 + DiningAndShops // 食堂及商铺 + Dormitories // 宿舍 + Academic // 教学服务(选课、转专业等) + Facilities // 校园设施 + Classrooms // 教室 + Library // 图书馆 + Transportation // 交通 + Security // 安保 + HealthCare // 医疗服务 + Policies // 学院相关政策(如综测等) + Others // 其他服务 +) diff --git a/app/controllers/qrcodeController/count.go b/app/controllers/qrcodeController/count.go new file mode 100644 index 0000000..2954a4b --- /dev/null +++ b/app/controllers/qrcodeController/count.go @@ -0,0 +1,26 @@ +package qrcodeController + +import ( + "4u-go/app/apiException" + "4u-go/app/services/qrcodeService" + "4u-go/app/utils" + "github.com/gin-gonic/gin" +) + +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 { + apiException.AbortWithException(c, apiException.ServerError, err) + return + } + utils.JsonSuccessResponse(c, nil) +} diff --git a/app/controllers/qrcodeController/delete.go b/app/controllers/qrcodeController/delete.go index af82471..9d78938 100644 --- a/app/controllers/qrcodeController/delete.go +++ b/app/controllers/qrcodeController/delete.go @@ -14,7 +14,7 @@ type deleteQrcodeData struct { ID uint `json:"id"` } -// DeleteQrcode 创建一个权益码 +// DeleteQrcode 删除一个权益码 func DeleteQrcode(c *gin.Context) { var data deleteQrcodeData err := c.ShouldBindJSON(&data) diff --git a/app/controllers/qrcodeController/getList.go b/app/controllers/qrcodeController/getList.go index fb4c1e0..af7913a 100644 --- a/app/controllers/qrcodeController/getList.go +++ b/app/controllers/qrcodeController/getList.go @@ -10,6 +10,7 @@ import ( type filter struct { College []uint `json:"college"` FeedbackType []uint `json:"feedback_type"` + Status *bool `json:"status"` } type getListData struct { @@ -37,7 +38,9 @@ func GetList(c *gin.Context) { qrcodeListResp := make([]qrcodeResp, 0) qrcodeList, total, err := qrcodeService.GetList( - filter.College, filter.FeedbackType, + filter.College, + filter.FeedbackType, + filter.Status, data.Keyword, data.Page, data.PageSize) if err != nil { apiException.AbortWithException(c, apiException.ParamError, err) diff --git a/app/controllers/qrcodeController/toggleStatus.go b/app/controllers/qrcodeController/toggleStatus.go deleted file mode 100644 index e6f3b5e..0000000 --- a/app/controllers/qrcodeController/toggleStatus.go +++ /dev/null @@ -1,51 +0,0 @@ -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 toggleStatusData struct { - ID uint `json:"id" binding:"required"` - Status bool `json:"status"` -} - -// ToggleStatus 更新学院信息 -func ToggleStatus(c *gin.Context) { - var data toggleStatusData - err := c.ShouldBindJSON(&data) - if err != nil { - apiException.AbortWithException(c, apiException.ParamError, err) - return - } - - 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) - return - } - - { // 更新权益码状态 - qrcode.Status = data.Status - } - - err = qrcodeService.SaveQrcode(qrcode) - - if err != nil { - apiException.AbortWithException(c, apiException.ServerError, err) - return - } - - utils.JsonSuccessResponse(c, nil) -} diff --git a/app/controllers/qrcodeController/update.go b/app/controllers/qrcodeController/update.go index 5e164c3..d3cd86a 100644 --- a/app/controllers/qrcodeController/update.go +++ b/app/controllers/qrcodeController/update.go @@ -14,12 +14,13 @@ 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" binding:"required"` + Description string `json:"description"` FeedbackType uint `json:"feedback_type" binding:"required"` Location string `json:"location" binding:"required"` + Status bool `json:"status"` } -// UpdateQrcode 更新学院信息 +// UpdateQrcode 更新权益码信息 func UpdateQrcode(c *gin.Context) { var data updateQrcodeData err := c.ShouldBindJSON(&data) @@ -44,6 +45,7 @@ func UpdateQrcode(c *gin.Context) { qrcode.Description = data.Description qrcode.FeedbackType = data.FeedbackType qrcode.Location = data.Location + qrcode.Status = data.Status } err = qrcodeService.SaveQrcode(qrcode) diff --git a/app/models/counter.go b/app/models/counter.go new file mode 100644 index 0000000..59bdc33 --- /dev/null +++ b/app/models/counter.go @@ -0,0 +1,9 @@ +package models + +// Counter 用于记录每天相关字段的增量 +type Counter struct { + ID uint + Day int64 // 基于时间戳计算当前天数 `time.Now()/86400` + Name string // 统计的字段名 + Count int64 // 计数器 +} diff --git a/app/models/qrcode.go b/app/models/qrcode.go index 0b6e96f..5346d28 100644 --- a/app/models/qrcode.go +++ b/app/models/qrcode.go @@ -5,22 +5,6 @@ import ( "time" ) -// FeedbackType -const ( - FbActivities uint = iota // 校园活动 - FbDiningAndShops // 食堂及商铺 - FbDormitories // 宿舍 - FbAcademic // 教学服务(选课、转专业等) - FbFacilities // 校园设施 - FbClassrooms // 教室 - FbLibrary // 图书馆 - FbTransportation // 交通 - FbSecurity // 安保 - FbHealthCare // 医疗服务 - FbPolicies // 学院相关政策(如综测等) - FbOthers // 其他服务 -) - // Qrcode 权益码的结构体 type Qrcode struct { ID uint `json:"id"` diff --git a/app/services/qrcodeService/addScanCount.go b/app/services/qrcodeService/addScanCount.go new file mode 100644 index 0000000..dcc0114 --- /dev/null +++ b/app/services/qrcodeService/addScanCount.go @@ -0,0 +1,23 @@ +package qrcodeService + +import ( + "4u-go/app/common/counterName" + trackService "4u-go/app/services/trackService/counter" +) + +// AddScanCount 用于更新ScanCount +func AddScanCount(id uint) error { + // 1. 更新新增量 + err := trackService.AddCount(counterName.QrcodeScan) + if err != nil { + return err + } + + // 2. 更新总量 + qrcode, err := GetQrcodeById(id) + if err != nil { + return err + } + qrcode.ScanCount++ + return SaveQrcode(qrcode) +} diff --git a/app/services/qrcodeService/getList.go b/app/services/qrcodeService/getList.go index 73813e3..fd7ebee 100644 --- a/app/services/qrcodeService/getList.go +++ b/app/services/qrcodeService/getList.go @@ -10,6 +10,7 @@ import ( func GetList( collegeFilter []uint, feedbackFilter []uint, + status *bool, keyword string, page int, pageSize int, ) (qrcodeList []models.Qrcode, total int64, err error) { @@ -23,6 +24,15 @@ func GetList( "OR description LIKE ?", keyword, "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%") } + // 筛选`权益码状态` + if status != nil { + if *status { + query = query.Where("status = ?", 1) + } else { + query = query.Where("status = ?", 0) + } + } + // 筛选`责任部门` if len(collegeFilter) > 0 { query = query.Scopes(dbUtils.Filter("college", collegeFilter)) @@ -34,7 +44,7 @@ func GetList( } // 分页查找 - err = query.Count(&total). + err = query.Debug().Count(&total). Scopes(dbUtils.Paginate(page, pageSize)). Find(&qrcodeList).Error diff --git a/app/services/trackService/counter/addCount.go b/app/services/trackService/counter/addCount.go new file mode 100644 index 0000000..3ef78ce --- /dev/null +++ b/app/services/trackService/counter/addCount.go @@ -0,0 +1,38 @@ +package trackService + +import ( + "errors" + "time" + + "4u-go/app/models" + "4u-go/config/database" + "gorm.io/gorm" +) + +// AddCount 用于更新某一字段在某一天的增量 +func AddCount(name string) error { + day := time.Now().Unix() / 86400 + counter, err := getCounter(name, day) + + if errors.Is(err, gorm.ErrRecordNotFound) { + // 记录不存在则创建 + counter.Name = name + counter.Day = day + counter.Count = 1 + return saveCounter(counter) + } + if err != nil { + return err + } + counter.Count++ + return saveCounter(counter) +} + +func getCounter(name string, day int64) (counter models.Counter, err error) { + err = database.DB.Where("name=? AND day=?", name, day).First(&counter).Error + return counter, err +} + +func saveCounter(counter models.Counter) error { + return database.DB.Save(&counter).Error +} diff --git a/config/database/migrations.go b/config/database/migrations.go index a3955f5..fc02db2 100755 --- a/config/database/migrations.go +++ b/config/database/migrations.go @@ -14,5 +14,6 @@ func autoMigrate(db *gorm.DB) error { &models.Website{}, &models.College{}, &models.Qrcode{}, + &models.Counter{}, ) } diff --git a/config/router/router.go b/config/router/router.go index e2f5be7..fac8b3c 100644 --- a/config/router/router.go +++ b/config/router/router.go @@ -69,7 +69,6 @@ func Init(r *gin.Engine) { adminQrcode := admin.Group("/qrcode", midwares.CheckAdmin) { - adminQrcode.PUT("/status", qrcodeController.ToggleStatus) adminQrcode.POST("", qrcodeController.CreateQrcode) adminQrcode.DELETE("", midwares.CheckSuperAdmin, qrcodeController.DeleteQrcode) adminQrcode.GET("", qrcodeController.GetQrcode) @@ -99,5 +98,10 @@ func Init(r *gin.Engine) { { website.GET("/list", websiteController.GetWebsiteList) } + + track := api.Group("/track") + { + track.GET("/qrcode/scan_count", qrcodeController.ScanCount) + } } } diff --git a/makefile b/makefile index b7d1723..8a12a8e 100644 --- a/makefile +++ b/makefile @@ -13,7 +13,7 @@ check-gcc: # 构建目标 build: - check-gcc + $(check-gcc) @echo "Building $(TARGET)..." go build -v -o $(TARGET) .