user.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package models
  2. import (
  3. "eta/eta_mini_crm_ht/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type User struct {
  8. Id int `orm:"pk" description:"用户id"`
  9. TemplateUserId int `description:"临时用户ID"`
  10. RealName string `description:"姓名"`
  11. IdNo string `description:"证件号"`
  12. IdKind int `description:"证件类型"`
  13. IdBeginDate time.Time `description:"证件开始日期"`
  14. IdEndDate time.Time `description:"证件结束日期"`
  15. AccountStatus AccountStatus `description:"账号状态"`
  16. CreatedTime time.Time `description:"创建时间"`
  17. UpdatedTime time.Time `description:"更新时间"`
  18. }
  19. func (u User) FillUserInfo(user *TemplateUser) UserView {
  20. return UserView{
  21. RealName: u.RealName,
  22. AccountStatus: u.AccountStatus,
  23. LastReadTime: user.LastReadTime.Format(time.DateTime),
  24. FollowingGzh: user.FollowingGzh,
  25. ReadCount: user.ReadCount,
  26. Mobile: user.Mobile,
  27. CreatedTime: u.CreatedTime.Format(time.DateTime),
  28. }
  29. }
  30. type UserView struct {
  31. Id int
  32. TemplateUserId int
  33. CompanyName string
  34. RealName string `description:"姓名"`
  35. Mobile string `description:"手机号码"`
  36. FollowingGzh bool
  37. FollowingGzhStr string
  38. LastReadTime string
  39. ReadCount int
  40. AccountStatus AccountStatus `description:"账号状态"`
  41. CreatedTime string
  42. }
  43. func GetPageOfficialUserList(condition string, pars []interface{}, sortStr string, startSize int, pageSize int) (total int, userList []*UserView, err error) {
  44. o := orm.NewOrm()
  45. totalSql := `SELECT distinct template_user_id FROM users`
  46. var officialIds []int
  47. _, err = o.Raw(totalSql).QueryRows(&officialIds)
  48. if err != nil {
  49. return
  50. }
  51. total = len(officialIds)
  52. idCondition := " and tus.id in (" + utils.GetOrmReplaceHolder(len(officialIds)) + ")"
  53. sql := `SELECT us.id as id,us.template_user_id as template_user_id, us.real_name as real_name, tus.mobile,tus.following_gzh,tus.created_time,tus.read_count,tus.last_read_time,tus.account_status FROM template_users tus LEFT JOIN (select id, real_name,template_user_id from users) us on us.template_user_id=tus.id WHERE 1=1`
  54. sql = sql + idCondition
  55. if condition != "" {
  56. sql += condition
  57. }
  58. if sortStr != `` {
  59. sql += ` ORDER BY ` + sortStr
  60. }
  61. sql += ` LIMIT ?,? `
  62. _, err = o.Raw(sql, officialIds, pars, startSize, pageSize).QueryRows(&userList)
  63. if userList == nil {
  64. userList = []*UserView{}
  65. }
  66. return
  67. }
  68. func GetPageOfficialUserByCondition(condition string, pars []interface{}, sortStr string) (userList []UserView, err error) {
  69. o := orm.NewOrm()
  70. totalSql := `SELECT distinct template_user_id FROM users`
  71. var officialIds []int
  72. _, err = o.Raw(totalSql).QueryRows(&officialIds)
  73. if err != nil {
  74. return
  75. }
  76. idCondition := " and tus.id in (" + utils.GetOrmReplaceHolder(len(officialIds)) + ")"
  77. sql := `SELECT us.id as id,us.template_user_id as template_user_id, us.real_name as real_name, tus.mobile,tus.following_gzh,tus.created_time,tus.read_count,tus.last_read_time,tus.account_status FROM template_users tus LEFT JOIN (select id, real_name,template_user_id from users) us on us.template_user_id=tus.id WHERE 1=1`
  78. sql = sql + idCondition
  79. if condition != "" {
  80. sql += condition
  81. }
  82. if sortStr != `` {
  83. sql += ` ORDER BY ` + sortStr
  84. }
  85. _, err = o.Raw(sql, officialIds, pars).QueryRows(&userList)
  86. if userList == nil {
  87. userList = []UserView{}
  88. return
  89. }
  90. for i := 0; i < len(userList); i++ {
  91. if userList[i].FollowingGzh {
  92. userList[i].FollowingGzhStr = "是"
  93. } else {
  94. userList[i].FollowingGzhStr = "否"
  95. }
  96. }
  97. return
  98. }