package repository import ( "electricity_bill_calc/config" "electricity_bill_calc/global" "electricity_bill_calc/logger" "electricity_bill_calc/model" "electricity_bill_calc/tools/serial" "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" "go.uber.org/zap" ) type _TopUpRepository struct { log *zap.Logger ds goqu.DialectWrapper } var TopUpRepository = _TopUpRepository{ log: logger.Named("Repository", "TopUp"), ds: goqu.Dialect("postgres"), } // 检索符合条件的商户充值记录 func (tur _TopUpRepository) ListTopUps(pid string, startDate, endDate *types.Date, keyword *string, page uint) ([]*model.TopUp, int64, error) { tur.log.Info("查询符合条件的商户充值记录", zap.String("Park", pid), logger.DateFieldp("StartDate", startDate), logger.DateFieldp("EndDate", endDate), zap.Stringp("keyword", keyword), zap.Uint("page", page)) ctx, cancel := global.TimeoutContext() defer cancel() topUpQuery := tur.ds. From(goqu.T("tenement_top_ups").As("t")). LeftJoin(goqu.T("tenement").As("te"), goqu.On(goqu.I("te.id").Eq(goqu.I("t.tenement_id")), goqu.I("te.park_id").Eq(goqu.I("t.park_id")))). LeftJoin(goqu.T("meter").As("m"), goqu.On(goqu.I("m.code").Eq(goqu.I("t.meter_id")), goqu.I("m.park_id").Eq(goqu.I("t.park_id")))). Select("t.*", goqu.I("te.full_name").As("tenement_name"), goqu.I("m.address").As("meter_address")). Where(goqu.I("t.park_id").Eq(pid)) countQuery := tur.ds. From(goqu.T("tenement_top_ups").As("t")). LeftJoin(goqu.T("tenement").As("te"), goqu.On(goqu.I("te.id").Eq(goqu.I("t.tenement_id")), goqu.I("te.park_id").Eq(goqu.I("t.park_id")))). LeftJoin(goqu.T("meter").As("m"), goqu.On(goqu.I("m.code").Eq(goqu.I("t.meter_id")), goqu.I("m.park_id").Eq(goqu.I("t.park_id")))). Select(goqu.COUNT("t.*")). Where(goqu.I("t.park_id").Eq(pid)) if keyword != nil && len(*keyword) > 0 { pattern := fmt.Sprintf("%%%s%%", *keyword) topUpQuery = topUpQuery.Where(goqu.Or( goqu.I("te.full_name").ILike(pattern), goqu.I("te.short_name").ILike(pattern), goqu.I("te.abbr").ILike(pattern), goqu.I("m.code").ILike(pattern), goqu.I("m.address").ILike(pattern), )) countQuery = countQuery.Where(goqu.Or( goqu.I("te.full_name").ILike(pattern), goqu.I("te.short_name").ILike(pattern), goqu.I("te.abbr").ILike(pattern), goqu.I("m.code").ILike(pattern), goqu.I("m.address").ILike(pattern), )) } if startDate != nil { topUpQuery = topUpQuery.Where(goqu.I("t.topped_up_at").Gte(startDate.ToBeginningOfDate())) countQuery = countQuery.Where(goqu.I("t.topped_up_at").Gte(startDate.ToBeginningOfDate())) } if endDate != nil { topUpQuery = topUpQuery.Where(goqu.I("t.topped_up_at").Lte(endDate.ToEndingOfDate())) countQuery = countQuery.Where(goqu.I("t.topped_up_at").Lte(endDate.ToEndingOfDate())) } startRow := (page - 1) * config.ServiceSettings.ItemsPageSize topUpQuery = topUpQuery.Order(goqu.I("t.topped_up_at").Desc()).Offset(startRow).Limit(config.ServiceSettings.ItemsPageSize) topUpSql, topUpArgs, _ := topUpQuery.Prepared(true).ToSQL() countSql, countArgs, _ := countQuery.Prepared(true).ToSQL() var ( topUps []*model.TopUp = make([]*model.TopUp, 0) total int64 = 0 ) if err := pgxscan.Select(ctx, global.DB, &topUps, topUpSql, topUpArgs...); err != nil { tur.log.Error("查询商户充值记录失败", zap.Error(err)) return topUps, 0, err } if err := pgxscan.Get(ctx, global.DB, &total, countSql, countArgs...); err != nil { tur.log.Error("查询商户充值记录总数失败", zap.Error(err)) return topUps, 0, err } return topUps, total, nil } // 取得一个充值记录的详细信息 func (tur _TopUpRepository) GetTopUp(pid, topUpCode string) (*model.TopUp, error) { tur.log.Info("查询充值记录", zap.String("Park", pid), zap.String("TopUpCode", topUpCode)) ctx, cancel := global.TimeoutContext() defer cancel() topUpSql, topUpArgs, _ := tur.ds. From(goqu.T("tenement_top_ups").As("t")). LeftJoin(goqu.T("tenement").As("te"), goqu.On(goqu.I("te.id").Eq(goqu.I("t.tenement_id")), goqu.I("te.park_id").Eq(goqu.I("t.park_id")))). LeftJoin(goqu.T("meter").As("m"), goqu.On(goqu.I("m.code").Eq(goqu.I("t.meter_id")), goqu.I("m.park_id").Eq(goqu.I("t.park_id")))). Select("t.*", goqu.I("te.full_name").As("tenement_name"), goqu.I("m.address").As("meter_address")). Where(goqu.I("t.park_id").Eq(pid), goqu.I("t.top_up_code").Eq(topUpCode)). Prepared(true).ToSQL() var topUp model.TopUp if err := pgxscan.Get(ctx, global.DB, &topUp, topUpSql, topUpArgs...); err != nil { tur.log.Error("查询充值记录失败", zap.Error(err)) return nil, err } return &topUp, nil } // 创建一条新的充值记录 func (tur _TopUpRepository) CreateTopUp(pid string, form *vo.TopUpCreationForm) error { tur.log.Info("创建一条新的充值记录", zap.String("Park", pid), zap.String("Tenement", form.Tenement), zap.String("Meter", form.Meter)) ctx, cancel := global.TimeoutContext() defer cancel() serial.StringSerialRequestChan <- 1 topUpCode := serial.Prefix("U", <-serial.StringSerialResponseChan) topUpTime := types.Now() topUpSql, topUpArgs, _ := tur.ds. Insert("tenement_top_ups"). Cols("top_up_code", "park_id", "tenement_id", "meter_id", "topped_up_at", "amount", "payment_type"). Vals(goqu.Vals{ topUpCode, pid, form.Tenement, form.Meter, topUpTime, form.Amount, model.PAYMENT_CASH, }). Prepared(true).ToSQL() if _, err := global.DB.Exec(ctx, topUpSql, topUpArgs...); err != nil { tur.log.Error("创建充值记录失败", zap.Error(err)) return err } return nil } // 删除一条充值记录 func (tur _TopUpRepository) DeleteTopUp(pid, topUpCode string) error { tur.log.Info("删除一条充值记录", zap.String("Park", pid), zap.String("TopUpCode", topUpCode)) ctx, cancel := global.TimeoutContext() defer cancel() topUpSql, topUpArgs, _ := tur.ds. Update("tenement_top_ups"). Set(goqu.Record{"cancelled_at": types.Now()}). Where(goqu.I("park_id").Eq(pid), goqu.I("top_up_code").Eq(topUpCode)). Prepared(true).ToSQL() if _, err := global.DB.Exec(ctx, topUpSql, topUpArgs...); err != nil { tur.log.Error("删除充值记录失败", zap.Error(err)) return err } return nil }