sys_user.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package models
  2. import (
  3. "time"
  4. "github.com/beego/beego/v2/client/orm"
  5. )
  6. type SysUser struct {
  7. SysUserId int `orm:"pk" description:"系统用户id"`
  8. SysUserName string `description:"账号"`
  9. SysRealName string `description:"姓名"`
  10. Password string `description:"密码"`
  11. Email string `description:"邮箱"`
  12. Phone string `description:"手机号"`
  13. AreaCode string `description:"手机区号"`
  14. SysRoleId int `description:"角色id"`
  15. SysRoleName string `description:"角色名称"`
  16. SysDepartmentId int `description:"所属部门id"`
  17. SysDepartmentId1 int `description:"所属部门一级id"`
  18. SysDepartmentId2 int `description:"所属部门二级id"`
  19. SysDepartmentId3 int `description:"所属部门三级id"`
  20. SysDepartmentId4 int `description:"所属部门四级id"`
  21. Province string `description:"省"`
  22. City string `description:"市"`
  23. PositionName string `description:"职位名称"`
  24. PositionCode int `description:"职位编码"`
  25. IsEnabled bool `description:"是否启用"`
  26. CreateTime time.Time `description:"创建时间"`
  27. ModifyTime time.Time `description:"更新时间"`
  28. }
  29. type SysUserView struct {
  30. SysUserId int `orm:"pk" description:"系统用户id"`
  31. SysUserName string `description:"账号"`
  32. SysRealName string `description:"姓名"`
  33. Password string `description:"密码"`
  34. Email string `description:"邮箱"`
  35. Phone string `description:"手机号"`
  36. AreaCode string `description:"手机区号"`
  37. SysRoleId int `description:"角色id"`
  38. SysRoleName string `description:"角色名称"`
  39. SysDepartmentId int `description:"所属部门id"`
  40. SysDepartmentName string `description:"所属部门全路径"`
  41. Province string `description:"省"`
  42. City string `description:"市"`
  43. IsEnabled bool `description:"是否启用"`
  44. PositionName string `description:"职位名称"`
  45. PositionCode int `description:"职位编码"`
  46. CreateTime time.Time `description:"创建时间"`
  47. ModifyTime time.Time `description:"更新时间"`
  48. }
  49. type SysUserMapping struct {
  50. SysUserId int `orm:"pk" description:"系统用户id"`
  51. SysUserName string `description:"账号"`
  52. SysRealName string `description:"姓名"`
  53. Password string `description:"密码"`
  54. Email string `description:"邮箱"`
  55. Phone string `description:"手机号"`
  56. AreaCode string `description:"手机区号"`
  57. SysRoleId int `description:"角色id"`
  58. SysRoleName string `description:"角色名称"`
  59. SysDepartmentId int `description:"所属部门id"`
  60. SysDepartmentName1 string `description:"所属部门一级名称"`
  61. SysDepartmentName2 string `description:"所属部门二级名称"`
  62. SysDepartmentName3 string `description:"所属部门三级名称"`
  63. SysDepartmentName4 string `description:"所属部门四级名称"`
  64. Province string `description:"省"`
  65. City string `description:"市"`
  66. PositionName string `description:"职位名称"`
  67. PositionCode int `description:"职位编码"`
  68. IsEnabled bool `description:"是否启用"`
  69. CreateTime time.Time `description:"创建时间"`
  70. ModifyTime time.Time `description:"更新时间"`
  71. }
  72. func (s *SysUser) Save() (err error) {
  73. o := orm.NewOrm()
  74. _, err = o.InsertOrUpdate(s, "sys_user_id")
  75. return
  76. }
  77. func (s *SysUser) Update(cols []string) (err error) {
  78. o := orm.NewOrm()
  79. _, err = o.Update(s, cols...)
  80. return
  81. }
  82. func SaveSysUser(sysUser *SysUser, sysDepartmendPathIds []int) (err error) {
  83. o := orm.NewOrm()
  84. sysUser.SysDepartmentId1 = sysDepartmendPathIds[0]
  85. sysUser.SysDepartmentId2 = sysDepartmendPathIds[1]
  86. sysUser.SysDepartmentId3 = sysDepartmendPathIds[2]
  87. _, err = o.InsertOrUpdate(sysUser)
  88. return
  89. }
  90. func GetSysUserBySysUserName(sysUserName string) (item *SysUser, err error) {
  91. sql := `SELECT * FROM sys_user WHERE sys_user_name=?`
  92. o := orm.NewOrm()
  93. err = o.Raw(sql, sysUserName).QueryRow(&item)
  94. return
  95. }
  96. func GetSysUserByDepartmentId(sysDepartmentId int, enabled bool) (items []*SysUser, err error) {
  97. sql := `SELECT * FROM sys_user WHERE sys_department_id=? AND is_enabled=?`
  98. o := orm.NewOrm()
  99. _, err = o.Raw(sql, sysDepartmentId, enabled).QueryRows(&items)
  100. return
  101. }
  102. func GetSysUserById(sysUserId int) (item *SysUser, err error) {
  103. sql := `SELECT * FROM sys_user WHERE sys_user_id=?`
  104. o := orm.NewOrm()
  105. err = o.Raw(sql, sysUserId).QueryRow(&item)
  106. return
  107. }
  108. func GetSysUserCountById(sysUserId int) (count int, err error) {
  109. sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_user_id=?`
  110. o := orm.NewOrm()
  111. err = o.Raw(sql, sysUserId).QueryRow(&count)
  112. return
  113. }
  114. func GetSysUserCountBySysUserName(sysUserName string) (count int, err error) {
  115. sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_user_name=?`
  116. o := orm.NewOrm()
  117. err = o.Raw(sql, sysUserName).QueryRow(&count)
  118. return
  119. }
  120. func GetSysUserCount(condition string, pars []interface{}) (count int, err error) {
  121. sql := `SELECT COUNT(1) AS count FROM sys_user WHERE 1=1`
  122. if condition != "" {
  123. sql += condition
  124. }
  125. o := orm.NewOrm()
  126. err = o.Raw(sql, pars...).QueryRow(&count)
  127. return
  128. }
  129. func GetSysUserCountByRoleId(roleId int) (count int, err error) {
  130. sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_role_id=?`
  131. o := orm.NewOrm()
  132. err = o.Raw(sql, roleId).QueryRow(&count)
  133. return
  134. }
  135. func GetSysUserListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*SysUser, err error) {
  136. sql := `SELECT * FROM sys_user u WHERE 1=1 `
  137. if condition != "" {
  138. sql += condition
  139. }
  140. sql += ` ORDER BY u.is_enabled DESC, u.modify_time DESC LIMIT ?,?`
  141. o := orm.NewOrm()
  142. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  143. return
  144. }
  145. func GetSysUserListByIsEnabled(isEnabled bool) (items []*SysUser, err error) {
  146. sql := `SELECT * FROM sys_user u WHERE 1=1 AND is_enabled=?`
  147. o := orm.NewOrm()
  148. _, err = o.Raw(sql, isEnabled).QueryRows(&items)
  149. return
  150. }
  151. func GetSysUserIdList() (items []int, err error) {
  152. sql := ` SELECT sys_user_id FROM sys_user WHERE 1=1 `
  153. o := orm.NewOrm()
  154. _, err = o.Raw(sql).QueryRows(&items)
  155. return
  156. }