110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/security"
|
|
"electricity_bill_calc/service"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
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.RetreiveParkSummary(requestReportId)
|
|
if err != nil {
|
|
result.NotFound(err.Error())
|
|
return
|
|
}
|
|
result.Json(http.StatusOK, "已经获取到指定报表中的园区概况。", gin.H{"summary": summary})
|
|
}
|