forked from free-lancers/electricity_bill_calc_service
feat(report):基本完成报表的初始化、基本索引信息获取和步骤信息获取。
This commit is contained in:
99
controller/report.go
Normal file
99
controller/report.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
sure, err := service.ParkService.EnsurePark(userSession.Uid, requestParkId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if !sure {
|
||||
result.Unauthorized("不能访问不属于自己的园区。")
|
||||
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
|
||||
}
|
||||
newReport := model.Report{ParkId: requestParkId, Period: reportPeriod}
|
||||
err = service.ReportService.InitializeNewReport(newReport)
|
||||
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")
|
||||
userSession, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
result.Unauthorized(err.Error())
|
||||
return
|
||||
}
|
||||
requestReport, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
||||
if err != nil {
|
||||
result.NotFound(err.Error())
|
||||
return
|
||||
}
|
||||
sure, err := service.ParkService.EnsurePark(userSession.Uid, requestReport.ParkId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if !sure {
|
||||
result.Unauthorized("不能访问不属于自己的园区。")
|
||||
return
|
||||
}
|
||||
result.Json(http.StatusOK, "已经获取到指定报表的填写状态。", gin.H{"steps": requestReport.StepState})
|
||||
}
|
Reference in New Issue
Block a user