sys_admin.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package system
  2. import (
  3. "context"
  4. "hongze/hrms_api/global"
  5. "hongze/hrms_api/models/base"
  6. "time"
  7. )
  8. // 管理员表
  9. type SysAdmin struct {
  10. AdminId uint64 `gorm:"primaryKey;column:admin_id" json:"admin_id"` //账号ID
  11. AdminName string `gorm:"column:admin_name" json:"admin_name"` //账号名
  12. AdminAvatar string `gorm:"column:admin_avatar" json:"admin_avatar"` //用户头像
  13. RealName string `gorm:"column:real_name" json:"real_name"` //真实姓名
  14. Password string `gorm:"column:password" json:"password"` //密码
  15. Enabled int8 `gorm:"column:enabled" json:"enabled"` //1:有效,0:禁用
  16. Email string `gorm:"column:email" json:"email"`
  17. LastLoginTime time.Time `gorm:"column:last_login_time" json:"last_login_time"` //最近登陆时间
  18. Mobile string `gorm:"column:mobile" json:"mobile"` //手机号
  19. DeptId int64 `gorm:"column:dept_id" json:"dept_id"` //部门id
  20. DisableTime time.Time `gorm:"column:disable_time" json:"disable_time"` //禁用时间
  21. RoleId int64 `gorm:"column:role_id" json:"role_id"` //角色ID
  22. Position string `gorm:"column:position" json:"position"` //职位
  23. Remark string `gorm:"column:remark" json:"remark"` //备注
  24. base.TimeBase
  25. }
  26. // TableName get sql table name.获取数据库表名
  27. func (a *SysAdmin) TableName() string {
  28. return "sys_admin"
  29. }
  30. // Add 新增
  31. func (a *SysAdmin) Add() (err error) {
  32. err = global.DEFAULT_MYSQL.Create(a).Error
  33. return
  34. }
  35. type SysAdminAddReq struct {
  36. AdminName string `json:"admin_name" binding:"required"` //账号名
  37. RealName string `json:"real_name" binding:"required"` //真实姓名
  38. Password string `json:"password" binding:"required"` //密码
  39. Enabled int8 `json:"enabled" binding:"oneof=0 1"` //账号状态:1:有效,0:禁用
  40. Email string `json:"email" binding:"omitempty,email"` //邮箱
  41. Mobile string `json:"mobile" binding:"required,number,len=11"` //手机号
  42. DeptId int64 `json:"dept_id" binding:"required,gte=1"` //部门id
  43. RoleId int64 `json:"role_id" binding:"gte=1"` //角色ID
  44. Position string `json:"position"` //职位
  45. Remark string `json:"remark"` //备注
  46. }
  47. type SysAdminEditReq struct {
  48. AdminId uint64 `json:"admin_id" binding:"required,gte=1"` //账号ID
  49. AdminName string `json:"admin_name" binding:"required"` //账号名
  50. RealName string `json:"real_name" binding:"required"` //真实姓名
  51. Enabled int8 `json:"enabled" binding:"oneof=0 1"` //账号状态:1:有效,0:禁用
  52. Email string `json:"email" binding:"omitempty,email"` //邮箱
  53. Mobile string `json:"mobile" binding:"required,number,len=11"` //手机号
  54. DeptId int64 `json:"dept_id" binding:"required,gte=1"` //部门id
  55. RoleId int64 `json:"role_id" binding:"gte=1"` //角色ID
  56. Position string `json:"position"` //职位
  57. Remark string `json:"remark"` //备注
  58. }
  59. func (a *SysAdmin) GetAdminByAdminName(adminName string) (item *SysAdmin, err error) {
  60. err = global.DEFAULT_MYSQL.Model(a).Where("admin_name = ?", adminName).First(&item).Error
  61. return
  62. }
  63. func (a *SysAdmin) GetAdminByMobile(mobile string) (item *SysAdmin, err error) {
  64. err = global.DEFAULT_MYSQL.Model(a).Where("mobile = ?", mobile).First(&item).Error
  65. return
  66. }
  67. // 修改
  68. func (a *SysAdmin) Update(updateCols []string) (err error) {
  69. err = global.DEFAULT_MYSQL.Model(a).Select(updateCols).Updates(a).Error
  70. return
  71. }
  72. func (a *SysAdmin) UpdateByCondition(condition string, pars []interface{}, updates map[string]interface{}) (err error) {
  73. err = global.DEFAULT_MYSQL.Model(a).Where(condition, pars...).Updates(updates).Error
  74. return
  75. }
  76. // SelectPage 分页查询
  77. func (a *SysAdmin) SelectPage(page base.IPage, condition string, pars []interface{}) (count int64, results []SysAdminListTmpItem, err error) {
  78. query := global.DEFAULT_MYSQL.WithContext(context.TODO()).Model(a).
  79. Select("sys_admin.*, r.role_name").
  80. Joins("Left JOIN sys_role r ON r.role_id = sys_admin.role_id").
  81. Where(condition, pars...)
  82. query.Count(&count)
  83. if len(page.GetOrderItemsString()) > 0 {
  84. query = query.Order(page.GetOrderItemsString())
  85. }
  86. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  87. return
  88. }
  89. type SysAdminListReq struct {
  90. DeptId int64 `json:"dept_id" form:"dept_id"` //部门id
  91. KeyWord string `json:"key_word" form:"key_word"`
  92. Enabled string `json:"enabled" form:"enabled"` //-1全部,1:有效,0:禁用
  93. base.PageReq
  94. }
  95. type SysAdminListTmpItem struct {
  96. AdminId uint64 `json:"admin_id"` //账号id
  97. AdminName string `json:"admin_name"` //账号名
  98. RealName string `json:"real_name"` //真实姓名
  99. Enabled int8 `json:"enabled"` //1:有效,0:禁用
  100. Email string `json:"email"` //邮箱
  101. CreateTime time.Time `json:"create_time"` //创建时间
  102. ModifyTime time.Time `json:"modify_time"` //最后更新时间
  103. Mobile string `json:"mobile"` //手机号
  104. DeptId int64 `json:"dept_id"` //部门id
  105. RoleId int64 `json:"role_id"` //角色ID
  106. RoleName string `json:"role_name"` //角色名称
  107. DeptFullName string `json:"dept_full_name"` //部门全称
  108. Position string `json:"position"` //职位
  109. Remark string `json:"remark"` //备注
  110. }
  111. type SysAdminListItem struct {
  112. AdminId uint64 `json:"admin_id"` //账号id
  113. AdminName string `json:"admin_name"` //账号名
  114. RealName string `json:"real_name"` //真实姓名
  115. Enabled int8 `json:"enabled"` //1:有效,0:禁用
  116. Email string `json:"email"` //邮箱
  117. Mobile string `json:"mobile"` //手机号
  118. DeptId int64 `json:"dept_id"` //部门id
  119. RoleId int64 `json:"role_id"` //角色ID
  120. RoleName string `json:"role_name"` //角色名称
  121. DeptFullName string `json:"dept_full_name"` //部门全称
  122. CreateTime string `json:"create_time"` //创建时间
  123. ModifyTime string `json:"modify_time"` //最后更新时间
  124. Position string `json:"position"` //职位
  125. Remark string `json:"remark"` //备注
  126. }
  127. // GetAdminByAdminId 根据adminId获取用户信息
  128. func (a *SysAdmin) GetAdminByAdminId(adminId uint64) (item *SysAdmin, err error) {
  129. err = global.DEFAULT_MYSQL.Model(a).Where("admin_id = ? ", adminId).First(&item).Error
  130. return
  131. }
  132. type LoginReq struct {
  133. AdminName string `json:"admin_name" binding:"required"` //账号名
  134. Password string `json:"password" binding:"required"` //密码
  135. IsRemember bool `json:"is_remember"` //是否属于受信设备
  136. }
  137. type LoginResp struct {
  138. AdminId uint64 `json:"admin_id"` //账号id
  139. AdminName string `json:"admin_name"` //账号名
  140. RealName string `json:"real_name"` //真实姓名
  141. Token string `json:"token"` //登录token
  142. ChangePwd bool `json:"change_pwd"` //是否需要修改密码
  143. }
  144. type ModifyPwdReq struct {
  145. AdminId uint64 `json:"admin_id" binding:"required,gte=1"` //账号ID
  146. Pwd string `json:"pwd" binding:"required"` //原密码
  147. ConfirmPwd string `json:"confirm_pwd" binding:"required,eqcsfield=Pwd"` //新密码
  148. }
  149. type ModifyMyPwdReq struct {
  150. OldPwd string `json:"old_pwd" binding:"required"` //原密码
  151. NewPwd string `json:"new_pwd" binding:"required"` //新密码
  152. ConfirmPwd string `json:"confirm_pwd" binding:"required,eqcsfield=NewPwd"` //新密码确认
  153. }
  154. type ModifyMyInitPwdReq struct {
  155. NewPwd string `json:"new_pwd" binding:"required"` //新密码
  156. ConfirmPwd string `json:"confirm_pwd" binding:"required,eqcsfield=NewPwd"` //新密码确认
  157. }
  158. type SysAdminReq struct {
  159. AdminId uint64 `json:"admin_id" binding:"required,gte=1"` //账号ID
  160. }
  161. type SysAdminModifyEnabledReq struct {
  162. AdminId uint64 `json:"admin_id" binding:"required,gte=1"` //账号ID
  163. Enabled int8 `json:"enabled" binding:"oneof=0 1"` //1:有效,0:禁用
  164. }
  165. // 删除
  166. func (a *SysAdmin) Delete() (err error) {
  167. err = global.DEFAULT_MYSQL.Delete(a).Error
  168. return
  169. }
  170. func (a *SysAdmin) GetAdminListByCondition(condition string, pars []interface{}) (list []*SysAdmin, err error) {
  171. err = global.DEFAULT_MYSQL.WithContext(context.TODO()).Model(a).
  172. Where(condition, pars...).Order("admin_id desc").Find(&list).Error
  173. return
  174. }