feat(report):完成待摊薄维护费的批量导入和添加功能。

This commit is contained in:
徐涛 2022-08-21 18:25:56 +08:00
parent b198bfc423
commit ef8bd0c617
2 changed files with 139 additions and 0 deletions

View File

@ -2,13 +2,16 @@ package controller
import (
"electricity_bill_calc/exceptions"
"electricity_bill_calc/model"
"electricity_bill_calc/response"
"electricity_bill_calc/security"
"electricity_bill_calc/service"
"electricity_bill_calc/tools"
"log"
"net/http"
"time"
"github.com/fufuok/utils"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"github.com/samber/lo"
@ -23,6 +26,9 @@ func InitializeReportController(router *gin.Engine) {
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)
router.GET("/report/:rid/maintenance", security.EnterpriseAuthorize, fetchWillDilutedFees)
router.POST("/report/:rid/maintenance", security.EnterpriseAuthorize, createTemporaryWillDilutedFee)
router.POST("/report/:rid/maintenance/import", security.EnterpriseAuthorize, importPredefinedMaintenanceFees)
}
func ensureReportBelongs(c *gin.Context, result *response.Result, requestReportId string) bool {
@ -192,3 +198,102 @@ func progressReportSummary(c *gin.Context) {
}
result.Success("已经完成园区概况的计算,并可以进行到下一步骤。")
}
func fetchWillDilutedFees(c *gin.Context) {
result := response.NewResult(c)
requestReportId := c.Param("rid")
if !ensureReportBelongs(c, result, requestReportId) {
return
}
fees, err := service.ReportService.FetchWillDulutedMaintenanceFees(requestReportId)
if err != nil {
result.NotFound(err.Error())
return
}
result.Json(http.StatusOK, "待摊薄费用已经获取到。", gin.H{"fees": fees})
}
type DilutedFeeCreationFormData struct {
ParkId string `json:"parkId" form:"parkId"`
Name string `json:"name" form:"name"`
Fee decimal.Decimal `json:"fee" form:"fee"`
Memo *string `json:"memo" form:"memo"`
}
func createTemporaryWillDilutedFee(c *gin.Context) {
result := response.NewResult(c)
requestReportId := c.Param("rid")
if !ensureReportBelongs(c, result, requestReportId) {
return
}
formData := new(DilutedFeeCreationFormData)
c.BindJSON(formData)
report, err := service.ReportService.RetreiveReportIndex(requestReportId)
if err != nil {
result.NotFound(err.Error())
return
}
if formData.ParkId != report.ParkId {
result.NotAccept("选择的园区与公示报表所属的园区不一致。")
return
}
newWillDilutedFee := new(model.WillDilutedFee)
copier.Copy(newWillDilutedFee, formData)
newWillDilutedFee.ReportId = report.Id
err = service.ReportService.CreateTemporaryWillDilutedMaintenanceFee(*newWillDilutedFee)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Created("公示报表中所要使用的临时待摊薄费用已添加。")
}
func importPredefinedMaintenanceFees(c *gin.Context) {
result := response.NewResult(c)
requestReportId := c.Param("rid")
if !ensureReportBelongs(c, result, requestReportId) {
return
}
report, err := service.ReportService.RetreiveReportIndex(requestReportId)
if err != nil {
result.NotFound(err.Error())
return
}
maintenanceFees, err := service.MaintenanceFeeService.ListMaintenanceFees([]string{report.ParkId})
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
log.Printf("[cotroller] [debug] fees: %+v", maintenanceFees)
enabledMaintenanceFees := lo.Filter(
maintenanceFees,
func(elem model.MaintenanceFee, index int) bool {
return elem.Enabled
},
)
log.Printf("[cotroller] [debug] fees: %+v", enabledMaintenanceFees)
if len(enabledMaintenanceFees) == 0 {
result.NotFound("没有找到可供导入的配电维护费记录。")
return
}
dilutedFees := lo.Map(
enabledMaintenanceFees,
func(elem model.MaintenanceFee, index int) model.WillDilutedFee {
fee := &model.WillDilutedFee{
Id: utils.UUIDString(),
ReportId: report.Id,
SourceId: lo.ToPtr(elem.Id),
Name: elem.Name,
Fee: elem.Fee,
Memo: elem.Memo,
}
return *fee
},
)
err = service.ReportService.BatchSaveMaintenanceFee(dilutedFees)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Created("预定义的配电维护费已经导入。")
}

View File

@ -7,6 +7,7 @@ import (
"electricity_bill_calc/tools"
"time"
"github.com/fufuok/utils"
"github.com/google/uuid"
"github.com/samber/lo"
"xorm.io/builder"
@ -203,3 +204,36 @@ func (_ReportService) CalculateSummaryAndFinishStep(reportId string) error {
}
return nil
}
func (_ReportService) FetchWillDulutedMaintenanceFees(reportId string) ([]model.WillDilutedFee, error) {
fees := make([]model.WillDilutedFee, 0)
err := global.DBConn.Where(builder.Eq{"report_id": reportId}).Asc("created_at").Find(&fees)
if err != nil {
return make([]model.WillDilutedFee, 0), nil
}
return fees, nil
}
func (_ReportService) CreateTemporaryWillDilutedMaintenanceFee(fee model.WillDilutedFee) error {
fee.Id = utils.UUIDString()
_, err := global.DBConn.Insert(fee)
return err
}
func (_ReportService) BatchSaveMaintenanceFee(fees []model.WillDilutedFee) error {
tx := global.DBConn.NewSession()
if err := tx.Begin(); err != nil {
return err
}
defer tx.Close()
_, err := tx.Insert(fees)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return err
}
return nil
}