forked from free-lancers/electricity_bill_calc_service
		
	refactor(types):将日期时间类型提取到公共的类型定义包中。
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| package model | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/shopspring/decimal" | ||||
| @@ -13,7 +14,7 @@ type UserChargeDetail struct { | ||||
| 	Fee         *decimal.Decimal `json:"fee"` | ||||
| 	Discount    *decimal.Decimal `json:"discount"` | ||||
| 	Amount      *decimal.Decimal `json:"amount"` | ||||
| 	ChargeTo    Date             `json:"charge_to"` | ||||
| 	ChargeTo    types.Date       `json:"charge_to"` | ||||
| 	Settled     bool             `json:"settled"` | ||||
| 	SettledAt   *time.Time       `json:"settled_at"` | ||||
| 	Cancelled   bool             `json:"cancelled"` | ||||
| @@ -26,5 +27,5 @@ type ChargeRecordCreationForm struct { | ||||
| 	Fee      *decimal.Decimal `json:"fee"` | ||||
| 	Discount *decimal.Decimal `json:"discount"` | ||||
| 	Amount   *decimal.Decimal `json:"amount"` | ||||
| 	ChargeTo Date             `json:"chargeTo"` | ||||
| 	ChargeTo types.Date       `json:"chargeTo"` | ||||
| } | ||||
|   | ||||
							
								
								
									
										100
									
								
								model/types.go
									
									
									
									
									
								
							
							
						
						
									
										100
									
								
								model/types.go
									
									
									
									
									
								
							| @@ -1,100 +0,0 @@ | ||||
| package model | ||||
|  | ||||
| import ( | ||||
| 	"database/sql" | ||||
| 	"database/sql/driver" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	et "electricity_bill_calc/tools/time" | ||||
| ) | ||||
|  | ||||
| type Date struct { | ||||
| 	time.Time | ||||
| } | ||||
|  | ||||
| func NewDate(t time.Time) Date { | ||||
| 	t = t.In(et.Loc) | ||||
| 	return Date{ | ||||
| 		Time: time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, et.Loc), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func SpecificDate(year int, month time.Month, date int) Date { | ||||
| 	return NewDate(et.Time(year, month, date, 0, 0, 0, 0)) | ||||
| } | ||||
|  | ||||
| func NewEmptyDate() Date { | ||||
| 	return Date{ | ||||
| 		Time: time.Time{}.In(et.Loc), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func ParseDate(t string) (Date, error) { | ||||
| 	d, err := time.ParseInLocation("2006-01-02", t, et.Loc) | ||||
| 	if err != nil { | ||||
| 		return NewEmptyDate(), fmt.Errorf("unable to parse given time, %w", err) | ||||
| 	} | ||||
| 	return Date{ | ||||
| 		Time: d, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (d Date) IsEmpty() bool { | ||||
| 	return d.Time.IsZero() | ||||
| } | ||||
|  | ||||
| func (d Date) Format(fmt string) string { | ||||
| 	return d.Time.Format(fmt) | ||||
| } | ||||
|  | ||||
| func (d Date) ToString() string { | ||||
| 	return d.Time.Format("2006-01-02") | ||||
| } | ||||
|  | ||||
| var _ driver.Valuer = (*Date)(nil) | ||||
|  | ||||
| func (d Date) Value() (driver.Value, error) { | ||||
| 	return d.In(et.Loc).Format("2006-01-02"), nil | ||||
| } | ||||
|  | ||||
| var _ sql.Scanner = (*Date)(nil) | ||||
|  | ||||
| // Scan scans the time parsing it if necessary using timeFormat. | ||||
| func (d *Date) Scan(src interface{}) (err error) { | ||||
| 	switch src := src.(type) { | ||||
| 	case time.Time: | ||||
| 		*d = NewDate(src) | ||||
| 		return nil | ||||
| 	case string: | ||||
| 		d.Time, err = time.ParseInLocation("2006-01-02", src, et.Loc) | ||||
| 		return err | ||||
| 	case []byte: | ||||
| 		d.Time, err = time.ParseInLocation("2006-01-02", string(src), et.Loc) | ||||
| 		return err | ||||
| 	case nil: | ||||
| 		d.Time = time.Time{} | ||||
| 		return nil | ||||
| 	default: | ||||
| 		return fmt.Errorf("unsupported data type: %T", src) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| var _ json.Marshaler = (*Date)(nil) | ||||
|  | ||||
| func (d Date) MarshalJSON() ([]byte, error) { | ||||
| 	return json.Marshal(d.Time.Format("2006-01-02")) | ||||
| } | ||||
|  | ||||
| var _ json.Unmarshaler = (*Date)(nil) | ||||
|  | ||||
| func (d *Date) UnmarshalJSON(raw []byte) error { | ||||
| 	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, et.Loc) | ||||
| 	return err | ||||
| } | ||||
| @@ -1,6 +1,7 @@ | ||||
| package model | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/types" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/shopspring/decimal" | ||||
| @@ -13,14 +14,14 @@ const ( | ||||
| ) | ||||
|  | ||||
| type ManagementAccountCreationForm struct { | ||||
| 	Id       *string `json:"id"` | ||||
| 	Username string  `json:"username"` | ||||
| 	Name     string  `json:"name"` | ||||
| 	Contact  *string `json:"contact"` | ||||
| 	Phone    *string `json:"phone"` | ||||
| 	Type     int16   `json:"type"` | ||||
| 	Enabled  bool    `json:"enabled"` | ||||
| 	Expires  Date    `json:"expires"` | ||||
| 	Id       *string    `json:"id"` | ||||
| 	Username string     `json:"username"` | ||||
| 	Name     string     `json:"name"` | ||||
| 	Contact  *string    `json:"contact"` | ||||
| 	Phone    *string    `json:"phone"` | ||||
| 	Type     int16      `json:"type"` | ||||
| 	Enabled  bool       `json:"enabled"` | ||||
| 	Expires  types.Date `json:"expires"` | ||||
| } | ||||
|  | ||||
| func (m ManagementAccountCreationForm) IntoUser() *User { | ||||
| @@ -83,7 +84,7 @@ type UserDetail struct { | ||||
| 	Contact           *string         `json:"contact"` | ||||
| 	Phone             *string         `json:"phone"` | ||||
| 	UnitServiceFee    decimal.Decimal `db:"unit_service_fee" json:"unitServiceFee"` | ||||
| 	ServiceExpiration Date            `json:"serviceExpiration"` | ||||
| 	ServiceExpiration types.Date      `json:"serviceExpiration"` | ||||
| 	CreatedAt         time.Time       `json:"createdAt"` | ||||
| 	CreatedBy         *string         `json:"createdBy"` | ||||
| 	LastModifiedAt    time.Time       `json:"lastModifiedAt"` | ||||
| @@ -105,7 +106,7 @@ type UserWithDetail struct { | ||||
| 	Contact           *string         `json:"contact"` | ||||
| 	Phone             *string         `json:"phone"` | ||||
| 	UnitServiceFee    decimal.Decimal `db:"unit_service_fee" json:"unitServiceFee"` | ||||
| 	ServiceExpiration Date            `json:"serviceExpiration"` | ||||
| 	ServiceExpiration types.Date      `json:"serviceExpiration"` | ||||
| 	CreatedAt         time.Time       `json:"createdAt"` | ||||
| 	CreatedBy         *string         `json:"createdBy"` | ||||
| 	LastModifiedAt    time.Time       `json:"lastModifiedAt"` | ||||
|   | ||||
		Reference in New Issue
	
	Block a user