model.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package model
  2. import (
  3. "encoding/json"
  4. "errors"
  5. xtime "go-common/library/time"
  6. "strconv"
  7. "strings"
  8. )
  9. // EmbedMid is
  10. type EmbedMid struct {
  11. Mid int64 `json:"mid"`
  12. }
  13. // PassportSummary is
  14. type PassportSummary struct {
  15. EmbedMid
  16. TelStatus int64 `json:"tel_status"`
  17. CountryID int64 `json:"country_id"`
  18. JoinIP string `json:"join_ip"`
  19. JoinTime string `json:"join_time"`
  20. EmailSuffix string `json:"email_suffix"`
  21. OriginType int64 `json:"origin_type"`
  22. RegType int64 `json:"reg_type"`
  23. }
  24. // RelationStat is
  25. type RelationStat struct {
  26. EmbedMid
  27. Following int64 `json:"following"`
  28. Whisper int64 `json:"whisper"`
  29. Black int64 `json:"black"`
  30. Follower int64 `json:"follower"`
  31. }
  32. // BlockSummary is
  33. type BlockSummary struct {
  34. EmbedMid
  35. BlockStatus int64 `json:"block_status"`
  36. StartTime string `json:"start_time"`
  37. EndTime string `json:"end_time"`
  38. }
  39. // MemberBase is
  40. type MemberBase struct {
  41. EmbedMid
  42. Birthday string `json:"birthday"`
  43. Face string `json:"face"`
  44. Name string `json:"name"`
  45. Rank int64 `json:"rank"`
  46. Sex int64 `json:"sex"`
  47. Sign string `json:"sign"`
  48. }
  49. // MemberOfficial is
  50. type MemberOfficial struct {
  51. EmbedMid
  52. Role int64 `json:"role"`
  53. Title string `json:"title"`
  54. Description string `json:"description"`
  55. }
  56. // MemberExp is
  57. type MemberExp struct {
  58. EmbedMid
  59. Exp int64 `json:"exp"`
  60. }
  61. // AccountSummary is
  62. type AccountSummary struct {
  63. MemberBase
  64. Exp *MemberExp `json:"exp"`
  65. Official *MemberOfficial `json:"official"`
  66. RelationStat *RelationStat `json:"relation_stat"`
  67. Block *BlockSummary `json:"block"`
  68. Passport *PassportSummary `json:"passport"`
  69. }
  70. // NewAccountSummary is
  71. func NewAccountSummary() *AccountSummary {
  72. return &AccountSummary{
  73. Exp: &MemberExp{},
  74. Official: &MemberOfficial{},
  75. Passport: &PassportSummary{},
  76. RelationStat: &RelationStat{},
  77. Block: &BlockSummary{},
  78. }
  79. }
  80. func (sum *AccountSummary) String() string {
  81. b, _ := json.Marshal(sum)
  82. return string(b)
  83. }
  84. // Key is
  85. func (e *EmbedMid) Key() (string, error) {
  86. if e.Mid == 0 {
  87. return "", errors.New("Empty mid")
  88. }
  89. return MidKey(e.Mid), nil
  90. }
  91. // Marshal is
  92. func (b *MemberBase) Marshal() (map[string][]byte, error) {
  93. data := map[string][]byte{
  94. "birthday": []byte(b.Birthday),
  95. "face": []byte(b.Face),
  96. "mid": []byte(strconv.FormatInt(b.Mid, 10)),
  97. "name": []byte(b.Name),
  98. "rank": []byte(strconv.FormatInt(b.Rank, 10)),
  99. "sex": []byte(strconv.FormatInt(b.Sex, 10)),
  100. "sign": []byte(b.Sign),
  101. }
  102. return data, nil
  103. }
  104. // Marshal is
  105. func (o *MemberOfficial) Marshal() (map[string][]byte, error) {
  106. data := map[string][]byte{
  107. "official.mid": []byte(strconv.FormatInt(o.Mid, 10)),
  108. "official.role": []byte(strconv.FormatInt(o.Role, 10)),
  109. "official.title": []byte(o.Title),
  110. "official.description": []byte(o.Description),
  111. }
  112. return data, nil
  113. }
  114. // Marshal is
  115. func (e *MemberExp) Marshal() (map[string][]byte, error) {
  116. data := map[string][]byte{
  117. "exp.mid": []byte(strconv.FormatInt(e.Mid, 10)),
  118. "exp.exp": []byte(strconv.FormatInt(e.Exp, 10)),
  119. }
  120. return data, nil
  121. }
  122. // reverse returns its argument string reversed rune-wise left to right.
  123. func reverse(s string) string {
  124. r := []rune(s)
  125. for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
  126. r[i], r[j] = r[j], r[i]
  127. }
  128. return string(r)
  129. }
  130. func rpad(s string, c string, l int) string {
  131. dt := l - len(s)
  132. if dt <= 0 {
  133. return s
  134. }
  135. return s + strings.Repeat(c, dt)
  136. }
  137. // MidKey is
  138. func MidKey(mid int64) string {
  139. ms := strconv.FormatInt(mid, 10)
  140. return rpad(reverse(ms), "0", 16)
  141. }
  142. // Marshal is
  143. func (r *RelationStat) Marshal() (map[string][]byte, error) {
  144. data := map[string][]byte{
  145. "relation.mid": []byte(strconv.FormatInt(r.Mid, 10)),
  146. "relation.following": []byte(strconv.FormatInt(r.Following, 10)),
  147. "relation.whisper": []byte(strconv.FormatInt(r.Whisper, 10)),
  148. "relation.black": []byte(strconv.FormatInt(r.Black, 10)),
  149. "relation.follower": []byte(strconv.FormatInt(r.Follower, 10)),
  150. }
  151. return data, nil
  152. }
  153. // Marshal is
  154. func (b *BlockSummary) Marshal() (map[string][]byte, error) {
  155. data := map[string][]byte{
  156. "block.mid": []byte(strconv.FormatInt(b.Mid, 10)),
  157. "block.block_status": []byte(strconv.FormatInt(b.BlockStatus, 10)),
  158. "block.start_time": []byte(b.StartTime),
  159. "block.end_time": []byte(b.EndTime),
  160. }
  161. return data, nil
  162. }
  163. // Marshal is
  164. func (p *PassportSummary) Marshal() (map[string][]byte, error) {
  165. data := map[string][]byte{
  166. "passport.mid": []byte(strconv.FormatInt(p.Mid, 10)),
  167. "passport.tel_status": []byte(strconv.FormatInt(p.TelStatus, 10)),
  168. "passport.country_id": []byte(strconv.FormatInt(p.CountryID, 10)),
  169. "passport.join_ip": []byte(p.JoinIP),
  170. "passport.join_time": []byte(p.JoinTime),
  171. "passport.email_suffix": []byte(p.EmailSuffix),
  172. "passport.reg_type": []byte(strconv.FormatInt(p.RegType, 10)),
  173. "passport.origin_type": []byte(strconv.FormatInt(p.OriginType, 10)),
  174. }
  175. return data, nil
  176. }
  177. // Date convert timestamp to date
  178. func Date(in xtime.Time) string {
  179. return in.Time().Format("2006-01-02")
  180. }
  181. // Datetime convert timestamp to date time
  182. func Datetime(in xtime.Time) string {
  183. return in.Time().Format("2006-01-02 15:04:05")
  184. }