sys_user.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. type LoginReq struct {
  10. Username string `description:"账号"`
  11. Password string `description:"密码"`
  12. IsRemember bool `description:"是否属于受信设备"`
  13. }
  14. type LoginResp struct {
  15. Authorization string
  16. AdminName string `description:"系统用户名称"`
  17. RealName string `description:"系统用户姓名"`
  18. RoleName string `description:"角色名称"`
  19. RoleTypeCode string `description:"角色类型编码"`
  20. SysRoleTypeCode string `description:"角色类型编码"`
  21. AdminId int `description:"系统用户id"`
  22. ProductName string `description:"产品名称:admin,ficc,权益"`
  23. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  24. }
  25. type Admin struct {
  26. //AdminId int `grm:"column:admin_id;identity:true" description:"系统用户id"`
  27. AdminId int `gorm:"primaryKey;autoIncrement;column:admin_id"`
  28. AdminName string `description:"系统用户名称"`
  29. AdminAvatar string `description:"用户头像"`
  30. RealName string `description:"系统用户姓名"`
  31. Password string `json:"-"`
  32. LastUpdatedPasswordTime string `json:"-"`
  33. Enabled int
  34. Email string `description:"系统用户邮箱"`
  35. LastLoginTime string
  36. CreatedTime time.Time
  37. LastUpdatedTime string
  38. Role string `description:"系统用户角色"`
  39. Mobile string `description:"手机号"`
  40. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  41. RoleId int `description:"角色ID"`
  42. RoleName string `description:"角色名称"`
  43. RoleTypeCode string `description:"角色类型编码"`
  44. DepartmentId int `description:"部门id"`
  45. DepartmentName string `description:"部门名称"`
  46. GroupId int `description:"分组id"`
  47. GroupName string `description:"分组名称"`
  48. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,3:超级管理员,4:ficc销售组长"`
  49. Position string `description:"职位"`
  50. DisableTime time.Time `description:"禁用时间"`
  51. ChartPermission int8 `description:"图表权限id"`
  52. OpenId string `description:"国能部门公众号的openid"`
  53. UnionId string `description:"微信公众平台唯一标识"`
  54. EdbPermission int8 `description:"指标库操作权限,0:只能操作 自己的,1:所有指标可操作"`
  55. MysteelChemicalPermission int8 `description:"钢联化工指标操作权限,0:只能操作 自己的,1:所有指标可操作"`
  56. PredictEdbPermission int8 `description:"预测指标库操作权限,0:只能操作 自己的,1:所有预测指标可操作"`
  57. Province string `description:"省"`
  58. ProvinceCode string `description:"省编码"`
  59. City string `description:"市"`
  60. CityCode string `description:"市编码"`
  61. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  62. TelAreaCode string `description:"手机区号"`
  63. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  64. }
  65. // Update 更新用户基础信息
  66. func (item *Admin) Update(cols []string) (err error) {
  67. err = global.DEFAULT_DmSQL.Select(cols).Updates(*item).Error
  68. return
  69. }
  70. func CheckSysUser(userName, password string) (item *Admin, err error) {
  71. sql := ` SELECT a.*,b.role_type_code FROM "admin" AS a
  72. INNER JOIN sys_role AS b ON a.role_id=b.role_id WHERE a.admin_name=? AND a.password=? LIMIT 1`
  73. err = global.DEFAULT_DmSQL.Raw(sql, userName, password).First(&item).Error
  74. return
  75. }
  76. func GetSysUserById(sysUserId int) (item *Admin, err error) {
  77. sql := `SELECT * FROM "admin" WHERE admin_id=? LIMIT 1`
  78. err = global.DEFAULT_DmSQL.Raw(sql, sysUserId).First(&item).Error
  79. return
  80. }
  81. func ModifyPwd(sysUserId int, newPwd string) (err error) {
  82. sql := `UPDATE "admin" SET password=?,last_updated_time=NOW() WHERE admin_id=? `
  83. err = global.DEFAULT_DmSQL.Exec(sql, newPwd, sysUserId).Error
  84. return
  85. }
  86. type ModifyPwdReq struct {
  87. OldPwd string `description:"旧密码"`
  88. NewPwd string `description:"新密码"`
  89. }
  90. // AdminWxUserRelation 管理员-微信用户关联
  91. type AdminWxUserRelation struct {
  92. AdminId int `json:"admin_id"`
  93. AdminName string `json:"admin_name"`
  94. RealName string `json:"real_name"`
  95. UserId int `json:"user_id"`
  96. }
  97. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  98. func GetAdminListByIdList(idList []int) (items []*Admin, err error) {
  99. lenNum := len(idList)
  100. if lenNum <= 0 {
  101. return
  102. }
  103. sql := `SELECT * FROM "admin" WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) and enabled=1 `
  104. err = global.DEFAULT_DmSQL.Raw(sql, idList).Find(&items).Error
  105. return
  106. }
  107. // GetAdminListByIdListWithoutEnable 根据用户id列表获取系统用户列表
  108. func GetAdminListByIdListWithoutEnable(idList []int) (items []*Admin, err error) {
  109. lenNum := len(idList)
  110. if lenNum <= 0 {
  111. return
  112. }
  113. sql := `SELECT * FROM "admin" WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) `
  114. err = global.DEFAULT_DmSQL.Raw(sql, idList).Find(&items).Error
  115. return
  116. }
  117. func (item *Admin) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  118. sql := fmt.Sprintf(`SELECT COUNT(1) FROM "admin" WHERE 1=1 %s`, condition)
  119. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  120. return
  121. }
  122. func (item *Admin) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  123. fields := strings.Join(fieldArr, ",")
  124. if len(fieldArr) == 0 {
  125. fields = `*`
  126. }
  127. order := `ORDER BY created_time DESC`
  128. if orderRule != "" {
  129. order = ` ORDER BY ` + orderRule
  130. }
  131. sql := fmt.Sprintf(`SELECT %s FROM "admin" WHERE 1=1 %s %s`, fields, condition, order)
  132. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  133. return
  134. }
  135. // GetSysUserByMobile 手机号获取用户
  136. func GetSysUserByMobile(mobile string) (item *Admin, err error) {
  137. sql := `SELECT
  138. a.*, b.role_type_code
  139. FROM
  140. "admin" AS a
  141. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  142. WHERE
  143. a.mobile = ?
  144. LIMIT 1`
  145. err = global.DEFAULT_DmSQL.Raw(sql, mobile).First(&item).Error
  146. return
  147. }
  148. // GetSysUserByEmail 邮箱获取用户
  149. func GetSysUserByEmail(email string) (item *Admin, err error) {
  150. sql := `SELECT
  151. a.*, b.role_type_code
  152. FROM
  153. "admin" AS a
  154. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  155. WHERE
  156. a.email = ?
  157. LIMIT 1`
  158. err = global.DEFAULT_DmSQL.Raw(sql, email).First(&item).Error
  159. return
  160. }
  161. // GetSysUserByAdminName 账号获取用户
  162. func GetSysUserByAdminName(adminName string) (item *Admin, err error) {
  163. o := global.DEFAULT_DmSQL
  164. sql := `SELECT
  165. a.*, b.role_type_code
  166. FROM
  167. "admin" AS a
  168. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  169. WHERE
  170. a.admin_name = ?
  171. LIMIT 1`
  172. err = o.Raw(sql, adminName).Find(&item).Error
  173. return
  174. }