135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/logger"
|
|
"electricity_bill_calc/repository"
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/security"
|
|
"electricity_bill_calc/vo"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
"net/http"
|
|
)
|
|
|
|
var withdrawLog = logger.Named("Handler", "Withdraw")
|
|
|
|
func InitializeWithdrawHandlers(router *fiber.App) {
|
|
router.Get("/withdraw", security.OPSAuthorize, withdraw)
|
|
router.Put("/withdraw/:rid", security.OPSAuthorize, reviewRequestWithdraw)
|
|
router.Delete("/withdraw/:rid", security.EnterpriseAuthorize, recallReport)
|
|
}
|
|
|
|
//用于分页检索用户的核算报表
|
|
func withdraw(c *fiber.Ctx) error {
|
|
//记录日志
|
|
withdrawLog.Info("带分页的待审核的核算撤回申请列表")
|
|
//获取请求参数
|
|
result := response.NewResult(c)
|
|
keyword := c.Query("keyword", "")
|
|
page := c.QueryInt("page", 1)
|
|
withdrawLog.Info("参数为: ", zap.String("keyword", keyword), zap.Int("page", page))
|
|
//中间数据库操作暂且省略。。。。
|
|
//首先进行核算报表的分页查询
|
|
withdraws, total, err := repository.WithdrawRepository.FindWithdraw(uint(page), &keyword)
|
|
if err != nil {
|
|
withdrawLog.Error("检索用户核算报表失败。", zap.Error(err))
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
//TODO: 2023-07-18 此处返回值是个示例,具体返回值需要查询数据库(完成)
|
|
return result.Success(
|
|
"withdraw请求成功",
|
|
response.NewPagedResponse(page, total).ToMap(),
|
|
fiber.Map{"records": withdraws},
|
|
)
|
|
}
|
|
|
|
//用于审核撤回报表
|
|
func reviewRequestWithdraw(c *fiber.Ctx) error {
|
|
Rid := c.Params("rid", "")
|
|
Data := new(vo.ReviewWithdraw)
|
|
result := response.NewResult(c)
|
|
|
|
if err := c.BodyParser(&Data); err != nil {
|
|
withdrawLog.Error("无法解析审核指定报表的请求数据", zap.Error(err))
|
|
return result.BadRequest("无法解析审核指定报表的请求数据。")
|
|
}
|
|
|
|
if Data.Audit == true { //审核通过
|
|
ok, err := repository.WithdrawRepository.ReviewTrueReportWithdraw(Rid)
|
|
if err != nil {
|
|
withdrawLog.Error("审核同意撤回报表失败")
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
if !ok {
|
|
withdrawLog.Error("审核同意撤回报表失败")
|
|
return result.NotAccept("审核同意撤回报表失败")
|
|
} else {
|
|
return result.Success("审核同意撤回报表成功!")
|
|
}
|
|
} else { //审核不通过
|
|
ok, err := repository.WithdrawRepository.ReviewFalseReportWithdraw(Rid)
|
|
if err != nil {
|
|
withdrawLog.Error("审核拒绝撤回报表失败")
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
if !ok {
|
|
withdrawLog.Error("审核拒绝撤回报表失败")
|
|
return result.NotAccept("审核拒绝撤回报表失败")
|
|
} else {
|
|
return result.Success("审核拒绝撤回报表成功!")
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//用于撤回电费核算
|
|
func recallReport(c *fiber.Ctx) error {
|
|
// 获取用户会话信息和参数
|
|
rid := c.Params("rid", "")
|
|
result := response.NewResult(c)
|
|
session, err := _retreiveSession(c)
|
|
if err != nil {
|
|
withdrawLog.Error("无法获取当前用户的会话。")
|
|
return result.Unauthorized(err.Error())
|
|
}
|
|
// 检查指定报表的所属情况
|
|
isBelongsTo, err := repository.ReportRepository.IsBelongsTo(rid, session.Uid)
|
|
if err != nil {
|
|
withdrawLog.Error("检查报表所属情况出现错误。", zap.Error(err))
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
if err == nil && isBelongsTo == true {
|
|
// 判断指定报表是否是当前园区的最后一张报表
|
|
isLastReport, err := repository.ReportRepository.IsLastReport(rid)
|
|
if err != nil {
|
|
withdrawLog.Error("判断指定报表是否为当前园区的最后一张报表时出错", zap.Error(err))
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
if err == nil && isLastReport == true {
|
|
// 申请撤回指定的核算报表
|
|
//TODO: 2023.07.25 申请撤回指定核算报表,正确状态未处理(完成)
|
|
ok, err := repository.ReportRepository.ApplyWithdrawReport(rid)
|
|
if err != nil {
|
|
withdrawLog.Error("申请撤回指定核算报表出错", zap.Error(err))
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
if ok {
|
|
withdrawLog.Info("申请撤回指定核算报表成功")
|
|
return result.Success("申请撤回指定核算报表成功")
|
|
}
|
|
} else {
|
|
withdrawLog.Info("当前报表不是当前园区的最后一张报表")
|
|
return result.Error(http.StatusForbidden, "当前报表不是当前园区的最后一张报表")
|
|
}
|
|
} else {
|
|
withdrawLog.Info("指定的核算报表不属于当前用户。")
|
|
return result.Error(http.StatusForbidden, "指定的核算报表不属于当前用户")
|
|
}
|
|
|
|
return result.Error(http.StatusInternalServerError, "其他错误")
|
|
}
|