From e79d34a1367deb1f5aaedc91f1f6f3d3ec40adfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 13 Aug 2022 15:58:07 +0800 Subject: [PATCH] =?UTF-8?q?enhance(time):=E8=B0=83=E6=95=B4=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4=E5=9C=A8=E8=BE=93=E5=87=BA=E4=BD=8D?= =?UTF-8?q?JSON=E6=97=B6=E7=9A=84=E9=BB=98=E8=AE=A4=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 3 +++ main.go | 6 ++++++ model/report.go | 8 ++++---- model/session.go | 2 +- model/shared.go | 6 +++--- model/user_charges.go | 4 ++-- model/user_detail.go | 11 ++++++++++- 8 files changed, 30 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index ec30bb5..db2f405 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect + github.com/liamylian/jsontime/v2 v2.0.0 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect diff --git a/go.sum b/go.sum index 19f281c..0d3a8a6 100644 --- a/go.sum +++ b/go.sum @@ -313,6 +313,7 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= @@ -338,6 +339,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/liamylian/jsontime/v2 v2.0.0 h1:3if2kDW/boymUdO+4Qj/m4uaXMBSF6np9KEgg90cwH0= +github.com/liamylian/jsontime/v2 v2.0.0/go.mod h1:UHp1oAPqCBfspokvGmaGe0IAl2IgOpgOgDaKPcvcGGY= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= diff --git a/main.go b/main.go index ccb9645..61c583e 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "time" "github.com/gin-gonic/gin" + jsontime "github.com/liamylian/jsontime/v2/v2" "github.com/shopspring/decimal" ) @@ -66,6 +67,11 @@ func init() { log.Fatalf("Singularity account intialize failed: %v", err) } log.Println("Singularity account intialized.") + + timeZoneShanghai, _ := time.LoadLocation("Asia/Shanghai") + jsontime.AddTimeFormatAlias("simple_datetime", "2006-01-02 15:04:05") + jsontime.AddTimeFormatAlias("simple_date", "2006-01-02") + jsontime.AddLocaleAlias("shanghai", timeZoneShanghai) } func initializeRegions() error { diff --git a/model/report.go b/model/report.go index 192dd99..7bf6539 100644 --- a/model/report.go +++ b/model/report.go @@ -7,12 +7,12 @@ type Report struct { Deleted `xorm:"extends"` Id string `xorm:"varchar(120) pk not null" json:"id"` ParkId string `xorm:"varchar(120) not null" json:"parkId"` - Period time.Time `xorm:"date not null" json:"period"` + Period time.Time `xorm:"date not null" json:"period" time_format:"simple_date" time_location:"shanghai"` Published bool `xorm:"bool not null default false" json:"published"` - PublishedAt *time.Time `xorm:"timestampz" json:"publishedAt"` + PublishedAt *time.Time `xorm:"timestampz" json:"publishedAt" time_format:"simple_datetime" time_location:"shanghai"` Withdraw int8 `xorm:"smallint not null default 0" json:"withdraw"` - LastWithdrawAppliedAt *time.Time `xorm:"timestampz" json:"lastWithdrawAppliedAt"` - LastWithdrawAuditAt *time.Time `xorm:"timestampz" json:"lastWithdrawAuditAt"` + LastWithdrawAppliedAt *time.Time `xorm:"timestampz" json:"lastWithdrawAppliedAt" time_format:"simple_datetime" time_location:"shanghai"` + LastWithdrawAuditAt *time.Time `xorm:"timestampz" json:"lastWithdrawAuditAt" time_format:"simple_datetime" time_location:"shanghai"` } func (Report) TableName() string { diff --git a/model/session.go b/model/session.go index 9f15dc9..536d823 100644 --- a/model/session.go +++ b/model/session.go @@ -7,5 +7,5 @@ type Session struct { Name string `json:"name"` Type int8 `json:"type"` Token string `json:"token"` - ExpiresAt time.Time `json:"expiresAt"` + ExpiresAt time.Time `json:"expiresAt" time_format:"simple_datetime" time_location:"shanghai"` } diff --git a/model/shared.go b/model/shared.go index dd567e1..6347556 100644 --- a/model/shared.go +++ b/model/shared.go @@ -3,7 +3,7 @@ package model import "time" type Created struct { - CreatedAt time.Time `xorm:"timestampz not null created" json:"createdAt"` + CreatedAt time.Time `xorm:"timestampz not null created" json:"createdAt" time_format:"simple_datetime" time_location:"shanghai"` } type CreatedWithUser struct { @@ -12,7 +12,7 @@ type CreatedWithUser struct { } type Deleted struct { - DeletedAt *time.Time `xorm:"timestampz deleted" json:"deletedAt"` + DeletedAt *time.Time `xorm:"timestampz deleted" json:"deletedAt" time_format:"simple_datetime" time_location:"shanghai"` } type DeletedWithUser struct { @@ -22,7 +22,7 @@ type DeletedWithUser struct { type CreatedAndModified struct { Created `xorm:"extends"` - LastModifiedAt *time.Time `xorm:"timestampz updated" json:"lastModifiedAt"` + LastModifiedAt *time.Time `xorm:"timestampz updated" json:"lastModifiedAt" time_format:"simple_datetime" time_location:"shanghai"` } type CreatedAndModifiedWithUser struct { diff --git a/model/user_charges.go b/model/user_charges.go index ef47c21..9403128 100644 --- a/model/user_charges.go +++ b/model/user_charges.go @@ -14,9 +14,9 @@ type UserCharge struct { Discount decimal.Decimal `xorm:"numeric(5,4) not null" json:"discount"` Amount decimal.Decimal `xorm:"numeric(12,2) not null" json:"amount"` Settled bool `xorm:"bool not null default false" json:"settled"` - SettledAt *time.Time `xorm:"timestampz" json:"settledAt"` + SettledAt *time.Time `xorm:"timestampz" json:"settledAt" time_format:"simple_datetime" time_location:"shanghai"` Refunded bool `xorm:"bool not null default false" json:"refunded"` - RefundedAt *time.Time `xorm:"timestampz" json:"refundedAt"` + RefundedAt *time.Time `xorm:"timestampz" json:"refundedAt" time_format:"simple_datetime" time_location:"shanghai"` } func (UserCharge) TableName() string { diff --git a/model/user_detail.go b/model/user_detail.go index 5086b54..79c3675 100644 --- a/model/user_detail.go +++ b/model/user_detail.go @@ -17,7 +17,7 @@ type UserDetail struct { Contact *string `xorm:"varchar(100)" json:"contact"` Phone *string `xorm:"varchar(50)" json:"phone"` UnitServiceFee decimal.Decimal `xorm:"numeric(8,2) not null" json:"unitServiceFee"` - ServiceExpiration time.Time `xorm:"date not null" json:"serviceExpiration"` + ServiceExpiration time.Time `xorm:"date not null" json:"serviceExpiration" time_format:"simple_date" time_location:"shanghai"` } func (UserDetail) TableName() string { @@ -34,3 +34,12 @@ type JoinedUserDetail struct { func (JoinedUserDetail) TableName() string { return "user" } + +type FullJoinedUserDetail struct { + User `xorm:"extends"` + UserDetail `xorm:"extends"` +} + +func (FullJoinedUserDetail) TableName() string { + return "user" +}