Skip to content

Commit

Permalink
失物招领优化
Browse files Browse the repository at this point in the history
  • Loading branch information
XiMo-210 committed Aug 6, 2023
1 parent c619b1b commit 358a6b4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package lostAndFoundRecordController

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"image/jpeg"
"image/png"
"math"
Expand All @@ -20,87 +18,84 @@ import (
"wejh-go/app/services/lostAndFoundRecordServices"
"wejh-go/app/services/sessionServices"
"wejh-go/app/utils"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
)

type LostAndFoundForm struct {
ID int `json:"id"`
Type bool `json:"type"`
Campus string `json:"campus"`
Kind string `json:"kind"`
Img1 interface{} `json:"img1"`
Img2 interface{} `json:"img2"`
Img3 interface{} `json:"img3"`
Content string `json:"content"`
type GetRecordsData struct {
LostOrFound int `form:"lost_or_found"`
PageNum int `form:"page_num"`
PageSize int `form:"page_size"`
}

func GetRecords(c *gin.Context) {

campus, _ := url.QueryUnescape(c.Query("campus"))
kind, _ := url.QueryUnescape(c.Query("kind"))
pageNumRaw := c.Query("page_num")
pageSizeRaw := c.Query("page_size")
pageNum, err := strconv.Atoi(pageNumRaw)
pageSize, err := strconv.Atoi(pageSizeRaw)
var data GetRecordsData
err := c.ShouldBindQuery(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

var lostAndFoundRecords []models.LostAndFoundRecord
if kind == "全部" {
lostAndFoundRecords, err = lostAndFoundRecordServices.GetAllKindRecord(campus, pageNum, pageSize)
if data.LostOrFound == 2 {
lostAndFoundRecords, err = lostAndFoundRecordServices.GetAllLostAndFoundRecord(campus, kind, data.PageNum, data.PageSize)
} else {
lostAndFoundRecords, err = lostAndFoundRecordServices.GetRecord(campus, kind, pageNum, pageSize)
lostAndFoundRecords, err = lostAndFoundRecordServices.GetRecord(campus, kind, data.LostOrFound, data.PageNum, data.PageSize)
}
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

var totalPageNum *int64
if kind == "全部" {
totalPageNum, err = lostAndFoundRecordServices.GetRecordAllKindTotalPageNum(campus)
if data.LostOrFound == 2 {
totalPageNum, err = lostAndFoundRecordServices.GetAllLostAndFoundTotalPageNum(campus, kind)
} else {
totalPageNum, err = lostAndFoundRecordServices.GetRecordTotalPageNum(campus, kind)
totalPageNum, err = lostAndFoundRecordServices.GetTotalPageNum(campus, kind, data.LostOrFound)
}
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, gin.H{
"data": lostAndFoundRecords,
"total_page_num": math.Ceil(float64(*totalPageNum) / float64(pageSize)),
"total_page_num": math.Ceil(float64(*totalPageNum) / float64(data.PageSize)),
})
}

func GetRecordsByAdmin(c *gin.Context) {
pageNumRaw := c.Query("page_num")
pageSizeRaw := c.Query("page_size")
pageNum, err := strconv.Atoi(pageNumRaw)
pageSize, err := strconv.Atoi(pageSizeRaw)
var data GetRecordsData
err := c.ShouldBindQuery(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

var lostAndFoundRecords []models.LostAndFoundRecord
var totalPageNum *int64
publisher := getPublisher(c)
if *publisher == "Admin" {
lostAndFoundRecords, err = lostAndFoundRecordServices.GetRecordBySuperAdmin(pageNum, pageSize)
lostAndFoundRecords, err = lostAndFoundRecordServices.GetRecordBySuperAdmin(data.LostOrFound, data.PageNum, data.PageSize)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
totalPageNum, err = lostAndFoundRecordServices.GetRecordTotalPageNumBySuperAdmin()
totalPageNum, err = lostAndFoundRecordServices.GetTotalPageNumBySuperAdmin(data.LostOrFound)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
} else {
lostAndFoundRecords, err = lostAndFoundRecordServices.GetRecordByAdmin(*publisher, pageNum, pageSize)
lostAndFoundRecords, err = lostAndFoundRecordServices.GetRecordByAdmin(*publisher, data.LostOrFound, data.PageNum, data.PageSize)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
totalPageNum, err = lostAndFoundRecordServices.GetRecordTotalPageNumByAdmin(*publisher)
totalPageNum, err = lostAndFoundRecordServices.GetTotalPageNumByAdmin(*publisher, data.LostOrFound)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
Expand All @@ -109,7 +104,7 @@ func GetRecordsByAdmin(c *gin.Context) {

utils.JsonSuccessResponse(c, gin.H{
"data": lostAndFoundRecords,
"total_page_num": math.Ceil(float64(*totalPageNum) / float64(pageSize)),
"total_page_num": math.Ceil(float64(*totalPageNum) / float64(data.PageSize)),
})
}

Expand All @@ -122,22 +117,44 @@ func GetKindList(c *gin.Context) {
utils.JsonSuccessResponse(c, kinds)
}

type LostAndFoundForm struct {
ID int `json:"id"`
Type bool `json:"type"`
Campus string `json:"campus"`
Kind string `json:"kind"`
Img1 interface{} `json:"img1"`
Img2 interface{} `json:"img2"`
Img3 interface{} `json:"img3"`
ItemName string `json:"item_name"` // 物品名称
LostOrFoundPlace string `json:"lost_or_found_place"` // 丢失或拾得地点
LostOrFoundTime string `json:"lost_or_found_time"` // 丢失或拾得时间
PickupPlace string `json:"pickup_place"` // 失物领取地点
Contact string `json:"contact"` // 寻物联系方式
Introduction string `json:"introduction"` // 物品介绍
}

func InsertRecord(c *gin.Context) {
var postForm LostAndFoundForm
err := c.ShouldBindJSON(&postForm)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

publisher := getPublisher(c)
record := models.LostAndFoundRecord{
Type: postForm.Type,
Campus: postForm.Campus,
Kind: postForm.Kind,
PublishTime: time.Now(),
IsProcessed: false,
Publisher: *publisher,
Content: postForm.Content,
Type: postForm.Type,
Campus: postForm.Campus,
Kind: postForm.Kind,
PublishTime: time.Now(),
IsProcessed: false,
Publisher: *publisher,
ItemName: postForm.ItemName,
LostOrFoundPlace: postForm.LostOrFoundPlace,
LostOrFoundTime: postForm.LostOrFoundTime,
PickupPlace: postForm.PickupPlace,
Contact: postForm.Contact,
Introduction: postForm.Introduction,
}
if postForm.Img1 != nil {
record.Img1 = postForm.Img1.(string)
Expand All @@ -148,11 +165,13 @@ func InsertRecord(c *gin.Context) {
if postForm.Img3 != nil {
record.Img3 = postForm.Img3.(string)
}

err = lostAndFoundRecordServices.CreateRecord(record)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, nil)
}

Expand All @@ -163,28 +182,52 @@ func UpdateRecord(c *gin.Context) {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

publisher := getPublisher(c)
record, err := lostAndFoundRecordServices.GetRecordById(postForm.ID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if *publisher != record.Publisher {

if *publisher != record.Publisher && *publisher != "Admin" {
_ = c.AbortWithError(200, apiException.NotAdmin)
return
}

var img1, img2, img3 string
if postForm.Img1 != nil {
img1 = postForm.Img1.(string)
}
if postForm.Img2 != nil {
img2 = postForm.Img2.(string)
}
if postForm.Img3 != nil {
img3 = postForm.Img3.(string)
}

// 移除更新后不需要的图片
lostAndFoundRecordServices.RemoveImg(record, img2, img2, img3)

err = lostAndFoundRecordServices.UpdateRecord(postForm.ID, models.LostAndFoundRecord{
Type: postForm.Type,
Campus: postForm.Campus,
Kind: postForm.Kind,
Content: postForm.Content,
Publisher: *publisher,
IsProcessed: false,
Campus: postForm.Campus,
Kind: postForm.Kind,
IsProcessed: false,
Img1: img1,
Img2: img2,
Img3: img3,
ItemName: postForm.ItemName,
LostOrFoundPlace: postForm.LostOrFoundPlace,
LostOrFoundTime: postForm.LostOrFoundTime,
PickupPlace: postForm.PickupPlace,
Contact: postForm.Contact,
Introduction: postForm.Introduction,
})
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, nil)
}

Expand All @@ -196,11 +239,11 @@ func getPublisher(c *gin.Context) *string {
}
var publisher string
if user.Username == "zhforu" {
publisher = "For You 朝晖校区"
publisher = "For You”工程 朝晖校区"
} else if user.Username == "pfforu" {
publisher = "For You 屏峰校区"
publisher = "For You”工程 屏峰校区"
} else if user.Username == "mgsforu" {
publisher = "For You 莫干山校区"
publisher = "For You”工程 莫干山校区"
} else if user.Username == "zhstuac" {
publisher = "朝晖学生事务大厅"
} else if user.Username == "pfstuac" {
Expand Down Expand Up @@ -278,26 +321,38 @@ func DeleteRecord(c *gin.Context) {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

publisher := getPublisher(c)
record, err := lostAndFoundRecordServices.GetRecordById(lostId)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if *publisher != record.Publisher {

if *publisher != record.Publisher && *publisher != "Admin" {
_ = c.AbortWithError(200, apiException.NotAdmin)
return
}

err = lostAndFoundRecordServices.UpdateRecord(lostId, models.LostAndFoundRecord{
Type: record.Type,
Campus: record.Campus,
Kind: record.Kind,
Content: record.Content,
Publisher: *publisher,
IsProcessed: true,
Campus: record.Campus,
Kind: record.Kind,
IsProcessed: true,
ItemName: record.ItemName,
LostOrFoundPlace: record.LostOrFoundPlace,
LostOrFoundTime: record.LostOrFoundTime,
PickupPlace: record.PickupPlace,
Contact: record.Contact,
Introduction: record.Introduction,
})
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

_ = os.Remove("./img/" + strings.TrimPrefix(record.Img1, config.GetWebpUrlKey()))
_ = os.Remove("./img/" + strings.TrimPrefix(record.Img2, config.GetWebpUrlKey()))
_ = os.Remove("./img/" + strings.TrimPrefix(record.Img3, config.GetWebpUrlKey()))

utils.JsonSuccessResponse(c, nil)
}
27 changes: 16 additions & 11 deletions app/models/lostAndFoundRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package models
import "time"

type LostAndFoundRecord struct {
ID int `json:"id"`
Type bool `json:"type"`
Campus string `json:"campus"`
Kind string `json:"kind"`
PublishTime time.Time `json:"publish_time" gorm:"type:timestamp;"`
IsProcessed bool `json:"-"`
Img1 string `json:"img1"`
Img2 string `json:"img2"`
Img3 string `json:"img3"`
Publisher string `json:"publisher"`
Content string `json:"content"`
ID int `json:"id"`
Type bool `json:"type"` // 1-失物 0-寻物
Campus string `json:"campus"`
Kind string `json:"kind"`
PublishTime time.Time `json:"publish_time" gorm:"type:timestamp;"`
IsProcessed bool `json:"-"`
Img1 string `json:"img1"`
Img2 string `json:"img2"`
Img3 string `json:"img3"`
Publisher string `json:"publisher"`
ItemName string `json:"item_name"` // 物品名称
LostOrFoundPlace string `json:"lost_or_found_place"` // 丢失或拾得地点
LostOrFoundTime string `json:"lost_or_found_time"` // 丢失或拾得时间
PickupPlace string `json:"pickup_place"` // 失物领取地点
Contact string `json:"contact"` // 寻物联系方式
Introduction string `json:"introduction"` // 物品介绍
}
Loading

0 comments on commit 358a6b4

Please sign in to comment.