user.go 6.9 KB

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