forked from free-lancers/electricity_bill_calc_service
		
	fix(charge):改进用户充值部分的查询错误。
This commit is contained in:
		| @@ -27,16 +27,8 @@ func searchCharges(c *fiber.Ctx) error { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	keyword := c.Query("keyword", "") | 	keyword := c.Query("keyword", "") | ||||||
| 	page := c.QueryInt("page", 1) | 	page := c.QueryInt("page", 1) | ||||||
| 	beginTime, err := types.ParseDate(c.Query("begin")) | 	beginTime := types.ParseDateWithDefault(c.Query("begin"), types.NewEmptyDate()) | ||||||
| 	if err != nil { | 	endTime := types.ParseDateWithDefault(c.Query("end"), types.MaxDate()) | ||||||
| 		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()) |  | ||||||
| 	} |  | ||||||
| 	charges, total, err := repository.ChargeRepository.FindCharges(uint(page), &beginTime, &endTime, &keyword) | 	charges, total, err := repository.ChargeRepository.FindCharges(uint(page), &beginTime, &endTime, &keyword) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		chargeLog.Error("检索用户的充值记录列表失败。", zap.Error(err)) | 		chargeLog.Error("检索用户的充值记录列表失败。", zap.Error(err)) | ||||||
|   | |||||||
| @@ -2,30 +2,29 @@ package model | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"electricity_bill_calc/types" | 	"electricity_bill_calc/types" | ||||||
| 	"time" |  | ||||||
|  |  | ||||||
| 	"github.com/shopspring/decimal" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type UserChargeDetail struct { | type UserChargeDetail struct { | ||||||
| 	Seq         int64           `json:"seq"` | 	Seq         int64           `json:"seq"` | ||||||
| 	UserId      string           `json:"user_id"` | 	UserId      string          `json:"userId" db:"user_id"` | ||||||
| 	Name        string          `json:"name"` | 	Name        string          `json:"name"` | ||||||
| 	Fee         *decimal.Decimal `json:"fee"` | 	Fee         *float64        `json:"fee"` | ||||||
| 	Discount    *decimal.Decimal `json:"discount"` | 	Discount    *float64        `json:"discount"` | ||||||
| 	Amount      *decimal.Decimal `json:"amount"` | 	Amount      *float64        `json:"amount"` | ||||||
| 	ChargeTo    types.Date       `json:"charge_to"` | 	ChargeTo    types.Date      `json:"chargeTo"` | ||||||
| 	Settled     bool            `json:"settled"` | 	Settled     bool            `json:"settled"` | ||||||
| 	SettledAt   *time.Time       `json:"settled_at"` | 	SettledAt   *types.DateTime `json:"settledAt"` | ||||||
| 	Cancelled   bool            `json:"cancelled"` | 	Cancelled   bool            `json:"cancelled"` | ||||||
| 	CancelledAt *time.Time       `json:"cancelled_at"` | 	CancelledAt *types.DateTime `json:"cancelledAt"` | ||||||
| 	CreatedAt   time.Time        `json:"created_at"` | 	Refunded    bool            `json:"refunded"` | ||||||
|  | 	RefundedAt  *types.DateTime `json:"refundedAt"` | ||||||
|  | 	CreatedAt   types.DateTime  `json:"createdAt"` | ||||||
| } | } | ||||||
|  |  | ||||||
| type ChargeRecordCreationForm struct { | type ChargeRecordCreationForm struct { | ||||||
| 	UserId   string     `json:"userId"` | 	UserId   string     `json:"userId"` | ||||||
| 	Fee      *decimal.Decimal `json:"fee"` | 	Fee      *float64   `json:"fee"` | ||||||
| 	Discount *decimal.Decimal `json:"discount"` | 	Discount *float64   `json:"discount"` | ||||||
| 	Amount   *decimal.Decimal `json:"amount"` | 	Amount   *float64   `json:"amount"` | ||||||
| 	ChargeTo types.Date `json:"chargeTo"` | 	ChargeTo types.Date `json:"chargeTo"` | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,7 +16,6 @@ import ( | |||||||
| 	"github.com/georgysavva/scany/v2/pgxscan" | 	"github.com/georgysavva/scany/v2/pgxscan" | ||||||
| 	"github.com/jackc/pgx/v5" | 	"github.com/jackc/pgx/v5" | ||||||
| 	"github.com/samber/lo" | 	"github.com/samber/lo" | ||||||
| 	"github.com/shopspring/decimal" |  | ||||||
| 	"go.uber.org/zap" | 	"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 }, beginTime, beginTime.Format("2006-01-02"), "UNDEF"), | ||||||
| 		tools.CondFn(func(t *types.Date) bool { return t != nil }, endTime, endTime.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)) | 		cr.log.Info("从缓存中获取用户的充值记录成功。", zap.Int("count", len(*charges)), zap.Int64("total", total)) | ||||||
| 		return *charges, total, nil | 		return *charges, total, nil | ||||||
| 	} | 	} | ||||||
| @@ -49,15 +48,15 @@ func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Dat | |||||||
| 	chargeQuery := cr.ds. | 	chargeQuery := cr.ds. | ||||||
| 		From(goqu.T("user_charge").As("c")). | 		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_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( | 		Select( | ||||||
| 			"c.seq", "c.user_id", "ud.name", "c.fee", "c.discount", "c.amount", "c.charge_to", | 			"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. | 	countQuery := cr.ds. | ||||||
| 		From(goqu.T("user_charge").As("c")). | 		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_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("*")) | 		Select(goqu.COUNT("*")) | ||||||
|  |  | ||||||
| 	if keyword != nil && len(*keyword) > 0 { | 	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)) | 		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 | 	currentPostion := (page - 1) * config.ServiceSettings.ItemsPageSize | ||||||
| 	chargeQuery = chargeQuery.Offset(currentPostion).Limit(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. | 	createQuery, createArgs, _ := cr.ds. | ||||||
| 		Insert(goqu.T("user_charge")). | 		Insert(goqu.T("user_charge")). | ||||||
| 		Cols("user_id", "fee", "discount", "amount", "charge_to", "created_at"). | 		Cols("user_id", "fee", "discount", "amount", "charge_to", "created_at"). | ||||||
|   | |||||||
| @@ -8,7 +8,6 @@ import ( | |||||||
| 	"electricity_bill_calc/types" | 	"electricity_bill_calc/types" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  |  | ||||||
| 	"github.com/shopspring/decimal" |  | ||||||
| 	"go.uber.org/zap" | 	"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( | 	cs.log.Info( | ||||||
| 		"创建一条新的用户充值记录。", | 		"创建一条新的用户充值记录。", | ||||||
| 		zap.String("uid", uid), | 		zap.String("uid", uid), | ||||||
| 		logger.DecimalFieldp("fee", fee), | 		zap.Float64p("fee", fee), | ||||||
| 		logger.DecimalFieldp("discount", discount), | 		zap.Float64p("discount", discount), | ||||||
| 		logger.DecimalFieldp("amount", amount), | 		zap.Float64p("amount", amount), | ||||||
| 		logger.DateField("chargeTo", chargeTo), | 		logger.DateField("chargeTo", chargeTo), | ||||||
| 		zap.Bool("extendExpriationIgnoringSettle", extendExpriationIgnoringSettle), | 		zap.Bool("extendExpriationIgnoringSettle", extendExpriationIgnoringSettle), | ||||||
| 	) | 	) | ||||||
|   | |||||||
| @@ -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 { | func NowDate() Date { | ||||||
| 	return Now().Date() | 	return Now().Date() | ||||||
| } | } | ||||||
|  |  | ||||||
| func ParseDate(t string) (Date, error) { | func ParseDate(t string) (Date, error) { | ||||||
|  | 	if len(t) == 0 { | ||||||
|  | 		return NewEmptyDate(), fmt.Errorf("不能解析空白的日期时间。") | ||||||
|  | 	} | ||||||
| 	d, err := time.ParseInLocation("2006-01-02", t, loc) | 	d, err := time.ParseInLocation("2006-01-02", t, loc) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return NewEmptyDate(), fmt.Errorf("无法解析给定的日期, %w", err) | 		return NewEmptyDate(), fmt.Errorf("无法解析给定的日期, %w", err) | ||||||
| @@ -40,6 +51,17 @@ func ParseDate(t string) (Date, error) { | |||||||
| 	}, nil | 	}, 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) | var _ driver.Valuer = (*Date)(nil) | ||||||
|  |  | ||||||
| func (dt Date) Value() (driver.Value, error) { | func (dt Date) Value() (driver.Value, error) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user