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") | 	keyword := c.Query("keyword") | ||||||
| 	page := c.QueryInt("page", 1) | 	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 { | 	if err != nil { | ||||||
| 		meterLog.Error("无法查询指定园区下的表计信息,无法获取表计列表", zap.Error(err)) | 		meterLog.Error("无法查询指定园区下的表计信息,无法获取表计列表", zap.Error(err)) | ||||||
| 		return result.Error(http.StatusInternalServerError, err.Error()) | 		return result.Error(http.StatusInternalServerError, err.Error()) | ||||||
| @@ -349,6 +350,7 @@ func listUnboundTenementMeters(c *fiber.Ctx) error { | |||||||
| func queryMeterReadings(c *fiber.Ctx) error { | func queryMeterReadings(c *fiber.Ctx) error { | ||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	parkId := c.Params("pid") | 	parkId := c.Params("pid") | ||||||
|  | 	mtype := c.QueryInt("type") | ||||||
| 	if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass { | 	if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -373,7 +375,7 @@ func queryMeterReadings(c *fiber.Ctx) error { | |||||||
| 			endDate = &parsedDate | 			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 { | 	if err != nil { | ||||||
| 		meterLog.Error("查询指定园区中的表计读数,无法获取表计读数列表", zap.Error(err)) | 		meterLog.Error("查询指定园区中的表计读数,无法获取表计读数列表", zap.Error(err)) | ||||||
| 		return result.Error(http.StatusInternalServerError, err.Error()) | 		return result.Error(http.StatusInternalServerError, err.Error()) | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ import ( | |||||||
| 	"electricity_bill_calc/tools" | 	"electricity_bill_calc/tools" | ||||||
| 	"electricity_bill_calc/types" | 	"electricity_bill_calc/types" | ||||||
| 	"electricity_bill_calc/vo" | 	"electricity_bill_calc/vo" | ||||||
|  | 	"log" | ||||||
|  |  | ||||||
| 	"github.com/gofiber/fiber/v2" | 	"github.com/gofiber/fiber/v2" | ||||||
| 	"github.com/jinzhu/copier" | 	"github.com/jinzhu/copier" | ||||||
| @@ -97,6 +98,7 @@ func initNewReportCalculateTask(c *fiber.Ctx) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	ok, err := service.ReportService.CreateNewReport(&form) | 	ok, err := service.ReportService.CreateNewReport(&form) | ||||||
|  | 	log.Println("??????????????????????kkkkkkkkkkkkkkkkkkkkkkkkk", ok) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		reportLog.Error("无法创建核算报表", zap.Error(err)) | 		reportLog.Error("无法创建核算报表", zap.Error(err)) | ||||||
| 		return result.Error(fiber.StatusInternalServerError, "无法创建核算报表。") | 		return result.Error(fiber.StatusInternalServerError, "无法创建核算报表。") | ||||||
|   | |||||||
| @@ -38,12 +38,20 @@ func (cr _CalculateRepository) UpdateReportCalculateStatus(rid string, status st | |||||||
| 	ctx, cancel := global.TimeoutContext() | 	ctx, cancel := global.TimeoutContext() | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
|  |  | ||||||
|  | 	var atio int | ||||||
|  | 	var err error | ||||||
|  | 	log.Println("11111111111", status) | ||||||
| 	currentTime := time.Now() | 	currentTime := time.Now() | ||||||
|  | 	if status == "success" { | ||||||
|  | 		atio = 0 | ||||||
|  | 	} else { | ||||||
|  | 		atio = 1 | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	updateResultSql, updateResultArgs, _ := cr.ds. | 	updateResultSql, updateResultArgs, _ := cr.ds. | ||||||
| 		Update(goqu.T("report_task")). | 		Update(goqu.T("report_task")). | ||||||
| 		Set(goqu.Record{ | 		Set(goqu.Record{ | ||||||
| 			"status":           status, | 			"status":           int16(atio), | ||||||
| 			"last_modified_at": currentTime, | 			"last_modified_at": currentTime, | ||||||
| 			"message":          message, | 			"message":          message, | ||||||
| 		}).Where(goqu.I("id").Eq(rid)). | 		}).Where(goqu.I("id").Eq(rid)). | ||||||
| @@ -124,7 +132,7 @@ func (cr _CalculateRepository) GetAllPoolingMeterRelations(pid string, revokedAf | |||||||
|  |  | ||||||
| 	var meterRelation []model.MeterRelation | 	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 { | 	if err != nil { | ||||||
| 		cr.log.Error("获取当前园区中所有公摊表计与商户表计之间的关联关系,包括已经解除的出错", zap.Error(err)) | 		cr.log.Error("获取当前园区中所有公摊表计与商户表计之间的关联关系,包括已经解除的出错", zap.Error(err)) | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @@ -152,7 +160,7 @@ func (cr _CalculateRepository) GetAllTenementMeterRelations(pid string, associat | |||||||
|  |  | ||||||
| 	var tenementMeter []model.TenementMeter | 	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 { | 	if err != nil { | ||||||
| 		cr.log.Error("获取当前园区中所有的商户与表计的关联关系,包括已经解除的", zap.Error(err)) | 		cr.log.Error("获取当前园区中所有的商户与表计的关联关系,包括已经解除的", zap.Error(err)) | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @@ -178,13 +186,13 @@ func (cr _CalculateRepository) GetMeterReadings(rid string, meterType int16) ([] | |||||||
| 			goqu.I("r.id").Eq(rid), | 			goqu.I("r.id").Eq(rid), | ||||||
| 			goqu.I("mr.meter_type").Eq(meterType), | 			goqu.I("mr.meter_type").Eq(meterType), | ||||||
| 			// TODO:2023.08.02 此方法出错优先查看是否这里出问题 | 			// 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() | 		Order(goqu.I("mr.read_at").Asc()).Select(goqu.I("mr.*")).ToSQL() | ||||||
|  |  | ||||||
| 	var readings []model.MeterReading | 	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 { | 	if err != nil { | ||||||
| 		cr.log.Error("获取指定报表中所有涉及到的指定类型表计在核算时间段内的所有读数数据出错", zap.Error(err)) | 		cr.log.Error("获取指定报表中所有涉及到的指定类型表计在核算时间段内的所有读数数据出错", zap.Error(err)) | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @@ -199,7 +207,8 @@ func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16 | |||||||
| 	ctx, cancel := global.TimeoutContext() | 	ctx, cancel := global.TimeoutContext() | ||||||
| 	defer cancel() | 	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( | 		Select( | ||||||
| 			goqu.MAX("mr.read_at").As("read_at"), | 			goqu.MAX("mr.read_at").As("read_at"), | ||||||
| 			goqu.I("mr.park_id"), | 			goqu.I("mr.park_id"), | ||||||
| @@ -219,7 +228,7 @@ func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16 | |||||||
| 		Where( | 		Where( | ||||||
| 			goqu.I("r.id").Eq(rid), | 			goqu.I("r.id").Eq(rid), | ||||||
| 			goqu.I("mr.meter_type").Eq(meterType), | 			goqu.I("mr.meter_type").Eq(meterType), | ||||||
| 			goqu.I(" mr.read_at::date <= lower(r.period)"), | 			goqu.L(" read_at <= lower(r.period)"), | ||||||
| 		). | 		). | ||||||
| 		GroupBy( | 		GroupBy( | ||||||
| 			goqu.I("mr.park_id"), | 			goqu.I("mr.park_id"), | ||||||
| @@ -235,11 +244,13 @@ func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16 | |||||||
| 		).ToSQL() | 		).ToSQL() | ||||||
|  |  | ||||||
| 	var readings []model.MeterReading | 	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 { | 	if err != nil { | ||||||
| 		cr.log.Error("获取指定报表中所有涉及到的表计在核算起始日期前的最后一次读数出错", zap.Error(err)) | 		cr.log.Error("获取指定报表中所有涉及到的表计在核算起始日期前的最后一次读数出错", zap.Error(err)) | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | 	fmt.Println(">>>>>>>>>>>>", readingsSql) | ||||||
|  | 	fmt.Println(";;;;;;;;;;;;;;;;;;", readings) | ||||||
| 	return readings, nil | 	return readings, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -266,11 +277,11 @@ func (cr _CalculateRepository) GetAllTenements(rid string) ([]model.Tenement, er | |||||||
| 		). | 		). | ||||||
| 		Where( | 		Where( | ||||||
| 			goqu.I("r.id").Eq(rid), | 			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() | 		).ToSQL() | ||||||
|  | 	fmt.Println(tenementQuerySql) | ||||||
| 	var tenements []model.Tenement | 	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 { | 	if err != nil { | ||||||
| 		cr.log.Error("取得指定报表所涉及的所有商户信息出错", zap.Error(err)) | 		cr.log.Error("取得指定报表所涉及的所有商户信息出错", zap.Error(err)) | ||||||
| 		return nil, err | 		return nil, err | ||||||
|   | |||||||
| @@ -12,7 +12,6 @@ import ( | |||||||
| 	"electricity_bill_calc/types" | 	"electricity_bill_calc/types" | ||||||
| 	"electricity_bill_calc/vo" | 	"electricity_bill_calc/vo" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  |  | ||||||
| 	"github.com/doug-martin/goqu/v9" | 	"github.com/doug-martin/goqu/v9" | ||||||
| 	_ "github.com/doug-martin/goqu/v9/dialect/postgres" | 	_ "github.com/doug-martin/goqu/v9/dialect/postgres" | ||||||
| 	"github.com/georgysavva/scany/v2/pgxscan" | 	"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, ""))) | 	mr.log.Info("分页列出指定园区下的表计信息", zap.String("park id", pid), zap.Uint("page", page), zap.String("keyword", tools.DefaultTo(keyword, ""))) | ||||||
| 	ctx, cancel := global.TimeoutContext() | 	ctx, cancel := global.TimeoutContext() | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
|  |  | ||||||
| 	meterQuery := mr.ds. | 	meterQuery := mr.ds. | ||||||
| 		From(goqu.T("meter_04kv").As("m")). | 		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")))). | 		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( | 		Where( | ||||||
| 			goqu.I("m.park_id").Eq(pid), | 			goqu.I("m.park_id").Eq(pid), | ||||||
| 			goqu.I("m.detached_at").IsNull(), | 			goqu.I("m.detached_at").IsNull(), | ||||||
|  | 			goqu.I("m.meter_type").Eq(mtype), | ||||||
| 		) | 		) | ||||||
| 	countQuery := mr.ds. | 	countQuery := mr.ds. | ||||||
| 		From(goqu.T("meter_04kv").As("m")). | 		From(goqu.T("meter_04kv").As("m")). | ||||||
| @@ -142,6 +141,7 @@ func (mr _MeterRepository) MetersIn(pid string, page uint, keyword *string) ([]* | |||||||
| 		Where( | 		Where( | ||||||
| 			goqu.I("m.park_id").Eq(pid), | 			goqu.I("m.park_id").Eq(pid), | ||||||
| 			goqu.I("m.detached_at").IsNull(), | 			goqu.I("m.detached_at").IsNull(), | ||||||
|  | 			goqu.I("m.meter_type").Eq(mtype), | ||||||
| 		) | 		) | ||||||
|  |  | ||||||
| 	if keyword != nil && len(*keyword) > 0 { | 	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)) | 		mr.log.Error("查询表计信息失败", zap.Error(err)) | ||||||
| 		return make([]*model.MeterDetail, 0), 0, err | 		return make([]*model.MeterDetail, 0), 0, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := pgxscan.Get(ctx, global.DB, &total, countSql, countArgs...); err != nil { | 	if err := pgxscan.Get(ctx, global.DB, &total, countSql, countArgs...); err != nil { | ||||||
| 		mr.log.Error("查询表计数量失败", zap.Error(err)) | 		mr.log.Error("查询表计数量失败", zap.Error(err)) | ||||||
| 		return make([]*model.MeterDetail, 0), 0, err | 		return make([]*model.MeterDetail, 0), 0, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return meters, total, nil | 	return meters, total, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -209,7 +209,6 @@ func (mr _MeterRepository) ListMetersByIDs(pid string, ids []string) ([]*model.M | |||||||
| 		mr.log.Error("查询表计信息失败", zap.Error(err)) | 		mr.log.Error("查询表计信息失败", zap.Error(err)) | ||||||
| 		return make([]*model.MeterDetail, 0), err | 		return make([]*model.MeterDetail, 0), err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return meters, nil | 	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, ""))) | 	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() | 	ctx, cancel := global.TimeoutContext() | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
|  |  | ||||||
| 	readingQuery := mr.ds. | 	readingQuery := mr.ds. | ||||||
| 		From(goqu.T("meter_reading").As("r")). | 		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.*"). | 		Select("r.*"). | ||||||
| 		Where( | 		Where( | ||||||
| 			goqu.I("r.park_id").Eq(pid), | 			goqu.I("r.park_id").Eq(pid), | ||||||
|  | 			goqu.I("m.meter_type").Eq(mtype), | ||||||
| 		) | 		) | ||||||
| 	countQuery := mr.ds. | 	countQuery := mr.ds. | ||||||
| 		From(goqu.T("meter_reading").As("r")). | 		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("*")). | 		Select(goqu.COUNT("*")). | ||||||
| 		Where( | 		Where( | ||||||
| 			goqu.I("r.park_id").Eq(pid), | 			goqu.I("r.park_id").Eq(pid), | ||||||
|  | 			goqu.I("m.meter_type").Eq(mtype), | ||||||
| 		) | 		) | ||||||
|  |  | ||||||
| 	if keyword != nil && len(*keyword) > 0 { | 	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)) | 		mr.log.Error("查询抄表记录数量失败", zap.Error(err)) | ||||||
| 		return make([]*model.MeterReading, 0), 0, err | 		return make([]*model.MeterReading, 0), 0, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return readings, total, nil | 	return readings, total, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,7 +9,9 @@ import ( | |||||||
| 	"electricity_bill_calc/tools/serial" | 	"electricity_bill_calc/tools/serial" | ||||||
| 	"electricity_bill_calc/types" | 	"electricity_bill_calc/types" | ||||||
| 	"electricity_bill_calc/vo" | 	"electricity_bill_calc/vo" | ||||||
|  | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"log" | ||||||
|  |  | ||||||
| 	"github.com/doug-martin/goqu/v9" | 	"github.com/doug-martin/goqu/v9" | ||||||
| 	_ "github.com/doug-martin/goqu/v9/dialect/postgres" | 	_ "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. | 	createSql, createArgs, _ := rr.ds. | ||||||
| 		Insert(goqu.T("report")). | 		Insert(goqu.T("report")). | ||||||
| 		Cols( | 		Cols( | ||||||
| 			"id", "park_id", "period", "category", "meter_o4kv_type", "price_policy", | 			"id", "park_id", "period", "category", "meter_04kv_type", "price_policy", | ||||||
| 			"basic_pooled", "adjust_pooled", "loss_pooled", "public_pooled", "created_at", | 			"basis_pooled", "adjust_pooled", "loss_pooled", "public_pooled", "created_at", | ||||||
| 			"last_modified_at", | 			"last_modified_at", | ||||||
| 		). | 		). | ||||||
| 		Vals(goqu.Vals{ | 		Vals(goqu.Vals{ | ||||||
| @@ -134,38 +136,45 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, str | |||||||
| 			createTime, | 			createTime, | ||||||
| 		}). | 		}). | ||||||
| 		Prepared(true).ToSQL() | 		Prepared(true).ToSQL() | ||||||
| 	summarySql, summaryArgs, _ := rr.ds. | 	critical := model.ConsumptionUnit{ | ||||||
| 		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{ |  | ||||||
| 		Amount: form.Critical, | 		Amount: form.Critical, | ||||||
| 		Fee:    form.CriticalFee, | 		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, | 		Amount: form.Peak, | ||||||
| 		Fee:    form.PeakFee, | 		Fee:    form.PeakFee, | ||||||
| 			}, | 	} | ||||||
| 			model.ConsumptionUnit{ | 	peakData, _ := json.Marshal(peak) | ||||||
|  | 	flat := model.ConsumptionUnit{ | ||||||
| 		Amount: form.Flat, | 		Amount: form.Flat, | ||||||
| 		Fee:    form.FlatFee, | 		Fee:    form.FlatFee, | ||||||
| 			}, | 	} | ||||||
| 			model.ConsumptionUnit{ | 	flatData, _ := json.Marshal(flat) | ||||||
|  | 	valley := model.ConsumptionUnit{ | ||||||
| 		Amount: form.Valley, | 		Amount: form.Valley, | ||||||
| 		Fee:    form.ValleyFee, | 		Fee:    form.ValleyFee, | ||||||
| 			}, | 	} | ||||||
| 			form.BasicFee, | 	valleyData, _ := json.Marshal(valley) | ||||||
| 			form.AdjustFee, | 	summarySql, summaryArgs, err5 := rr.ds. | ||||||
| 		}). | 		Insert(goqu.T("report_summary")). | ||||||
| 		Prepared(true).ToSQL() | 		//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. | 	taskSql, taskArgs, _ := rr.ds. | ||||||
| 		Insert(goqu.T("report_task")). | 		Insert(goqu.T("report_task")). | ||||||
| 		Cols("id", "status", "last_modified_at"). | 		Cols("id", "status", "last_modified_at"). | ||||||
| @@ -189,11 +198,14 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, str | |||||||
| 		tx.Rollback(ctx) | 		tx.Rollback(ctx) | ||||||
| 		return false, "", err | 		return false, "", err | ||||||
| 	} | 	} | ||||||
|  | 	log.Println("?????????", summarySql, summaryArgs) | ||||||
|  | 	log.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", resSummary.RowsAffected()) | ||||||
| 	if resSummary.RowsAffected() == 0 { | 	if resSummary.RowsAffected() == 0 { | ||||||
| 		rr.log.Error("保存核算报表汇总时出现错误", zap.Error(err)) | 		rr.log.Error("保存核算报表汇总时出现错误", zap.Error(err)) | ||||||
| 		tx.Rollback(ctx) | 		tx.Rollback(ctx) | ||||||
| 		return false, "", exceptions.NewUnsuccessCreateError("创建核算报表汇总时出现错误") | 		return false, "", exceptions.NewUnsuccessCreateError("创建核算报表汇总时出现错误") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	resTask, err := tx.Exec(ctx, taskSql, taskArgs...) | 	resTask, err := tx.Exec(ctx, taskSql, taskArgs...) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		rr.log.Error("创建核算报表任务时出现错误", zap.Error(err)) | 		rr.log.Error("创建核算报表任务时出现错误", zap.Error(err)) | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ import ( | |||||||
| 	"electricity_bill_calc/model" | 	"electricity_bill_calc/model" | ||||||
| 	"electricity_bill_calc/model/calculate" | 	"electricity_bill_calc/model/calculate" | ||||||
| 	"electricity_bill_calc/repository" | 	"electricity_bill_calc/repository" | ||||||
|  | 	"fmt" | ||||||
| 	"time" | 	"time" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -22,6 +23,11 @@ func MetersParkCalculate(report model.ReportIndex, periodStart time.Time, | |||||||
|  |  | ||||||
| 	parkMeterReadings = append(parkMeterReadings, lastTermParkMeterReadings...) | 	parkMeterReadings = append(parkMeterReadings, lastTermParkMeterReadings...) | ||||||
|  |  | ||||||
|  | 	if len(parkMeterReadings) <= 0 { | ||||||
|  | 		fmt.Println(parkMeterReadings) | ||||||
|  | 		return []calculate.Meter{}, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	var parkMetersReports []calculate.Meter | 	var parkMetersReports []calculate.Meter | ||||||
| 	for _, meter := range meterDetail { | 	for _, meter := range meterDetail { | ||||||
| 		if meter.MeterType == model.METER_INSTALLATION_PARK { | 		if meter.MeterType == model.METER_INSTALLATION_PARK { | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ import ( | |||||||
| 	"electricity_bill_calc/model" | 	"electricity_bill_calc/model" | ||||||
| 	"electricity_bill_calc/model/calculate" | 	"electricity_bill_calc/model/calculate" | ||||||
| 	"electricity_bill_calc/repository" | 	"electricity_bill_calc/repository" | ||||||
|  | 	"fmt" | ||||||
| 	"github.com/shopspring/decimal" | 	"github.com/shopspring/decimal" | ||||||
| 	"time" | 	"time" | ||||||
| 	"unsafe" | 	"unsafe" | ||||||
| @@ -24,6 +25,10 @@ func PooledMetersCalculate(report *model.ReportIndex, periodStart time.Time, | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	poolingMeterReadings = append(poolingMeterReadings, lastTermPoolingMeterReadings...) | 	poolingMeterReadings = append(poolingMeterReadings, lastTermPoolingMeterReadings...) | ||||||
|  | 	fmt.Println(poolingMeterReadings, "==============================================") | ||||||
|  | 	if len(poolingMeterReadings) <= 0 { | ||||||
|  | 		return nil, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	var poolingMetersReports []calculate.Meter | 	var poolingMetersReports []calculate.Meter | ||||||
| 	for _, meter := range meterDetails { | 	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 { | 	publicTotal *decimal.Decimal, summary *calculate.Summary) error { | ||||||
| 	summary.Loss = summary.Overall.Amount.Sub(summary.TotalConsumption) | 	summary.Loss = summary.Overall.Amount.Sub(summary.TotalConsumption) | ||||||
|  |  | ||||||
| @@ -72,11 +72,15 @@ func LossCalculate(report *model.ReportIndex, Public *[]calculate.Meter, | |||||||
|  |  | ||||||
| 	differentialLoss := summary.LossDilutedPrice.Sub(summary.AuthoizeLoss.Amount) | 	differentialLoss := summary.LossDilutedPrice.Sub(summary.AuthoizeLoss.Amount) | ||||||
|  |  | ||||||
|  | 	fmt.Println(publicTotal.InexactFloat64()) | ||||||
| 	if publicTotal.InexactFloat64() <= decimal.Zero.InexactFloat64() { | 	if publicTotal.InexactFloat64() <= decimal.Zero.InexactFloat64() { | ||||||
| 		return errors.New("园区公共表计的电量总和为非正值,或者园区未设置公共表计,无法计算核定线损") | 		return errors.New("园区公共表计的电量总和为非正值,或者园区未设置公共表计,无法计算核定线损") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for _, meter := range *Public { | 	if Public == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	for _, meter := range Public { | ||||||
| 		amountProportion := meter.Overall.Amount.InexactFloat64() / publicTotal.InexactFloat64() | 		amountProportion := meter.Overall.Amount.InexactFloat64() / publicTotal.InexactFloat64() | ||||||
| 		adjustAmount := differentialLoss.InexactFloat64() * decimal.NewFromFloat(-1.0).InexactFloat64() | 		adjustAmount := differentialLoss.InexactFloat64() * decimal.NewFromFloat(-1.0).InexactFloat64() | ||||||
| 		meter.AdjustLoss = model.ConsumptionUnit{ | 		meter.AdjustLoss = model.ConsumptionUnit{ | ||||||
|   | |||||||
| @@ -67,7 +67,7 @@ func MainCalculateProcess(rid string) error { | |||||||
| 	parkTotal := TotalConsumptionCalculate(tenementReports, summary) | 	parkTotal := TotalConsumptionCalculate(tenementReports, summary) | ||||||
|  |  | ||||||
| 	//计算线损以及调整线损 | 	//计算线损以及调整线损 | ||||||
| 	err = LossCalculate(report, &parkMetersReports, &parkTotal, &summary) | 	err = LossCalculate(report, parkMetersReports, &parkTotal, &summary) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fmt.Println("9", err) | 		fmt.Println("9", err) | ||||||
| 		return err | 		return err | ||||||
| @@ -79,15 +79,13 @@ func MainCalculateProcess(rid string) error { | |||||||
| 		fmt.Println("10", err) | 		fmt.Println("10", err) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | 	// 计算基本电费分摊、调整电费分摊、电费摊薄单价。 | ||||||
| 	err = CalculatePrices(&summary) | 	err = CalculatePrices(&summary) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fmt.Println("11", err) | 		fmt.Println("11", err) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | 	fmt.Println("计算数据读取完成") | ||||||
| 	//=========================================================================== |  | ||||||
| 	// 计算基本电费分摊、调整电费分摊、电费摊薄单价。 |  | ||||||
| 	err = CalculatePrices(&summary) |  | ||||||
| 	// 收集目前所有已经处理的表计,统一对其进行摊薄计算。 | 	// 收集目前所有已经处理的表计,统一对其进行摊薄计算。 | ||||||
| 	meters, err := CollectMeters(tenementReports, poolingMetersReports, parkMetersReports) | 	meters, err := CollectMeters(tenementReports, poolingMetersReports, parkMetersReports) | ||||||
| 	if err != nil { | 	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( | 	ms.log.Info( | ||||||
| 		"查询符合条件的表计读数记录", | 		"查询符合条件的表计读数记录", | ||||||
| 		zap.String("park id", pid), | 		zap.String("park id", pid), | ||||||
| @@ -597,7 +597,7 @@ func (ms _MeterService) SearchMeterReadings(pid string, building *string, start, | |||||||
| 		zap.Uint("page", page), | 		zap.Uint("page", page), | ||||||
| 		zap.Stringp("keyword", keyword), | 		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 { | 	if err != nil { | ||||||
| 		ms.log.Error("无法查询符合条件的表计读数记录。", zap.Error(err)) | 		ms.log.Error("无法查询符合条件的表计读数记录。", zap.Error(err)) | ||||||
| 		return make([]*model.DetailedMeterReading, 0), 0, err | 		return make([]*model.DetailedMeterReading, 0), 0, err | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ import ( | |||||||
| 	"electricity_bill_calc/types" | 	"electricity_bill_calc/types" | ||||||
| 	"electricity_bill_calc/vo" | 	"electricity_bill_calc/vo" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
|  | 	"log" | ||||||
| 	"sync" | 	"sync" | ||||||
|  |  | ||||||
| 	"github.com/doug-martin/goqu/v9" | 	"github.com/doug-martin/goqu/v9" | ||||||
| @@ -216,19 +217,24 @@ func (rs _ReportService) CreateNewReport(createFrom *vo.ReportCreationForm) (boo | |||||||
| 		rs.log.Error("创建核算报表错误", zap.Error(err)) | 		rs.log.Error("创建核算报表错误", zap.Error(err)) | ||||||
| 		return false, err | 		return false, err | ||||||
| 	} | 	} | ||||||
|  | 	log.Println("创建成功,successsssssssssssssssssssssssssssssssssssssssssssssssssssss") | ||||||
|  |  | ||||||
| 	if !state { | 	if !state { | ||||||
| 		status, err := repository.CalculateRepository.UpdateReportCalculateStatus(report, "InsufficientData", "创建报表时发生错误,需手动再次计算") | 		status, err := repository.CalculateRepository.UpdateReportCalculateStatus(report, "InsufficientData", | ||||||
|  | 			"创建报表时发生错误,需手动再次计算") | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			rs.log.Error("创建报表时发生错误,需手动再次计算", zap.Error(err)) | 			rs.log.Error("创建报表时发生错误,需手动再次计算", zap.Error(err)) | ||||||
| 			return false, err | 			return false, err | ||||||
| 		} | 		} | ||||||
| 		return status, nil | 		return status, nil | ||||||
| 	} | 	} | ||||||
|  | 	log.Println("更新成功succssssssssssssssss") | ||||||
| 	err = rs.CalculateReport(report) | 	err = rs.CalculateReport(report) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		rs.log.Error("计算时出错", zap.Error(err)) | 		rs.log.Error("计算时出错", zap.Error(err)) | ||||||
| 		return false, err | 		return false, err | ||||||
| 	} | 	} | ||||||
|  | 	log.Println("返回truettttttttttttttttttt") | ||||||
| 	return true, nil | 	return true, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -258,13 +264,13 @@ var CALCULATE_TASK_PARALLEL_CONTROL = func() *sync.Mutex { | |||||||
|  |  | ||||||
| // 执行一个核算报表的计算任务 | // 执行一个核算报表的计算任务 | ||||||
| func (rs _ReportService) CalculateReport(rid string) error { | func (rs _ReportService) CalculateReport(rid string) error { | ||||||
| 	semaphore := CALCULATE_TASK_PARALLEL_CONTROL | 	//semaphore := CALCULATE_TASK_PARALLEL_CONTROL | ||||||
|  | 	// | ||||||
| 	semaphore.Lock() | 	//semaphore.Lock() | ||||||
| 	defer semaphore.Unlock() | 	//defer semaphore.Unlock() | ||||||
|  |  | ||||||
| 	errs := calculate.MainCalculateProcess(rid) | 	errs := calculate.MainCalculateProcess(rid) | ||||||
|  | 	log.Println("开始计算555555555555555555555555555") | ||||||
| 	if errs == nil { | 	if errs == nil { | ||||||
| 		_, err := repository.CalculateRepository.UpdateReportCalculateStatus(rid, "success", "") | 		_, err := repository.CalculateRepository.UpdateReportCalculateStatus(rid, "success", "") | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|   | |||||||
| @@ -12,9 +12,9 @@ Server: | |||||||
|   ReadTimeout: 60 |   ReadTimeout: 60 | ||||||
|   WriteTimeout: 60 |   WriteTimeout: 60 | ||||||
| Redis: | Redis: | ||||||
|   Host: 127.0.0.1 |   Host: 192.168.88.129 | ||||||
|   Port: 6379 |   Port: 6379 | ||||||
|   Password: |   Password: 123456 | ||||||
|   DB: 1 |   DB: 1 | ||||||
| Service: | Service: | ||||||
|   MaxSessionLife: 2h |   MaxSessionLife: 2h | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user