customer.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 Customer struct {
  15. service.Service
  16. }
  17. // GetPage 获取Customer列表
  18. func (e *Customer) GetPage(c *dto.CustomerGetPageReq, p *actions.DataPermission, list *[]model.SysUser, count *int64) error {
  19. var err error
  20. var data model.SysUser
  21. var userList []model.SysUser
  22. err = e.Orm.
  23. Scopes(
  24. cDto.MakeCondition(c.GetNeedSearch()),
  25. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  26. actions.UserPermission(data.TableName(), p),
  27. ).
  28. Where("user_type = ?", model.UserTypeCustomer).
  29. Find(&userList).Limit(-1).Offset(-1).
  30. Count(count).Error
  31. if err != nil {
  32. e.Log.Errorf("db error: %s", err)
  33. return global.GetFailedErr
  34. }
  35. *list = userList
  36. return nil
  37. }
  38. // Get 获取Customer对象
  39. func (e *Customer) Get(d *dto.CustomerGetReq, p *actions.DataPermission, userModel *model.SysUser) error {
  40. var data model.SysUser
  41. //var post model.SysPost
  42. err := e.Orm.Model(&data).
  43. Scopes(actions.UserPermission(data.TableName(), p)).
  44. First(userModel, d.GetId()).Error
  45. if err != nil {
  46. e.Log.Errorf("db error: %s", err)
  47. if errors.Is(err, gorm.ErrRecordNotFound) {
  48. return global.GetNotFoundOrNoPermissionErr
  49. }
  50. return global.GetFailedErr
  51. }
  52. return nil
  53. }
  54. // Insert 创建Customer对象
  55. func (e *Customer) Insert(c *dto.CustomerInsertReq) error {
  56. var err error
  57. tx := e.Orm.Begin()
  58. defer func() {
  59. if err != nil {
  60. tx.Rollback()
  61. } else {
  62. tx.Commit()
  63. }
  64. }()
  65. var dept model.SysDept
  66. var deptCount int64
  67. err = e.Orm.Model(&dept).Where("id = ?", c.DeptId).Count(&deptCount).Error
  68. if err != nil {
  69. e.Log.Errorf("db error: %s", err)
  70. return global.CreateFailedErr
  71. }
  72. if deptCount == 0 {
  73. err = errors.New("公司不存在!")
  74. e.Log.Errorf("db error: %s", err)
  75. return err
  76. }
  77. var data model.SysUser
  78. var i int64
  79. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  80. if err != nil {
  81. e.Log.Errorf("db error: %s", err)
  82. return global.CreateFailedErr
  83. }
  84. if i > 0 {
  85. err = errors.New("用户名已存在!")
  86. e.Log.Errorf("db error: %s", err)
  87. return err
  88. }
  89. var uuid string
  90. for {
  91. uuid = utils.GetUUID()
  92. var j int64
  93. err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error
  94. if err != nil {
  95. continue
  96. }
  97. if j == 0 {
  98. break
  99. }
  100. }
  101. c.Generate(&data)
  102. data.Uuid = uuid
  103. err = tx.Create(&data).Error
  104. if err != nil {
  105. e.Log.Errorf("db error: %s", err)
  106. return global.CreateFailedErr
  107. }
  108. c.Id = data.Id
  109. return nil
  110. }
  111. // Update 修改Customer对象
  112. func (e *Customer) Update(c *dto.CustomerUpdateReq, p *actions.DataPermission) error {
  113. var err error
  114. tx := e.Orm.Begin()
  115. defer func() {
  116. if err != nil {
  117. tx.Rollback()
  118. } else {
  119. tx.Commit()
  120. }
  121. }()
  122. var userModel model.SysUser
  123. err = e.Orm.
  124. Scopes(actions.UserPermission(userModel.TableName(), p)).
  125. First(&userModel, c.GetId()).Error
  126. if err != nil {
  127. e.Log.Errorf("db error: %s", err)
  128. if errors.Is(err, gorm.ErrRecordNotFound) {
  129. return global.UpdateNotFoundOrNoPermissionErr
  130. }
  131. return global.UpdateFailedErr
  132. }
  133. c.Generate(&userModel)
  134. err = tx.Model(&userModel).Where("id = ?", &userModel.Id).
  135. Omit("password", "salt").Updates(&userModel).Error
  136. if err != nil {
  137. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  138. return model.ErrForbidUpdateSysRole
  139. }
  140. e.Log.Errorf("db error: %s", err)
  141. return global.UpdateFailedErr
  142. }
  143. return nil
  144. }
  145. // UpdateStatus 更新用户状态
  146. func (e *Customer) UpdateStatus(c *dto.UpdateCustomerStatusReq, p *actions.DataPermission) error {
  147. var userModel model.SysUser
  148. err := e.Orm.
  149. Scopes(actions.UserPermission(userModel.TableName(), p)).
  150. First(&userModel, c.GetId()).Error
  151. if err != nil {
  152. e.Log.Errorf("db error: %s", err)
  153. if errors.Is(err, gorm.ErrRecordNotFound) {
  154. return global.UpdateNotFoundOrNoPermissionErr
  155. }
  156. return global.UpdateFailedErr
  157. }
  158. err = e.Orm.Table(userModel.TableName()).Where("id = ? ", c.Id).Updates(c).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. // ResetPwd 重置用户密码
  169. func (e *Customer) ResetPwd(c *dto.ResetCustomerPwdReq, 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. c.Generate(&userModel)
  182. err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&userModel).Error
  183. if err != nil {
  184. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  185. return model.ErrForbidUpdateSysRole
  186. }
  187. e.Log.Errorf("db error: %s", err)
  188. return global.UpdateFailedErr
  189. }
  190. return nil
  191. }
  192. // Remove 删除Customer
  193. func (e *Customer) Remove(c *dto.CustomerDeleteReq, p *actions.DataPermission) error {
  194. var err error
  195. tx := e.Orm.Begin()
  196. defer func() {
  197. if err != nil {
  198. tx.Rollback()
  199. } else {
  200. tx.Commit()
  201. }
  202. }()
  203. var data model.SysUser
  204. db := e.Orm.Model(&data).
  205. Scopes(actions.UserPermission(data.TableName(), p)).
  206. Find(&data, c.GetId())
  207. if err = db.Error; err != nil {
  208. e.Log.Errorf("db error: %s", err)
  209. return err
  210. }
  211. if db.RowsAffected == 0 {
  212. return global.DeleteNotFoundOrNoPermissionErr
  213. }
  214. var car model.Car
  215. var j int64
  216. err = e.Orm.Model(&car).Where("user_id = ?", c.GetId()).Count(&j).Error
  217. if err != nil {
  218. e.Log.Errorf("db error: %s", err)
  219. return global.CreateFailedErr
  220. }
  221. if j > 0 {
  222. err = errors.New("该用户已绑定车辆,禁止删除!")
  223. e.Log.Errorf("db error: %s", err)
  224. return err
  225. }
  226. if err := tx.Delete(&data, c.GetId()).Error; err != nil {
  227. if errors.Is(err, model.ErrForbidDeleteSysRole) {
  228. return model.ErrForbidDeleteSysRole
  229. }
  230. e.Log.Errorf("db error: %s", err)
  231. return err
  232. }
  233. return nil
  234. }
  235. // UpdatePwd 修改Customer对象密码
  236. func (e *Customer) UpdatePwd(id int, oldPassword, newPassword string) error {
  237. var err error
  238. if newPassword == "" {
  239. return nil
  240. }
  241. c := &model.SysUser{}
  242. err = e.Orm.Model(c).
  243. Select("id", "password", "salt").
  244. First(c, id).Error
  245. if err != nil {
  246. e.Log.Errorf("db error: %s", err)
  247. if errors.Is(err, gorm.ErrRecordNotFound) {
  248. return global.UpdateNotFoundOrNoPermissionErr
  249. }
  250. return errors.New("密码修改失败")
  251. }
  252. var ok bool
  253. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  254. if err != nil {
  255. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  256. return errors.New("密码修改失败")
  257. }
  258. if !ok {
  259. err = errors.New("incorrect Password")
  260. e.Log.Warnf("user[%d] %s", id, err.Error())
  261. return err
  262. }
  263. c.Password = newPassword
  264. db := e.Orm.Model(c).Where("id = ?", id).
  265. Select("Password", "Salt").
  266. Updates(c)
  267. if err = db.Error; err != nil {
  268. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  269. return model.ErrForbidUpdateSysRole
  270. }
  271. e.Log.Errorf("db error: %s", err)
  272. return errors.New("密码修改失败")
  273. }
  274. return nil
  275. }
  276. func (e *Customer) GetProfile(c *dto.CustomerById, user *model.SysUser) error {
  277. err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
  278. if err != nil {
  279. return err
  280. }
  281. return nil
  282. }