user.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/beego/beego/v2/client/orm"
  8. )
  9. type User struct {
  10. UserId int `orm:"pk" description:"用户id"`
  11. RealName string `description:"姓名"`
  12. Phone string `description:"手机号"`
  13. AreaCode string `description:"区号"`
  14. Email string `description:"邮箱"`
  15. SellerId int `description:"销售id(SysUserId)"`
  16. Company string `description:"所属公司"`
  17. ValidStartTime time.Time `description:"有效期开始时间"`
  18. ValidEndTime time.Time `description:"有效期结束时间"`
  19. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  20. CreateTime time.Time `description:"系统中首次新增用户的时间"`
  21. ModifyTime time.Time `description:"系统中用户信息变更的时间"`
  22. RegisterTime time.Time `description:"用户首次登录小程序的时间"`
  23. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  24. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  25. }
  26. type UserView struct {
  27. UserId int `orm:"pk" description:"用户id"`
  28. RealName string `description:"姓名"`
  29. Phone string `description:"手机号"`
  30. AreaCode string `description:"区号"`
  31. Email string `description:"邮箱"`
  32. SellerId int `description:"销售id(SysUserId)"`
  33. SellerName string `description:"销售姓名"`
  34. Company string `description:"所属公司"`
  35. ValidStartTime string `description:"有效期开始时间"`
  36. ValidEndTime string `description:"有效期结束时间"`
  37. RestDate int `description:"剩余天数"`
  38. Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
  39. ReadCnt int `description:"用户阅读量"`
  40. CreateTime string `description:"系统中首次新增用户的时间"`
  41. ModifyTime string `description:"系统中用户信息变更的时间"`
  42. LastUpdateTime string `description:"最近一次阅读时间"`
  43. RegisterTime string `description:"用户首次登录小程序的时间"`
  44. IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
  45. IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
  46. }
  47. func (u *User) Save() (err error) {
  48. o := orm.NewOrm()
  49. _, err = o.InsertOrUpdate(u)
  50. return
  51. }
  52. func (u *User) Update(cols []string) (err error) {
  53. o := orm.NewOrm()
  54. _, err = o.Update(u, cols...)
  55. return
  56. }
  57. func UpdateUserStatus(condition string, pars []interface{}) (err error) {
  58. o := orm.NewOrm()
  59. sql := ` UPDATE user SET status=0 WHERE 1=1 `
  60. if condition != "" {
  61. sql += condition
  62. }
  63. _, err = o.Raw(sql, pars).Exec()
  64. return
  65. }
  66. func GetUserIdListByCondition(condition string, pars []interface{}) (items []int, err error) {
  67. o := orm.NewOrm()
  68. sql := ` SELECT user_id FROM user WHERE 1=1 `
  69. if condition != "" {
  70. sql += condition
  71. }
  72. _, err = o.Raw(sql, pars).QueryRows(&items)
  73. return
  74. }
  75. func SaveUser(user *User, chartPermissionIds []int) (err error) {
  76. o := orm.NewOrm()
  77. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  78. insertId, er := txOrm.InsertOrUpdate(user)
  79. if er != nil {
  80. return er
  81. }
  82. if user.UserId != 0 {
  83. insertId = int64(user.UserId)
  84. } else {
  85. user.UserId = int(insertId)
  86. }
  87. // 先删除再增加
  88. sql := `DELETE FROM user_chart_permission_mapping WHERE user_id=?`
  89. _, er = txOrm.Raw(sql, insertId).Exec()
  90. if er != nil {
  91. return er
  92. }
  93. for _, id := range chartPermissionIds {
  94. userChartPermissionMapping := new(UserChartPermissionMapping)
  95. userChartPermissionMapping.UserId = int(insertId)
  96. userChartPermissionMapping.ChartPermissionId = id
  97. _, er = txOrm.Insert(userChartPermissionMapping)
  98. if er != nil {
  99. return er
  100. }
  101. }
  102. return nil
  103. })
  104. return
  105. }
  106. func GetUserByPhone(phone, areaCode string) (item *User, err error) {
  107. o := orm.NewOrm()
  108. sql := `SELECT * FROM user WHERE phone=? AND area_code=?`
  109. err = o.Raw(sql, phone, areaCode).QueryRow(&item)
  110. return
  111. }
  112. func GetUserByEmail(email string) (item *User, err error) {
  113. o := orm.NewOrm()
  114. sql := `SELECT * FROM user WHERE email=? `
  115. err = o.Raw(sql, email).QueryRow(&item)
  116. return
  117. }
  118. func GetUserById(userId int) (item *User, err error) {
  119. o := orm.NewOrm()
  120. sql := `SELECT * FROM user WHERE user_id=? `
  121. err = o.Raw(sql, userId).QueryRow(&item)
  122. return
  123. }
  124. func GetUserViewById(userId int) (item *UserView, err error) {
  125. o := orm.NewOrm()
  126. sql := `SELECT * FROM user WHERE user_id=? `
  127. err = o.Raw(sql, userId).QueryRow(&item)
  128. return
  129. }
  130. func GetUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  131. sql := `SELECT u.*, su.sys_real_name AS seller_name FROM user AS u
  132. LEFT JOIN sys_user AS su
  133. ON u.seller_id = su.sys_user_id
  134. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  135. if condition != "" {
  136. sql += condition
  137. }
  138. sql += ` ORDER BY modify_time DESC LIMIT ?,? `
  139. o := orm.NewOrm()
  140. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  141. return
  142. }
  143. func GetUserListByConditonSort(condition, sortConditon string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  144. sql := `SELECT u.*, su.sys_real_name AS seller_name
  145. FROM user AS u
  146. LEFT JOIN sys_user AS su
  147. ON u.seller_id = su.sys_user_id
  148. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  149. if condition != "" {
  150. sql += condition
  151. }
  152. if sortConditon != "" {
  153. sql += sortConditon
  154. }
  155. sql += ` LIMIT ?,? `
  156. o := orm.NewOrm()
  157. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  158. return
  159. }
  160. func GetPotentialUserCountByConditon(condition string, pars []interface{}) (count int, err error) {
  161. sql := `SELECT COUNT(DISTINCT u.user_id) AS count
  162. FROM user AS u
  163. LEFT JOIN user_read_record AS ur
  164. ON u.user_id = ur.user_id
  165. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) AND u.status=1`
  166. if condition != "" {
  167. sql += condition
  168. }
  169. o := orm.NewOrm()
  170. err = o.Raw(sql, pars).QueryRow(&count)
  171. return
  172. }
  173. func GetPotentialUserCountByConditonV2(condition string, pars []interface{}) (count int, err error) {
  174. sql := `SELECT COUNT(u.user_id) AS count
  175. FROM user AS u
  176. LEFT JOIN (
  177. SELECT user_id, MAX(create_time) AS create_time
  178. FROM user_read_record
  179. GROUP BY user_id
  180. ) AS ur
  181. ON u.user_id = ur.user_id
  182. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) AND u.status=1`
  183. if condition != "" {
  184. sql += condition
  185. }
  186. o := orm.NewOrm()
  187. err = o.Raw(sql, pars).QueryRow(&count)
  188. return
  189. }
  190. func GetPotentialUserIdsByConditonV2(condition string, pars []interface{}, sortConditon string, startSize, pageSize int) (items []*UserView, err error) {
  191. sql := `SELECT DISTINCT u.*, ur.read_cnt, ur.create_time AS last_update_time
  192. FROM user AS u
  193. LEFT JOIN (
  194. SELECT user_id, MAX(create_time) AS create_time, COUNT(user_id) AS read_cnt
  195. FROM user_read_record
  196. GROUP BY user_id
  197. ) AS ur
  198. ON u.user_id = ur.user_id
  199. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) AND u.status=1`
  200. if condition != "" {
  201. sql += condition
  202. }
  203. if sortConditon != "" {
  204. sql += sortConditon
  205. }
  206. sql += ` LIMIT ?,? `
  207. o := orm.NewOrm()
  208. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  209. return
  210. }
  211. func GetPotentialUserIdsByConditon(condition string, pars []interface{}) (userIds []string, err error) {
  212. sql := `SELECT DISTINCT u.user_id AS user_id
  213. FROM user AS u
  214. LEFT JOIN user_read_record AS ur
  215. ON u.user_id = ur.user_id
  216. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) AND u.status=1`
  217. if condition != "" {
  218. sql += condition
  219. }
  220. o := orm.NewOrm()
  221. _, err = o.Raw(sql, pars).QueryRows(&userIds)
  222. return
  223. }
  224. func GetPotentialUserListByConditonSort(userIds []string, sortConditon string, startSize, pageSize int) (items []*UserView, err error) {
  225. sql := `SELECT u.*, COUNT(ur.user_id) AS read_cnt, Max(ur.create_time) AS last_update_time
  226. FROM user AS u
  227. LEFT JOIN user_read_record AS ur
  228. ON u.user_id = ur.user_id
  229. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) AND u.status=1`
  230. if len(userIds) > 0 {
  231. sql += fmt.Sprintf(" AND u.user_id IN (%s)", strings.Join(userIds, ","))
  232. }
  233. sql += ` GROUP BY u.user_id`
  234. if sortConditon != "" {
  235. sql += sortConditon
  236. }
  237. sql += ` LIMIT ?,? `
  238. o := orm.NewOrm()
  239. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
  240. return
  241. }
  242. func GetUserReadList(condition, sortCondition string, pars []interface{}, startSize, pageSize int) (items []*UserView, err error) {
  243. 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
  244. FROM user AS u
  245. LEFT JOIN sys_user AS su
  246. ON u.seller_id = su.sys_user_id
  247. LEFT JOIN user_read_record AS ur
  248. ON u.user_id = ur.user_id
  249. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  250. if condition != "" {
  251. sql += condition
  252. }
  253. sql += ` GROUP BY u.user_id `
  254. if sortCondition != "" {
  255. sql += sortCondition
  256. } else {
  257. sql += ` ORDER BY read_cnt DESC `
  258. }
  259. sql += ` LIMIT ?,? `
  260. o := orm.NewOrm()
  261. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  262. return
  263. }
  264. func GetUserReadCount(condition string, pars []interface{}) (count int, err error) {
  265. sql := `SELECT COUNT(*) AS count
  266. FROM user AS u
  267. WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  268. if condition != "" {
  269. sql += condition
  270. }
  271. o := orm.NewOrm()
  272. err = o.Raw(sql, pars...).QueryRow(&count)
  273. return
  274. }
  275. func GetUserCount(condition string, pars []interface{}) (count int, err error) {
  276. sql := `SELECT COUNT(*) AS count FROM user AS u WHERE 1=1 AND (u.phone IS NOT NULL OR u.email IS NOT NULL) `
  277. if condition != "" {
  278. sql += condition
  279. }
  280. o := orm.NewOrm()
  281. err = o.Raw(sql, pars...).QueryRow(&count)
  282. return
  283. }
  284. func DeleteUserById(userId int) (err error) {
  285. o := orm.NewOrm()
  286. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  287. sql := `DELETE FROM user WHERE user_id=?`
  288. _, e := txOrm.Raw(sql, userId).Exec()
  289. if e != nil {
  290. return e
  291. }
  292. sql = `DELETE FROM user_chart_permission_mapping WHERE user_id=?`
  293. _, e = txOrm.Raw(sql, userId).Exec()
  294. if e != nil {
  295. return e
  296. }
  297. return nil
  298. })
  299. return
  300. }