forked from free-lancers/electricity_bill_calc_service
		
	Merge branch '0.2' of https://git.archgrid.xyz/free-lancers/electricity_bill_calc_service into 0.2
This commit is contained in:
		| @@ -54,7 +54,8 @@ func searchMetersWithinPark(c *fiber.Ctx) error { | ||||
| 	} | ||||
| 	keyword := c.Query("keyword") | ||||
| 	page := c.QueryInt("page", 1) | ||||
| 	meters, total, err := repository.MeterRepository.MetersIn(parkId, uint(page), &keyword) | ||||
| 	mtype := c.QueryInt("type", 0) | ||||
| 	meters, total, err := repository.MeterRepository.MetersIn(parkId, uint(page), &keyword, uint(mtype)) | ||||
| 	if err != nil { | ||||
| 		meterLog.Error("无法查询指定园区下的表计信息,无法获取表计列表", zap.Error(err)) | ||||
| 		return result.Error(http.StatusInternalServerError, err.Error()) | ||||
| @@ -349,6 +350,7 @@ func listUnboundTenementMeters(c *fiber.Ctx) error { | ||||
| func queryMeterReadings(c *fiber.Ctx) error { | ||||
| 	result := response.NewResult(c) | ||||
| 	parkId := c.Params("pid") | ||||
| 	mtype := c.QueryInt("type") | ||||
| 	if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -373,7 +375,7 @@ func queryMeterReadings(c *fiber.Ctx) error { | ||||
| 			endDate = &parsedDate | ||||
| 		} | ||||
| 	} | ||||
| 	readings, total, err := service.MeterService.SearchMeterReadings(parkId, building, startDate, endDate, uint(page), keyword) | ||||
| 	readings, total, err := service.MeterService.SearchMeterReadings(parkId, building, startDate, endDate, uint(page), keyword, uint(mtype)) | ||||
| 	if err != nil { | ||||
| 		meterLog.Error("查询指定园区中的表计读数,无法获取表计读数列表", zap.Error(err)) | ||||
| 		return result.Error(http.StatusInternalServerError, err.Error()) | ||||
|   | ||||
| @@ -10,6 +10,7 @@ import ( | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"electricity_bill_calc/vo" | ||||
| 	"log" | ||||
|  | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	"github.com/jinzhu/copier" | ||||
| @@ -97,6 +98,7 @@ func initNewReportCalculateTask(c *fiber.Ctx) error { | ||||
| 		return err | ||||
| 	} | ||||
| 	ok, err := service.ReportService.CreateNewReport(&form) | ||||
| 	log.Println("??????????????????????kkkkkkkkkkkkkkkkkkkkkkkkk", ok) | ||||
| 	if err != nil { | ||||
| 		reportLog.Error("无法创建核算报表", zap.Error(err)) | ||||
| 		return result.Error(fiber.StatusInternalServerError, "无法创建核算报表。") | ||||
|   | ||||
| @@ -32,18 +32,26 @@ var CalculateRepository = _CalculateRepository{ | ||||
| 	ds:  goqu.Dialect("postgres"), | ||||
| } | ||||
|  | ||||
| //更新当前报表的核算状态 | ||||
| // 更新当前报表的核算状态 | ||||
| func (cr _CalculateRepository) UpdateReportCalculateStatus(rid string, status string, | ||||
| 	message string) (bool, error) { | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
|  | ||||
| 	var atio int | ||||
| 	var err error | ||||
| 	log.Println("11111111111", status) | ||||
| 	currentTime := time.Now() | ||||
| 	if status == "success" { | ||||
| 		atio = 0 | ||||
| 	} else { | ||||
| 		atio = 1 | ||||
| 	} | ||||
|  | ||||
| 	updateResultSql, updateResultArgs, _ := cr.ds. | ||||
| 		Update(goqu.T("report_task")). | ||||
| 		Set(goqu.Record{ | ||||
| 			"status":           status, | ||||
| 			"status":           int16(atio), | ||||
| 			"last_modified_at": currentTime, | ||||
| 			"message":          message, | ||||
| 		}).Where(goqu.I("id").Eq(rid)). | ||||
| @@ -124,7 +132,7 @@ func (cr _CalculateRepository) GetAllPoolingMeterRelations(pid string, revokedAf | ||||
|  | ||||
| 	var meterRelation []model.MeterRelation | ||||
|  | ||||
| 	err := pgxscan.Select(ctx, global.DB, meterRelation, relationsSql, relationsArgs...) | ||||
| 	err := pgxscan.Select(ctx, global.DB, &meterRelation, relationsSql, relationsArgs...) | ||||
| 	if err != nil { | ||||
| 		cr.log.Error("获取当前园区中所有公摊表计与商户表计之间的关联关系,包括已经解除的出错", zap.Error(err)) | ||||
| 		return nil, err | ||||
| @@ -152,7 +160,7 @@ func (cr _CalculateRepository) GetAllTenementMeterRelations(pid string, associat | ||||
|  | ||||
| 	var tenementMeter []model.TenementMeter | ||||
|  | ||||
| 	err := pgxscan.Select(ctx, global.DB, tenementMeter, relationsQuerySql, relationsQueryArgs...) | ||||
| 	err := pgxscan.Select(ctx, global.DB, &tenementMeter, relationsQuerySql, relationsQueryArgs...) | ||||
| 	if err != nil { | ||||
| 		cr.log.Error("获取当前园区中所有的商户与表计的关联关系,包括已经解除的", zap.Error(err)) | ||||
| 		return nil, err | ||||
| @@ -178,13 +186,13 @@ func (cr _CalculateRepository) GetMeterReadings(rid string, meterType int16) ([] | ||||
| 			goqu.I("r.id").Eq(rid), | ||||
| 			goqu.I("mr.meter_type").Eq(meterType), | ||||
| 			// TODO:2023.08.02 此方法出错优先查看是否这里出问题 | ||||
| 			goqu.I("mr.read_at::date <@ r.period"), | ||||
| 			goqu.L("mr.read_at < lower(r.period)"), | ||||
| 		). | ||||
| 		Order(goqu.I("mr.read_at").Asc()).Select(goqu.I("mr.*")).ToSQL() | ||||
|  | ||||
| 	var readings []model.MeterReading | ||||
|  | ||||
| 	err := pgxscan.Select(ctx, global.DB, readings, readingsQuerySql, readingsQueryArgs...) | ||||
| 	err := pgxscan.Select(ctx, global.DB, &readings, readingsQuerySql, readingsQueryArgs...) | ||||
| 	if err != nil { | ||||
| 		cr.log.Error("获取指定报表中所有涉及到的指定类型表计在核算时间段内的所有读数数据出错", zap.Error(err)) | ||||
| 		return nil, err | ||||
| @@ -199,7 +207,8 @@ func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16 | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
|  | ||||
| 	readingsSql, readingsArgs, _ := cr.ds.From(goqu.T("meter_reading").As("mr")). | ||||
| 	readingsSql, readingsArgs, _ := cr.ds. | ||||
| 		From(goqu.T("meter_reading").As("mr")). | ||||
| 		Select( | ||||
| 			goqu.MAX("mr.read_at").As("read_at"), | ||||
| 			goqu.I("mr.park_id"), | ||||
| @@ -219,7 +228,7 @@ func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16 | ||||
| 		Where( | ||||
| 			goqu.I("r.id").Eq(rid), | ||||
| 			goqu.I("mr.meter_type").Eq(meterType), | ||||
| 			goqu.I(" mr.read_at::date <= lower(r.period)"), | ||||
| 			goqu.L(" read_at <= lower(r.period)"), | ||||
| 		). | ||||
| 		GroupBy( | ||||
| 			goqu.I("mr.park_id"), | ||||
| @@ -235,11 +244,13 @@ func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16 | ||||
| 		).ToSQL() | ||||
|  | ||||
| 	var readings []model.MeterReading | ||||
| 	err := pgxscan.Select(ctx, global.DB, readings, readingsSql, readingsArgs...) | ||||
| 	err := pgxscan.Select(ctx, global.DB, &readings, readingsSql, readingsArgs...) | ||||
| 	if err != nil { | ||||
| 		cr.log.Error("获取指定报表中所有涉及到的表计在核算起始日期前的最后一次读数出错", zap.Error(err)) | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	fmt.Println(">>>>>>>>>>>>", readingsSql) | ||||
| 	fmt.Println(";;;;;;;;;;;;;;;;;;", readings) | ||||
| 	return readings, nil | ||||
| } | ||||
|  | ||||
| @@ -266,11 +277,11 @@ func (cr _CalculateRepository) GetAllTenements(rid string) ([]model.Tenement, er | ||||
| 		). | ||||
| 		Where( | ||||
| 			goqu.I("r.id").Eq(rid), | ||||
| 			goqu.I("t.moved_in_at <= upper(r.period)"), | ||||
| 			goqu.L("t.moved_in_at <= upper(r.period)"), | ||||
| 		).ToSQL() | ||||
|  | ||||
| 	fmt.Println(tenementQuerySql) | ||||
| 	var tenements []model.Tenement | ||||
| 	err := pgxscan.Select(ctx, global.DB, tenements, tenementQuerySql, tenementQueryArgs...) | ||||
| 	err := pgxscan.Select(ctx, global.DB, &tenements, tenementQuerySql, tenementQueryArgs...) | ||||
| 	if err != nil { | ||||
| 		cr.log.Error("取得指定报表所涉及的所有商户信息出错", zap.Error(err)) | ||||
| 		return nil, err | ||||
|   | ||||
| @@ -12,7 +12,6 @@ import ( | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"electricity_bill_calc/vo" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/doug-martin/goqu/v9" | ||||
| 	_ "github.com/doug-martin/goqu/v9/dialect/postgres" | ||||
| 	"github.com/georgysavva/scany/v2/pgxscan" | ||||
| @@ -121,11 +120,10 @@ func (mr _MeterRepository) AllUsedMetersInReport(rid string) ([]*model.MeterDeta | ||||
| } | ||||
|  | ||||
| // 分页列出指定园区下的表计信息 | ||||
| func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]*model.MeterDetail, int64, error) { | ||||
| func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string, mtype uint) ([]*model.MeterDetail, int64, error) { | ||||
| 	mr.log.Info("分页列出指定园区下的表计信息", zap.String("park id", pid), zap.Uint("page", page), zap.String("keyword", tools.DefaultTo(keyword, ""))) | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
|  | ||||
| 	meterQuery := mr.ds. | ||||
| 		From(goqu.T("meter_04kv").As("m")). | ||||
| 		LeftJoin(goqu.T("park_building").As("b"), goqu.On(goqu.I("m.building").Eq(goqu.I("b.id")))). | ||||
| @@ -135,6 +133,7 @@ func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]* | ||||
| 		Where( | ||||
| 			goqu.I("m.park_id").Eq(pid), | ||||
| 			goqu.I("m.detached_at").IsNull(), | ||||
| 			goqu.I("m.meter_type").Eq(mtype), | ||||
| 		) | ||||
| 	countQuery := mr.ds. | ||||
| 		From(goqu.T("meter_04kv").As("m")). | ||||
| @@ -142,6 +141,7 @@ func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]* | ||||
| 		Where( | ||||
| 			goqu.I("m.park_id").Eq(pid), | ||||
| 			goqu.I("m.detached_at").IsNull(), | ||||
| 			goqu.I("m.meter_type").Eq(mtype), | ||||
| 		) | ||||
|  | ||||
| 	if keyword != nil && len(*keyword) > 0 { | ||||
| @@ -174,11 +174,11 @@ func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]* | ||||
| 		mr.log.Error("查询表计信息失败", zap.Error(err)) | ||||
| 		return make([]*model.MeterDetail, 0), 0, err | ||||
| 	} | ||||
|  | ||||
| 	if err := pgxscan.Get(ctx, global.DB, &total, countSql, countArgs...); err != nil { | ||||
| 		mr.log.Error("查询表计数量失败", zap.Error(err)) | ||||
| 		return make([]*model.MeterDetail, 0), 0, err | ||||
| 	} | ||||
|  | ||||
| 	return meters, total, nil | ||||
| } | ||||
|  | ||||
| @@ -209,7 +209,6 @@ func (mr _MeterRepository) ListMetersByIDs(pid string, ids []string) ([]*model.M | ||||
| 		mr.log.Error("查询表计信息失败", zap.Error(err)) | ||||
| 		return make([]*model.MeterDetail, 0), err | ||||
| 	} | ||||
|  | ||||
| 	return meters, nil | ||||
| } | ||||
|  | ||||
| @@ -777,24 +776,28 @@ func (mr _MeterRepository) ListUnboundTenementMeters(uid string, pid *string, ke | ||||
| } | ||||
|  | ||||
| // 查询指定园区中的符合条件的抄表记录 | ||||
| func (mr _MeterRepository) ListMeterReadings(pid string, keyword *string, page uint, start, end *types.Date, buidling *string) ([]*model.MeterReading, int64, error) { | ||||
| func (mr _MeterRepository) ListMeterReadings(pid string, keyword *string, page uint, start, end *types.Date, buidling *string, mtype uint) ([]*model.MeterReading, int64, error) { | ||||
| 	mr.log.Info("查询指定园区中的符合条件的抄表记录", zap.String("park id", pid), zap.String("keyword", tools.DefaultTo(keyword, "")), zap.Uint("page", page), logger.DateFieldp("start", start), logger.DateFieldp("end", end), zap.String("building", tools.DefaultTo(buidling, ""))) | ||||
| 	ctx, cancel := global.TimeoutContext() | ||||
| 	defer cancel() | ||||
|  | ||||
| 	readingQuery := mr.ds. | ||||
| 		From(goqu.T("meter_reading").As("r")). | ||||
| 		LeftJoin(goqu.T("meter_04kv").As("m"), goqu.On(goqu.I("r.meter_id").Eq(goqu.I("m.code")), goqu.I("m.park_id").Eq(goqu.I("r.park_id")))). | ||||
| 		LeftJoin(goqu.T("meter_04kv").As("m"), goqu.On(goqu.I("r.meter_id").Eq(goqu.I("m.code")), | ||||
| 			goqu.I("m.park_id").Eq(goqu.I("r.park_id")))). | ||||
| 		Select("r.*"). | ||||
| 		Where( | ||||
| 			goqu.I("r.park_id").Eq(pid), | ||||
| 			goqu.I("m.meter_type").Eq(mtype), | ||||
| 		) | ||||
| 	countQuery := mr.ds. | ||||
| 		From(goqu.T("meter_reading").As("r")). | ||||
| 		LeftJoin(goqu.T("meter_04kv").As("m"), goqu.On(goqu.I("r.meter_id").Eq(goqu.I("m.code")), goqu.I("m.park_id").Eq(goqu.I("r.park_id")))). | ||||
| 		LeftJoin(goqu.T("meter_04kv").As("m"), goqu.On(goqu.I("r.meter_id").Eq(goqu.I("m.code")), | ||||
| 			goqu.I("m.park_id").Eq(goqu.I("r.park_id")))). | ||||
| 		Select(goqu.COUNT("*")). | ||||
| 		Where( | ||||
| 			goqu.I("r.park_id").Eq(pid), | ||||
| 			goqu.I("m.meter_type").Eq(mtype), | ||||
| 		) | ||||
|  | ||||
| 	if keyword != nil && len(*keyword) > 0 { | ||||
| @@ -859,7 +862,6 @@ func (mr _MeterRepository) ListMeterReadings(pid string, keyword *string, page u | ||||
| 		mr.log.Error("查询抄表记录数量失败", zap.Error(err)) | ||||
| 		return make([]*model.MeterReading, 0), 0, err | ||||
| 	} | ||||
|  | ||||
| 	return readings, total, nil | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -9,7 +9,9 @@ import ( | ||||
| 	"electricity_bill_calc/tools/serial" | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"electricity_bill_calc/vo" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
|  | ||||
| 	"github.com/doug-martin/goqu/v9" | ||||
| 	_ "github.com/doug-martin/goqu/v9/dialect/postgres" | ||||
| @@ -124,8 +126,8 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, str | ||||
| 	createSql, createArgs, _ := rr.ds. | ||||
| 		Insert(goqu.T("report")). | ||||
| 		Cols( | ||||
| 			"id", "park_id", "period", "category", "meter_o4kv_type", "price_policy", | ||||
| 			"basic_pooled", "adjust_pooled", "loss_pooled", "public_pooled", "created_at", | ||||
| 			"id", "park_id", "period", "category", "meter_04kv_type", "price_policy", | ||||
| 			"basis_pooled", "adjust_pooled", "loss_pooled", "public_pooled", "created_at", | ||||
| 			"last_modified_at", | ||||
| 		). | ||||
| 		Vals(goqu.Vals{ | ||||
| @@ -134,38 +136,45 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, str | ||||
| 			createTime, | ||||
| 		}). | ||||
| 		Prepared(true).ToSQL() | ||||
| 	summarySql, summaryArgs, _ := rr.ds. | ||||
| 		Insert(goqu.T("report_summary")). | ||||
| 		Cols( | ||||
| 			"report_id", "overall", "critical", "peak", "flat", "valley", "basic_fee", | ||||
| 			"adjust_fee", | ||||
| 		). | ||||
| 		Vals(goqu.Vals{ | ||||
| 			reportId, | ||||
| 			model.ConsumptionUnit{ | ||||
| 				Amount: form.Overall, | ||||
| 				Fee:    form.OverallFee, | ||||
| 			}, | ||||
| 			model.ConsumptionUnit{ | ||||
| 	critical := model.ConsumptionUnit{ | ||||
| 		Amount: form.Critical, | ||||
| 		Fee:    form.CriticalFee, | ||||
| 			}, | ||||
| 			model.ConsumptionUnit{ | ||||
| 	} | ||||
| 	criticalData, _ := json.Marshal(critical) | ||||
| 	overall := model.ConsumptionUnit{ | ||||
| 		Amount: form.Overall, | ||||
| 		Fee:    form.OverallFee, | ||||
| 	} | ||||
| 	overallData, _ := json.Marshal(overall) | ||||
| 	peak := model.ConsumptionUnit{ | ||||
| 		Amount: form.Peak, | ||||
| 		Fee:    form.PeakFee, | ||||
| 			}, | ||||
| 			model.ConsumptionUnit{ | ||||
| 	} | ||||
| 	peakData, _ := json.Marshal(peak) | ||||
| 	flat := model.ConsumptionUnit{ | ||||
| 		Amount: form.Flat, | ||||
| 		Fee:    form.FlatFee, | ||||
| 			}, | ||||
| 			model.ConsumptionUnit{ | ||||
| 	} | ||||
| 	flatData, _ := json.Marshal(flat) | ||||
| 	valley := model.ConsumptionUnit{ | ||||
| 		Amount: form.Valley, | ||||
| 		Fee:    form.ValleyFee, | ||||
| 			}, | ||||
| 			form.BasicFee, | ||||
| 			form.AdjustFee, | ||||
| 		}). | ||||
| 		Prepared(true).ToSQL() | ||||
| 	} | ||||
| 	valleyData, _ := json.Marshal(valley) | ||||
| 	summarySql, summaryArgs, err5 := rr.ds. | ||||
| 		Insert(goqu.T("report_summary")). | ||||
| 		//Cols("report_id", "overall", "critical", "peak", "flat", "valley", "basic_fee", "adjust_fee"). | ||||
| 		Rows(goqu.Record{ | ||||
| 			"report_id":  reportId, | ||||
| 			"overall":    string(overallData), | ||||
| 			"critical":   string(criticalData), | ||||
| 			"peak":       string(peakData), | ||||
| 			"flat":       string(flatData), | ||||
| 			"valley":     string(valleyData), | ||||
| 			"basic_fee":  form.BasicFee, | ||||
| 			"adjust_fee": form.AdjustFee, | ||||
| 		}).Prepared(true).ToSQL() | ||||
| 	log.Println("errrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr:", err5) | ||||
| 	taskSql, taskArgs, _ := rr.ds. | ||||
| 		Insert(goqu.T("report_task")). | ||||
| 		Cols("id", "status", "last_modified_at"). | ||||
| @@ -189,11 +198,14 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, str | ||||
| 		tx.Rollback(ctx) | ||||
| 		return false, "", err | ||||
| 	} | ||||
| 	log.Println("?????????", summarySql, summaryArgs) | ||||
| 	log.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", resSummary.RowsAffected()) | ||||
| 	if resSummary.RowsAffected() == 0 { | ||||
| 		rr.log.Error("保存核算报表汇总时出现错误", zap.Error(err)) | ||||
| 		tx.Rollback(ctx) | ||||
| 		return false, "", exceptions.NewUnsuccessCreateError("创建核算报表汇总时出现错误") | ||||
| 	} | ||||
|  | ||||
| 	resTask, err := tx.Exec(ctx, taskSql, taskArgs...) | ||||
| 	if err != nil { | ||||
| 		rr.log.Error("创建核算报表任务时出现错误", zap.Error(err)) | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import ( | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/model/calculate" | ||||
| 	"electricity_bill_calc/repository" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| @@ -22,6 +23,11 @@ func MetersParkCalculate(report model.ReportIndex, periodStart time.Time, | ||||
|  | ||||
| 	parkMeterReadings = append(parkMeterReadings, lastTermParkMeterReadings...) | ||||
|  | ||||
| 	if len(parkMeterReadings) <= 0 { | ||||
| 		fmt.Println(parkMeterReadings) | ||||
| 		return []calculate.Meter{}, nil | ||||
| 	} | ||||
|  | ||||
| 	var parkMetersReports []calculate.Meter | ||||
| 	for _, meter := range meterDetail { | ||||
| 		if meter.MeterType == model.METER_INSTALLATION_PARK { | ||||
|   | ||||
| @@ -4,12 +4,13 @@ import ( | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/model/calculate" | ||||
| 	"electricity_bill_calc/repository" | ||||
| 	"fmt" | ||||
| 	"github.com/shopspring/decimal" | ||||
| 	"time" | ||||
| 	"unsafe" | ||||
| ) | ||||
|  | ||||
| //核算园区中的全部公摊表计的电量用量 | ||||
| // 核算园区中的全部公摊表计的电量用量 | ||||
| func PooledMetersCalculate(report *model.ReportIndex, periodStart time.Time, | ||||
| 	periodEnd time.Time, meterDetails []*model.MeterDetail, | ||||
| 	summary calculate.Summary) ([]calculate.Meter, error) { | ||||
| @@ -24,6 +25,10 @@ func PooledMetersCalculate(report *model.ReportIndex, periodStart time.Time, | ||||
| 	} | ||||
|  | ||||
| 	poolingMeterReadings = append(poolingMeterReadings, lastTermPoolingMeterReadings...) | ||||
| 	fmt.Println(poolingMeterReadings, "==============================================") | ||||
| 	if len(poolingMeterReadings) <= 0 { | ||||
| 		return nil, nil | ||||
| 	} | ||||
|  | ||||
| 	var poolingMetersReports []calculate.Meter | ||||
| 	for _, meter := range meterDetails { | ||||
|   | ||||
| @@ -42,7 +42,7 @@ func removeDuplicates(meters []calculate.Meter) []calculate.Meter { | ||||
| } | ||||
|  | ||||
| // 计算线损以及调整线损 | ||||
| func LossCalculate(report *model.ReportIndex, Public *[]calculate.Meter, | ||||
| func LossCalculate(report *model.ReportIndex, Public []calculate.Meter, | ||||
| 	publicTotal *decimal.Decimal, summary *calculate.Summary) error { | ||||
| 	summary.Loss = summary.Overall.Amount.Sub(summary.TotalConsumption) | ||||
|  | ||||
| @@ -56,7 +56,7 @@ func LossCalculate(report *model.ReportIndex, Public *[]calculate.Meter, | ||||
| 	summary.LossProportion = summary.Loss.Div(summaryAmount) | ||||
|  | ||||
| 	var authorizedLossRate decimal.Decimal | ||||
| 	//TODO: 2023.08.04 在此发现reportIndex结构体与数据库中的report表字段不对应缺少两个相应字段,在此添加的,如在其他地方有错误优先查找这里 | ||||
| 	// TODO: 2023.08.04 在此发现reportIndex结构体与数据库中的report表字段不对应缺少两个相应字段,在此添加的,如在其他地方有错误优先查找这里 | ||||
| 	if summary.LossProportion.InexactFloat64() > report.AuthorizedLossRate { | ||||
| 		authorizedLossRate = summary.LossProportion | ||||
| 	} else { | ||||
| @@ -72,11 +72,15 @@ func LossCalculate(report *model.ReportIndex, Public *[]calculate.Meter, | ||||
|  | ||||
| 	differentialLoss := summary.LossDilutedPrice.Sub(summary.AuthoizeLoss.Amount) | ||||
|  | ||||
| 	fmt.Println(publicTotal.InexactFloat64()) | ||||
| 	if publicTotal.InexactFloat64() <= decimal.Zero.InexactFloat64() { | ||||
| 		return errors.New("园区公共表计的电量总和为非正值,或者园区未设置公共表计,无法计算核定线损") | ||||
| 	} | ||||
|  | ||||
| 	for _, meter := range *Public { | ||||
| 	if Public == nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	for _, meter := range Public { | ||||
| 		amountProportion := meter.Overall.Amount.InexactFloat64() / publicTotal.InexactFloat64() | ||||
| 		adjustAmount := differentialLoss.InexactFloat64() * decimal.NewFromFloat(-1.0).InexactFloat64() | ||||
| 		meter.AdjustLoss = model.ConsumptionUnit{ | ||||
|   | ||||
| @@ -66,8 +66,8 @@ func MainCalculateProcess(rid string) error { | ||||
| 	// 计算所有表计的总电量 | ||||
| 	parkTotal := TotalConsumptionCalculate(tenementReports, summary) | ||||
|  | ||||
| 	// 计算线损以及调整线损 | ||||
| 	err = LossCalculate(report, &parkMetersReports, &parkTotal, &summary) | ||||
| 	//计算线损以及调整线损 | ||||
| 	err = LossCalculate(report, parkMetersReports, &parkTotal, &summary) | ||||
| 	if err != nil { | ||||
| 		fmt.Println("9", err) | ||||
| 		return err | ||||
| @@ -79,15 +79,13 @@ func MainCalculateProcess(rid string) error { | ||||
| 		fmt.Println("10", err) | ||||
| 		return err | ||||
| 	} | ||||
| 	// 计算基本电费分摊、调整电费分摊、电费摊薄单价。 | ||||
| 	err = CalculatePrices(&summary) | ||||
| 	if err != nil { | ||||
| 		fmt.Println("11", err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	//=========================================================================== | ||||
| 	// 计算基本电费分摊、调整电费分摊、电费摊薄单价。 | ||||
| 	err = CalculatePrices(&summary) | ||||
| 	fmt.Println("计算数据读取完成") | ||||
| 	// 收集目前所有已经处理的表计,统一对其进行摊薄计算。 | ||||
| 	meters, err := CollectMeters(tenementReports, poolingMetersReports, parkMetersReports) | ||||
| 	if err != nil { | ||||
|   | ||||
| @@ -587,7 +587,7 @@ func (ms _MeterService) UnbindMeter(pid, masterMeter string, slaveMeters []strin | ||||
| } | ||||
|  | ||||
| // 查询符合条件的表计读数记录 | ||||
| func (ms _MeterService) SearchMeterReadings(pid string, building *string, start, end *types.Date, page uint, keyword *string) ([]*model.DetailedMeterReading, int64, error) { | ||||
| func (ms _MeterService) SearchMeterReadings(pid string, building *string, start, end *types.Date, page uint, keyword *string, mtype uint) ([]*model.DetailedMeterReading, int64, error) { | ||||
| 	ms.log.Info( | ||||
| 		"查询符合条件的表计读数记录", | ||||
| 		zap.String("park id", pid), | ||||
| @@ -597,7 +597,7 @@ func (ms _MeterService) SearchMeterReadings(pid string, building *string, start, | ||||
| 		zap.Uint("page", page), | ||||
| 		zap.Stringp("keyword", keyword), | ||||
| 	) | ||||
| 	readings, total, err := repository.MeterRepository.ListMeterReadings(pid, keyword, page, start, end, building) | ||||
| 	readings, total, err := repository.MeterRepository.ListMeterReadings(pid, keyword, page, start, end, building, mtype) | ||||
| 	if err != nil { | ||||
| 		ms.log.Error("无法查询符合条件的表计读数记录。", zap.Error(err)) | ||||
| 		return make([]*model.DetailedMeterReading, 0), 0, err | ||||
|   | ||||
| @@ -9,6 +9,7 @@ import ( | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"electricity_bill_calc/vo" | ||||
| 	"github.com/pkg/errors" | ||||
| 	"log" | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/doug-martin/goqu/v9" | ||||
| @@ -209,30 +210,35 @@ func (rs _ReportService) ReportCalcuateDispatch(rid string) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| //创建一个新的核算报表,并同时完成核算报表的计算 | ||||
| // 创建一个新的核算报表,并同时完成核算报表的计算 | ||||
| func (rs _ReportService) CreateNewReport(createFrom *vo.ReportCreationForm) (bool, error) { | ||||
| 	state, report, err := repository.ReportRepository.CreateReport(createFrom) | ||||
| 	if err != nil { | ||||
| 		rs.log.Error("创建核算报表错误", zap.Error(err)) | ||||
| 		return false, err | ||||
| 	} | ||||
| 	log.Println("创建成功,successsssssssssssssssssssssssssssssssssssssssssssssssssssss") | ||||
|  | ||||
| 	if !state { | ||||
| 		status, err := repository.CalculateRepository.UpdateReportCalculateStatus(report, "InsufficientData", "创建报表时发生错误,需手动再次计算") | ||||
| 		status, err := repository.CalculateRepository.UpdateReportCalculateStatus(report, "InsufficientData", | ||||
| 			"创建报表时发生错误,需手动再次计算") | ||||
| 		if err != nil { | ||||
| 			rs.log.Error("创建报表时发生错误,需手动再次计算", zap.Error(err)) | ||||
| 			return false, err | ||||
| 		} | ||||
| 		return status, nil | ||||
| 	} | ||||
| 	log.Println("更新成功succssssssssssssssss") | ||||
| 	err = rs.CalculateReport(report) | ||||
| 	if err != nil { | ||||
| 		rs.log.Error("计算时出错", zap.Error(err)) | ||||
| 		return false, err | ||||
| 	} | ||||
| 	log.Println("返回truettttttttttttttttttt") | ||||
| 	return true, nil | ||||
| } | ||||
|  | ||||
| //更新一个核算报表中的数据,并同时完成计算 | ||||
| // 更新一个核算报表中的数据,并同时完成计算 | ||||
| func (rs _ReportService) UpdateRepoet(rid string, updateForm *vo.ReportModifyForm) (bool, error) { | ||||
| 	state, err := repository.ReportRepository.UpdateReportSummary(rid, updateForm) | ||||
| 	if err != nil { | ||||
| @@ -258,13 +264,13 @@ var CALCULATE_TASK_PARALLEL_CONTROL = func() *sync.Mutex { | ||||
|  | ||||
| // 执行一个核算报表的计算任务 | ||||
| func (rs _ReportService) CalculateReport(rid string) error { | ||||
| 	semaphore := CALCULATE_TASK_PARALLEL_CONTROL | ||||
|  | ||||
| 	semaphore.Lock() | ||||
| 	defer semaphore.Unlock() | ||||
| 	//semaphore := CALCULATE_TASK_PARALLEL_CONTROL | ||||
| 	// | ||||
| 	//semaphore.Lock() | ||||
| 	//defer semaphore.Unlock() | ||||
|  | ||||
| 	errs := calculate.MainCalculateProcess(rid) | ||||
|  | ||||
| 	log.Println("开始计算555555555555555555555555555") | ||||
| 	if errs == nil { | ||||
| 		_, err := repository.CalculateRepository.UpdateReportCalculateStatus(rid, "success", "") | ||||
| 		if err != nil { | ||||
|   | ||||
| @@ -12,9 +12,9 @@ Server: | ||||
|   ReadTimeout: 60 | ||||
|   WriteTimeout: 60 | ||||
| Redis: | ||||
|   Host: 127.0.0.1 | ||||
|   Host: 192.168.88.129 | ||||
|   Port: 6379 | ||||
|   Password: | ||||
|   Password: 123456 | ||||
|   DB: 1 | ||||
| Service: | ||||
|   MaxSessionLife: 2h | ||||
|   | ||||
		Reference in New Issue
	
	Block a user