del.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-company/errors"
  14. dbmodel "property-company/model"
  15. pb_v1 "property-company/pb/v1"
  16. )
  17. //
  18. func CompanyUserDel(ctx context.Context, req *pb_v1.CompanyUserDelRequest) (reply *pb_v1.CompanyUserDelReply, err error) {
  19. reply = &pb_v1.CompanyUserDelReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.Id < 1 || req.Cid < 1 {
  33. return nil, errors.ParamsError
  34. }
  35. p := &dbmodel.TUser{}
  36. where := map[string]interface{}{
  37. "id": req.Id,
  38. "cid": req.Cid,
  39. }
  40. src := &dbmodel.TUser{}
  41. err = src.Find(database.DB(), where)
  42. if err != nil && err != gorm.ErrRecordNotFound {
  43. return nil, errors.DataBaseError
  44. }
  45. if src.ID == 0 {
  46. return nil, errors.ErrRecordNotFound
  47. }
  48. if src.Super {
  49. return nil, status.Error(10003, "初始用户不能删除")
  50. }
  51. reply.Origin = &pb_v1.CompanyUserUpdateRequest{
  52. Id: req.Id,
  53. UserName: src.User,
  54. Email: src.Email,
  55. Phone: src.Phone,
  56. Cid: src.Cid,
  57. }
  58. err = p.Delete(database.DB(), where)
  59. if err != nil {
  60. return nil, errors.DataBaseError
  61. }
  62. return reply, nil
  63. }