add.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. const (
  20. PropertyPackageTypeMonth = 1
  21. PropertyPackageTypeText = 2
  22. )
  23. var propertyPackageTypeM = map[int32]string {
  24. PropertyPackageTypeMonth:"按月",
  25. PropertyPackageTypeText:"按文本",
  26. }
  27. func checkPropertyPackageAddParam(req *pb_v1.PropertyPackageAddRequest) error {
  28. switch {
  29. case req.GardenId == 0:
  30. return status.Error(10003, "小区不能为空")
  31. case propertyPackageTypeM[req.PackageType] == "":
  32. return status.Error(10003, "赠送类型错误")
  33. case req.PackageType == PropertyPackageTypeMonth && req.GiveMonths < 1:
  34. return status.Error(10003, "赠送月数不能为空")
  35. case req.PackageType == PropertyPackageTypeText && req.GiveContent == "":
  36. return status.Error(10003, "赠送文本不能为空")
  37. case req.PayMonths < 1:
  38. return status.Error(10003, "缴费月数不能为空")
  39. }
  40. return nil
  41. }
  42. func PropertyPackageAdd(ctx context.Context, req *pb_v1.PropertyPackageAddRequest) (reply *pb_v1.PropertyPackageAddReply, err error) {
  43. reply = &pb_v1.PropertyPackageAddReply{}
  44. // 捕获各个task中的异常并返回给调用者
  45. defer func() {
  46. if r := recover(); r != nil {
  47. err = fmt.Errorf("%+v", r)
  48. e := &status.Status{}
  49. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  50. logger.Error("err",
  51. zap.String("system_err", err.Error()),
  52. zap.Stack("stacktrace"))
  53. }
  54. }
  55. }()
  56. // 参数检查
  57. err = checkPropertyPackageAddParam(req)
  58. if err != nil {
  59. return nil, err
  60. }
  61. now := time.Now()
  62. dbname := utils.GetGardenDbName(req.GardenId)
  63. if req.PackageType == PropertyPackageTypeMonth {
  64. p := dbmodel.NewPropertyPackage(dbname)
  65. where := map[string]interface{}{
  66. "package_type":PropertyPackageTypeMonth,
  67. "give_months":req.GiveMonths,
  68. "pay_months":req.PayMonths,
  69. }
  70. err = p.Find(database.DB(), where)
  71. if err != nil && err != gorm.ErrRecordNotFound{
  72. return nil, errors.DataBaseError
  73. }
  74. if p.ID > 0 {
  75. if p.Enable == 2 {
  76. return nil, status.Error(10003, "相同套餐已存在, 请启用")
  77. }
  78. return nil, status.Error(10003, "相同套餐已存在")
  79. }
  80. }
  81. enable := int32(1)
  82. if !req.Enable {
  83. enable = 2
  84. }
  85. p := dbmodel.TPropertyPackage{
  86. GiveContent:req.GiveContent,
  87. GiveMonths:req.GiveMonths,
  88. PackageType:req.PackageType,
  89. PayMonths:req.PayMonths,
  90. Enable:enable,
  91. UpdatedAt:now,
  92. CreatedAt:now,
  93. }
  94. p.SetTable(dbname)
  95. err = p.Insert(database.DB())
  96. if err != nil {
  97. return nil, errors.DataBaseError
  98. }
  99. reply.Id = p.ID
  100. return reply, nil
  101. }