del.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package property_package
  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-garden/errors"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkPropertyPackageDelParam(req *pb_v1.PropertyPackageDelRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case req.Id < 1:
  23. return status.Error(10003, "id 不能为空")
  24. }
  25. return nil
  26. }
  27. func PropertyPackageDel(ctx context.Context, req *pb_v1.PropertyPackageDelRequest) (reply *pb_v1.PropertyPackageDelReply, err error) {
  28. reply = &pb_v1.PropertyPackageDelReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. // 参数检查
  42. err = checkPropertyPackageDelParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. dbname := utils.GetGardenDbName(req.GardenId)
  47. // 获取旧数据
  48. // 获取旧数据
  49. old := dbmodel.NewPropertyPackage(dbname)
  50. where := map[string]interface{}{
  51. "id":req.Id,
  52. }
  53. err = old.Find(database.DB(), where)
  54. if err != nil && err != gorm.ErrRecordNotFound {
  55. return nil, errors.DataBaseError
  56. }
  57. if old.ID == 0 {
  58. return nil, errors.ErrRecordNotFound
  59. }
  60. reply.Origin = &pb_v1.PropertyPackageUpdateRequest{
  61. GardenId:req.GardenId,
  62. Id:req.Id,
  63. GiveMonths:old.GiveMonths,
  64. GiveContent:old.GiveContent,
  65. PayMonths:old.PayMonths,
  66. PackageType:old.PackageType,
  67. Enable:false,
  68. }
  69. if old.Enable == 1 {
  70. reply.Origin.Enable = true
  71. }
  72. // 删除数据
  73. propertyPackage := dbmodel.NewPropertyPackage(dbname)
  74. err = propertyPackage.Delete(database.DB(), where)
  75. if err != nil {
  76. return nil, errors.DataBaseError
  77. }
  78. return reply, nil
  79. }