From 7d3fafeb04670a85426d286d097654523e0ac4c2 Mon Sep 17 00:00:00 2001 From: ZiHangQin <1420014281@qq.com> Date: Tue, 15 Aug 2023 11:05:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(#27):=20=E6=9C=AA=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/calculate/calculate.go | 28 ++++----- model/tenement.go | 2 +- repository/calculate.go | 115 +++++++++++++++++++--------------- repository/report.go | 4 +- service/calculate/persist.go | 9 --- service/calculate/wattCost.go | 1 + 6 files changed, 83 insertions(+), 76 deletions(-) diff --git a/model/calculate/calculate.go b/model/calculate/calculate.go index 2c70197..4eca038 100644 --- a/model/calculate/calculate.go +++ b/model/calculate/calculate.go @@ -49,20 +49,20 @@ type PrimaryTenementStatistics struct { } type TenementCharge struct { - Tenement string - Overall model.ConsumptionUnit - Critical model.ConsumptionUnit - Peak model.ConsumptionUnit - Flat model.ConsumptionUnit - Valley model.ConsumptionUnit - BasicFee decimal.Decimal - AdjustFee decimal.Decimal - LossPooled decimal.Decimal - PublicPooled decimal.Decimal - FinalCharges decimal.Decimal - Loss decimal.Decimal - Submeters []*Meter - Poolings []*Meter + Tenement string `json:"tenement"` + Overall model.ConsumptionUnit `json:"overall"` + Critical model.ConsumptionUnit `json:"critical"` + Peak model.ConsumptionUnit `json:"peak"` + Flat model.ConsumptionUnit `json:"flat"` + Valley model.ConsumptionUnit `json:"valley"` + BasicFee decimal.Decimal `json:"basic_fee"` + AdjustFee decimal.Decimal `json:"adjust_fee"` + LossPooled decimal.Decimal `json:"loss_pooled"` + PublicPooled decimal.Decimal `json:"public_pooled"` + FinalCharges decimal.Decimal `json:"final_charges"` + Loss decimal.Decimal `json:"loss"` + Submeters []*Meter `json:"submeters"` + Poolings []*Meter `json:"poolings"` } type Summary struct { diff --git a/model/tenement.go b/model/tenement.go index c5c0282..323228f 100644 --- a/model/tenement.go +++ b/model/tenement.go @@ -7,7 +7,7 @@ type Tenement struct { Park string `json:"parkId" db:"park_id"` FullName string `json:"fullName" db:"full_name"` ShortName *string `json:"shortName" db:"short_name"` - Abbr string `json:"-"` + Abbr string `json:"abbr"` Address string `json:"address"` ContactName string `json:"contactName" db:"contact_name"` ContactPhone string `json:"contactPhone" db:"contact_phone"` diff --git a/repository/calculate.go b/repository/calculate.go index ea4a3ff..9b9a5b4 100644 --- a/repository/calculate.go +++ b/repository/calculate.go @@ -14,7 +14,6 @@ import ( "github.com/shopspring/decimal" "golang.org/x/sync/errgroup" "log" - "strings" "time" "github.com/doug-martin/goqu/v9" @@ -337,12 +336,12 @@ func (cr _CalculateRepository) SaveReportPublics(tx pgx.Tx, ctx context.Context, "loss_adjust", "consumption_total", "loss_adjust_total", "final_total", ) // 添加值到插入表达式中 - overall,_ := json.Marshal(meter.Overall) - criyical,_ := json.Marshal(meter.Critical) - peak,_ := json.Marshal(meter.Peak) - flat,_ := json.Marshal(meter.Flat) - valley,_ := json.Marshal(meter.Valley) - adjustLoss,_ := json.Marshal(meter.AdjustLoss) + overall, _ := json.Marshal(meter.Overall) + criyical, _ := json.Marshal(meter.Critical) + peak, _ := json.Marshal(meter.Peak) + flat, _ := json.Marshal(meter.Flat) + valley, _ := json.Marshal(meter.Valley) + adjustLoss, _ := json.Marshal(meter.AdjustLoss) insertExpr = insertExpr.Vals(goqu.Vals{ rid, meter.Code, @@ -370,12 +369,12 @@ func (cr _CalculateRepository) SaveReportPublics(tx pgx.Tx, ctx context.Context, } func (cr _CalculateRepository) SaveReportSummary(tx pgx.Tx, ctx context.Context, summary calculate.Summary) error { // 构建插入表达式 - Overall,_ := json.Marshal(summary.Overall) - Critical,_ := json.Marshal(summary.Critical) - Peak,_ := json.Marshal(summary.Peak) - Flat,_ := json.Marshal(summary.Flat) - Valley,_ := json.Marshal(summary.Valley) - AuthoizeLoss,_ := json.Marshal(summary.AuthoizeLoss) + Overall, _ := json.Marshal(summary.Overall) + Critical, _ := json.Marshal(summary.Critical) + Peak, _ := json.Marshal(summary.Peak) + Flat, _ := json.Marshal(summary.Flat) + Valley, _ := json.Marshal(summary.Valley) + AuthoizeLoss, _ := json.Marshal(summary.AuthoizeLoss) insertsql, insertArgs, err := cr.ds.Insert(goqu.T("report_summary")). Cols( "report_id", "overall", "critical", "peak", "flat", "valley", @@ -465,11 +464,11 @@ func (cr _CalculateRepository) SaveReportPoolings(tx pgx.Tx, if err != nil { return err } - overall,_ := json.Marshal(meter.Overall) - criyical,_ := json.Marshal(meter.Critical) - peak,_ := json.Marshal(meter.Peak) - flat,_ := json.Marshal(meter.Flat) - valley,_ := json.Marshal(meter.Valley) + overall, _ := json.Marshal(meter.Overall) + criyical, _ := json.Marshal(meter.Critical) + peak, _ := json.Marshal(meter.Peak) + flat, _ := json.Marshal(meter.Flat) + valley, _ := json.Marshal(meter.Valley) insertQuery := goqu.Insert("report_pooled_consumption"). Cols("report_id", "pooled_meter_id", "overall", "critical", "peak", "flat", "valley", "pooled_area", "diluted"). @@ -500,40 +499,59 @@ func (cr _CalculateRepository) SaveReportTenement(tx pgx.Tx, report model.Report cr.log.Info("保存商户报表。") ctx, cancel := global.TimeoutContext() defer cancel() - insertQuery := cr.ds.Insert("report_tenement").Prepared(true) - values := []goqu.Record{} + insertQuery := cr.ds. + Insert("report_tenement") + //var values []goqu.Record for _, tenement := range tenements { - charge := findTenementCharge(tenementCharges, tenement.Id) - values = append(values, goqu.Record{ - "report_id": report.Id, - "tenement_id": tenement.Id, - "tenement_detail": toJSONString(tenement), - "calc_period": report.Period, - "overall": toJSONString(charge.Overall), - "critical": toJSONString(charge.Critical), - "peak": toJSONString(charge.Peak), - "flat": toJSONString(charge.Flat), - "valley": toJSONString(charge.Valley), - "loss": toJSONString(charge.Loss), - "basic_fee_pooled": charge.BasicFee, - "adjust_fee_pooled": charge.AdjustFee, - "loss_fee_pooled": charge.LossPooled, - "final_pooled": charge.PublicPooled, - "final_charge": charge.FinalCharges, - "meters": toJSONString(convertToNestedMeters(charge.Submeters)), - "pooled": toJSONString(convertToNestedMeters(charge.Poolings)), - }) + //charge := findTenementCharge(tenementCharges, tenement.Id) + for _, charge := range tenementCharges { + tenementStr, _ := json.Marshal(tenement) + overallStr, _ := json.Marshal(charge.Overall) + criticalStr, _ := json.Marshal(charge.Critical) + peakStr, _ := json.Marshal(charge.Peak) + flatStr, _ := json.Marshal(charge.Flat) + valleyStr, _ := json.Marshal(charge.Valley) + lossStr, _ := json.Marshal(charge.Loss) + meters, _ := json.Marshal(convertToNestedMeters(charge.Submeters)) + pooled, _ := json.Marshal(convertToNestedMeters(charge.Poolings)) + fmt.Println(report.Id, tenement.Id, "123") + insertQuery = insertQuery. + Cols( + "report_id", "tenement_id", "tenement_detail", "calc_period", "overall", "critical", "peak", "flat", "valley", "loss", "basic_fee_pooled", + "adjust_fee_pooled", "loss_fee_pooled", "final_charge", "invoice", "meters", "pooled", + ). + Vals(goqu.Vals{report.Id, + tenement.Id, + tenementStr, + report.Period, + overallStr, + criticalStr, + peakStr, + flatStr, + valleyStr, + lossStr, + charge.BasicFee, + charge.AdjustFee, + charge.LossPooled, + charge.PublicPooled, + charge.FinalCharges, + meters, + pooled}) + } + } - sql, params, err := insertQuery.Rows(values).Prepared(true).ToSQL() - if err != nil { - log.Println("sql出现问题................................") - return err - } - tx.Exec(ctx, sql, params...) + sql, params, err := insertQuery.Prepared(true).ToSQL() if err != nil { + fmt.Println(err) + } + fmt.Println("====================================================================", sql) + _, err = tx.Exec(ctx, sql, params...) + if err != nil { + fmt.Println(err.Error()) return err } + fmt.Println("保存成功") return nil } @@ -562,8 +580,3 @@ func convertToNestedMeters(meters []*calculate.Meter) []NestedMeter { } return nestedMeters } - -// toJSONString 将对象转换为 JSON 字符串 -func toJSONString(obj interface{}) string { - return `"` + strings.ReplaceAll(fmt.Sprintf("%#v", obj), `"`, `\"`) + `"` -} diff --git a/repository/report.go b/repository/report.go index 93372be..81b6030 100644 --- a/repository/report.go +++ b/repository/report.go @@ -563,15 +563,17 @@ func (rr _ReportRepository) ListTenementInReport(rid string, page uint, keyword Offset(startRow).Limit(config.ServiceSettings.ItemsPageSize) var ( - tenements []*model.ReportTenement = make([]*model.ReportTenement, 0) + tenements = make([]*model.ReportTenement, 0) count int64 ) querySql, queryArgs, _ := reportQuery.Prepared(true).ToSQL() countSql, countArgs, _ := countQuery.Prepared(true).ToSQL() + fmt.Println(querySql, rid) if err := pgxscan.Select(ctx, global.DB, &tenements, querySql, queryArgs...); err != nil { rr.log.Error("查询指定核算报表下的商户简要计费信息时出现错误", zap.Error(err)) return tenements, 0, err } + if err := pgxscan.Get(ctx, global.DB, &count, countSql, countArgs...); err != nil { rr.log.Error("查询指定核算报表下的商户简要计费信息总数量时出现错误", zap.Error(err)) return tenements, 0, err diff --git a/service/calculate/persist.go b/service/calculate/persist.go index 453803a..c004f4e 100644 --- a/service/calculate/persist.go +++ b/service/calculate/persist.go @@ -2,7 +2,6 @@ package calculate import ( "context" - "electricity_bill_calc/global" "electricity_bill_calc/model" "electricity_bill_calc/model/calculate" "electricity_bill_calc/repository" @@ -40,8 +39,6 @@ func SavePublics(tx pgx.Tx, ctx context.Context, report model.ReportIndex, meter } func SavePoolings(tx pgx.Tx, report model.ReportIndex, meters MeterMap, relations []model.MeterRelation) error { - ctx, cancel := global.TimeoutContext() - defer cancel() var poolingMeters []calculate.Meter var tenementMeters []calculate.Meter // 根据条件筛选 Meter 并保存到对应的数组中 @@ -56,13 +53,9 @@ func SavePoolings(tx pgx.Tx, report model.ReportIndex, meters MeterMap, relation if err != nil { return err } - tx.Commit(ctx) - return nil } func SaveTenements(tx pgx.Tx, report model.ReportIndex, tenement []calculate.PrimaryTenementStatistics, tc []calculate.TenementCharge) error { - ctx, cancel := global.TimeoutContext() - defer cancel() var ts []model.Tenement for _, r := range tenement { ts = append(ts, r.Tenement) @@ -71,7 +64,5 @@ func SaveTenements(tx pgx.Tx, report model.ReportIndex, tenement []calculate.Pri if err != nil { return err } - tx.Commit(ctx) - return nil } diff --git a/service/calculate/wattCost.go b/service/calculate/wattCost.go index f14427c..2f0771b 100644 --- a/service/calculate/wattCost.go +++ b/service/calculate/wattCost.go @@ -158,6 +158,7 @@ func MainCalculateProcess(rid string) error { fmt.Println("22", err) return err } + fmt.Println("商户分摊关系保存成功") _ = tx.Commit(ctx) return nil }