forked from free-lancers/electricity_bill_calc_service
enhance(meter):完成大部分表计相关的接口。
This commit is contained in:
@@ -215,7 +215,7 @@ func (mr _MeterRepository) ListMetersByIDs(pid string, ids []string) ([]*model.M
|
||||
pid,
|
||||
strings.Join(ids, ","),
|
||||
}
|
||||
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("meter_slice", cacheConditions...); err == nil {
|
||||
if meters, err := cache.RetrieveSearch[[]*model.MeterDetail]("meter_slice", cacheConditions...); err == nil && meters != nil {
|
||||
mr.log.Info("从缓存中获取到了指定园区中所需的表计信息", zap.Int("count", len(*meters)))
|
||||
return *meters, nil
|
||||
}
|
||||
@@ -231,7 +231,7 @@ func (mr _MeterRepository) ListMetersByIDs(pid string, ids []string) ([]*model.M
|
||||
).
|
||||
Where(
|
||||
goqu.I("m.park_id").Eq(pid),
|
||||
goqu.I("m.id").Eq(goqu.Func("any", ids)),
|
||||
goqu.I("m.code").In(ids),
|
||||
).
|
||||
Order(goqu.I("m.seq").Asc()).
|
||||
Prepared(true).ToSQL()
|
||||
@@ -311,6 +311,43 @@ func (mr _MeterRepository) CreateMeter(tx pgx.Tx, ctx context.Context, pid strin
|
||||
return ok.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 创建或者更新一条表计的信息
|
||||
func (mr _MeterRepository) CreateOrUpdateMeter(tx pgx.Tx, ctx context.Context, pid string, meter vo.MeterCreationForm) (bool, error) {
|
||||
mr.log.Info("创建或者更新一条表计的信息", zap.String("park id", pid), zap.String("meter code", meter.Code))
|
||||
timeNow := types.Now()
|
||||
meterSql, meterArgs, _ := mr.ds.
|
||||
Insert(goqu.T("meter_04kv")).
|
||||
Cols(
|
||||
"park_id", "code", "address", "ratio", "seq", "meter_type", "building", "on_floor", "area", "enabled",
|
||||
"attached_at", "created_at", "last_modified_at",
|
||||
).
|
||||
Vals(
|
||||
goqu.Vals{pid, meter.Code, meter.Address, meter.Ratio, meter.Seq, meter.MeterType, meter.Building, meter.OnFloor, meter.Area, meter.Enabled,
|
||||
timeNow, timeNow, timeNow,
|
||||
},
|
||||
).
|
||||
OnConflict(
|
||||
goqu.DoUpdate("meter_04kv_pkey",
|
||||
goqu.Record{
|
||||
"address": goqu.I("excluded.address"),
|
||||
"seq": goqu.I("excluded.seq"),
|
||||
"ratio": goqu.I("excluded.ratio"),
|
||||
"meter_type": goqu.I("excluded.meter_type"),
|
||||
"building": goqu.I("excluded.building"),
|
||||
"on_floor": goqu.I("excluded.on_floor"),
|
||||
"area": goqu.I("excluded.area"),
|
||||
"last_modified_at": goqu.I("excluded.last_modified_at"),
|
||||
}),
|
||||
).
|
||||
Prepared(true).ToSQL()
|
||||
res, err := tx.Exec(ctx, meterSql, meterArgs...)
|
||||
if err != nil {
|
||||
mr.log.Error("创建或者更新表计信息失败", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
return res.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 记录一条表计的抄表信息
|
||||
func (mr _MeterRepository) RecordReading(tx pgx.Tx, ctx context.Context, pid, code string, meterType int16, ratio decimal.Decimal, reading *vo.MeterReadingForm) (bool, error) {
|
||||
mr.log.Info("记录一条表计的抄表信息", zap.String("park id", pid), zap.String("meter code", code))
|
||||
@@ -848,19 +885,16 @@ 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) {
|
||||
mr.log.Info("查询指定园区中的符合条件的抄表记录", zap.String("park id", pid), zap.String("keyword", tools.DefaultTo(keyword, "")), zap.Uint("page", page), zap.Any("start", start), zap.Any("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, "")))
|
||||
cacheConditions := []string{
|
||||
pid,
|
||||
tools.DefaultOrEmptyStr(keyword, "UNDEF"),
|
||||
cache.NullableStringKey(keyword),
|
||||
fmt.Sprintf("%d", page),
|
||||
tools.CondFn(func(val *types.Date) bool {
|
||||
return val != nil
|
||||
}, start, start.ToString(), "UNDEF"),
|
||||
tools.CondFn(func(val *types.Date) bool {
|
||||
return val != nil
|
||||
}, end, end.ToString(), "UNDEF"),
|
||||
cache.NullableConditionKey(start),
|
||||
cache.NullableConditionKey(end),
|
||||
cache.NullableStringKey(buidling),
|
||||
}
|
||||
if readings, total, err := cache.RetrievePagedSearch[[]*model.MeterReading]("meter_reading", cacheConditions...); err == nil {
|
||||
if readings, total, err := cache.RetrievePagedSearch[[]*model.MeterReading]("meter_reading", cacheConditions...); err == nil && readings != nil && total != -1 {
|
||||
mr.log.Info("从缓存中获取到了指定园区中的抄表记录", zap.Int("count", len(*readings)), zap.Int64("total", total))
|
||||
return *readings, total, nil
|
||||
}
|
||||
@@ -870,6 +904,7 @@ func (mr _MeterRepository) ListMeterReadings(pid string, keyword *string, page u
|
||||
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")))).
|
||||
Select("r.*").
|
||||
Where(
|
||||
goqu.I("r.park_id").Eq(pid),
|
||||
)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -388,6 +390,36 @@ func (pr _ParkRepository) CreateParkBuilding(pid, name string, floor *string) (b
|
||||
return rs.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 在指定园区中创建一个建筑,这个方法会使用事务
|
||||
func (pr _ParkRepository) CreateParkBuildingWithTransaction(tx pgx.Tx, ctx context.Context, pid, name string, floor *string) (bool, error) {
|
||||
timeNow := types.Now()
|
||||
serial.StringSerialRequestChan <- 1
|
||||
code := serial.Prefix("B", <-serial.StringSerialResponseChan)
|
||||
createSql, createArgs, _ := pr.ds.
|
||||
Insert("park_building").
|
||||
Cols(
|
||||
"id", "park_id", "name", "floors", "enabled", "created_at", "last_modified_at",
|
||||
).
|
||||
Vals(goqu.Vals{
|
||||
code,
|
||||
pid, name, floor, true, timeNow, timeNow,
|
||||
}).
|
||||
Prepared(true).ToSQL()
|
||||
|
||||
rs, err := tx.Exec(ctx, createSql, createArgs...)
|
||||
if err != nil {
|
||||
pr.log.Error("在指定园区中创建一个新建筑失败!", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
|
||||
if rs.RowsAffected() > 0 {
|
||||
cache.AbolishRelation("park_building")
|
||||
cache.AbolishRelation(fmt.Sprintf("park_building:%s", pid))
|
||||
}
|
||||
|
||||
return rs.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 修改指定园区中指定建筑的信息
|
||||
func (pr _ParkRepository) ModifyParkBuilding(id, pid, name string, floor *string) (bool, error) {
|
||||
pr.log.Info("修改指定园区中指定建筑的信息", zap.String("id", id), zap.String("pid", pid), zap.String("name", name), zap.Stringp("floor", floor))
|
||||
|
Reference in New Issue
Block a user