charge_generate_bill.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "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. "property-garden/impl/v1/charge_utils"
  15. "property-garden/impl/v1/fee"
  16. dbmodel "property-garden/model"
  17. pb_v1 "property-garden/pb/v1"
  18. "property-garden/utils"
  19. )
  20. func checkChargeGenerateBillParam(req *pb_v1.ChargeGenerateBillRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.BindId == 0:
  25. return status.Error(10003, "关系id不能为空")
  26. }
  27. return nil
  28. }
  29. func generatePowerBill(bind *dbmodel.TChargeBind, chargeInfo *dbmodel.TChargeConf, dbname string) error {
  30. p := dbmodel.NewChargePower(dbname)
  31. where := map[string]interface{}{
  32. "charge_type":bind.ChargeType,
  33. "house_id":bind.ObjId,
  34. "bill_id":0,
  35. }
  36. list, err := p.List(database.DB(), where, nil, -1, -1)
  37. if err != nil {
  38. return errors.DataBaseError
  39. }
  40. if len(list) == 0 {
  41. return status.Error(10003, "无账单可生成")
  42. }
  43. house := dbmodel.NewHouseUnitBuilding(dbname)
  44. where = map[string]interface{}{
  45. "id":bind.ObjId,
  46. }
  47. err = house.Find(database.DB(), where)
  48. if err != nil && err != gorm.ErrRecordNotFound{
  49. return errors.DataBaseError
  50. }
  51. if house.ID == 0 {
  52. return status.Error(10003, "房屋已被删除")
  53. }
  54. db := database.DB().Begin()
  55. for i, _ := range list {
  56. param := &BillAddParam{
  57. HouseInfo:house,
  58. PowerInfo:&list[i],
  59. VehicleInfo: nil,
  60. SpaceInfo: nil,
  61. ChargeInfo: chargeInfo,
  62. BindInfo: bind,
  63. ObjType: charge_utils.ObjTypeHouse,
  64. }
  65. err = BillAddHousePower(param, db, dbname)
  66. if err != nil {
  67. db.Rollback()
  68. return err
  69. }
  70. }
  71. db.Commit()
  72. return nil
  73. }
  74. func ChargeGenerateBill(ctx context.Context, req *pb_v1.ChargeGenerateBillRequest) (reply *pb_v1.ChargeGenerateBillReply, err error) {
  75. reply = &pb_v1.ChargeGenerateBillReply{}
  76. // 捕获各个task中的异常并返回给调用者
  77. defer func() {
  78. if r := recover(); r != nil {
  79. err = fmt.Errorf("%+v", r)
  80. e := &status.Status{}
  81. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  82. logger.Error("err",
  83. zap.String("system_err", err.Error()),
  84. zap.Stack("stacktrace"))
  85. }
  86. }
  87. }()
  88. // 参数检查
  89. err = checkChargeGenerateBillParam(req)
  90. if err != nil {
  91. return nil, err
  92. }
  93. dbname := utils.GetGardenDbName(req.GardenId)
  94. bind := dbmodel.NewChargeBind(dbname)
  95. where := map[string]interface{}{
  96. "id":req.BindId,
  97. }
  98. err = bind.Find(database.DB(), where)
  99. if err != nil && err != gorm.ErrRecordNotFound{
  100. return nil, errors.DataBaseError
  101. }
  102. if bind.ID == 0 {
  103. return nil, errors.ErrRecordNotFound
  104. }
  105. chargeInfo := dbmodel.NewChargeConf(dbname)
  106. where = map[string]interface{}{
  107. "id":bind.ChargeId,
  108. }
  109. err = chargeInfo.Find(database.DB(), where)
  110. if err != nil && err != gorm.ErrRecordNotFound{
  111. return nil, errors.DataBaseError
  112. }
  113. if chargeInfo.ID == 0 {
  114. return nil, errors.ErrRecordNotFound
  115. }
  116. if bind.ChargeType == charge_utils.ChargeTypeVehicle {
  117. return nil, status.Error(10003, "车辆费用不能手动生成账单")
  118. }
  119. if chargeInfo.ChargeTimeType == charge_utils.ChargeTimeTypeOne {
  120. return nil, status.Error(10003, "一次性费用不能手动生成账单")
  121. }
  122. if charge_utils.IsPowerChargeType[bind.ChargeType] {
  123. return reply, generatePowerBill(bind, chargeInfo, dbname)
  124. }
  125. //周期性费用
  126. mreq := pb_v1.GenerateBillRequest{
  127. GardenId:req.GardenId,
  128. ChargeBindId:req.BindId,
  129. EndTime:req.End,
  130. }
  131. _, err = fee.GenerateBill(ctx, &mreq)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return reply, nil
  136. }