forked from free-lancers/electricity_bill_calc_service
enhance(model):调整自定义的日期类型,以及相关的查询。
This commit is contained in:
@@ -187,9 +187,9 @@ func (c _ChargeService) CancelCharge(seq int64, uid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch _ChargeService) updateUserExpiration(tx *bun.Tx, ctx context.Context, uid string, expiration time.Time) error {
|
||||
func (ch _ChargeService) updateUserExpiration(tx *bun.Tx, ctx context.Context, uid string, expiration model.Date) error {
|
||||
_, err := tx.NewUpdate().Model((*model.UserDetail)(nil)).
|
||||
Set("service_expiration = ?", expiration.Format("2006-01-02")).
|
||||
Set("service_expiration = ?", expiration).
|
||||
Where("id = ?", uid).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
@@ -264,26 +264,21 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
||||
return chargesWithName, int64(total), err
|
||||
}
|
||||
|
||||
func (_ChargeService) lastValidChargeTo(tx *bun.Tx, ctx *context.Context, uid string) (time.Time, error) {
|
||||
veryBlankTime, _ := time.Parse("2006-01-02 15:04:05", "0001-01-01 00:00:00")
|
||||
var records []string
|
||||
func (_ChargeService) lastValidChargeTo(tx *bun.Tx, ctx *context.Context, uid string) (model.Date, error) {
|
||||
var records []model.Date
|
||||
err := tx.NewSelect().Table("user_charge").
|
||||
Where("settled = ? and cancelled = ? and refunded = ? and user_id = ?", true, false, false, uid).
|
||||
Column("charge_to").
|
||||
Scan(*ctx, &records)
|
||||
if err != nil {
|
||||
return veryBlankTime, nil
|
||||
return model.NewEmptyDate(), nil
|
||||
}
|
||||
mappedRecords := lo.Map(records, func(elem string, index int) time.Time {
|
||||
t, _ := time.Parse(time.RFC3339, elem)
|
||||
return utils.BeginOfDay(t)
|
||||
})
|
||||
lastValid := lo.Reduce(mappedRecords, func(acc, elem time.Time, index int) time.Time {
|
||||
if elem.After(acc) {
|
||||
lastValid := lo.Reduce(records, func(acc, elem model.Date, index int) model.Date {
|
||||
if elem.Time.After(acc.Time) {
|
||||
return elem
|
||||
} else {
|
||||
return acc
|
||||
}
|
||||
}, veryBlankTime)
|
||||
}, model.NewEmptyDate())
|
||||
return lastValid, nil
|
||||
}
|
||||
|
@@ -79,7 +79,7 @@ func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPerio
|
||||
}
|
||||
for _, p := range parks {
|
||||
if c, ok := groupedParks[p.Id]; ok {
|
||||
if c.Period != nil && p.Period != nil && p.Period.After(*c.Period) {
|
||||
if c.Period != nil && p.Period != nil && p.Period.After(c.Period.Time) {
|
||||
groupedParks[p.Id] = p
|
||||
}
|
||||
if c.Period == nil && p.Period != nil {
|
||||
|
@@ -52,7 +52,7 @@ func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*mo
|
||||
return nil, authErr
|
||||
}
|
||||
userDetial, _ := u.retreiveUserDetail(user.Id)
|
||||
if userDetial.ServiceExpiration.Before(time.Now()) {
|
||||
if userDetial.ServiceExpiration.Time.Before(time.Now()) {
|
||||
return nil, exceptions.NewAuthenticationError(406, "用户服务期限已过。")
|
||||
}
|
||||
session := &model.Session{
|
||||
@@ -310,7 +310,7 @@ func (_UserService) findUserWithCredentialsByUsername(username string) (*model.U
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
user := new(model.UserWithCredentials)
|
||||
err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx)
|
||||
err := global.DB.NewSelect().Model(user).Where("username = ?", username).Scan(ctx)
|
||||
if err == nil {
|
||||
cache.CacheSearch(*user, []string{fmt.Sprintf("user:%s", user.Id)}, "user_with_credentials", username)
|
||||
}
|
||||
@@ -340,7 +340,7 @@ func (_UserService) retreiveUserDetail(uid string) (*model.UserDetail, error) {
|
||||
user := &model.UserDetail{
|
||||
Id: uid,
|
||||
}
|
||||
err := global.DB.NewSelect().Model(&user).WherePK().Scan(ctx)
|
||||
err := global.DB.NewSelect().Model(user).WherePK().Scan(ctx)
|
||||
if err == nil {
|
||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user:%s", uid)}, "user_detail", uid)
|
||||
}
|
||||
|
Reference in New Issue
Block a user