user.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 time.Time `description:"有效期开始时间"`
  34. ValidEndTime time.Time `description:"有效期结束时间"`
  35. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  36. CreateTime time.Time `description:"系统中首次新增用户的时间"`
  37. ModifyTime time.Time `description:"系统中用户信息变更的时间"`
  38. RegisterTime time.Time `description:"用户首次登录小程序的时间"`
  39. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  40. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  41. }
  42. func (u *User) Save() (err error) {
  43. o := orm.NewOrmUsingDB("master")
  44. _, err = o.InsertOrUpdate(u)
  45. return
  46. }
  47. func (u *User) Update(cols []string) (err error) {
  48. o := orm.NewOrmUsingDB("master")
  49. _, err = o.Update(u, cols...)
  50. return
  51. }
  52. func UpdateUserStatus(condition string, pars []interface{}) (err error) {
  53. o := orm.NewOrmUsingDB("master")
  54. sql := ` UPDATE user SET status=0 WHERE 1=1 `
  55. if condition != "" {
  56. sql += condition
  57. }
  58. _, err = o.Raw(sql, pars).Exec()
  59. return
  60. }
  61. func SaveUser(user *User, chartPermissionIds []int) (err error) {
  62. o := orm.NewOrmUsingDB("master")
  63. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  64. insertId, er := txOrm.InsertOrUpdate(user)
  65. if er != nil {
  66. return er
  67. }
  68. if user.UserId != 0 {
  69. insertId = int64(user.UserId)
  70. }
  71. // 先删除再增加
  72. sql := `DELETE FROM user_chart_permission_mapping WHERE user_id=?`
  73. _, er = txOrm.Raw(sql, insertId).Exec()
  74. if er != nil {
  75. return er
  76. }
  77. for _, id := range chartPermissionIds {
  78. userChartPermissionMapping := new(UserChartPermissionMapping)
  79. userChartPermissionMapping.UserId = int(insertId)
  80. userChartPermissionMapping.ChartPermissionId = id
  81. _, er = txOrm.Insert(userChartPermissionMapping)
  82. if er != nil {
  83. return er
  84. }
  85. }
  86. return nil
  87. })
  88. return
  89. }
  90. func GetUserByPhone(phone, areaCode string) (item *User, err error) {
  91. o := orm.NewOrmUsingDB("master")
  92. sql := `SELECT * FROM user WHERE phone=? AND area_code=?`
  93. err = o.Raw(sql, phone, areaCode).QueryRow(&item)
  94. return
  95. }
  96. func GetUserByEmail(email string) (item *User, err error) {
  97. o := orm.NewOrmUsingDB("master")
  98. sql := `SELECT * FROM user WHERE email=? `
  99. err = o.Raw(sql, email).QueryRow(&item)
  100. return
  101. }
  102. func GetUserById(userId int) (item *User, err error) {
  103. o := orm.NewOrmUsingDB("master")
  104. sql := `SELECT * FROM user WHERE user_id=? `
  105. err = o.Raw(sql, userId).QueryRow(&item)
  106. return
  107. }
  108. func GetUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  109. sql := `SELECT u.*, su.sys_real_name AS seller_name FROM user AS u
  110. LEFT JOIN sys_user AS su
  111. ON u.seller_id = su.sys_user_id
  112. WHERE 1=1 `
  113. if condition != "" {
  114. sql += condition
  115. }
  116. sql += ` ORDER BY modify_time DESC LIMIT ?,? `
  117. o := orm.NewOrmUsingDB("master")
  118. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  119. return
  120. }
  121. func GetUserCount(condition string, pars []interface{}) (count int, err error) {
  122. sql := `SELECT COUNT(*) AS count FROM user WHERE 1=1 `
  123. if condition != "" {
  124. sql += condition
  125. }
  126. o := orm.NewOrmUsingDB("master")
  127. err = o.Raw(sql, pars...).QueryRow(&count)
  128. return
  129. }
  130. func DeleteUserById(userId int) (err error) {
  131. o := orm.NewOrmUsingDB("master")
  132. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  133. sql := `DELETE FROM user WHERE user_id=?`
  134. _, e := txOrm.Raw(sql, userId).Exec()
  135. if e != nil {
  136. return e
  137. }
  138. sql = `DELETE FROM user_chart_permission_mapping WHERE user_id=?`
  139. _, e = txOrm.Raw(sql, userId).Exec()
  140. if e != nil {
  141. return e
  142. }
  143. return nil
  144. })
  145. return
  146. }