feat(report):已完成用户抄表记录的上传处理。
This commit is contained in:
parent
ba7af386c4
commit
1d60706108
@ -15,6 +15,7 @@ import (
|
||||
func InitializeEndUserController(router *gin.Engine) {
|
||||
router.GET("/report/:rid/submeter", security.EnterpriseAuthorize, fetchEndUserInReport)
|
||||
router.GET("/report/:rid/meter/template", security.EnterpriseAuthorize, downloadEndUserRegisterTemplate)
|
||||
router.POST("/report/:rid/meter/batch", security.EnterpriseAuthorize, uploadEndUserRegisterTemplate)
|
||||
}
|
||||
|
||||
func fetchEndUserInReport(c *gin.Context) {
|
||||
@ -78,3 +79,45 @@ func downloadEndUserRegisterTemplate(c *gin.Context) {
|
||||
gen.WriteMeterData(users)
|
||||
gen.WriteTo(c.Writer)
|
||||
}
|
||||
|
||||
func uploadEndUserRegisterTemplate(c *gin.Context) {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Param("rid")
|
||||
if !ensureReportBelongs(c, result, requestReportId) {
|
||||
return
|
||||
}
|
||||
meterType, err := service.ReportService.RetreiveParkEndUserMeterType(requestReportId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if meterType == -1 {
|
||||
result.NotFound("未能确定用户表计类型。")
|
||||
return
|
||||
}
|
||||
|
||||
uploadedFile, err := c.FormFile("data")
|
||||
if err != nil {
|
||||
result.NotAccept("没有接收到上传的档案文件。")
|
||||
return
|
||||
}
|
||||
archiveFile, err := uploadedFile.Open()
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if meterType == 0 {
|
||||
errs := service.EndUserService.BatchImportNonPVRegister(requestReportId, archiveFile)
|
||||
if errs.Len() > 0 {
|
||||
result.Json(http.StatusInternalServerError, "上传抄表文件存在解析错误", gin.H{"errors": errs.Errs})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
errs := service.EndUserService.BatchImportPVRegister(requestReportId, archiveFile)
|
||||
if errs.Len() > 0 {
|
||||
result.Json(http.StatusInternalServerError, "上传抄表文件存在解析错误", gin.H{"errors": errs.Errs})
|
||||
return
|
||||
}
|
||||
}
|
||||
result.Json(http.StatusOK, "已经成功完成抄表记录的导入。", gin.H{"errors": make([]error, 0)})
|
||||
}
|
||||
|
@ -55,6 +55,14 @@ func (e AnalysisError) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(e.Err.Error())
|
||||
}
|
||||
|
||||
func (e AnalysisError) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
func (e ExcelAnalysisError) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
func (r *ColumnRecognizer) Recognize(cellValue string) bool {
|
||||
matches := make([]bool, 0)
|
||||
for _, p := range r.Pattern {
|
||||
|
31
excel/end_user.go
Normal file
31
excel/end_user.go
Normal file
@ -0,0 +1,31 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
endUserNonPVRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1},
|
||||
{Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1},
|
||||
{Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1},
|
||||
}
|
||||
endUserPVRecognizers = append(
|
||||
endUserNonPVRecognizers,
|
||||
&ColumnRecognizer{Pattern: []string{"本期", "(尖峰)"}, Tag: "currentPeriodCritical", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"本期", "(峰)"}, Tag: "currentPeriodPeak", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"本期", "(谷)"}, Tag: "currentPeriodValley", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"退补", "(尖峰)"}, Tag: "adjustCritical", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"退补", "(峰)"}, Tag: "adjustPeak", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"退补", "(谷)"}, Tag: "adjustValley", MatchIndex: -1},
|
||||
)
|
||||
)
|
||||
|
||||
func NewEndUserNonPVExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.EndUserImport], error) {
|
||||
return NewExcelAnalyzer[model.EndUserImport](file, endUserNonPVRecognizers)
|
||||
}
|
||||
|
||||
func NewEndUserPVExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.EndUserImport], error) {
|
||||
return NewExcelAnalyzer[model.EndUserImport](file, endUserPVRecognizers)
|
||||
}
|
34
exceptions/batch_error.go
Normal file
34
exceptions/batch_error.go
Normal file
@ -0,0 +1,34 @@
|
||||
package exceptions
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/excel"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type BatchError struct {
|
||||
Errs []error
|
||||
}
|
||||
|
||||
func NewBatchError() *BatchError {
|
||||
return &BatchError{Errs: make([]error, 0)}
|
||||
}
|
||||
|
||||
func (e *BatchError) AddError(errs ...error) {
|
||||
e.Errs = append(e.Errs, errs...)
|
||||
}
|
||||
|
||||
func (e BatchError) Len() int {
|
||||
return len(e.Errs)
|
||||
}
|
||||
|
||||
func NewBatchExcelAnalysisError(errs []excel.ExcelAnalysisError) *BatchError {
|
||||
be := &BatchError{Errs: make([]error, 0)}
|
||||
for _, e := range errs {
|
||||
be.Errs = append(be.Errs, e)
|
||||
}
|
||||
return be
|
||||
}
|
||||
|
||||
func (e BatchError) Error() string {
|
||||
return fmt.Sprintf("存在批量错误,共 %d 个", len(e.Errs))
|
||||
}
|
@ -77,3 +77,16 @@ func (d *EndUserDetail) CalculatePeriod() {
|
||||
d.Flat = decimal.NewNullDecimal(d.CurrentPeriodFlat.Sub(d.LastPeriodFlat).Add(d.AdjustFlat).RoundBank(2))
|
||||
d.Valley = decimal.NewNullDecimal(d.CurrentPeriodValley.Sub(d.LastPeriodValley).Add(d.AdjustValley).RoundBank(2))
|
||||
}
|
||||
|
||||
type EndUserImport struct {
|
||||
MeterId string `excel:"meterId"`
|
||||
CurrentPeriodOverall decimal.Decimal `excel:"currentPeriodOverall"`
|
||||
CurrentPeriodCritical decimal.NullDecimal `excel:"currentPeriodCritical"`
|
||||
CurrentPeriodPeak decimal.NullDecimal `excel:"currentPeriodPeak"`
|
||||
CurrentPeriodValley decimal.NullDecimal `excel:"currentPeriodValley"`
|
||||
AdjustOverall decimal.Decimal `excel:"adjustOverall"`
|
||||
AdjustCritical decimal.NullDecimal `excel:"adjustCritical"`
|
||||
AdjustPeak decimal.NullDecimal `excel:"adjustPeak"`
|
||||
AdjustFlat decimal.NullDecimal `excel:"adjustFlat"`
|
||||
AdjustValley decimal.NullDecimal `excel:"adjustValley"`
|
||||
}
|
||||
|
@ -2,10 +2,17 @@ package service
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/excel"
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm/schemas"
|
||||
)
|
||||
|
||||
type _EndUserService struct{}
|
||||
@ -47,3 +54,165 @@ func (_EndUserService) AllEndUserRecord(reportId string) ([]model.EndUserDetail,
|
||||
Find(&users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
func (es _EndUserService) BatchImportNonPVRegister(reportId string, file io.Reader) *exceptions.BatchError {
|
||||
errs := exceptions.NewBatchError()
|
||||
users, err := es.AllEndUserRecord(reportId)
|
||||
if err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
return errs
|
||||
}
|
||||
indexedUsers := lo.Reduce(
|
||||
users,
|
||||
func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail {
|
||||
acc[elem.MeterId] = elem
|
||||
return acc
|
||||
},
|
||||
make(map[string]model.EndUserDetail, 0),
|
||||
)
|
||||
analyzer, err := excel.NewEndUserNonPVExcelAnalyzer(file)
|
||||
if err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
return errs
|
||||
}
|
||||
imports, excelErrs := analyzer.Analysis(*new(model.EndUserImport))
|
||||
if len(excelErrs) > 0 {
|
||||
for _, e := range excelErrs {
|
||||
errs.AddError(e)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
tx := global.DBConn.NewSession()
|
||||
if err = tx.Begin(); err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
return errs
|
||||
}
|
||||
defer tx.Close()
|
||||
|
||||
for _, im := range imports {
|
||||
if elem, ok := indexedUsers[im.MeterId]; ok {
|
||||
elem.CurrentPeriodOverall = im.CurrentPeriodOverall
|
||||
elem.AdjustOverall = im.AdjustOverall
|
||||
elem.CurrentPeriodCritical = decimal.Zero
|
||||
elem.CurrentPeriodPeak = decimal.Zero
|
||||
elem.CurrentPeriodValley = decimal.Zero
|
||||
elem.AdjustCritical = decimal.Zero
|
||||
elem.AdjustPeak = decimal.Zero
|
||||
elem.AdjustValley = decimal.Zero
|
||||
elem.CalculatePeriod()
|
||||
_, err = tx.ID(schemas.NewPK(elem.ReportId, elem.ParkId, elem.MeterId)).
|
||||
Cols(
|
||||
"current_period_overall",
|
||||
"adjust_overall",
|
||||
"current_period_critical",
|
||||
"current_period_peak",
|
||||
"current_period_flat",
|
||||
"current_perios_valley",
|
||||
"adjust_critical",
|
||||
"adjust_peak",
|
||||
"adjust_flat",
|
||||
"adjust_valley",
|
||||
).
|
||||
Update(elem)
|
||||
if err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
}
|
||||
} else {
|
||||
errs.AddError(exceptions.NewNotFoundError(fmt.Sprintf("表计 %s 未找到", im.MeterId)))
|
||||
}
|
||||
}
|
||||
if errs.Len() > 0 {
|
||||
tx.Rollback()
|
||||
return errs
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (_EndUserService) newVirtualExcelAnalysisError(err error) *excel.ExcelAnalysisError {
|
||||
return &excel.ExcelAnalysisError{Col: -1, Row: -1, Err: excel.AnalysisError{Err: err}}
|
||||
}
|
||||
|
||||
func (es _EndUserService) BatchImportPVRegister(reportId string, file io.Reader) *exceptions.BatchError {
|
||||
errs := exceptions.NewBatchError()
|
||||
users, err := es.AllEndUserRecord(reportId)
|
||||
if err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
return errs
|
||||
}
|
||||
indexedUsers := lo.Reduce(
|
||||
users,
|
||||
func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail {
|
||||
acc[elem.MeterId] = elem
|
||||
return acc
|
||||
},
|
||||
make(map[string]model.EndUserDetail, 0),
|
||||
)
|
||||
analyzer, err := excel.NewEndUserNonPVExcelAnalyzer(file)
|
||||
if err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
return errs
|
||||
}
|
||||
imports, excelErrs := analyzer.Analysis(*new(model.EndUserImport))
|
||||
if len(excelErrs) > 0 {
|
||||
for _, e := range excelErrs {
|
||||
errs.AddError(e)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
tx := global.DBConn.NewSession()
|
||||
if err = tx.Begin(); err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
return errs
|
||||
}
|
||||
defer tx.Close()
|
||||
|
||||
for _, im := range imports {
|
||||
if elem, ok := indexedUsers[im.MeterId]; ok {
|
||||
elem.CurrentPeriodOverall = im.CurrentPeriodOverall
|
||||
elem.AdjustOverall = im.AdjustOverall
|
||||
elem.CurrentPeriodCritical = im.CurrentPeriodCritical.Decimal
|
||||
elem.CurrentPeriodPeak = im.CurrentPeriodPeak.Decimal
|
||||
elem.CurrentPeriodValley = im.CurrentPeriodValley.Decimal
|
||||
elem.AdjustCritical = im.AdjustCritical.Decimal
|
||||
elem.AdjustPeak = im.AdjustPeak.Decimal
|
||||
elem.AdjustValley = im.AdjustValley.Decimal
|
||||
elem.CalculatePeriod()
|
||||
_, err = tx.ID(schemas.NewPK(elem.ReportId, elem.ParkId, elem.MeterId)).
|
||||
Cols(
|
||||
"current_period_overall",
|
||||
"adjust_overall",
|
||||
"current_period_critical",
|
||||
"current_period_peak",
|
||||
"current_period_flat",
|
||||
"current_perios_valley",
|
||||
"adjust_critical",
|
||||
"adjust_peak",
|
||||
"adjust_flat",
|
||||
"adjust_valley",
|
||||
).
|
||||
Update(elem)
|
||||
if err != nil {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
}
|
||||
} else {
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(exceptions.NewNotFoundError(fmt.Sprintf("表计 %s 未找到", im.MeterId))))
|
||||
}
|
||||
}
|
||||
if errs.Len() > 0 {
|
||||
tx.Rollback()
|
||||
return errs
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
errs.AddError(es.newVirtualExcelAnalysisError(err))
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user