users.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package models
  2. import (
  3. "eta/eta_mini_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // Users 用户表
  10. type Users struct {
  11. UserId int `orm:"column(user_id);pk"`
  12. RealName string `description:"用户姓名"`
  13. Mobile string `description:"手机号"`
  14. AreaCode string `description:"区号"`
  15. SellerId int `description:"销售ID"`
  16. SellerName string `description:"销售名称"`
  17. SysUserId int `description:"操作人ID"`
  18. SysUserRealName string `description:"操作人姓名"`
  19. CompanyId int `description:"所属机构ID"`
  20. CompanyName string `description:"所属机构名称"`
  21. Status int `description:"用户类型:1-试用用户;2-正式用户"`
  22. AuthStatus int `description:"权限状态:0-关闭;1-开启"`
  23. IsRegistered int `description:"注册状态:0-未注册;1-已注册"`
  24. RegisterTime time.Time `description:"注册时间"`
  25. Subscribe int `description:"是否关注公众号:0-未关注;1-已关注"`
  26. LastReadTime time.Time `description:"最后一次阅读时间"`
  27. ReadTimes int `description:"阅读次数"`
  28. OpenId string `description:"open_id"`
  29. UnionId string `description:"用户统一标识"`
  30. IsDeleted int `description:"删除状态:0-正常;1-已删除"`
  31. CreateTime time.Time `description:"创建时间"`
  32. ModifyTime time.Time `description:"修改时间"`
  33. UserLevelId int `description:"用户等级ID"`
  34. UserLevelName string `description:"用户等级名称"`
  35. }
  36. func (m *Users) TableName() string {
  37. return "users"
  38. }
  39. type UsersCols struct {
  40. PrimaryId string
  41. RealName string
  42. Mobile string
  43. AreaCode string
  44. SellerId string
  45. SellerName string
  46. SysUserId string
  47. SysUserRealName string
  48. CompanyId string
  49. CompanyName string
  50. Status string
  51. AuthStatus string
  52. IsRegistered string
  53. RegisterTime string
  54. Subscribe string
  55. LastReadTime string
  56. ReadTimes string
  57. OpenId string
  58. UnionId string
  59. IsDeleted string
  60. CreateTime string
  61. ModifyTime string
  62. }
  63. func (m *Users) Cols() UsersCols {
  64. return UsersCols{
  65. PrimaryId: "user_id",
  66. RealName: "real_name",
  67. Mobile: "mobile",
  68. AreaCode: "area_code",
  69. SellerId: "seller_id",
  70. SellerName: "seller_name",
  71. SysUserId: "sys_user_id",
  72. SysUserRealName: "sys_user_real_name",
  73. CompanyId: "company_id",
  74. CompanyName: "company_name",
  75. Status: "status",
  76. AuthStatus: "auth_status",
  77. IsRegistered: "is_registered",
  78. RegisterTime: "register_time",
  79. Subscribe: "subscribe",
  80. LastReadTime: "last_read_time",
  81. ReadTimes: "read_times",
  82. OpenId: "open_id",
  83. UnionId: "union_id",
  84. IsDeleted: "is_deleted",
  85. CreateTime: "create_time",
  86. ModifyTime: "modify_time",
  87. }
  88. }
  89. func (m *Users) Create() (err error) {
  90. o := orm.NewOrm()
  91. id, err := o.Insert(m)
  92. if err != nil {
  93. return
  94. }
  95. m.UserId = int(id)
  96. return
  97. }
  98. func (m *Users) Update(cols []string) (err error) {
  99. o := orm.NewOrm()
  100. _, err = o.Update(m, cols...)
  101. return
  102. }
  103. func (m *Users) GetItemById(id int) (item *Users, err error) {
  104. o := orm.NewOrm()
  105. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = %d AND %s = ? LIMIT 1`, m.TableName(), m.Cols().IsDeleted, RowDeletedDefault, m.Cols().PrimaryId)
  106. err = o.Raw(sql, id).QueryRow(&item)
  107. return
  108. }
  109. func (m *Users) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *Users, err error) {
  110. o := orm.NewOrm()
  111. order := ``
  112. if orderRule != "" {
  113. order = ` ORDER BY ` + orderRule
  114. }
  115. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = %d %s %s LIMIT 1`, m.TableName(), m.Cols().IsDeleted, RowDeletedDefault, condition, order)
  116. err = o.Raw(sql, pars).QueryRow(&item)
  117. return
  118. }
  119. func (m *Users) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  120. o := orm.NewOrm()
  121. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE %s = %d %s`, m.TableName(), m.Cols().IsDeleted, RowDeletedDefault, condition)
  122. err = o.Raw(sql, pars).QueryRow(&count)
  123. return
  124. }
  125. func (m *Users) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Users, err error) {
  126. o := orm.NewOrm()
  127. fields := strings.Join(fieldArr, ",")
  128. if len(fieldArr) == 0 {
  129. fields = `*`
  130. }
  131. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  132. if orderRule != "" {
  133. order = ` ORDER BY ` + orderRule
  134. }
  135. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE %s = %d %s %s`, fields, m.TableName(), m.Cols().IsDeleted, RowDeletedDefault, condition, order)
  136. _, err = o.Raw(sql, pars).QueryRows(&items)
  137. return
  138. }
  139. func (m *Users) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*Users, err error) {
  140. o := orm.NewOrm()
  141. fields := strings.Join(fieldArr, ",")
  142. if len(fieldArr) == 0 {
  143. fields = `*`
  144. }
  145. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  146. if orderRule != "" {
  147. order = ` ORDER BY ` + orderRule
  148. }
  149. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE %s = %d %s %s LIMIT ?,?`, fields, m.TableName(), m.Cols().IsDeleted, RowDeletedDefault, condition, order)
  150. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  151. return
  152. }
  153. // UsersItem 用户信息
  154. type UsersItem struct {
  155. UserId int `description:"用户ID"`
  156. RealName string `description:"用户姓名"`
  157. Mobile string `description:"手机号"`
  158. MobileHidden string `description:"手机号(隐藏中间四位)"`
  159. AreaCode string `description:"区号"`
  160. SellerId int `description:"销售ID"`
  161. SellerName string `description:"销售名称"`
  162. CompanyId int `description:"所属机构ID"`
  163. CompanyName string `description:"所属机构名称"`
  164. Status int `description:"用户类型:1-试用用户;2-正式用户"`
  165. AuthStatus int `description:"权限状态:0-关闭;1-开启"`
  166. IsRegistered int `description:"注册状态:0-未注册;1-已注册"`
  167. RegisterTime string `description:"注册时间"`
  168. Subscribe int `description:"是否关注公众号:0-未关注;1-已关注"`
  169. LastReadTime string `description:"最后一次阅读时间"`
  170. ReadTimes int `description:"阅读次数"`
  171. TrialExpired bool `description:"试用到期:true-已到期"`
  172. CreateTime string `description:"创建时间"`
  173. ModifyTime string `description:"修改时间"`
  174. UserLevelId int `description:"用户等级ID"`
  175. UserLevelName string `description:"用户等级名称"`
  176. }
  177. func (m *Users) Format2Item() (item *UsersItem) {
  178. item = new(UsersItem)
  179. item.UserId = m.UserId
  180. item.RealName = m.RealName
  181. item.Mobile = m.Mobile
  182. item.MobileHidden = utils.HideMobileMiddle(m.Mobile)
  183. item.AreaCode = m.AreaCode
  184. item.SellerId = m.SellerId
  185. item.SellerName = m.SellerName
  186. item.CompanyId = m.CompanyId
  187. item.CompanyName = m.CompanyName
  188. item.Status = m.Status
  189. item.AuthStatus = m.AuthStatus
  190. item.IsRegistered = m.IsRegistered
  191. item.RegisterTime = utils.TimeTransferString(utils.FormatDateTime, m.RegisterTime)
  192. item.Subscribe = m.Subscribe
  193. item.LastReadTime = utils.TimeTransferString(utils.FormatDateTime, m.LastReadTime)
  194. item.ReadTimes = m.ReadTimes
  195. // 试用到期
  196. if m.Status == UserStatusTrial && !m.RegisterTime.IsZero() {
  197. expiredTime := m.RegisterTime.Add(3 * 24 * time.Hour)
  198. if expiredTime.Before(time.Now().Local()) {
  199. item.TrialExpired = true
  200. item.AuthStatus = AuthStatusClose
  201. }
  202. }
  203. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  204. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  205. item.UserLevelId = m.UserLevelId
  206. item.UserLevelName = m.UserLevelName
  207. return
  208. }
  209. func (m *Users) UpdateUserReadTimes(userId int) (err error) {
  210. o := orm.NewOrm()
  211. sql := fmt.Sprintf(`UPDATE %s SET %s = %s + 1, %s = NOW() WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().ReadTimes, m.Cols().ReadTimes, m.Cols().LastReadTime, m.Cols().PrimaryId)
  212. _, err = o.Raw(sql, userId).Exec()
  213. return
  214. }