charge_house_not_bind_list.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. )
  19. func checkChargeHouseNotBindListParam(req *pb_v1.ChargeHouseNotBindListRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case req.ChargeId < 1:
  24. return status.Error(10003, "费用项目不能为空")
  25. }
  26. if req.Page == 0 {
  27. req.Page = 1
  28. }
  29. if req.PageSize == 0 {
  30. req.PageSize = 10
  31. }
  32. return nil
  33. }
  34. func ChargeHouseNotBindList(ctx context.Context, req *pb_v1.ChargeHouseNotBindListRequest) (reply *pb_v1.ChargeHouseNotBindListReply, err error) {
  35. reply = &pb_v1.ChargeHouseNotBindListReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. // 参数检查
  49. err = checkChargeHouseNotBindListParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. reply.Page = req.Page
  54. dbname := utils.GetGardenDbName(req.GardenId)
  55. chargeInfo := dbmodel.NewChargeConf(dbname)
  56. filter := map[string]interface{}{
  57. "id": req.ChargeId,
  58. }
  59. err = chargeInfo.Find(database.DB(), filter)
  60. if err != nil && err != gorm.ErrRecordNotFound {
  61. return nil, errors.DataBaseError
  62. }
  63. if chargeInfo.ID == 0 {
  64. return nil, errors.ErrRecordNotFound
  65. }
  66. house := dbmodel.NewHouseUnitBuilding(dbname)
  67. where := map[string]interface{}{}
  68. notExist := fmt.Sprintf("not exists(select obj_id from %s.t_charge_bind where t_charge_bind.obj_id=t_house_unit_building.id and t_charge_bind.charge_id=? && t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeHouse)
  69. where[notExist] = req.ChargeId
  70. if !charge_utils.IsOtherChargeType[chargeInfo.ChargeType] {
  71. notExist = fmt.Sprintf("not exists(select obj_id from %s.t_charge_bind where t_charge_bind.obj_id=t_house_unit_building.id and t_charge_bind.charge_type=? && t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeHouse)
  72. where[notExist] = chargeInfo.ChargeType
  73. }
  74. if req.HouseId > 0 {
  75. where["id"] = req.HouseId
  76. }
  77. if req.HouseId == 0 && req.Layer > 0 {
  78. where["layer"] = req.Layer
  79. }
  80. if req.HouseId == 0 && req.UnitId > 0 {
  81. where["unit_id"] = req.UnitId
  82. }
  83. if req.HouseId == 0 && req.UnitId == 0 && req.BuildingId > 0 {
  84. where["building_id"] = req.BuildingId
  85. }
  86. if req.HouseNumber != "" {
  87. where["house_number"] = req.HouseNumber
  88. }
  89. reply.Total, err = house.Count(database.DB(), where, nil)
  90. if err != nil {
  91. return nil, errors.DataBaseError
  92. }
  93. if reply.Total == 0 {
  94. return reply, nil
  95. }
  96. list, err := house.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  97. if err != nil {
  98. return nil, errors.DataBaseError
  99. }
  100. reply.List = make([]*pb_v1.ChargeHouseData, len(list))
  101. for i, v := range list {
  102. item := &pb_v1.ChargeHouseData{
  103. HouseId: v.ID,
  104. HouseArea: v.HouseArea,
  105. HouseUsedArea: v.HouseUsedArea,
  106. HouseNumber: v.HouseNumber,
  107. Layer: v.Layer,
  108. HouseType: int32(v.HouseType),
  109. HouseStatus: int32(v.Status),
  110. HouseName: fmt.Sprintf("%v-%v-%v",
  111. v.BuildingNumber,
  112. v.UnitNumber,
  113. v.HouseNumber),
  114. }
  115. reply.List[i] = item
  116. }
  117. return reply, nil
  118. }