a_base_entity.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package models
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  10. )
  11. type BaseEntity interface {
  12. TableName() string
  13. }
  14. func NewMyTime(t time.Time) MyTime {
  15. return MyTime{Time: t}
  16. }
  17. type MyTime struct {
  18. time.Time
  19. }
  20. // UnmarshalJSON ..
  21. func (d *MyTime) UnmarshalJSON(b []byte) (err error) {
  22. s := strings.Trim(string(b), "\"")
  23. if s == "null" || s == "0" {
  24. d.Time, err = time.Parse("2006-01-02 15:04:05.000", "0001-01-01 00:00:00.000")
  25. return err
  26. }
  27. if len(s) > 0 && len(s) <= 13 {
  28. millSec, err := strconv.ParseInt(s, 10, 64)
  29. if err != nil {
  30. return err
  31. }
  32. d.Time = time.UnixMilli(millSec)
  33. } else {
  34. return errors.New("invalid timestamp")
  35. }
  36. return nil
  37. }
  38. // MarshalJSON ..
  39. func (d MyTime) MarshalJSON() ([]byte, error) {
  40. if d.Time.Year() < 1800 {
  41. return json.Marshal(0)
  42. }
  43. return json.Marshal(d.Time.UnixMilli())
  44. }
  45. // Scan 从数据库读取进行绑定时用到
  46. func (d *MyTime) Scan(b interface{}) (err error) {
  47. switch x := b.(type) {
  48. case time.Time:
  49. d.Time = x
  50. case []byte:
  51. t, err := time.Parse("2006-01-02 15:04:05.000", string(b.([]byte)))
  52. if err != nil {
  53. return errors.Wrap(err, fmt.Sprintf("从数据库绑定DateTime时出错: %s", err))
  54. }
  55. d.Time = t
  56. default:
  57. d.Time = time.Time{}
  58. }
  59. return nil
  60. }
  61. // Value 写入数据库时用到
  62. func (d MyTime) Value() (driver.Value, error) {
  63. if d.Time.IsZero() {
  64. return `0001-01-01 00:00:00`, nil
  65. }
  66. v := d.Time.Format("2006-01-02 15:04:05.000")
  67. return v, nil
  68. }
  69. func (d *MyTime) GetMillSec() int64 {
  70. if d.Time.IsZero() {
  71. return 0
  72. }
  73. return d.Time.UnixMilli()
  74. }
  75. func (d MyTime) GetYearFirstDay() MyTime {
  76. if d.Time.IsZero() {
  77. d.Time = time.Now()
  78. }
  79. startOfYear := time.Date(d.Time.Year(), time.January, 1, 0, 0, 0, 0, time.Local)
  80. return MyTime{Time: startOfYear}
  81. }
  82. // 是否删除
  83. const (
  84. DeleteYes = 1
  85. DeleteNo = 2
  86. )
  87. type TimeStatType string
  88. const (
  89. TimeStatYear TimeStatType = "year" // 年
  90. TimeStatMonth TimeStatType = "month" // 月
  91. TimeStatDay TimeStatType = "day" // 日
  92. )
  93. func (e TimeStatType) Format(column, alias string) string {
  94. str := ""
  95. switch e {
  96. case TimeStatYear:
  97. str = fmt.Sprintf("DATE_FORMAT(%s,'%Y') ", column)
  98. case TimeStatMonth:
  99. str = fmt.Sprintf("DATE_FORMAT(%s,'%Y-%m') ", column)
  100. case TimeStatDay:
  101. str = fmt.Sprintf("DATE(%s) ", column)
  102. default:
  103. str = fmt.Sprintf("DATE(%s) ", column)
  104. }
  105. if alias != "" {
  106. str += fmt.Sprintf("AS %s ", alias)
  107. }
  108. return str
  109. }