123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package model
- import (
- "database/sql/driver"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "time"
- )
- type ControlBy struct {
- CreateBy int `json:"createBy" gorm:"index;comment:创建者"` // 创建者
- UpdateBy int `json:"updateBy" gorm:"index;comment:更新者"` // 更新者
- }
- // SetCreateBy 设置创建人id
- func (e *ControlBy) SetCreateBy(createBy int) {
- e.CreateBy = createBy
- }
- // SetUpdateBy 设置修改人id
- func (e *ControlBy) SetUpdateBy(updateBy int) {
- e.UpdateBy = updateBy
- }
- type DeptBy struct {
- DeptId int `json:"deptId" gorm:"index;comment:部门id"` // 部门id
- }
- // SetCreateBy 设置创建人id
- func (e *DeptBy) SetDeptId(deptId int) {
- e.DeptId = deptId
- }
- type Model struct {
- Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"` // 主键编码
- }
- type ModelTime struct {
- CreatedAt Time `json:"createdAt" gorm:"type:timestamp;comment:创建时间"` // 创建时间
- UpdatedAt Time `json:"updatedAt" gorm:"type:timestamp;comment:最后更新时间;autoUpdateTime"` // 最后更新时间
- DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`
- }
- const timeFormat = "2006-01-02 15:04:05"
- const timezone = "Asia/Shanghai"
- // 全局定义
- type Time time.Time
- func (t Time) MarshalJSON() ([]byte, error) {
- b := make([]byte, 0, len(timeFormat)+2)
- if time.Time(t).IsZero() {
- b = append(b, '"')
- b = append(b, '"')
- return b, nil
- }
- b = append(b, '"')
- b = time.Time(t).AppendFormat(b, timeFormat)
- b = append(b, '"')
- return b, nil
- }
- func (t *Time) UnmarshalJSON(data []byte) (err error) {
- now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local)
- *t = Time(now)
- return
- }
- func (t Time) String() string {
- if time.Time(t).IsZero() {
- return ""
- }
- return time.Time(t).Format(timeFormat)
- }
- func (t Time) Local() time.Time {
- loc, _ := time.LoadLocation(timezone)
- return time.Time(t).In(loc)
- }
- func (t Time) Value() (driver.Value, error) {
- var zeroTime time.Time
- var ti = time.Time(t)
- if ti.UnixNano() == zeroTime.UnixNano() {
- return nil, nil
- }
- return ti, nil
- }
- func (t *Time) Scan(v interface{}) error {
- value, ok := v.(time.Time)
- if ok {
- *t = Time(value)
- return nil
- }
- return fmt.Errorf("can not convert %v to timestamp", v)
- }
- type StringList []string
- func (e StringList) Value() (driver.Value, error) {
- d, err := json.Marshal(e)
- return string(d), err
- }
- func (e *StringList) Scan(src interface{}) error {
- return json.Unmarshal(src.([]byte), e)
- }
- func (e *StringList) Self() []string {
- return *e
- }
|