user.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package models
  2. import (
  3. "context"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type User struct {
  8. UserId int `orm:"pk" description:"用户id"`
  9. RealName string `description:"姓名"`
  10. Phone string `description:"手机号"`
  11. AreaCode string `description:"区号"`
  12. Email string `description:"邮箱"`
  13. SellerId int `description:"销售id(SysUserId)"`
  14. Company string `description:"所属公司"`
  15. ValidStartTime time.Time `description:"有效期开始时间"`
  16. ValidEndTime time.Time `description:"有效期结束时间"`
  17. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  18. CreateTime time.Time `description:"系统中首次新增用户的时间"`
  19. ModifyTime time.Time `description:"系统中用户信息变更的时间"`
  20. LastUpdateTime time.Time `description:"最近一次登录时间"`
  21. RegisterTime time.Time `description:"用户首次登录小程序的时间"`
  22. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  23. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  24. }
  25. type UserView struct {
  26. UserId int `orm:"pk" description:"用户id"`
  27. RealName string `description:"姓名"`
  28. Phone string `description:"手机号"`
  29. AreaCode string `description:"区号"`
  30. Email string `description:"邮箱"`
  31. SellerId int `description:"销售id(SysUserId)"`
  32. SellerName string `description:"销售姓名"`
  33. Company string `description:"所属公司"`
  34. ValidStartTime string `description:"有效期开始时间"`
  35. ValidEndTime string `description:"有效期结束时间"`
  36. RestDate int `description:"剩余天数"`
  37. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  38. ReadCnt int `description:"用户阅读量"`
  39. CreateTime string `description:"系统中首次新增用户的时间"`
  40. ModifyTime string `description:"系统中用户信息变更的时间"`
  41. LastUpdateTime string `description:"最近一次登录时间"`
  42. RegisterTime string `description:"用户首次登录小程序的时间"`
  43. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  44. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  45. }
  46. func (u *User) Save() (err error) {
  47. o := orm.NewOrm()
  48. _, err = o.InsertOrUpdate(u)
  49. return
  50. }
  51. func (u *User) Update(cols []string) (err error) {
  52. o := orm.NewOrm()
  53. _, err = o.Update(u, cols...)
  54. return
  55. }
  56. func UpdateUserStatus(condition string, pars []interface{}) (err error) {
  57. o := orm.NewOrm()
  58. sql := ` UPDATE user SET status=0 WHERE 1=1 `
  59. if condition != "" {
  60. sql += condition
  61. }
  62. _, err = o.Raw(sql, pars).Exec()
  63. return
  64. }
  65. func SaveUser(user *User, chartPermissionIds []int) (err error) {
  66. o := orm.NewOrm()
  67. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  68. insertId, er := txOrm.InsertOrUpdate(user)
  69. if er != nil {
  70. return er
  71. }
  72. if user.UserId != 0 {
  73. insertId = int64(user.UserId)
  74. } else {
  75. user.UserId = int(insertId)
  76. }
  77. // 先删除再增加
  78. sql := `DELETE FROM user_chart_permission_mapping WHERE user_id=?`
  79. _, er = txOrm.Raw(sql, insertId).Exec()
  80. if er != nil {
  81. return er
  82. }
  83. for _, id := range chartPermissionIds {
  84. userChartPermissionMapping := new(UserChartPermissionMapping)
  85. userChartPermissionMapping.UserId = int(insertId)
  86. userChartPermissionMapping.ChartPermissionId = id
  87. _, er = txOrm.Insert(userChartPermissionMapping)
  88. if er != nil {
  89. return er
  90. }
  91. }
  92. return nil
  93. })
  94. return
  95. }
  96. func GetUserByPhone(phone, areaCode string) (item *User, err error) {
  97. o := orm.NewOrm()
  98. sql := `SELECT * FROM user WHERE phone=? AND area_code=?`
  99. err = o.Raw(sql, phone, areaCode).QueryRow(&item)
  100. return
  101. }
  102. func GetUserByEmail(email string) (item *User, err error) {
  103. o := orm.NewOrm()
  104. sql := `SELECT * FROM user WHERE email=? `
  105. err = o.Raw(sql, email).QueryRow(&item)
  106. return
  107. }
  108. func GetUserById(userId int) (item *User, err error) {
  109. o := orm.NewOrm()
  110. sql := `SELECT * FROM user WHERE user_id=? `
  111. err = o.Raw(sql, userId).QueryRow(&item)
  112. return
  113. }
  114. func GetUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  115. sql := `SELECT u.*, su.sys_real_name AS seller_name FROM user AS u
  116. LEFT JOIN sys_user AS su
  117. ON u.seller_id = su.sys_user_id
  118. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  119. if condition != "" {
  120. sql += condition
  121. }
  122. sql += ` ORDER BY modify_time DESC LIMIT ?,? `
  123. o := orm.NewOrm()
  124. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  125. return
  126. }
  127. func GetUserListByConditonSort(condition, sortConditon string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  128. sql := `SELECT u.*, su.sys_real_name AS seller_name, TIMESTAMPDIFF(DAY, NOW(), valid_end_time) AS rest_day
  129. FROM user AS u
  130. LEFT JOIN sys_user AS su
  131. ON u.seller_id = su.sys_user_id
  132. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  133. if condition != "" {
  134. sql += condition
  135. }
  136. if sortConditon != "" {
  137. sql += sortConditon
  138. }
  139. sql += ` LIMIT ?,? `
  140. o := orm.NewOrm()
  141. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  142. return
  143. }
  144. func GetUserReadList(condition string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  145. sql := `SELECT u.*, su.sys_real_name AS seller_name, COUNT(ur.user_id) AS read_cnt
  146. FROM user AS u
  147. LEFT JOIN sys_user AS su
  148. ON u.seller_id = su.sys_user_id
  149. LEFT JOIN user_read_record AS ur
  150. ON u.user_id = ur.user_id
  151. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  152. if condition != "" {
  153. sql += condition
  154. }
  155. sql += ` GROUP BY u.user_id ORDER BY read_cnt DESC LIMIT ?,? `
  156. o := orm.NewOrm()
  157. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  158. return
  159. }
  160. func GetUserReadCount(condition string, pars []interface{}) (count int, err error) {
  161. sql := `SELECT COUNT(*) AS count
  162. FROM user AS u
  163. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  164. if condition != "" {
  165. sql += condition
  166. }
  167. o := orm.NewOrm()
  168. err = o.Raw(sql, pars...).QueryRow(&count)
  169. return
  170. }
  171. func GetUserCount(condition string, pars []interface{}) (count int, err error) {
  172. sql := `SELECT COUNT(*) AS count FROM user AS u WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  173. if condition != "" {
  174. sql += condition
  175. }
  176. o := orm.NewOrm()
  177. err = o.Raw(sql, pars...).QueryRow(&count)
  178. return
  179. }
  180. func DeleteUserById(userId int) (err error) {
  181. o := orm.NewOrm()
  182. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  183. sql := `DELETE FROM user WHERE user_id=?`
  184. _, e := txOrm.Raw(sql, userId).Exec()
  185. if e != nil {
  186. return e
  187. }
  188. sql = `DELETE FROM user_chart_permission_mapping WHERE user_id=?`
  189. _, e = txOrm.Raw(sql, userId).Exec()
  190. if e != nil {
  191. return e
  192. }
  193. return nil
  194. })
  195. return
  196. }