diff --git a/controller/charge.go b/controller/charge.go index f03750b..86b83cb 100644 --- a/controller/charge.go +++ b/controller/charge.go @@ -27,16 +27,8 @@ func searchCharges(c *fiber.Ctx) error { result := response.NewResult(c) keyword := c.Query("keyword", "") page := c.QueryInt("page", 1) - beginTime, err := types.ParseDate(c.Query("begin")) - if err != nil { - chargeLog.Error("无法解析查询起始时间。", zap.Error(err)) - return result.Error(http.StatusInternalServerError, err.Error()) - } - endTime, err := types.ParseDate(c.Query("end")) - if err != nil { - chargeLog.Error("无法解析查询结束时间。", zap.Error(err)) - return result.Error(http.StatusInternalServerError, err.Error()) - } + beginTime := types.ParseDateWithDefault(c.Query("begin"), types.NewEmptyDate()) + endTime := types.ParseDateWithDefault(c.Query("end"), types.MaxDate()) charges, total, err := repository.ChargeRepository.FindCharges(uint(page), &beginTime, &endTime, &keyword) if err != nil { chargeLog.Error("检索用户的充值记录列表失败。", zap.Error(err)) diff --git a/model/charge.go b/model/charge.go index 27bdbbd..d09783c 100644 --- a/model/charge.go +++ b/model/charge.go @@ -2,30 +2,29 @@ package model import ( "electricity_bill_calc/types" - "time" - - "github.com/shopspring/decimal" ) type UserChargeDetail struct { - Seq int64 `json:"seq"` - UserId string `json:"user_id"` - Name string `json:"name"` - Fee *decimal.Decimal `json:"fee"` - Discount *decimal.Decimal `json:"discount"` - Amount *decimal.Decimal `json:"amount"` - ChargeTo types.Date `json:"charge_to"` - Settled bool `json:"settled"` - SettledAt *time.Time `json:"settled_at"` - Cancelled bool `json:"cancelled"` - CancelledAt *time.Time `json:"cancelled_at"` - CreatedAt time.Time `json:"created_at"` + Seq int64 `json:"seq"` + UserId string `json:"userId" db:"user_id"` + Name string `json:"name"` + Fee *float64 `json:"fee"` + Discount *float64 `json:"discount"` + Amount *float64 `json:"amount"` + ChargeTo types.Date `json:"chargeTo"` + Settled bool `json:"settled"` + SettledAt *types.DateTime `json:"settledAt"` + Cancelled bool `json:"cancelled"` + CancelledAt *types.DateTime `json:"cancelledAt"` + Refunded bool `json:"refunded"` + RefundedAt *types.DateTime `json:"refundedAt"` + CreatedAt types.DateTime `json:"createdAt"` } type ChargeRecordCreationForm struct { - UserId string `json:"userId"` - Fee *decimal.Decimal `json:"fee"` - Discount *decimal.Decimal `json:"discount"` - Amount *decimal.Decimal `json:"amount"` - ChargeTo types.Date `json:"chargeTo"` + UserId string `json:"userId"` + Fee *float64 `json:"fee"` + Discount *float64 `json:"discount"` + Amount *float64 `json:"amount"` + ChargeTo types.Date `json:"chargeTo"` } diff --git a/repository/charge.go b/repository/charge.go index eb1419a..1857b66 100644 --- a/repository/charge.go +++ b/repository/charge.go @@ -16,7 +16,6 @@ import ( "github.com/georgysavva/scany/v2/pgxscan" "github.com/jackc/pgx/v5" "github.com/samber/lo" - "github.com/shopspring/decimal" "go.uber.org/zap" ) @@ -39,7 +38,7 @@ func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Dat tools.CondFn(func(t *types.Date) bool { return t != nil }, beginTime, beginTime.Format("2006-01-02"), "UNDEF"), tools.CondFn(func(t *types.Date) bool { return t != nil }, endTime, endTime.Format("2006-01-02"), "UNDEF"), } - if charges, total, err := cache.RetrievePagedSearch[[]*model.UserChargeDetail]("charges", cacheConditions...); err == nil { + if charges, total, err := cache.RetrievePagedSearch[[]*model.UserChargeDetail]("charges", cacheConditions...); err == nil && charges != nil { cr.log.Info("从缓存中获取用户的充值记录成功。", zap.Int("count", len(*charges)), zap.Int64("total", total)) return *charges, total, nil } @@ -49,15 +48,15 @@ func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Dat chargeQuery := cr.ds. From(goqu.T("user_charge").As("c")). Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.I("c.user_id").Eq(goqu.I("ud.id")))). - Join(goqu.T("user").As("u"), goqu.On(goqu.I("ud.user_id").Eq(goqu.I("u.id")))). + Join(goqu.T("user").As("u"), goqu.On(goqu.I("ud.id").Eq(goqu.I("u.id")))). Select( "c.seq", "c.user_id", "ud.name", "c.fee", "c.discount", "c.amount", "c.charge_to", - "c.settled", "c.settled_at", "c.cancelled", "c.cancelled_at", "c.created_at", + "c.settled", "c.settled_at", "c.cancelled", "c.cancelled_at", "c.refunded", "c.refunded_at", "c.created_at", ) countQuery := cr.ds. From(goqu.T("user_charge").As("c")). Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.I("c.user_id").Eq(goqu.I("ud.id")))). - Join(goqu.T("user").As("u"), goqu.On(goqu.I("ud.user_id").Eq(goqu.I("u.id")))). + Join(goqu.T("user").As("u"), goqu.On(goqu.I("ud.id").Eq(goqu.I("u.id")))). Select(goqu.COUNT("*")) if keyword != nil && len(*keyword) > 0 { @@ -84,7 +83,7 @@ func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Dat countQuery = countQuery.Where(goqu.I("c.charge_to").Lte(*endTime)) } - chargeQuery = chargeQuery.Order(goqu.I("c.created_by").Desc()) + chargeQuery = chargeQuery.Order(goqu.I("c.created_at").Desc()) currentPostion := (page - 1) * config.ServiceSettings.ItemsPageSize chargeQuery = chargeQuery.Offset(currentPostion).Limit(config.ServiceSettings.ItemsPageSize) @@ -109,7 +108,7 @@ func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Dat } // 在用户充值记录中创建一条新的记录 -func (cr _ChargeRepository) CreateChargeRecord(tx pgx.Tx, ctx context.Context, uid string, fee, discount, amount *decimal.Decimal, chargeTo types.Date) (bool, error) { +func (cr _ChargeRepository) CreateChargeRecord(tx pgx.Tx, ctx context.Context, uid string, fee, discount, amount *float64, chargeTo types.Date) (bool, error) { createQuery, createArgs, _ := cr.ds. Insert(goqu.T("user_charge")). Cols("user_id", "fee", "discount", "amount", "charge_to", "created_at"). diff --git a/service/charge.go b/service/charge.go index 4bf7b92..807e617 100644 --- a/service/charge.go +++ b/service/charge.go @@ -8,7 +8,6 @@ import ( "electricity_bill_calc/types" "fmt" - "github.com/shopspring/decimal" "go.uber.org/zap" ) @@ -21,13 +20,13 @@ var ChargeService = &_ChargeService{ } // 创建一条新的用户充值记录,同时更新用户的服务期限 -func (cs _ChargeService) RecordUserCharge(uid string, fee, discount, amount *decimal.Decimal, chargeTo types.Date, extendExpriationIgnoringSettle bool) (bool, error) { +func (cs _ChargeService) RecordUserCharge(uid string, fee, discount, amount *float64, chargeTo types.Date, extendExpriationIgnoringSettle bool) (bool, error) { cs.log.Info( "创建一条新的用户充值记录。", zap.String("uid", uid), - logger.DecimalFieldp("fee", fee), - logger.DecimalFieldp("discount", discount), - logger.DecimalFieldp("amount", amount), + zap.Float64p("fee", fee), + zap.Float64p("discount", discount), + zap.Float64p("amount", amount), logger.DateField("chargeTo", chargeTo), zap.Bool("extendExpriationIgnoringSettle", extendExpriationIgnoringSettle), ) diff --git a/types/date.go b/types/date.go index e9c8e8d..6713219 100644 --- a/types/date.go +++ b/types/date.go @@ -26,11 +26,22 @@ func NewEmptyDate() Date { } } +func MinDate() Date { + return NewDate(1, 1, 1) +} + +func MaxDate() Date { + return NewDate(9999, 12, 31) +} + func NowDate() Date { return Now().Date() } func ParseDate(t string) (Date, error) { + if len(t) == 0 { + return NewEmptyDate(), fmt.Errorf("不能解析空白的日期时间。") + } d, err := time.ParseInLocation("2006-01-02", t, loc) if err != nil { return NewEmptyDate(), fmt.Errorf("无法解析给定的日期, %w", err) @@ -40,6 +51,17 @@ func ParseDate(t string) (Date, error) { }, nil } +func ParseDateWithDefault(t string, defaultDate Date) Date { + if len(t) == 0 { + return defaultDate + } + d, err := ParseDate(t) + if err != nil { + return defaultDate + } + return d +} + var _ driver.Valuer = (*Date)(nil) func (dt Date) Value() (driver.Value, error) {