enhance(model):调整自定义的日期类型,以及相关的查询。
This commit is contained in:
parent
d885538500
commit
a9f93d5239
@ -46,7 +46,7 @@ type _NewChargeFormData struct {
|
|||||||
Fee decimal.NullDecimal `json:"fee" form:"fee"`
|
Fee decimal.NullDecimal `json:"fee" form:"fee"`
|
||||||
Discount decimal.NullDecimal `json:"discount" form:"discount"`
|
Discount decimal.NullDecimal `json:"discount" form:"discount"`
|
||||||
Amount decimal.NullDecimal `json:"amount" form:"amount"`
|
Amount decimal.NullDecimal `json:"amount" form:"amount"`
|
||||||
ChargeTo time.Time `json:"chargeTo" form:"chargeTo" time_format:"simple_date" time_location:"shanghai"`
|
ChargeTo model.Date `json:"chargeTo" form:"chargeTo"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func recordNewCharge(c *gin.Context) {
|
func recordNewCharge(c *gin.Context) {
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
@ -225,7 +224,7 @@ func createOPSAndManagementAccount(c *gin.Context) {
|
|||||||
newUserDetail.Contact = creationForm.Contact
|
newUserDetail.Contact = creationForm.Contact
|
||||||
newUserDetail.Phone = creationForm.Phone
|
newUserDetail.Phone = creationForm.Phone
|
||||||
newUserDetail.UnitServiceFee = decimal.Zero
|
newUserDetail.UnitServiceFee = decimal.Zero
|
||||||
newUserDetail.ServiceExpiration, _ = time.Parse("2006-01-02 15:04:05", "2099-12-31 23:59:59")
|
newUserDetail.ServiceExpiration, _ = model.ParseDate("2099-12-31")
|
||||||
verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail)
|
verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error(http.StatusInternalServerError, err.Error())
|
result.Error(http.StatusInternalServerError, err.Error())
|
||||||
@ -290,7 +289,7 @@ func createEnterpriseAccount(c *gin.Context) {
|
|||||||
result.BadRequest("用户月服务费无法解析。")
|
result.BadRequest("用户月服务费无法解析。")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newUserDetail.ServiceExpiration = time.Now()
|
newUserDetail.ServiceExpiration = model.NewEmptyDate()
|
||||||
|
|
||||||
verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail)
|
verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
2
main.go
2
main.go
@ -143,7 +143,7 @@ func intializeSingularity() error {
|
|||||||
Enabled: true,
|
Enabled: true,
|
||||||
}
|
}
|
||||||
singularityName := "Singularity"
|
singularityName := "Singularity"
|
||||||
singularityExpires, err := time.Parse("2006-01-02 15:04:05", "2099-12-31 23:59:59")
|
singularityExpires, err := model.ParseDate("2099-12-31")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("singularity expires time parse failed: %w", err)
|
return fmt.Errorf("singularity expires time parse failed: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -19,10 +19,34 @@ func NewDate(t time.Time) Date {
|
|||||||
}
|
}
|
||||||
t = t.In(loc)
|
t = t.In(loc)
|
||||||
return Date{
|
return Date{
|
||||||
Time: time.Date(0, 1, 1, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), loc),
|
Time: time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewEmptyDate() Date {
|
||||||
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return Date{
|
||||||
|
Time: time.Date(0, 1, 1, 0, 0, 0, 0, loc),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseDate(t string) (Date, error) {
|
||||||
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||||
|
if err != nil {
|
||||||
|
return NewEmptyDate(), fmt.Errorf("unable to load time zone, %w", err)
|
||||||
|
}
|
||||||
|
d, err := time.ParseInLocation("2006-01-02", t, loc)
|
||||||
|
if err != nil {
|
||||||
|
return NewEmptyDate(), fmt.Errorf("unable to parse given time, %w", err)
|
||||||
|
}
|
||||||
|
return Date{
|
||||||
|
Time: d,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
var _ driver.Valuer = (*Date)(nil)
|
var _ driver.Valuer = (*Date)(nil)
|
||||||
|
|
||||||
func (d Date) Value() (driver.Value, error) {
|
func (d Date) Value() (driver.Value, error) {
|
||||||
@ -62,7 +86,7 @@ func (d *Date) Scan(src interface{}) (err error) {
|
|||||||
var _ json.Marshaler = (*Date)(nil)
|
var _ json.Marshaler = (*Date)(nil)
|
||||||
|
|
||||||
func (d Date) MarshalJSON() ([]byte, error) {
|
func (d Date) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(d.Time.Format("2006-01-02")), nil
|
return json.Marshal(d.Time.Format("2006-01-02"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ json.Unmarshaler = (*Date)(nil)
|
var _ json.Unmarshaler = (*Date)(nil)
|
||||||
@ -72,7 +96,11 @@ func (d *Date) UnmarshalJSON(raw []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load time zone, %w", err)
|
return fmt.Errorf("unable to load time zone, %w", err)
|
||||||
}
|
}
|
||||||
s := string(raw)
|
var s string
|
||||||
|
err = json.Unmarshal(raw, &s)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to unmarshal value, %w", err)
|
||||||
|
}
|
||||||
d.Time, err = time.ParseInLocation("2006-01-02", s, loc)
|
d.Time, err = time.ParseInLocation("2006-01-02", s, loc)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -187,9 +187,9 @@ func (c _ChargeService) CancelCharge(seq int64, uid string) error {
|
|||||||
return nil
|
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)).
|
_, err := tx.NewUpdate().Model((*model.UserDetail)(nil)).
|
||||||
Set("service_expiration = ?", expiration.Format("2006-01-02")).
|
Set("service_expiration = ?", expiration).
|
||||||
Where("id = ?", uid).
|
Where("id = ?", uid).
|
||||||
Exec(ctx)
|
Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -264,26 +264,21 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||||||
return chargesWithName, int64(total), err
|
return chargesWithName, int64(total), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ChargeService) lastValidChargeTo(tx *bun.Tx, ctx *context.Context, uid string) (time.Time, error) {
|
func (_ChargeService) lastValidChargeTo(tx *bun.Tx, ctx *context.Context, uid string) (model.Date, error) {
|
||||||
veryBlankTime, _ := time.Parse("2006-01-02 15:04:05", "0001-01-01 00:00:00")
|
var records []model.Date
|
||||||
var records []string
|
|
||||||
err := tx.NewSelect().Table("user_charge").
|
err := tx.NewSelect().Table("user_charge").
|
||||||
Where("settled = ? and cancelled = ? and refunded = ? and user_id = ?", true, false, false, uid).
|
Where("settled = ? and cancelled = ? and refunded = ? and user_id = ?", true, false, false, uid).
|
||||||
Column("charge_to").
|
Column("charge_to").
|
||||||
Scan(*ctx, &records)
|
Scan(*ctx, &records)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return veryBlankTime, nil
|
return model.NewEmptyDate(), nil
|
||||||
}
|
}
|
||||||
mappedRecords := lo.Map(records, func(elem string, index int) time.Time {
|
lastValid := lo.Reduce(records, func(acc, elem model.Date, index int) model.Date {
|
||||||
t, _ := time.Parse(time.RFC3339, elem)
|
if elem.Time.After(acc.Time) {
|
||||||
return utils.BeginOfDay(t)
|
|
||||||
})
|
|
||||||
lastValid := lo.Reduce(mappedRecords, func(acc, elem time.Time, index int) time.Time {
|
|
||||||
if elem.After(acc) {
|
|
||||||
return elem
|
return elem
|
||||||
} else {
|
} else {
|
||||||
return acc
|
return acc
|
||||||
}
|
}
|
||||||
}, veryBlankTime)
|
}, model.NewEmptyDate())
|
||||||
return lastValid, nil
|
return lastValid, nil
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPerio
|
|||||||
}
|
}
|
||||||
for _, p := range parks {
|
for _, p := range parks {
|
||||||
if c, ok := groupedParks[p.Id]; ok {
|
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
|
groupedParks[p.Id] = p
|
||||||
}
|
}
|
||||||
if c.Period == nil && p.Period != nil {
|
if c.Period == nil && p.Period != nil {
|
||||||
|
@ -52,7 +52,7 @@ func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*mo
|
|||||||
return nil, authErr
|
return nil, authErr
|
||||||
}
|
}
|
||||||
userDetial, _ := u.retreiveUserDetail(user.Id)
|
userDetial, _ := u.retreiveUserDetail(user.Id)
|
||||||
if userDetial.ServiceExpiration.Before(time.Now()) {
|
if userDetial.ServiceExpiration.Time.Before(time.Now()) {
|
||||||
return nil, exceptions.NewAuthenticationError(406, "用户服务期限已过。")
|
return nil, exceptions.NewAuthenticationError(406, "用户服务期限已过。")
|
||||||
}
|
}
|
||||||
session := &model.Session{
|
session := &model.Session{
|
||||||
@ -310,7 +310,7 @@ func (_UserService) findUserWithCredentialsByUsername(username string) (*model.U
|
|||||||
ctx, cancel := global.TimeoutContext()
|
ctx, cancel := global.TimeoutContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
user := new(model.UserWithCredentials)
|
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 {
|
if err == nil {
|
||||||
cache.CacheSearch(*user, []string{fmt.Sprintf("user:%s", user.Id)}, "user_with_credentials", username)
|
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{
|
user := &model.UserDetail{
|
||||||
Id: uid,
|
Id: uid,
|
||||||
}
|
}
|
||||||
err := global.DB.NewSelect().Model(&user).WherePK().Scan(ctx)
|
err := global.DB.NewSelect().Model(user).WherePK().Scan(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user:%s", uid)}, "user_detail", uid)
|
cache.CacheEntity(*user, []string{fmt.Sprintf("user:%s", uid)}, "user_detail", uid)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user