sys_user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package service
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service/dto"
  5. "cold-delivery/common/actions"
  6. cDto "cold-delivery/common/dto"
  7. "cold-delivery/common/global"
  8. "errors"
  9. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
  11. "gogs.baozhida.cn/zoie/OAuth-core/service"
  12. "gorm.io/gorm"
  13. )
  14. type SysUser struct {
  15. service.Service
  16. }
  17. // GetPage 获取SysUser列表
  18. func (e *SysUser) GetPage(c *dto.SysUserGetPageReq, p *actions.DataPermission, list *[]model.SysUser, count *int64) error {
  19. var err error
  20. var data model.SysUser
  21. var userList []model.SysUser
  22. if p.DeptId == 0 {
  23. err = e.Orm.
  24. Scopes(
  25. cDto.MakeCondition(c.GetNeedSearch()),
  26. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  27. ).Where("create_by = ?", p.UserId).
  28. Where("user_type = ?", model.UserTypeSys).
  29. Preload("Dept").
  30. Find(&userList).Limit(-1).Offset(-1).
  31. Count(count).Error
  32. } else {
  33. err = e.Orm.
  34. Scopes(
  35. cDto.MakeCondition(c.GetNeedSearch()),
  36. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  37. actions.UserPermission(data.TableName(), p),
  38. ).
  39. Where("user_type = ?", model.UserTypeSys).
  40. Preload("Dept").
  41. Find(&userList).Limit(-1).Offset(-1).
  42. Count(count).Error
  43. }
  44. if err != nil {
  45. e.Log.Errorf("db error: %s", err)
  46. return global.GetFailedErr
  47. }
  48. *list = userList
  49. return nil
  50. }
  51. func GetSysUserCompanyIdScopes(companyId int) func(db *gorm.DB) *gorm.DB {
  52. return func(db *gorm.DB) *gorm.DB {
  53. if companyId == 0 {
  54. return db
  55. }
  56. return db.Where("dept_id = ?", companyId)
  57. }
  58. }
  59. func (e *SysUser) GetPageByDeptId(c *dto.SysUserGetPageByDeptIdReq, p *actions.DataPermission, list *[]model.SysUser, count *int64) error {
  60. var err error
  61. var userList []model.SysUser
  62. var operatingUser model.SysUser
  63. err = e.Orm.Model(&operatingUser).
  64. Scopes(actions.UserPermission(operatingUser.TableName(), p)).
  65. Find(&operatingUser, p.UserId).Error
  66. if err != nil {
  67. e.Log.Errorf("获取操作用户信息失败: %s", err)
  68. return errors.New("获取操作用户信息失败")
  69. }
  70. if operatingUser.Type != model.SysUserTypeSys {
  71. return nil
  72. }
  73. err = e.Orm.
  74. Scopes(
  75. cDto.MakeCondition(c.GetNeedSearch()),
  76. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  77. GetSysUserCompanyIdScopes(c.CompanyId),
  78. ).
  79. Where("user_type = ?", model.UserTypeSys).
  80. Preload("Dept").
  81. Preload("CreateUser").
  82. Find(&userList).Limit(-1).Offset(-1).
  83. Count(count).Error
  84. if err != nil {
  85. e.Log.Errorf("db error: %s", err)
  86. return global.GetFailedErr
  87. }
  88. *list = userList
  89. return nil
  90. }
  91. // Get 获取SysUser对象
  92. func (e *SysUser) Get(d *dto.SysUserGetReq, p *actions.DataPermission, userModel *model.SysUser) error {
  93. var data model.SysUser
  94. err := e.Orm.Model(&data).
  95. Scopes(actions.UserPermission(data.TableName(), p)).
  96. Preload("Dept").
  97. First(userModel, d.GetId()).Error
  98. if err != nil {
  99. e.Log.Errorf("db error: %s", err)
  100. if errors.Is(err, gorm.ErrRecordNotFound) {
  101. return global.GetNotFoundOrNoPermissionErr
  102. }
  103. return global.GetFailedErr
  104. }
  105. return nil
  106. }
  107. func IdScopesUser(id int) func(db *gorm.DB) *gorm.DB {
  108. return func(db *gorm.DB) *gorm.DB {
  109. if id == 0 {
  110. return db
  111. }
  112. return db.Where("id = ? ", id)
  113. }
  114. }
  115. func (e *SysUser) GetUser(id int, userModel *model.SysUser) error {
  116. var data model.SysUser
  117. var err error
  118. err = e.Orm.Model(&data).
  119. Scopes(IdScopesUser(id)).
  120. First(userModel).Error
  121. if err != nil {
  122. e.Log.Errorf("db error: %s", err)
  123. if errors.Is(err, gorm.ErrRecordNotFound) {
  124. return global.GetNotFoundOrNoPermissionErr
  125. }
  126. return global.GetFailedErr
  127. }
  128. return nil
  129. }
  130. // Insert 创建SysUser对象
  131. func (e *SysUser) Insert(c *dto.SysUserInsertReq, p *actions.DataPermission) error {
  132. var err error
  133. tx := e.Orm.Begin()
  134. defer func() {
  135. if err != nil {
  136. tx.Rollback()
  137. } else {
  138. tx.Commit()
  139. }
  140. }()
  141. var dept model.SysDept
  142. var deptCount int64
  143. err = e.Orm.Model(&dept).Where("id = ?", c.DeptId).Count(&deptCount).Error
  144. if err != nil {
  145. e.Log.Errorf("db error: %s", err)
  146. return global.CreateFailedErr
  147. }
  148. if deptCount == 0 {
  149. err = errors.New("公司不存在!")
  150. e.Log.Errorf("db error: %s", err)
  151. return err
  152. }
  153. var operatingUser model.SysUser
  154. err = e.Orm.Model(&operatingUser).
  155. Scopes(actions.UserPermission(operatingUser.TableName(), p)).
  156. Find(&operatingUser, p.UserId).Error
  157. if err != nil {
  158. e.Log.Errorf("获取操作用户信息失败: %s", err)
  159. return errors.New("获取操作用户信息失败")
  160. }
  161. //if (operatingUser.Type != model.SysUserTypeSys) || (operatingUser.Id != 1) {
  162. // err = errors.New("无权添加用户,请联系管理员!")
  163. // return err
  164. //}
  165. if operatingUser.Type != model.SysUserTypeSys {
  166. err = errors.New("无权添加用户,请联系管理员!")
  167. return err
  168. }
  169. var data model.SysUser
  170. var i int64
  171. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  172. if err != nil {
  173. e.Log.Errorf("db error: %s", err)
  174. return global.CreateFailedErr
  175. }
  176. if i > 0 {
  177. err = errors.New("用户名已存在!")
  178. e.Log.Errorf("db error: %s", err)
  179. return err
  180. }
  181. var uuid string
  182. for {
  183. uuid = utils.GetUUID()
  184. var j int64
  185. err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error
  186. if err != nil {
  187. continue
  188. }
  189. if j == 0 {
  190. break
  191. }
  192. }
  193. c.Generate(&data)
  194. data.Uuid = uuid
  195. err = tx.Create(&data).Error
  196. if err != nil {
  197. e.Log.Errorf("db error: %s", err)
  198. return global.CreateFailedErr
  199. }
  200. c.Id = data.Id
  201. return nil
  202. }
  203. // Update 修改SysUser对象
  204. func (e *SysUser) Update(c *dto.SysUserUpdateReq, p *actions.DataPermission) error {
  205. var err error
  206. tx := e.Orm.Begin()
  207. defer func() {
  208. if err != nil {
  209. tx.Rollback()
  210. } else {
  211. tx.Commit()
  212. }
  213. }()
  214. var userModel model.SysUser
  215. err = e.Orm.
  216. Scopes(actions.UserPermission(userModel.TableName(), p)).
  217. First(&userModel, c.GetId()).Error
  218. if err != nil {
  219. e.Log.Errorf("db error: %s", err)
  220. if errors.Is(err, gorm.ErrRecordNotFound) {
  221. return global.UpdateNotFoundOrNoPermissionErr
  222. }
  223. return global.UpdateFailedErr
  224. }
  225. c.Generate(&userModel)
  226. err = tx.Model(&userModel).Where("id = ?", &userModel.Id).
  227. Omit("password", "salt").Updates(&userModel).Error
  228. if err != nil {
  229. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  230. return model.ErrForbidUpdateSysRole
  231. }
  232. e.Log.Errorf("db error: %s", err)
  233. return global.UpdateFailedErr
  234. }
  235. return nil
  236. }
  237. // UpdateStatus 更新用户状态
  238. func (e *SysUser) UpdateStatus(c *dto.UpdateSysUserStatusReq, p *actions.DataPermission) error {
  239. var userModel model.SysUser
  240. err := e.Orm.
  241. Scopes(actions.UserPermission(userModel.TableName(), p)).
  242. First(&userModel, c.GetId()).Error
  243. if err != nil {
  244. e.Log.Errorf("db error: %s", err)
  245. if errors.Is(err, gorm.ErrRecordNotFound) {
  246. return global.UpdateNotFoundOrNoPermissionErr
  247. }
  248. return global.UpdateFailedErr
  249. }
  250. err = e.Orm.Table(userModel.TableName()).Where("id = ? ", c.Id).Updates(c).Error
  251. if err != nil {
  252. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  253. return model.ErrForbidUpdateSysRole
  254. }
  255. e.Log.Errorf("db error: %s", err)
  256. return global.UpdateFailedErr
  257. }
  258. return nil
  259. }
  260. // ResetPwd 重置用户密码
  261. func (e *SysUser) ResetPwd(c *dto.ResetSysUserPwdReq, p *actions.DataPermission) error {
  262. var userModel model.SysUser
  263. err := e.Orm.
  264. Scopes(actions.UserPermission(userModel.TableName(), p)).
  265. First(&userModel, c.GetId()).Error
  266. if err != nil {
  267. e.Log.Errorf("db error: %s", err)
  268. if errors.Is(err, gorm.ErrRecordNotFound) {
  269. return global.UpdateNotFoundOrNoPermissionErr
  270. }
  271. return global.UpdateFailedErr
  272. }
  273. c.Generate(&userModel)
  274. err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&userModel).Error
  275. if err != nil {
  276. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  277. return model.ErrForbidUpdateSysRole
  278. }
  279. e.Log.Errorf("db error: %s", err)
  280. return global.UpdateFailedErr
  281. }
  282. return nil
  283. }
  284. // Remove 删除SysUser
  285. func (e *SysUser) Remove(c *dto.SysUserDeleteReq, p *actions.DataPermission) error {
  286. var err error
  287. tx := e.Orm.Begin()
  288. defer func() {
  289. if err != nil {
  290. tx.Rollback()
  291. } else {
  292. tx.Commit()
  293. }
  294. }()
  295. var operatingUser model.SysUser
  296. err = e.Orm.Model(&operatingUser).
  297. Scopes(actions.UserPermission(operatingUser.TableName(), p)).
  298. Find(&operatingUser, p.UserId).Error
  299. if err != nil {
  300. e.Log.Errorf("获取操作用户信息失败: %s", err)
  301. return errors.New("获取操作用户信息失败")
  302. }
  303. var data model.SysUser
  304. err = e.Orm.Model(&data).
  305. Scopes(actions.UserPermission(data.TableName(), p)).
  306. Find(&data, c.GetId()).Error
  307. if err != nil {
  308. e.Log.Errorf("获取操作用户信息失败: %s", err)
  309. return errors.New("获取操作用户信息失败")
  310. }
  311. if p.UserId != 1 {
  312. if data.CreateBy == 1 && data.Type == model.SysUserTypeSys {
  313. return errors.New("禁止删除管理员")
  314. }
  315. }
  316. // 管理员1 门店管理员2 配送员3
  317. if operatingUser.Type != model.SysUserTypeSys && data.Type == model.SysUserTypeSys {
  318. return errors.New("禁止删除管理员")
  319. }
  320. if err = tx.Delete(&data, c.GetId()).Error; err != nil {
  321. if errors.Is(err, model.ErrForbidDeleteSysRole) {
  322. return model.ErrForbidDeleteSysRole
  323. }
  324. e.Log.Errorf("db error: %s", err)
  325. return err
  326. }
  327. return nil
  328. }
  329. // UpdatePwd 修改SysUser对象密码
  330. func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string) error {
  331. var err error
  332. if newPassword == "" {
  333. return nil
  334. }
  335. c := &model.SysUser{}
  336. err = e.Orm.Model(c).
  337. Select("id", "password", "salt").
  338. First(c, id).Error
  339. if err != nil {
  340. e.Log.Errorf("db error: %s", err)
  341. if errors.Is(err, gorm.ErrRecordNotFound) {
  342. return global.UpdateNotFoundOrNoPermissionErr
  343. }
  344. return errors.New("密码修改失败")
  345. }
  346. var ok bool
  347. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  348. if err != nil {
  349. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  350. return errors.New("密码修改失败")
  351. }
  352. if !ok {
  353. err = errors.New("incorrect Password")
  354. e.Log.Warnf("user[%d] %s", id, err.Error())
  355. return err
  356. }
  357. c.Password = newPassword
  358. db := e.Orm.Model(c).Where("id = ?", id).
  359. Select("Password", "Salt").
  360. Updates(c)
  361. if err = db.Error; err != nil {
  362. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  363. return model.ErrForbidUpdateSysRole
  364. }
  365. e.Log.Errorf("db error: %s", err)
  366. return errors.New("密码修改失败")
  367. }
  368. return nil
  369. }
  370. func (e *SysUser) GetProfile(c *dto.SysUserById, user *model.SysUser) error {
  371. err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
  372. if err != nil {
  373. return err
  374. }
  375. return nil
  376. }
  377. // GetUserProfile 获取当前用户权限
  378. func GetUserProfile(e *gorm.DB, userid int) (err error, user model.SysUser) {
  379. err = e.Preload("Dept").First(&user, userid).Error
  380. if err != nil {
  381. if errors.Is(err, gorm.ErrRecordNotFound) {
  382. return errors.New("User not found"), user
  383. }
  384. return err, user
  385. }
  386. return nil, user
  387. }