byat.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type ControlBy struct {
  11. CreateBy int `json:"createBy" gorm:"index;comment:创建者"` // 创建者
  12. UpdateBy int `json:"updateBy" gorm:"index;comment:更新者"` // 更新者
  13. }
  14. // SetCreateBy 设置创建人id
  15. func (e *ControlBy) SetCreateBy(createBy int) {
  16. e.CreateBy = createBy
  17. }
  18. // SetUpdateBy 设置修改人id
  19. func (e *ControlBy) SetUpdateBy(updateBy int) {
  20. e.UpdateBy = updateBy
  21. }
  22. type DeptBy struct {
  23. DeptId int `json:"deptId" gorm:"index;comment:部门id"` // 部门id
  24. }
  25. // SetCreateBy 设置创建人id
  26. func (e *DeptBy) SetDeptId(deptId int) {
  27. e.DeptId = deptId
  28. }
  29. type Model struct {
  30. Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"` // 主键编码
  31. }
  32. type ModelTime struct {
  33. CreatedAt Time `json:"createdAt" gorm:"type:timestamp;comment:创建时间"` // 创建时间
  34. UpdatedAt Time `json:"updatedAt" gorm:"type:timestamp;comment:最后更新时间;autoUpdateTime"` // 最后更新时间
  35. DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`
  36. }
  37. const timeFormat = "2006-01-02 15:04:05"
  38. const timezone = "Asia/Shanghai"
  39. // 全局定义
  40. type Time time.Time
  41. func (t Time) MarshalJSON() ([]byte, error) {
  42. b := make([]byte, 0, len(timeFormat)+2)
  43. if time.Time(t).IsZero() {
  44. b = append(b, '"')
  45. b = append(b, '"')
  46. return b, nil
  47. }
  48. b = append(b, '"')
  49. b = time.Time(t).AppendFormat(b, timeFormat)
  50. b = append(b, '"')
  51. return b, nil
  52. }
  53. func (t *Time) UnmarshalJSON(data []byte) (err error) {
  54. now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local)
  55. *t = Time(now)
  56. return
  57. }
  58. func (t Time) String() string {
  59. if time.Time(t).IsZero() {
  60. return ""
  61. }
  62. return time.Time(t).Format(timeFormat)
  63. }
  64. func (t Time) Local() time.Time {
  65. loc, _ := time.LoadLocation(timezone)
  66. return time.Time(t).In(loc)
  67. }
  68. func (t Time) Value() (driver.Value, error) {
  69. var zeroTime time.Time
  70. var ti = time.Time(t)
  71. if ti.UnixNano() == zeroTime.UnixNano() {
  72. return nil, nil
  73. }
  74. return ti, nil
  75. }
  76. func (t *Time) Scan(v interface{}) error {
  77. value, ok := v.(time.Time)
  78. if ok {
  79. *t = Time(value)
  80. return nil
  81. }
  82. return fmt.Errorf("can not convert %v to timestamp", v)
  83. }
  84. type StringList []string
  85. type FloatList []float64
  86. func (f *FloatList) Scan(value interface{}) error {
  87. if value == nil {
  88. return nil
  89. }
  90. switch v := value.(type) {
  91. case string:
  92. return json.Unmarshal([]byte(v), f)
  93. case []byte:
  94. return json.Unmarshal(v, f)
  95. case float64:
  96. *f = FloatList{v}
  97. return nil
  98. default:
  99. return errors.New("cannot unmarshal type into FloatList")
  100. }
  101. }
  102. func (f FloatList) Value() (driver.Value, error) {
  103. return json.Marshal(f)
  104. }
  105. func (e StringList) Value() (driver.Value, error) {
  106. d, err := json.Marshal(e)
  107. return string(d), err
  108. }
  109. func (e *StringList) Scan(src interface{}) error {
  110. return json.Unmarshal(src.([]byte), e)
  111. }