Skip to content

Commit

Permalink
Merge pull request #73 from XiMo-210/main
Browse files Browse the repository at this point in the history
修复正装借用记录排序问题并添加批量操作接口
  • Loading branch information
XiMo-210 authored Apr 21, 2024
2 parents 3c3c6fe + e0cc16c commit 6111956
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 7 deletions.
3 changes: 2 additions & 1 deletion app/apiException/apiException.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var (
PersonalInfoNotFill = NewError(http.StatusInternalServerError, 200519, "请先填写个人基本信息")
StockNotEnough = NewError(http.StatusInternalServerError, 200520, "物资库存不足")
RecordAlreadyExisted = NewError(http.StatusInternalServerError, 200521, "该用户已经申请过该物资")
RecordRejected = NewError(http.StatusInternalServerError, 200522, "该申请已经被驳回")
RecordAlreadyRejected = NewError(http.StatusInternalServerError, 200522, "含有已经被驳回的申请,请重新选择")
NotBorrowingRecord = NewError(http.StatusInternalServerError, 200523, "含有非借用中的记录,请重新选择")
NotInit = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound))
NotFound = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound))
Unknown = NewError(http.StatusInternalServerError, 300500, "系统异常,请稍后重试!")
Expand Down
126 changes: 123 additions & 3 deletions app/controllers/funcControllers/suppliesController/borrowRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ type CheckRecordData struct {
ID int `json:"id" binding:"required"` // 申请id
}

// 管理员审批
// 管理员单次审批
func CheckRecordByAdmin(c *gin.Context) {
// 获取参数
var data CheckRecordData
Expand All @@ -348,7 +348,7 @@ func CheckRecordByAdmin(c *gin.Context) {
}
// 判断是否已经待审核
if record.Status == 2 {
_ = c.AbortWithError(200, apiException.RecordRejected)
_ = c.AbortWithError(200, apiException.RecordAlreadyRejected)
return
} else if record.Status != 1 && record.Status != 2 {
_ = c.AbortWithError(200, apiException.ServerError)
Expand Down Expand Up @@ -378,6 +378,75 @@ func CheckRecordByAdmin(c *gin.Context) {
utils.JsonSuccessResponse(c, nil)
}

type BatchCheckRecordData struct {
SuppliesCheck int `json:"supplies_check" binding:"required,oneof=1 2"` // 1:通过 2:驳回
IDs []int `json:"ids" binding:"required"` // 申请id
}

// 管理员批量审批
func BatchCheckRecordByAdmin(c *gin.Context) {
// 获取参数
var data BatchCheckRecordData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}
// 判断鉴权
user, err := sessionServices.GetUserSession(c)
if err != nil {
_ = c.AbortWithError(200, apiException.NotLogin)
return
}
if user.Type != models.Admin && user.Type != models.StudentAffairsCenter {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
// 获取记录,逐个判断是否存在且合法
records, err := suppliesServices.GetBorrowRecordsByBorrowIDs(data.IDs)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if len(records) != len(data.IDs) {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
for _, record := range records {
if record.Status == 2 {
_ = c.AbortWithError(200, apiException.RecordAlreadyRejected)
return
} else if record.Status != 1 && record.Status != 2 {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if data.SuppliesCheck == 1 {
//查询物资是否充足
var supplies models.Supplies
supplies, err = suppliesServices.GetALLSuppliesById(record.SuppliesID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if supplies.Stock < record.Count {
_ = c.AbortWithError(200, apiException.StockNotEnough)
return
}
}
}
// 批量审批
if data.SuppliesCheck == 1 {
err = suppliesServices.PassBorrows(data.IDs)
} else if data.SuppliesCheck == 2 {
err = suppliesServices.RejectBorrows(data.IDs)
}
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
utils.JsonSuccessResponse(c, nil)
}

// 管理员取消驳回
type CancelRejectlDate struct {
ID int `json:"id" binding:"required"`
Expand Down Expand Up @@ -453,7 +522,7 @@ func ReturnRecordByAdmin(c *gin.Context) {
}
// 判断是否已经审批
if record.Status != 3 {
_ = c.AbortWithError(200, apiException.ServerError)
_ = c.AbortWithError(200, apiException.NotBorrowingRecord)
return
}
// 归还清点
Expand All @@ -472,6 +541,57 @@ func ReturnRecordByAdmin(c *gin.Context) {
utils.JsonSuccessResponse(c, nil)
}

// 管理员批量归还清点
type BatchReturnRecordData struct {
SuppliesReturn int `json:"supplies_return" binding:"required,oneof=1 2"` // 1:确认归还 2:取消借出
IDs []int `json:"ids" binding:"required"`
}

func BatchReturnRecordByAdmin(c *gin.Context) {
// 获取参数
var data BatchReturnRecordData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}
// 判断鉴权
user, err := sessionServices.GetUserSession(c)
if err != nil {
_ = c.AbortWithError(200, apiException.NotLogin)
return
}
if user.Type != models.Admin && user.Type != models.StudentAffairsCenter {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
records, err := suppliesServices.GetBorrowRecordsByBorrowIDs(data.IDs)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if len(records) != len(data.IDs) {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
for _, record := range records {
if record.Status != 3 {
_ = c.AbortWithError(200, apiException.NotBorrowingRecord)
return
}
}
if data.SuppliesReturn == 1 {
err = suppliesServices.ReturnBorrows(data.IDs)
} else if data.SuppliesReturn == 2 {
err = suppliesServices.CancelBorrows(data.IDs)
}
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
utils.JsonSuccessResponse(c, nil)
}

type CancelReturnDate struct {
ID int `json:"id" binding:"required"`
}
Expand Down
101 changes: 98 additions & 3 deletions app/services/suppliesServices/borrowRecordService.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func GetBorrowRecordByBorrowID(borrowID int) (models.BorrowRecord, error) {
return borrowRecord, nil
}

func GetBorrowRecordsByBorrowIDs(borrowIDs []int) ([]models.BorrowRecord, error) {
var borrowRecords []models.BorrowRecord
result := database.DB.Where("id IN ?", borrowIDs).Find(&borrowRecords)
if result.Error != nil {
return nil, result.Error
}
for i := range borrowRecords {
aesDecryptContact(&borrowRecords[i])
}
return borrowRecords, nil
}

func DeleteRecord(RecordID int) error {
result := database.DB.Delete(models.BorrowRecord{ID: RecordID})
return result.Error
Expand Down Expand Up @@ -97,11 +109,11 @@ func GetRecordByAdmin(pageNum, pageSize, status, choice, id int, campus uint8, s
} else {
switch choice {
case 0:
query = query.Where("status IN ?", []int{1, 2, 3, 4}).Order("apply_time desc")
query = query.Where("status IN ?", []int{1, 2, 3, 4}).Order("status").Order("apply_time desc")
case 1:
query = query.Where("status IN ?", []int{1, 2}).Order("apply_time desc")
query = query.Where("status IN ?", []int{1, 2}).Order("status").Order("apply_time desc")
case 2:
query = query.Where("status IN ?", []int{3, 4}).Order("CASE WHEN DATE_ADD(borrow_time, INTERVAL 7 DAY) > NOW() THEN 1 ELSE 0 END, borrow_time").Order("apply_time desc")
query = query.Where("status IN ?", []int{3, 4}).Order("status").Order("CASE WHEN DATE_ADD(borrow_time, INTERVAL 7 DAY) > NOW() THEN 1 ELSE 0 END, borrow_time").Order("apply_time desc")
}
}

Expand Down Expand Up @@ -142,11 +154,35 @@ func PassBorrow(id int, sid int, num uint) error {
return result.Error
}

func PassBorrows(ids []int) error {
result := database.DB.Model(models.BorrowRecord{}).Where("id IN ?", ids).Updates(map[string]interface{}{"status": 3, "borrow_time": time.Now()})
if result.Error != nil {
return result.Error
}
var records []models.BorrowRecord
result = database.DB.Where("id IN ?", ids).Find(&records)
if result.Error != nil {
return result.Error
}
for i := range records {
result = database.DB.Model(models.Supplies{}).Unscoped().Where(models.Supplies{ID: records[i].SuppliesID}).Updates(map[string]interface{}{"stock": gorm.Expr("stock - ?", records[i].Count), "borrowed": gorm.Expr("borrowed + ?", records[i].Count)})
if result.Error != nil {
return result.Error
}
}
return nil
}

func RejectBorrow(id int) error {
result := database.DB.Model(models.BorrowRecord{}).Where(models.BorrowRecord{ID: id}).Update("status", 2)
return result.Error
}

func RejectBorrows(id []int) error {
result := database.DB.Model(models.BorrowRecord{}).Where("id IN ?", id).Update("status", 2)
return result.Error
}

func CancelRejectBorrow(id int) error {
result := database.DB.Model(models.BorrowRecord{}).Where(models.BorrowRecord{ID: id}).Update("status", 1)
return result.Error
Expand All @@ -168,6 +204,36 @@ func ReturnBorrow(id int, sid int, num uint) error {
return result.Error
}

func ReturnBorrows(ids []int) error {
result := database.DB.Model(models.BorrowRecord{}).Where("id IN ?", ids).Updates(map[string]interface{}{"status": 4, "return_time": time.Now()})
if result.Error != nil {
return result.Error
}
var records []models.BorrowRecord
result = database.DB.Where("id IN ?", ids).Find(&records)
if result.Error != nil {
return result.Error
}
var supplies []models.Supplies
for i := range records {
var supply models.Supplies
result := database.DB.Where(models.Supplies{ID: records[i].SuppliesID}).Unscoped().First(&supply)
if result.Error != nil {
return result.Error
}
supplies = append(supplies, supply)
}
for i := range supplies {
if supplies[i].Kind == "正装" {
result = database.DB.Model(models.Supplies{}).Where(models.Supplies{ID: supplies[i].ID}).Unscoped().Updates(map[string]interface{}{"stock": gorm.Expr("stock + ?", records[i].Count), "borrowed": gorm.Expr("borrowed - ?", records[i].Count)})
if result.Error != nil {
return result.Error
}
}
}
return nil
}

func CancelBorrow(id int, sid int, num uint) error {
var supplies models.Supplies
result := database.DB.Where(models.Supplies{ID: sid}).Unscoped().First(&supplies)
Expand All @@ -186,6 +252,35 @@ func CancelBorrow(id int, sid int, num uint) error {
return result.Error
}

func CancelBorrows(id []int) error {
var records []models.BorrowRecord
result := database.DB.Where("id IN ?", id).Find(&records)
if result.Error != nil {
return result.Error
}
var supplies []models.Supplies
for i := range records {
var supply models.Supplies
result := database.DB.Where(models.Supplies{ID: records[i].SuppliesID}).Unscoped().First(&supply)
if result.Error != nil {
return result.Error
}
supplies = append(supplies, supply)
}
for i := range supplies {
if supplies[i].Kind == "正装" {
result = database.DB.Model(models.BorrowRecord{}).Where("id IN ?", id).Updates(map[string]interface{}{"status": 1, "borrow_time": nil})
if result.Error != nil {
return result.Error
}
result = database.DB.Model(models.Supplies{}).Where(models.Supplies{ID: supplies[i].ID}).Unscoped().Updates(map[string]interface{}{"stock": gorm.Expr("stock + ?", records[i].Count), "borrowed": gorm.Expr("borrowed - ?", records[i].Count)})
} else {
result = database.DB.Delete(models.BorrowRecord{ID: records[i].ID})
}
}
return result.Error
}

func CancelReturnBorrow(id int, sid int, num uint) error {
result := database.DB.Model(models.BorrowRecord{}).Where(models.BorrowRecord{ID: id}).Updates(map[string]interface{}{"status": 3, "return_time": nil})
if result.Error != nil {
Expand Down
2 changes: 2 additions & 0 deletions config/router/adminRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ func adminRouterInit(r *gin.RouterGroup) {

suppliesBorrow.GET("record", suppliesController.GetSuppliesRecordByAdmin)
suppliesBorrow.POST("supplies-check", suppliesController.CheckRecordByAdmin)
suppliesBorrow.POST("batch-supplies-check", suppliesController.BatchCheckRecordByAdmin)
suppliesBorrow.POST("cancel-reject", suppliesController.CancelRejectRecordByAdmin)
suppliesBorrow.POST("supplies-return", suppliesController.ReturnRecordByAdmin)
suppliesBorrow.POST("batch-supplies-return", suppliesController.BatchReturnRecordByAdmin)
suppliesBorrow.POST("supplies-cancel", suppliesController.CancelReturnRecordByAdmin)
suppliesBorrow.PUT("supplies-update", suppliesController.UpdateRecordByAdmin)

Expand Down

0 comments on commit 6111956

Please sign in to comment.