195 lines
6.3 KiB
Go
195 lines
6.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/exceptions"
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/security"
|
|
"electricity_bill_calc/service"
|
|
"electricity_bill_calc/tools"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/copier"
|
|
"github.com/samber/lo"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
func InitializeReportController(router *gin.Engine) {
|
|
router.GET("/reports/with/drafts", security.EnterpriseAuthorize, fetchNewestReportOfParkWithDraft)
|
|
router.POST("/park/:pid/report", security.EnterpriseAuthorize, initializeNewReport)
|
|
router.GET("/report/:rid/step/state", security.EnterpriseAuthorize, fetchReportStepStates)
|
|
router.GET("/report/:rid/summary", security.EnterpriseAuthorize, fetchReportParkSummary)
|
|
router.PUT("/report/:rid/summary", security.EnterpriseAuthorize, fillReportSummary)
|
|
router.GET("/report/:rid/summary/calculate", security.EnterpriseAuthorize, testCalculateReportSummary)
|
|
router.POST("/report/:rid/summary/calculate", security.EnterpriseAuthorize, progressReportSummary)
|
|
}
|
|
|
|
func ensureReportBelongs(c *gin.Context, result *response.Result, requestReportId string) bool {
|
|
_, err := _retreiveSession(c)
|
|
if err != nil {
|
|
result.Unauthorized(err.Error())
|
|
return false
|
|
}
|
|
requestReport, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
|
if err != nil {
|
|
result.NotFound(err.Error())
|
|
return false
|
|
}
|
|
if requestReport == nil {
|
|
result.NotFound("指定报表未能找到。")
|
|
return false
|
|
}
|
|
return ensureParkBelongs(c, result, requestReport.ParkId)
|
|
}
|
|
|
|
func fetchNewestReportOfParkWithDraft(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
userSession, err := _retreiveSession(c)
|
|
if err != nil {
|
|
result.Unauthorized(err.Error())
|
|
return
|
|
}
|
|
parks, err := service.ReportService.FetchParksWithNewestReport(userSession.Uid)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
result.Json(http.StatusOK, "已获取到指定用户下所有园区的最新报表记录。", gin.H{"parks": parks})
|
|
}
|
|
|
|
func initializeNewReport(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestParkId := c.Param("pid")
|
|
userSession, err := _retreiveSession(c)
|
|
if err != nil {
|
|
result.Unauthorized(err.Error())
|
|
return
|
|
}
|
|
if !ensureParkBelongs(c, result, requestParkId) {
|
|
return
|
|
}
|
|
requestPeriod := c.Query("period")
|
|
reportPeriod, err := time.Parse("2006-01", requestPeriod)
|
|
if err != nil {
|
|
result.NotAccept("提供的初始化期数格式不正确。")
|
|
return
|
|
}
|
|
valid, err := service.ReportService.IsNewPeriodValid(userSession.Uid, reportPeriod)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
if !valid {
|
|
result.NotAccept("只能初始化已发布报表下一个月份的新报表。")
|
|
return
|
|
}
|
|
err = service.ReportService.InitializeNewReport(requestParkId, reportPeriod)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
result.Created("新一期报表初始化成功。")
|
|
}
|
|
|
|
func fetchReportStepStates(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestReportId := c.Param("rid")
|
|
if !ensureReportBelongs(c, result, requestReportId) {
|
|
return
|
|
}
|
|
requestReport, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
|
if err != nil {
|
|
result.NotFound(err.Error())
|
|
return
|
|
}
|
|
result.Json(http.StatusOK, "已经获取到指定报表的填写状态。", gin.H{"steps": requestReport.StepState})
|
|
}
|
|
|
|
func fetchReportParkSummary(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestReportId := c.Param("rid")
|
|
if !ensureReportBelongs(c, result, requestReportId) {
|
|
return
|
|
}
|
|
summary, err := service.ReportService.RetreiveReportSummary(requestReportId)
|
|
if err != nil {
|
|
result.NotFound(err.Error())
|
|
return
|
|
}
|
|
if summary == nil {
|
|
result.NotFound("指定报表未能找到。")
|
|
return
|
|
}
|
|
result.Json(http.StatusOK, "已经获取到指定报表中的园区概况。", gin.H{"summary": summary})
|
|
}
|
|
|
|
type ReportSummaryFormData struct {
|
|
Overall decimal.Decimal `json:"overall" form:"overall"`
|
|
OverallFee decimal.Decimal `json:"overallFee" form:"overallFee"`
|
|
Critical decimal.Decimal `json:"critical" form:"critical"`
|
|
CriticalFee decimal.Decimal `json:"criticalFee" form:"criticalFee"`
|
|
Peak decimal.Decimal `json:"peak" form:"peak"`
|
|
PeakFee decimal.Decimal `json:"peakFee" form:"peakFee"`
|
|
Valley decimal.Decimal `json:"valley" form:"valley"`
|
|
ValleyFee decimal.Decimal `json:"valleyFee" form:"valleyFee"`
|
|
BasicFee decimal.Decimal `json:"basicFee" form:"basicFee"`
|
|
AdjustFee decimal.Decimal `json:"adjustFee" from:"adjustFee"`
|
|
}
|
|
|
|
func fillReportSummary(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestReportId := c.Param("rid")
|
|
if !ensureReportBelongs(c, result, requestReportId) {
|
|
return
|
|
}
|
|
formData := new(ReportSummaryFormData)
|
|
c.BindJSON(formData)
|
|
originSummary, err := service.ReportService.RetreiveReportSummary(requestReportId)
|
|
if err != nil {
|
|
result.NotFound(err.Error())
|
|
return
|
|
}
|
|
copier.Copy(originSummary, formData)
|
|
err = service.ReportService.UpdateReportSummary(originSummary)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
result.Updated("指定电费公示报表中的园区概况基本数据已经完成更新。")
|
|
}
|
|
|
|
func testCalculateReportSummary(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestReportId := c.Param("rid")
|
|
if !ensureReportBelongs(c, result, requestReportId) {
|
|
return
|
|
}
|
|
summary, err := service.ReportService.RetreiveReportSummary(requestReportId)
|
|
if err != nil {
|
|
result.NotFound(err.Error())
|
|
return
|
|
}
|
|
summary.CalculatePrices()
|
|
calcResults := tools.ConvertStructToMap(summary)
|
|
result.Json(http.StatusOK, "已完成园区概况的试计算。", gin.H{"result": lo.PickByKeys(calcResults, []string{"overallPrice", "criticalPrice", "peakPrice", "flat", "flatFee", "flatPrice", "valleyPrice"})})
|
|
}
|
|
|
|
func progressReportSummary(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestReportId := c.Param("rid")
|
|
if !ensureReportBelongs(c, result, requestReportId) {
|
|
return
|
|
}
|
|
err := service.ReportService.CalculateSummaryAndFinishStep(requestReportId)
|
|
if err != nil {
|
|
if nfErr, ok := err.(exceptions.NotFoundError); ok {
|
|
result.NotFound(nfErr.Error())
|
|
} else {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return
|
|
}
|
|
result.Success("已经完成园区概况的计算,并可以进行到下一步骤。")
|
|
}
|