bind.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/charge_utils"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkChargeBindParam(req *pb_v1.ChargeBindRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.ChargeId == 0:
  25. return status.Error(10003, "费用项目不能为空")
  26. case len(req.ObjIds) == 0:
  27. return status.Error(10003, "绑定对象不能为空")
  28. }
  29. return nil
  30. }
  31. func ChargeBind(ctx context.Context, req *pb_v1.ChargeBindRequest) (reply *pb_v1.ChargeBindReply, err error) {
  32. reply = &pb_v1.ChargeBindReply{}
  33. // 捕获各个task中的异常并返回给调用者
  34. defer func() {
  35. if r := recover(); r != nil {
  36. err = fmt.Errorf("%+v", r)
  37. e := &status.Status{}
  38. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  39. logger.Error("err",
  40. zap.String("system_err", err.Error()),
  41. zap.Stack("stacktrace"))
  42. }
  43. }
  44. }()
  45. // 参数检查
  46. err = checkChargeBindParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. chargeInfo := dbmodel.NewChargeConf(dbname)
  52. where := map[string]interface{}{
  53. "id":req.ChargeId,
  54. //"garden_id":req.GardenId,
  55. }
  56. err = chargeInfo.Find(database.DB(), where)
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return nil, errors.DataBaseError
  59. }
  60. if chargeInfo.ID == 0 {
  61. return nil, errors.ErrRecordNotFound
  62. }
  63. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf && req.VehicleFee == 0 {
  64. return nil, status.Error(10003, "车辆自定义费用不能为空")
  65. }
  66. if req.Start > 0 {
  67. t := time.Unix(req.Start, 0)
  68. req.Start = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
  69. }
  70. db := database.DB().Begin()
  71. defer func() {
  72. if err != nil {
  73. db.Rollback()
  74. }
  75. }()
  76. switch chargeInfo.ChargeType{
  77. case charge_utils.ChargeTypeSpace:
  78. reply, err = addBySpaceIds(req, chargeInfo, db, dbname)
  79. case charge_utils.ChargeTypeVehicle:
  80. reply, err = addByVehicleIds(req, chargeInfo, db, dbname)
  81. default:
  82. reply, err = addByHouseIds(req, chargeInfo, db, dbname)
  83. }
  84. if err != nil {
  85. return nil, err
  86. }
  87. db.Commit()
  88. return reply, nil
  89. }