From 358a6b45b80132bbf32fdf00555341fde05d1989 Mon Sep 17 00:00:00 2001 From: XiMo-210 <2831802697@qq.com> Date: Sun, 6 Aug 2023 21:04:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=B1=E7=89=A9=E6=8B=9B=E9=A2=86=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lostAndFoundRecordController.go | 167 ++++++++++++------ app/models/lostAndFoundRecord.go | 27 +-- .../lostAndFoundRecordService.go | 55 ++++-- 3 files changed, 165 insertions(+), 84 deletions(-) diff --git a/app/controllers/funcControllers/lostAndFoundRecordController/lostAndFoundRecordController.go b/app/controllers/funcControllers/lostAndFoundRecordController/lostAndFoundRecordController.go index c4d6cee..64e5cec 100644 --- a/app/controllers/funcControllers/lostAndFoundRecordController/lostAndFoundRecordController.go +++ b/app/controllers/funcControllers/lostAndFoundRecordController/lostAndFoundRecordController.go @@ -2,8 +2,6 @@ package lostAndFoundRecordController import ( "fmt" - "github.com/gin-gonic/gin" - "github.com/google/uuid" "image/jpeg" "image/png" "math" @@ -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 @@ -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)), }) } @@ -122,6 +117,22 @@ 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) @@ -129,15 +140,21 @@ func InsertRecord(c *gin.Context) { _ = 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) @@ -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) } @@ -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) } @@ -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" { @@ -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) } diff --git a/app/models/lostAndFoundRecord.go b/app/models/lostAndFoundRecord.go index bd74cd2..7df42b8 100644 --- a/app/models/lostAndFoundRecord.go +++ b/app/models/lostAndFoundRecord.go @@ -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"` // 物品介绍 } diff --git a/app/services/lostAndFoundRecordServices/lostAndFoundRecordService.go b/app/services/lostAndFoundRecordServices/lostAndFoundRecordService.go index 401f3f8..12929c1 100644 --- a/app/services/lostAndFoundRecordServices/lostAndFoundRecordService.go +++ b/app/services/lostAndFoundRecordServices/lostAndFoundRecordService.go @@ -1,14 +1,18 @@ package lostAndFoundRecordServices import ( + "os" + "strings" + "wejh-go/app/config" "wejh-go/app/models" "wejh-go/config/database" ) -func GetAllKindRecord(campus string, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { +func GetAllLostAndFoundRecord(campus, kind string, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { var record []models.LostAndFoundRecord result := database.DB.Where(models.LostAndFoundRecord{ Campus: campus, + Kind: kind, }).Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&record) if result.Error != nil { return nil, result.Error @@ -16,12 +20,13 @@ func GetAllKindRecord(campus string, pageNum, pageSize int) ([]models.LostAndFou return record, nil } -func GetRecord(campus, kind string, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { +func GetRecord(campus, kind string, lostOrFound int, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { var record []models.LostAndFoundRecord result := database.DB.Where(models.LostAndFoundRecord{ Campus: campus, Kind: kind, - }).Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize). + }).Where(map[string]interface{}{"type": lostOrFound == 1}). + Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize). Order("publish_time desc").Find(&record) if result.Error != nil { return nil, result.Error @@ -29,7 +34,7 @@ func GetRecord(campus, kind string, pageNum, pageSize int) ([]models.LostAndFoun return record, nil } -func GetRecordTotalPageNum(campus, kind string) (*int64, error) { +func GetAllLostAndFoundTotalPageNum(campus, kind string) (*int64, error) { var pageNum int64 result := database.DB.Model(models.LostAndFoundRecord{}).Where(models.LostAndFoundRecord{ Campus: campus, @@ -41,22 +46,24 @@ func GetRecordTotalPageNum(campus, kind string) (*int64, error) { return &pageNum, nil } -func GetRecordAllKindTotalPageNum(campus string) (*int64, error) { +func GetTotalPageNum(campus, kind string, lostOrFound int) (*int64, error) { var pageNum int64 result := database.DB.Model(models.LostAndFoundRecord{}).Where(models.LostAndFoundRecord{ Campus: campus, - }).Not("is_processed", true).Count(&pageNum) + Kind: kind, + }).Where(map[string]interface{}{"type": lostOrFound == 1}).Not("is_processed", true).Count(&pageNum) if result.Error != nil { return nil, result.Error } return &pageNum, nil } -func GetRecordByAdmin(publisher string, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { +func GetRecordByAdmin(publisher string, lostOrFound, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { var record []models.LostAndFoundRecord result := database.DB.Where(models.LostAndFoundRecord{ Publisher: publisher, - }).Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize). + }).Where(map[string]interface{}{"type": lostOrFound == 1}). + Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize). Order("publish_time desc").Find(&record) if result.Error != nil { return nil, result.Error @@ -64,20 +71,22 @@ func GetRecordByAdmin(publisher string, pageNum, pageSize int) ([]models.LostAnd return record, nil } -func GetRecordTotalPageNumByAdmin(publisher string) (*int64, error) { +func GetTotalPageNumByAdmin(publisher string, lostOrFound int) (*int64, error) { var pageNum int64 result := database.DB.Model(models.LostAndFoundRecord{}).Where(models.LostAndFoundRecord{ Publisher: publisher, - }).Not("is_processed", true).Count(&pageNum) + }).Where(map[string]interface{}{"type": lostOrFound == 1}). + Not("is_processed", true).Count(&pageNum) if result.Error != nil { return nil, result.Error } return &pageNum, nil } -func GetRecordBySuperAdmin(pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { +func GetRecordBySuperAdmin(lostOrFound int, pageNum, pageSize int) ([]models.LostAndFoundRecord, error) { var record []models.LostAndFoundRecord - result := database.DB.Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize). + result := database.DB.Where(map[string]interface{}{"type": lostOrFound == 1}). + Not("is_processed", true).Limit(pageSize).Offset((pageNum - 1) * pageSize). Order("publish_time desc").Find(&record) if result.Error != nil { return nil, result.Error @@ -85,9 +94,10 @@ func GetRecordBySuperAdmin(pageNum, pageSize int) ([]models.LostAndFoundRecord, return record, nil } -func GetRecordTotalPageNumBySuperAdmin() (*int64, error) { +func GetTotalPageNumBySuperAdmin(lostOrFound int) (*int64, error) { var pageNum int64 - result := database.DB.Model(models.LostAndFoundRecord{}).Not("is_processed", true).Count(&pageNum) + result := database.DB.Model(models.LostAndFoundRecord{}).Where(map[string]interface{}{"type": lostOrFound == 1}). + Not("is_processed", true).Count(&pageNum) if result.Error != nil { return nil, result.Error } @@ -122,13 +132,24 @@ func CreateRecord(record models.LostAndFoundRecord) error { return nil } +func RemoveImg(record models.LostAndFoundRecord, img1 string, img2 string, img3 string) { + if record.Img1 != "" && record.Img1 != img1 && record.Img1 != img2 && record.Img1 != img3 { + _ = os.Remove("./img/" + strings.TrimPrefix(record.Img1, config.GetWebpUrlKey())) + } + if record.Img2 != "" && record.Img2 != img1 && record.Img2 != img2 && record.Img2 != img3 { + _ = os.Remove("./img/" + strings.TrimPrefix(record.Img2, config.GetWebpUrlKey())) + } + if record.Img3 != "" && record.Img3 != img1 && record.Img3 != img2 && record.Img3 != img3 { + _ = os.Remove("./img/" + strings.TrimPrefix(record.Img3, config.GetWebpUrlKey())) + } +} + func UpdateRecord(id int, record models.LostAndFoundRecord) error { - result := database.DB.Model(models.LostAndFoundRecord{}). - Select("type", "campus", "kind", "content", "is_processed"). + result := database.DB.Model(models.LostAndFoundRecord{}).Select("*"). + Omit("id", "type", "publish_time", "publisher"). Where(&models.LostAndFoundRecord{ID: id}).Updates(&record) if result.Error != nil { return result.Error } - return nil }