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 fce9fc0 commit 2d81179
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 19 deletions.
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 // 其他服务
)
26 changes: 26 additions & 0 deletions app/controllers/qrcodeController/count.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion app/controllers/qrcodeController/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/qrcodeController/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type updateQrcodeData struct {
Location string `json:"location" binding:"required"`
}

// UpdateQrcode 更新学院信息
// UpdateQrcode 更新权益码信息
func UpdateQrcode(c *gin.Context) {
var data updateQrcodeData
err := c.ShouldBindJSON(&data)
Expand Down
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 // 计数器
}
16 changes: 0 additions & 16 deletions app/models/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
23 changes: 23 additions & 0 deletions app/services/qrcodeService/addScanCount.go
Original file line number Diff line number Diff line change
@@ -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)
}
38 changes: 38 additions & 0 deletions app/services/trackService/counter/addCount.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions config/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ func autoMigrate(db *gorm.DB) error {
&models.Website{},
&models.College{},
&models.Qrcode{},
&models.Counter{},
)
}
5 changes: 5 additions & 0 deletions config/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,10 @@ func Init(r *gin.Engine) {
{
website.GET("/list", websiteController.GetWebsiteList)
}

track := api.Group("/track")
{
track.GET("/qrcode/scan_count", qrcodeController.ScanCount)
}
}
}
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ check-gcc:

# 构建目标
build:
check-gcc
$(check-gcc)
@echo "Building $(TARGET)..."
go build -v -o $(TARGET) .

Expand Down

0 comments on commit 2d81179

Please sign in to comment.