customer.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 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. if err = tx.Delete(&data, c.GetId()).Error; err != nil {
  215. if errors.Is(err, model.ErrForbidDeleteSysRole) {
  216. return model.ErrForbidDeleteSysRole
  217. }
  218. e.Log.Errorf("db error: %s", err)
  219. return err
  220. }
  221. return nil
  222. }
  223. // UpdatePwd 修改Customer对象密码
  224. func (e *Customer) UpdatePwd(id int, oldPassword, newPassword string) error {
  225. var err error
  226. if newPassword == "" {
  227. return nil
  228. }
  229. c := &model.SysUser{}
  230. err = e.Orm.Model(c).
  231. Select("id", "password", "salt").
  232. First(c, id).Error
  233. if err != nil {
  234. e.Log.Errorf("db error: %s", err)
  235. if errors.Is(err, gorm.ErrRecordNotFound) {
  236. return global.UpdateNotFoundOrNoPermissionErr
  237. }
  238. return errors.New("密码修改失败")
  239. }
  240. var ok bool
  241. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  242. if err != nil {
  243. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  244. return errors.New("密码修改失败")
  245. }
  246. if !ok {
  247. err = errors.New("incorrect Password")
  248. e.Log.Warnf("user[%d] %s", id, err.Error())
  249. return err
  250. }
  251. c.Password = newPassword
  252. db := e.Orm.Model(c).Where("id = ?", id).
  253. Select("Password", "Salt").
  254. Updates(c)
  255. if err = db.Error; err != nil {
  256. if errors.Is(err, model.ErrForbidUpdateSysRole) {
  257. return model.ErrForbidUpdateSysRole
  258. }
  259. e.Log.Errorf("db error: %s", err)
  260. return errors.New("密码修改失败")
  261. }
  262. return nil
  263. }
  264. func (e *Customer) GetProfile(c *dto.CustomerById, user *model.SysUser) error {
  265. err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
  266. if err != nil {
  267. return err
  268. }
  269. return nil
  270. }