sys_user.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package system
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  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 `orm:"column(admin_id);pk" description:"系统用户id"`
  27. AdminName string `description:"系统用户名称"`
  28. AdminAvatar string `description:"用户头像"`
  29. RealName string `description:"系统用户姓名"`
  30. Password string `json:"-"`
  31. LastUpdatedPasswordTime string `json:"-"`
  32. Enabled int
  33. Email string `description:"系统用户邮箱"`
  34. LastLoginTime string
  35. CreatedTime time.Time
  36. LastUpdatedTime string
  37. Role string `description:"系统用户角色"`
  38. Mobile string `description:"手机号"`
  39. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  40. RoleId int `description:"角色ID"`
  41. RoleName string `description:"角色名称"`
  42. RoleTypeCode string `description:"角色类型编码"`
  43. DepartmentId int `description:"部门id"`
  44. DepartmentName string `description:"部门名称"`
  45. GroupId int `description:"分组id"`
  46. GroupName string `description:"分组名称"`
  47. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,3:超级管理员,4:ficc销售组长"`
  48. Position string `description:"职位"`
  49. DisableTime time.Time `description:"禁用时间"`
  50. ChartPermission int8 `description:"图表权限id"`
  51. OpenId string `description:"弘则部门公众号的openid"`
  52. UnionId string `description:"微信公众平台唯一标识"`
  53. EdbPermission int8 `description:"指标库操作权限,0:只能操作 自己的,1:所有指标可操作"`
  54. MysteelChemicalPermission int8 `description:"钢联化工指标操作权限,0:只能操作 自己的,1:所有指标可操作"`
  55. PredictEdbPermission int8 `description:"预测指标库操作权限,0:只能操作 自己的,1:所有预测指标可操作"`
  56. Province string `description:"省"`
  57. ProvinceCode string `description:"省编码"`
  58. City string `description:"市"`
  59. CityCode string `description:"市编码"`
  60. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  61. TelAreaCode string `description:"手机区号"`
  62. }
  63. // Update 更新用户基础信息
  64. func (item *Admin) Update(cols []string) (err error) {
  65. o := orm.NewOrm()
  66. _, err = o.Update(item, cols...)
  67. return
  68. }
  69. func CheckSysUser(userName, password string) (item *Admin, err error) {
  70. sql := ` SELECT a.*,b.role_type_code FROM admin AS a
  71. INNER JOIN sys_role AS b ON a.role_id=b.role_id WHERE a.admin_name=? AND a.password=? LIMIT 1`
  72. o := orm.NewOrm()
  73. err = o.Raw(sql, userName, password).QueryRow(&item)
  74. return
  75. }
  76. func GetSysUserById(sysUserId int) (item *Admin, err error) {
  77. sql := `SELECT * FROM admin WHERE admin_id=? LIMIT 1`
  78. o := orm.NewOrm()
  79. err = o.Raw(sql, sysUserId).QueryRow(&item)
  80. return
  81. }
  82. // 根据权限code获取系统用户列表
  83. func GetAdminListByRoleCode(roleTypeCode string) (items []*Admin, err error) {
  84. sql := `SELECT * FROM admin WHERE role_type_code=? and enabled=1 `
  85. o := orm.NewOrm()
  86. _, err = o.Raw(sql, roleTypeCode).QueryRows(&items)
  87. return
  88. }
  89. // 根据权限id获取系统用户列表
  90. func GetAdminListByRoleId(roleId string) (items []*Admin, err error) {
  91. sql := `SELECT * FROM admin WHERE role_id=? and enabled=1 `
  92. o := orm.NewOrm()
  93. _, err = o.Raw(sql, roleId).QueryRows(&items)
  94. return
  95. }
  96. // GetAdminListByIds 根据用户id字符串获取系统用户列表
  97. func GetAdminListByIds(ids string) (items []*Admin, err error) {
  98. sql := `SELECT * FROM admin WHERE admin_id in (` + ids + `) and enabled=1 `
  99. o := orm.NewOrm()
  100. _, err = o.Raw(sql).QueryRows(&items)
  101. return
  102. }
  103. func ModifyPwd(sysUserId int, newPwd string) (err error) {
  104. sql := `UPDATE admin SET password=?,last_updated_time=NOW() WHERE admin_id=? `
  105. o := orm.NewOrm()
  106. _, err = o.Raw(sql, newPwd, sysUserId).Exec()
  107. return
  108. }
  109. type ModifyPwdReq struct {
  110. OldPwd string `description:"旧密码"`
  111. NewPwd string `description:"新密码"`
  112. }
  113. func GetAdminList() (items []*Admin, err error) {
  114. sql := `SELECT * FROM admin`
  115. o := orm.NewOrm()
  116. _, err = o.Raw(sql).QueryRows(&items)
  117. return
  118. }
  119. // GetSysUserByMobile 根据手机号获取管理信息
  120. //func GetSysUserByMobile(mobile string) (item *Admin, err error) {
  121. // sql := `SELECT * FROM admin WHERE mobile = ? LIMIT 1`
  122. // o := orm.NewOrm()
  123. // err = o.Raw(sql, mobile).QueryRow(&item)
  124. // return
  125. //}
  126. // 通过用户姓名跟身份获取管理员信息
  127. func CheckSysUserByName(userName, roleTypeCode string) (item *Admin, err error) {
  128. sql := ` SELECT a.* FROM admin AS a WHERE a.real_name=? AND a.role_type_code=? LIMIT 1`
  129. o := orm.NewOrm()
  130. err = o.Raw(sql, userName, roleTypeCode).QueryRow(&item)
  131. return
  132. }
  133. // GetAdminListByGroupId 根据分组id获取系统用户列表
  134. func GetAdminListByGroupId(groupId int) (items []*Admin, err error) {
  135. sql := `SELECT * FROM admin WHERE group_id=? and enabled=1 `
  136. o := orm.NewOrm()
  137. _, err = o.Raw(sql, groupId).QueryRows(&items)
  138. return
  139. }
  140. // AdminWxUserRelation 管理员-微信用户关联
  141. type AdminWxUserRelation struct {
  142. AdminId int `json:"admin_id"`
  143. AdminName string `json:"admin_name"`
  144. RealName string `json:"real_name"`
  145. UserId int `json:"user_id"`
  146. }
  147. // GetAdminWxUserRelationByWxUserIds 通过微信用户及平台获取后台关联信息
  148. func GetAdminWxUserRelationByWxUserIds(platform int, userIds []int) (list []*AdminWxUserRelation, err error) {
  149. userLen := len(userIds)
  150. if userLen == 0 {
  151. return
  152. }
  153. o := orm.NewOrm()
  154. sql := `SELECT
  155. ad.admin_id, ad.admin_name, ad.real_name, wu.user_id
  156. FROM
  157. admin AS ad
  158. JOIN wx_user AS wu ON ad.mobile = wu.mobile
  159. JOIN user_record AS ur ON wu.user_id = ur.user_id AND ur.create_platform = ?
  160. WHERE
  161. wu.user_id IN (` + utils.GetOrmInReplace(userLen) + `)`
  162. _, err = o.Raw(sql, platform, userIds).QueryRows(&list)
  163. return
  164. }
  165. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  166. func GetAdminListByIdList(idList []int) (items []*Admin, err error) {
  167. lenNum := len(idList)
  168. if lenNum <= 0 {
  169. return
  170. }
  171. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) and enabled=1 `
  172. o := orm.NewOrm()
  173. _, err = o.Raw(sql, idList).QueryRows(&items)
  174. return
  175. }
  176. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  177. func GetAdminListByIdListWithoutEnable(idList []int) (items []*Admin, err error) {
  178. lenNum := len(idList)
  179. if lenNum <= 0 {
  180. return
  181. }
  182. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) `
  183. o := orm.NewOrm()
  184. _, err = o.Raw(sql, idList).QueryRows(&items)
  185. return
  186. }
  187. func (item *Admin) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  188. o := orm.NewOrm()
  189. sql := fmt.Sprintf(`SELECT COUNT(1) FROM admin WHERE 1=1 %s`, condition)
  190. err = o.Raw(sql, pars).QueryRow(&count)
  191. return
  192. }
  193. func (item *Admin) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  194. o := orm.NewOrm()
  195. fields := strings.Join(fieldArr, ",")
  196. if len(fieldArr) == 0 {
  197. fields = `*`
  198. }
  199. order := `ORDER BY created_time DESC`
  200. if orderRule != "" {
  201. order = ` ORDER BY ` + orderRule
  202. }
  203. sql := fmt.Sprintf(`SELECT %s FROM admin WHERE 1=1 %s %s`, fields, condition, order)
  204. _, err = o.Raw(sql, pars).QueryRows(&items)
  205. return
  206. }
  207. // GetSysUserByMobile 手机号获取用户
  208. func GetSysUserByMobile(mobile string) (item *Admin, err error) {
  209. o := orm.NewOrm()
  210. sql := `SELECT
  211. a.*, b.role_type_code
  212. FROM
  213. admin AS a
  214. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  215. WHERE
  216. a.mobile = ?
  217. LIMIT 1`
  218. err = o.Raw(sql, mobile).QueryRow(&item)
  219. return
  220. }
  221. // GetSysUserByEmail 邮箱获取用户
  222. func GetSysUserByEmail(email string) (item *Admin, err error) {
  223. o := orm.NewOrm()
  224. sql := `SELECT
  225. a.*, b.role_type_code
  226. FROM
  227. admin AS a
  228. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  229. WHERE
  230. a.email = ?
  231. LIMIT 1`
  232. err = o.Raw(sql, email).QueryRow(&item)
  233. return
  234. }
  235. // GetSysUserByAdminName 账号获取用户
  236. func GetSysUserByAdminName(adminName string) (item *Admin, err error) {
  237. o := orm.NewOrm()
  238. sql := `SELECT
  239. a.*, b.role_type_code
  240. FROM
  241. admin AS a
  242. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  243. WHERE
  244. a.admin_name = ?
  245. LIMIT 1`
  246. err = o.Raw(sql, adminName).QueryRow(&item)
  247. return
  248. }