sys_user.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. // Insert 创建SysUser对象
  108. func (e *SysUser) Insert(c *dto.SysUserInsertReq, p *actions.DataPermission) error {
  109. var err error
  110. tx := e.Orm.Begin()
  111. defer func() {
  112. if err != nil {
  113. tx.Rollback()
  114. } else {
  115. tx.Commit()
  116. }
  117. }()
  118. var dept model.SysDept
  119. var deptCount int64
  120. err = e.Orm.Model(&dept).Where("id = ?", c.DeptId).Count(&deptCount).Error
  121. if err != nil {
  122. e.Log.Errorf("db error: %s", err)
  123. return global.CreateFailedErr
  124. }
  125. if deptCount == 0 {
  126. err = errors.New("公司不存在!")
  127. e.Log.Errorf("db error: %s", err)
  128. return err
  129. }
  130. var operatingUser model.SysUser
  131. err = e.Orm.Model(&operatingUser).
  132. Scopes(actions.UserPermission(operatingUser.TableName(), p)).
  133. Find(&operatingUser, p.UserId).Error
  134. if err != nil {
  135. e.Log.Errorf("获取操作用户信息失败: %s", err)
  136. return errors.New("获取操作用户信息失败")
  137. }
  138. if operatingUser.Type != model.SysUserTypeSys || operatingUser.Id != 1 {
  139. err = errors.New("无权添加用户,请联系管理员!")
  140. return err
  141. }
  142. var data model.SysUser
  143. var i int64
  144. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  145. if err != nil {
  146. e.Log.Errorf("db error: %s", err)
  147. return global.CreateFailedErr
  148. }
  149. if i > 0 {
  150. err = errors.New("用户名已存在!")
  151. e.Log.Errorf("db error: %s", err)
  152. return err
  153. }
  154. var uuid string
  155. for {
  156. uuid = utils.GetUUID()
  157. var j int64
  158. err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error
  159. if err != nil {
  160. continue
  161. }
  162. if j == 0 {
  163. break
  164. }
  165. }
  166. c.Generate(&data)
  167. data.Uuid = uuid
  168. err = tx.Create(&data).Error
  169. if err != nil {
  170. e.Log.Errorf("db error: %s", err)
  171. return global.CreateFailedErr
  172. }
  173. c.Id = data.Id
  174. return nil
  175. }
  176. // Update 修改SysUser对象
  177. func (e *SysUser) Update(c *dto.SysUserUpdateReq, p *actions.DataPermission) error {
  178. var err error
  179. tx := e.Orm.Begin()
  180. defer func() {
  181. if err != nil {
  182. tx.Rollback()
  183. } else {
  184. tx.Commit()
  185. }
  186. }()
  187. var userModel model.SysUser
  188. err = e.Orm.
  189. Scopes(actions.UserPermission(userModel.TableName(), p)).
  190. First(&userModel, c.GetId()).Error
  191. if err != nil {
  192. e.Log.Errorf("db error: %s", err)
  193. if errors.Is(err, gorm.ErrRecordNotFound) {
  194. return global.UpdateNotFoundOrNoPermissionErr
  195. }
  196. return global.UpdateFailedErr
  197. }
  198. c.Generate(&userModel)
  199. err = tx.Model(&userModel).Where("id = ?", &userModel.Id).
  200. Omit("password", "salt").Updates(&userModel).Error
  201. if err != nil {
  202. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  203. return model.ErrForbidUpdateSysRole
  204. }
  205. e.Log.Errorf("db error: %s", err)
  206. return global.UpdateFailedErr
  207. }
  208. return nil
  209. }
  210. // UpdateStatus 更新用户状态
  211. func (e *SysUser) UpdateStatus(c *dto.UpdateSysUserStatusReq, p *actions.DataPermission) error {
  212. var userModel model.SysUser
  213. err := e.Orm.
  214. Scopes(actions.UserPermission(userModel.TableName(), p)).
  215. First(&userModel, c.GetId()).Error
  216. if err != nil {
  217. e.Log.Errorf("db error: %s", err)
  218. if errors.Is(err, gorm.ErrRecordNotFound) {
  219. return global.UpdateNotFoundOrNoPermissionErr
  220. }
  221. return global.UpdateFailedErr
  222. }
  223. err = e.Orm.Table(userModel.TableName()).Where("id = ? ", c.Id).Updates(c).Error
  224. if err != nil {
  225. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  226. return model.ErrForbidUpdateSysRole
  227. }
  228. e.Log.Errorf("db error: %s", err)
  229. return global.UpdateFailedErr
  230. }
  231. return nil
  232. }
  233. // ResetPwd 重置用户密码
  234. func (e *SysUser) ResetPwd(c *dto.ResetSysUserPwdReq, p *actions.DataPermission) error {
  235. var userModel model.SysUser
  236. err := e.Orm.
  237. Scopes(actions.UserPermission(userModel.TableName(), p)).
  238. First(&userModel, c.GetId()).Error
  239. if err != nil {
  240. e.Log.Errorf("db error: %s", err)
  241. if errors.Is(err, gorm.ErrRecordNotFound) {
  242. return global.UpdateNotFoundOrNoPermissionErr
  243. }
  244. return global.UpdateFailedErr
  245. }
  246. c.Generate(&userModel)
  247. err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&userModel).Error
  248. if err != nil {
  249. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  250. return model.ErrForbidUpdateSysRole
  251. }
  252. e.Log.Errorf("db error: %s", err)
  253. return global.UpdateFailedErr
  254. }
  255. return nil
  256. }
  257. // Remove 删除SysUser
  258. func (e *SysUser) Remove(c *dto.SysUserDeleteReq, p *actions.DataPermission) error {
  259. var err error
  260. tx := e.Orm.Begin()
  261. defer func() {
  262. if err != nil {
  263. tx.Rollback()
  264. } else {
  265. tx.Commit()
  266. }
  267. }()
  268. var operatingUser model.SysUser
  269. err = e.Orm.Model(&operatingUser).
  270. Scopes(actions.UserPermission(operatingUser.TableName(), p)).
  271. Find(&operatingUser, p.UserId).Error
  272. if err != nil {
  273. e.Log.Errorf("获取操作用户信息失败: %s", err)
  274. return errors.New("获取操作用户信息失败")
  275. }
  276. var data model.SysUser
  277. err = e.Orm.Model(&data).
  278. Scopes(actions.UserPermission(data.TableName(), p)).
  279. Find(&data, c.GetId()).Error
  280. if err != nil {
  281. e.Log.Errorf("获取操作用户信息失败: %s", err)
  282. return errors.New("获取操作用户信息失败")
  283. }
  284. if p.UserId != 1 {
  285. if data.CreateBy == 1 && data.Type == model.SysUserTypeSys {
  286. return errors.New("禁止删除管理员")
  287. }
  288. }
  289. // 管理员1 门店管理员2 配送员3
  290. if operatingUser.Type != model.SysUserTypeSys && data.Type == model.SysUserTypeSys {
  291. return errors.New("禁止删除管理员")
  292. }
  293. if err = tx.Delete(&data, c.GetId()).Error; err != nil {
  294. if errors.Is(err, model.ErrForbidDeleteSysRole) {
  295. return model.ErrForbidDeleteSysRole
  296. }
  297. e.Log.Errorf("db error: %s", err)
  298. return err
  299. }
  300. return nil
  301. }
  302. // UpdatePwd 修改SysUser对象密码
  303. func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string) error {
  304. var err error
  305. if newPassword == "" {
  306. return nil
  307. }
  308. c := &model.SysUser{}
  309. err = e.Orm.Model(c).
  310. Select("id", "password", "salt").
  311. First(c, id).Error
  312. if err != nil {
  313. e.Log.Errorf("db error: %s", err)
  314. if errors.Is(err, gorm.ErrRecordNotFound) {
  315. return global.UpdateNotFoundOrNoPermissionErr
  316. }
  317. return errors.New("密码修改失败")
  318. }
  319. var ok bool
  320. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  321. if err != nil {
  322. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  323. return errors.New("密码修改失败")
  324. }
  325. if !ok {
  326. err = errors.New("incorrect Password")
  327. e.Log.Warnf("user[%d] %s", id, err.Error())
  328. return err
  329. }
  330. c.Password = newPassword
  331. db := e.Orm.Model(c).Where("id = ?", id).
  332. Select("Password", "Salt").
  333. Updates(c)
  334. if err = db.Error; err != nil {
  335. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  336. return model.ErrForbidUpdateSysRole
  337. }
  338. e.Log.Errorf("db error: %s", err)
  339. return errors.New("密码修改失败")
  340. }
  341. return nil
  342. }
  343. func (e *SysUser) GetProfile(c *dto.SysUserById, user *model.SysUser) error {
  344. err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
  345. if err != nil {
  346. return err
  347. }
  348. return nil
  349. }