user.go 12 KB

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