charge_house_group.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "property-garden/errors"
  13. "property-garden/impl/v1/charge_utils"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkChargeHouseGroupParam(req *pb_v1.ChargeHouseGroupRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. }
  23. if req.Page == 0 {
  24. req.Page = 1
  25. }
  26. if req.PageSize == 0 {
  27. req.PageSize = 10
  28. }
  29. return nil
  30. }
  31. func ChargeHouseGroup(ctx context.Context, req *pb_v1.ChargeHouseGroupRequest) (reply *pb_v1.ChargeHouseGroupReply, err error) {
  32. reply = &pb_v1.ChargeHouseGroupReply{}
  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 = checkChargeHouseGroupParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. reply.Page = req.Page
  51. dbname := utils.GetGardenDbName(req.GardenId)
  52. exist := fmt.Sprintf("exists(select obj_id from %s.t_charge_bind where t_charge_bind.obj_id=t_house_unit_building.id and t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeHouse)
  53. house := dbmodel.NewHouseUnitBuilding(dbname)
  54. where := map[string]interface{}{
  55. exist: "",
  56. }
  57. if req.ObjId > 0 {
  58. where["id"] = req.ObjId
  59. }
  60. reply.Total, err = house.Count(database.DB(), where, nil)
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. if reply.Total == 0 {
  65. return reply, nil
  66. }
  67. list, err := house.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. houseIds := make([]int64, len(list))
  72. for i, v := range list {
  73. houseIds[i] = v.ID
  74. }
  75. bind := dbmodel.NewChargeBind(dbname)
  76. where = map[string]interface{}{
  77. "obj_id in": houseIds,
  78. "obj_type": charge_utils.ObjTypeHouse,
  79. "charge_type": charge_utils.ChargeTypeProperty,
  80. }
  81. blist, err := bind.List(database.DB(), where, nil, -1, -1)
  82. if err != nil {
  83. return nil, errors.DataBaseError
  84. }
  85. m := map[int64]int64{}
  86. for _, v := range blist {
  87. m[v.ObjId] = v.ID
  88. }
  89. bill := dbmodel.NewChargeBill(dbname)
  90. where = map[string]interface{}{
  91. "obj_id in": houseIds,
  92. "obj_type": charge_utils.ObjTypeHouse,
  93. }
  94. type Bcount struct {
  95. Count int64 `json:"count"`
  96. ObjId int64 `json:"obj_id"`
  97. }
  98. billlist := []Bcount{}
  99. err = bill.SelectGroup(database.DB(), where, nil, "sum(1) as count, obj_id", "obj_id", -1, -1, &billlist)
  100. if err != nil {
  101. return nil, errors.DataBaseError
  102. }
  103. bm := map[int64]bool{}
  104. for _, v := range billlist {
  105. if v.Count > 0 {
  106. bm[v.ObjId] = true
  107. }
  108. }
  109. reply.List = make([]*pb_v1.ChargeHouseGroupItem, len(list))
  110. for i, v := range list {
  111. item := &pb_v1.ChargeHouseGroupItem{
  112. HouseId: v.ID,
  113. HouseArea: v.HouseArea,
  114. HouseUsedArea: v.HouseUsedArea,
  115. Layer: v.Layer,
  116. HouseType: int32(v.HouseType),
  117. HouseStatus: int32(v.Status),
  118. HouseName: fmt.Sprintf("%v-%v-%v",
  119. v.BuildingNumber,
  120. v.UnitNumber,
  121. v.HouseNumber),
  122. HousePropertyBindId: m[v.ID],
  123. HasUnpayBill: bm[v.ID],
  124. }
  125. reply.List[i] = item
  126. }
  127. return reply, nil
  128. }