sys_user.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package service
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service/dto"
  5. "cold-logistics/common/actions"
  6. cDto "cold-logistics/common/dto"
  7. "cold-logistics/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. // Get 获取SysUser对象
  52. func (e *SysUser) Get(d *dto.SysUserGetReq, p *actions.DataPermission, userModel *model.SysUser) error {
  53. var data model.SysUser
  54. //var dept model.SysDept
  55. //var role model.SysRole
  56. //var post model.SysPost
  57. err := e.Orm.Model(&data).
  58. Scopes(actions.UserPermission(data.TableName(), p)).
  59. Preload("Dept").
  60. First(userModel, d.GetId()).Error
  61. if err != nil {
  62. e.Log.Errorf("db error: %s", err)
  63. if errors.Is(err, gorm.ErrRecordNotFound) {
  64. return global.GetNotFoundOrNoPermissionErr
  65. }
  66. return global.GetFailedErr
  67. }
  68. //err = e.Orm.First(&dept, userModel.DeptId).Error
  69. //userModel.Dept = dept
  70. //
  71. //err = e.Orm.First(&role, userModel.RoleId).Error
  72. //userModel.Role = role
  73. //err = e.Orm.First(&post, userModel.PostId).Error
  74. //userModel.Post = post
  75. return nil
  76. }
  77. // Insert 创建SysUser对象
  78. func (e *SysUser) Insert(c *dto.SysUserInsertReq) error {
  79. var err error
  80. tx := e.Orm.Begin()
  81. defer func() {
  82. if err != nil {
  83. tx.Rollback()
  84. } else {
  85. tx.Commit()
  86. }
  87. }()
  88. var dept model.SysDept
  89. var deptCount int64
  90. err = e.Orm.Model(&dept).Where("id = ?", c.DeptId).Count(&deptCount).Error
  91. if err != nil {
  92. e.Log.Errorf("db error: %s", err)
  93. return global.CreateFailedErr
  94. }
  95. if deptCount == 0 {
  96. err = errors.New("公司不存在!")
  97. e.Log.Errorf("db error: %s", err)
  98. return err
  99. }
  100. var data model.SysUser
  101. var i int64
  102. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  103. if err != nil {
  104. e.Log.Errorf("db error: %s", err)
  105. return global.CreateFailedErr
  106. }
  107. if i > 0 {
  108. err = errors.New("用户名已存在!")
  109. e.Log.Errorf("db error: %s", err)
  110. return err
  111. }
  112. var uuid string
  113. for {
  114. uuid = utils.GetUUID()
  115. var j int64
  116. err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error
  117. if err != nil {
  118. continue
  119. }
  120. if j == 0 {
  121. break
  122. }
  123. }
  124. c.Generate(&data)
  125. data.Uuid = uuid
  126. err = tx.Create(&data).Error
  127. if err != nil {
  128. e.Log.Errorf("db error: %s", err)
  129. return global.CreateFailedErr
  130. }
  131. c.Id = data.Id
  132. return nil
  133. }
  134. // Update 修改SysUser对象
  135. func (e *SysUser) Update(c *dto.SysUserUpdateReq, p *actions.DataPermission) error {
  136. var err error
  137. tx := e.Orm.Begin()
  138. defer func() {
  139. if err != nil {
  140. tx.Rollback()
  141. } else {
  142. tx.Commit()
  143. }
  144. }()
  145. var userModel model.SysUser
  146. err = e.Orm.
  147. Scopes(actions.UserPermission(userModel.TableName(), p)).
  148. First(&userModel, c.GetId()).Error
  149. if err != nil {
  150. e.Log.Errorf("db error: %s", err)
  151. if errors.Is(err, gorm.ErrRecordNotFound) {
  152. return global.UpdateNotFoundOrNoPermissionErr
  153. }
  154. return global.UpdateFailedErr
  155. }
  156. c.Generate(&userModel)
  157. err = tx.Model(&userModel).Where("id = ?", &userModel.Id).
  158. Omit("password", "salt").Updates(&userModel).Error
  159. if err != nil {
  160. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  161. return model.ErrForbidUpdateSysRole
  162. }
  163. e.Log.Errorf("db error: %s", err)
  164. return global.UpdateFailedErr
  165. }
  166. return nil
  167. }
  168. // UpdateStatus 更新用户状态
  169. func (e *SysUser) UpdateStatus(c *dto.UpdateSysUserStatusReq, p *actions.DataPermission) error {
  170. var userModel model.SysUser
  171. err := e.Orm.
  172. Scopes(actions.UserPermission(userModel.TableName(), p)).
  173. First(&userModel, c.GetId()).Error
  174. if err != nil {
  175. e.Log.Errorf("db error: %s", err)
  176. if errors.Is(err, gorm.ErrRecordNotFound) {
  177. return global.UpdateNotFoundOrNoPermissionErr
  178. }
  179. return global.UpdateFailedErr
  180. }
  181. err = e.Orm.Table(userModel.TableName()).Where("id = ? ", c.Id).Updates(c).Error
  182. if err != nil {
  183. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  184. return model.ErrForbidUpdateSysRole
  185. }
  186. e.Log.Errorf("db error: %s", err)
  187. return global.UpdateFailedErr
  188. }
  189. return nil
  190. }
  191. // ResetPwd 重置用户密码
  192. func (e *SysUser) ResetPwd(c *dto.ResetSysUserPwdReq, p *actions.DataPermission) error {
  193. var userModel model.SysUser
  194. err := e.Orm.
  195. Scopes(actions.UserPermission(userModel.TableName(), p)).
  196. First(&userModel, c.GetId()).Error
  197. if err != nil {
  198. e.Log.Errorf("db error: %s", err)
  199. if errors.Is(err, gorm.ErrRecordNotFound) {
  200. return global.UpdateNotFoundOrNoPermissionErr
  201. }
  202. return global.UpdateFailedErr
  203. }
  204. c.Generate(&userModel)
  205. err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&userModel).Error
  206. if err != nil {
  207. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  208. return model.ErrForbidUpdateSysRole
  209. }
  210. e.Log.Errorf("db error: %s", err)
  211. return global.UpdateFailedErr
  212. }
  213. return nil
  214. }
  215. // Remove 删除SysUser
  216. func (e *SysUser) Remove(c *dto.SysUserDeleteReq, p *actions.DataPermission) error {
  217. var err error
  218. tx := e.Orm.Begin()
  219. defer func() {
  220. if err != nil {
  221. tx.Rollback()
  222. } else {
  223. tx.Commit()
  224. }
  225. }()
  226. var data model.SysUser
  227. db := e.Orm.Model(&data).
  228. Scopes(actions.UserPermission(data.TableName(), p)).
  229. Find(&data, c.GetId())
  230. if p.UserId != 1 {
  231. if data.CreateBy == 1 && data.Type == 1 {
  232. return errors.New("禁止删除管理员")
  233. }
  234. }
  235. if err = db.Error; err != nil {
  236. e.Log.Errorf("db error: %s", err)
  237. return err
  238. }
  239. if db.RowsAffected == 0 {
  240. return global.DeleteNotFoundOrNoPermissionErr
  241. }
  242. var car model.Car
  243. var j int64
  244. err = e.Orm.Model(&car).Where("user_id = ?", c.GetId()).Count(&j).Error
  245. if err != nil {
  246. e.Log.Errorf("db error: %s", err)
  247. return global.CreateFailedErr
  248. }
  249. if j > 0 {
  250. err = errors.New("该用户已绑定车辆,请解绑后删除!")
  251. e.Log.Errorf("db error: %s", err)
  252. return err
  253. }
  254. var warehouse model.Warehouse
  255. var k int64
  256. err = e.Orm.Model(&warehouse).Where("user_id = ?", c.GetId()).Count(&k).Error
  257. if err != nil {
  258. e.Log.Errorf("db error: %s", err)
  259. return global.CreateFailedErr
  260. }
  261. if k > 0 {
  262. err = errors.New("该用户已绑定仓库,请解绑后删除!")
  263. e.Log.Errorf("db error: %s", err)
  264. return err
  265. }
  266. if err := tx.Delete(&data, c.GetId()).Error; err != nil {
  267. if errors.Is(err, model.ErrForbidDeleteSysRole) {
  268. return model.ErrForbidDeleteSysRole
  269. }
  270. e.Log.Errorf("db error: %s", err)
  271. return err
  272. }
  273. return nil
  274. }
  275. // UpdatePwd 修改SysUser对象密码
  276. func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string) error {
  277. var err error
  278. if newPassword == "" {
  279. return nil
  280. }
  281. c := &model.SysUser{}
  282. err = e.Orm.Model(c).
  283. Select("id", "password", "salt").
  284. First(c, id).Error
  285. if err != nil {
  286. e.Log.Errorf("db error: %s", err)
  287. if errors.Is(err, gorm.ErrRecordNotFound) {
  288. return global.UpdateNotFoundOrNoPermissionErr
  289. }
  290. return errors.New("密码修改失败")
  291. }
  292. var ok bool
  293. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  294. if err != nil {
  295. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  296. return errors.New("密码修改失败")
  297. }
  298. if !ok {
  299. err = errors.New("incorrect Password")
  300. e.Log.Warnf("user[%d] %s", id, err.Error())
  301. return err
  302. }
  303. c.Password = newPassword
  304. db := e.Orm.Model(c).Where("id = ?", id).
  305. Select("Password", "Salt").
  306. Updates(c)
  307. if err = db.Error; err != nil {
  308. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  309. return model.ErrForbidUpdateSysRole
  310. }
  311. e.Log.Errorf("db error: %s", err)
  312. return errors.New("密码修改失败")
  313. }
  314. return nil
  315. }
  316. func (e *SysUser) GetProfile(c *dto.SysUserById, user *model.SysUser) error {
  317. err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
  318. if err != nil {
  319. return err
  320. }
  321. return nil
  322. }