feat(report):基本完成报表的初始化、基本索引信息获取和步骤信息获取。

This commit is contained in:
徐涛 2022-08-20 15:59:07 +08:00
parent 52a83bd34e
commit b2126a534b
6 changed files with 263 additions and 3 deletions

99
controller/report.go Normal file
View 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})
}

View File

@ -8,7 +8,7 @@ type EndUserDetail struct {
ParkId string `xorm:"varchar(120) pk not null" json:"parkId"`
MeterId string `xorm:"meter_04kv_id varchar(120) pk not null" json:"meterId"`
Seq int64 `xorm:"bigint not null default 0" json:"seq"`
Ratio decimal.Decimal `xorm:"numeric(8,4) not null deafult 1" json:"ratio"`
Ratio decimal.Decimal `xorm:"numeric(8,4) not null default 1" json:"ratio"`
Address *string `xorm:"varchar(100)" json:"address"`
CustomerName *string `xorm:"varchar(100)" json:"customerName"`
ContactName *string `xorm:"varchar(70)" json:"contactName"`

View File

@ -2,6 +2,13 @@ package model
import "time"
const (
REPORT_NOT_WITHDRAW int8 = iota
REPORT_WITHDRAW_APPLIED
REPORT_WITHDRAW_DENIED
REPORT_WITHDRAW_GRANTED
)
type Report struct {
CreatedAndModified `xorm:"extends"`
Id string `xorm:"varchar(120) pk not null" json:"id"`
@ -27,3 +34,29 @@ type Steps struct {
func (Report) TableName() string {
return "report"
}
func NewSteps() Steps {
return Steps{
Summary: false,
WillDiluted: false,
Submeter: false,
Calculate: false,
Preview: false,
Publish: false,
}
}
type ParkNewestReport struct {
Park Park `xorm:"extends" json:"park"`
Report *Report `xorm:"extends" json:"report"`
}
func (ParkNewestReport) TableName() string {
return "park"
}
func (p *ParkNewestReport) AfterLoad() {
if p.Report != nil && len(p.Report.Id) == 0 {
p.Report = nil
}
}

View File

@ -21,6 +21,7 @@ func Router() *gin.Engine {
controller.InitializeParkController(router)
controller.InitializeMaintenanceFeeController(router)
controller.InitializeMeter04kVController(router)
controller.InitializeReportController(router)
return router
}

127
service/report.go Normal file
View File

@ -0,0 +1,127 @@
package service
import (
"electricity_bill_calc/global"
"electricity_bill_calc/model"
"electricity_bill_calc/utils"
"time"
"github.com/google/uuid"
"xorm.io/builder"
)
type _ReportService struct{}
var ReportService _ReportService
func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewestReport, error) {
parks := make([]model.ParkNewestReport, 0)
err := global.DBConn.
Alias("p").
Join("LEFT", []string{"report", "r"}, "r.park_id=p.id").
Where(builder.Eq{"p.user_id": uid, "p.enabled": true}).
Find(&parks)
if err != nil {
return make([]model.ParkNewestReport, 0), err
}
reducedParks := utils.Reduce(
parks,
make(map[string]model.ParkNewestReport, 0),
func(acc map[string]model.ParkNewestReport, elem model.ParkNewestReport) map[string]model.ParkNewestReport {
if v, ok := acc[elem.Park.Id]; ok {
if elem.Report != nil {
if v.Report == nil || (elem.Report.Period.After(v.Report.Period)) {
acc[elem.Park.Id] = elem
}
}
} else {
acc[elem.Park.Id] = elem
}
return acc
},
)
return utils.Values(reducedParks), nil
}
func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, error) {
reports := make([]model.Report, 0)
err := global.DBConn.
Table("report").Alias("r").
Join("INNER", []string{"park", "p"}, "r.park_id=p.id").
Where(builder.Eq{"p.user_id": uid}).
Find(&reports)
if err != nil {
return false, nil
}
// 检查给定的期数在目前的记录中是否已经存在
exists := utils.Reduce(
reports,
false,
func(acc bool, elem model.Report) bool {
if elem.Period.Equal(period) {
return acc || true
} else {
return acc || false
}
},
)
if exists {
return false, nil
}
// 检查给定的期数与目前已发布的最大期数的关系
maxPublished := utils.Reduce(
reports,
nil,
func(acc *time.Time, elem model.Report) *time.Time {
if elem.Published {
if acc == nil || (acc != nil && elem.Period.After(*acc)) {
return &elem.Period
}
}
return acc
},
)
// 检查给定的期数与目前未发布的最大期数的关系
maxUnpublished := utils.Reduce(
reports,
nil,
func(acc *time.Time, elem model.Report) *time.Time {
if acc == nil || (acc != nil && elem.Period.After(*acc)) {
return &elem.Period
}
return acc
},
)
if maxUnpublished == nil {
return true, nil
}
if maxPublished != nil && maxUnpublished.Equal(*maxPublished) {
// 此时不存在未发布的报表
return utils.IsNextMonth(*maxPublished, period), nil
} else {
// 存在未发布的报表
return false, nil
}
}
func (_ReportService) InitializeNewReport(report model.Report) (err error) {
report.Id = uuid.New().String()
report.StepState = model.NewSteps()
report.Published = false
report.Withdraw = model.REPORT_NOT_WITHDRAW
_, err = global.DBConn.Insert(report)
if err != nil {
return
}
return nil
}
func (_ReportService) RetreiveReportIndex(rid string) (*model.Report, error) {
reports := make([]model.Report, 0)
err := global.DBConn.Where(builder.Eq{"id": rid}).Find(&reports)
if len(reports) > 0 {
return &reports[0], nil
} else {
return nil, err
}
}

View File

@ -19,6 +19,6 @@ func DifferenceInMonth(t1, t2 time.Time) int {
return differYear*12 + differMonth
}
func IsNextMonth(t1, t2 time.Time) bool {
return DifferenceInMonth(t1, t2) == -1
func IsNextMonth(origin, t time.Time) bool {
return DifferenceInMonth(t, origin) == 1
}