byat.go 2.5 KB

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