model.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "strconv"
  6. "time"
  7. xtime "go-common/library/time"
  8. )
  9. const (
  10. // BusinessArchive .
  11. BusinessArchive = "archive"
  12. // RankOrderByDesc .
  13. RankOrderByDesc = "desc"
  14. // RankOrderByAsc .
  15. RankOrderByAsc = "asc"
  16. // SyncInsert .
  17. SyncInsert = "insert"
  18. // SyncUpdate .
  19. SyncUpdate = "update"
  20. // SyncDelete .
  21. SyncDelete = "delete"
  22. // TimeFormat .
  23. TimeFormat = "2006-01-02 15:04:05"
  24. // FlagExist .
  25. FlagExist = true
  26. )
  27. // ArchiveMeta .
  28. type ArchiveMeta struct {
  29. ID int64 `json:"id"`
  30. Aid int64 `json:"aid"`
  31. Typeid int64 `json:"typeid"`
  32. Pubtime Stime `json:"pubtime"`
  33. *ArchiveType
  34. *ArchiveStat
  35. *ArchiveTv
  36. }
  37. // ArchiveType .
  38. type ArchiveType struct {
  39. ID int64 `json:"id"`
  40. Pid int64 `json:"pid"`
  41. }
  42. // ArchiveStat .
  43. type ArchiveStat struct {
  44. ID int64 `json:"id"`
  45. Aid int64 `json:"aid"`
  46. Click int64 `json:"click"`
  47. }
  48. // ArchiveTv .
  49. type ArchiveTv struct {
  50. ID int64 `json:"id"`
  51. Aid int64 `json:"aid"`
  52. Result int8 `json:"result"`
  53. Deleted int8 `json:"deleted"`
  54. Valid int8 `json:"valid"`
  55. }
  56. // StatViewMsg .
  57. type StatViewMsg struct {
  58. Type string `json:"type"`
  59. ID int64 `json:"id"`
  60. Count int `json:"count"`
  61. Timestamp int64 `json:"timestamp"`
  62. }
  63. // CanalMsg .
  64. type CanalMsg struct {
  65. Action string `json:"action"`
  66. Table string `json:"table"`
  67. New json.RawMessage `json:"new"`
  68. Old json.RawMessage `json:"old"`
  69. }
  70. // SetPubtime .
  71. func (a *ArchiveMeta) SetPubtime() xtime.Time {
  72. return xtime.Time(a.Pubtime)
  73. }
  74. // SetPid .
  75. func (a *ArchiveType) SetPid() int16 {
  76. return int16(a.Pid)
  77. }
  78. // SetClick .
  79. func (a *ArchiveStat) SetClick() int {
  80. return int(a.Click)
  81. }
  82. // DoReq .
  83. type DoReq struct {
  84. Business string `form:"business" validate:"required"`
  85. Action string `form:"action" validate:"required"`
  86. MinID int64 `form:"minid"`
  87. MaxID int64 `form:"maxid"`
  88. BeginTime string `form:"begintime"`
  89. EndTime string `form:"endtime"`
  90. }
  91. // MgetReq .
  92. type MgetReq struct {
  93. Business string `form:"business" validate:"required"`
  94. Oids []int64 `form:"oids,split" validate:"required"`
  95. }
  96. // MgetResp resp of mget
  97. type MgetResp struct {
  98. List map[int64]*Field `json:"list"`
  99. }
  100. // SortReq .
  101. type SortReq struct {
  102. Business string `form:"business" validate:"required"`
  103. Field string `form:"field" validate:"required"`
  104. Order string `form:"order" validate:"required"`
  105. Filters map[string]string `form:"filters" validate:"required"`
  106. Oids []int64 `form:"oids,split" validate:"required"`
  107. Pn int `form:"pn"`
  108. Ps int `form:"ps"`
  109. }
  110. // SortResp .
  111. type SortResp struct {
  112. Result []int64 `json:"result"`
  113. Page *Page `json:"page"`
  114. }
  115. // GroupReq .
  116. type GroupReq struct {
  117. Business string `form:"business" validate:"required"`
  118. Field string `form:"field" validate:"required"`
  119. Oids []int64 `form:"Oids,split" validate:"required"`
  120. }
  121. // GroupResp .
  122. type GroupResp struct {
  123. List []*Group `json:"list"`
  124. }
  125. // Group .
  126. type Group struct {
  127. Key string `json:"key"`
  128. Count int `json:"count"`
  129. }
  130. // Page Pager
  131. type Page struct {
  132. Pn int `json:"pn"`
  133. Ps int `json:"ps"`
  134. Total int `json:"total"`
  135. }
  136. // Stime .
  137. type Stime int64
  138. // Scan scan time.
  139. func (st *Stime) Scan(src interface{}) (err error) {
  140. switch sc := src.(type) {
  141. case time.Time:
  142. *st = Stime(sc.Unix())
  143. case string:
  144. var i int64
  145. i, err = strconv.ParseInt(sc, 10, 64)
  146. *st = Stime(i)
  147. }
  148. return
  149. }
  150. // Value get time value.
  151. func (st Stime) Value() (driver.Value, error) {
  152. return time.Unix(int64(st), 0), nil
  153. }
  154. // UnmarshalJSON implements the json.Unmarshaler interface.
  155. func (st *Stime) UnmarshalJSON(data []byte) error {
  156. timestamp, err := strconv.ParseInt(string(data), 10, 64)
  157. if err == nil {
  158. *st = Stime(timestamp)
  159. return nil
  160. }
  161. t, err := time.ParseInLocation(`"2006-01-02 15:04:05"`, string(data), time.Local)
  162. if err == nil {
  163. *st = Stime(t.Unix())
  164. }
  165. return nil
  166. }