user.go 12 KB

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