diff --git a/app/apiException/apiException.go b/app/apiException/apiException.go index 192af2c..78e0c0e 100644 --- a/app/apiException/apiException.go +++ b/app/apiException/apiException.go @@ -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, "系统异常,请稍后重试!") diff --git a/app/controllers/funcControllers/suppliesController/borrowRecord.go b/app/controllers/funcControllers/suppliesController/borrowRecord.go index 49cfded..9851b3d 100644 --- a/app/controllers/funcControllers/suppliesController/borrowRecord.go +++ b/app/controllers/funcControllers/suppliesController/borrowRecord.go @@ -321,7 +321,7 @@ type CheckRecordData struct { ID int `json:"id" binding:"required"` // 申请id } -// 管理员审批 +// 管理员单次审批 func CheckRecordByAdmin(c *gin.Context) { // 获取参数 var data CheckRecordData @@ -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) @@ -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"` @@ -453,7 +522,7 @@ func ReturnRecordByAdmin(c *gin.Context) { } // 判断是否已经审批 if record.Status != 3 { - _ = c.AbortWithError(200, apiException.ServerError) + _ = c.AbortWithError(200, apiException.NotBorrowingRecord) return } // 归还清点 @@ -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"` } diff --git a/app/services/suppliesServices/borrowRecordService.go b/app/services/suppliesServices/borrowRecordService.go index f66388d..b634884 100644 --- a/app/services/suppliesServices/borrowRecordService.go +++ b/app/services/suppliesServices/borrowRecordService.go @@ -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 @@ -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") } } @@ -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 @@ -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) @@ -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 { diff --git a/config/router/adminRouter.go b/config/router/adminRouter.go index 20489bf..5d3fccc 100755 --- a/config/router/adminRouter.go +++ b/config/router/adminRouter.go @@ -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)