bind_house.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package charge
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. "property-garden/errors"
  6. "property-garden/impl/v1/charge_utils"
  7. dbmodel "property-garden/model"
  8. pb_v1 "property-garden/pb/v1"
  9. "time"
  10. "git.getensh.com/common/gopkgs/database"
  11. "google.golang.org/grpc/status"
  12. )
  13. type houseBindParam struct {
  14. chargeInfo *dbmodel.TChargeConf
  15. houseIds []int64
  16. houseInfoM map[int64]dbmodel.THouseUnitBuilding
  17. }
  18. func bindChargeHouse(req *pb_v1.ChargeBindRequest, param *houseBindParam, db *gorm.DB, dbname string) (error) {
  19. now := time.Now()
  20. arrayChargeHouse := []dbmodel.TChargeBind{}
  21. // 去重
  22. olds := map[int64]bool{}
  23. binded := dbmodel.NewChargeBind(dbname)
  24. where := map[string]interface{}{
  25. "obj_id in": param.houseIds,
  26. "obj_type": charge_utils.ObjTypeHouse,
  27. }
  28. if charge_utils.IsOtherChargeType[param.chargeInfo.ChargeType] {
  29. // 不能重复绑定同一个项目
  30. where["charge_id"] = param.chargeInfo.ID
  31. } else {
  32. // 不能重复绑定同一种类型
  33. where["charge_type"] = param.chargeInfo.ChargeType
  34. }
  35. bindedList, err := binded.List(database.DB(), where, nil, -1, -1)
  36. if err != nil {
  37. return errors.DataBaseError
  38. }
  39. for _, v := range bindedList {
  40. olds[v.ObjId] = true
  41. }
  42. for _, v := range param.houseIds {
  43. // 已存在不重复添加
  44. if olds[v] {
  45. continue
  46. }
  47. tmp := dbmodel.TChargeBind{
  48. ObjId: v,
  49. ChargeId: req.ChargeId,
  50. Start: req.Start,
  51. CreatedAt: now,
  52. UpdatedAt: now,
  53. ChargeType: param.chargeInfo.ChargeType,
  54. ObjType: charge_utils.ObjTypeHouse,
  55. //ChargeBasis:param.chargeInfo.ChargeBasis,
  56. HouseId:v,
  57. ObjName: fmt.Sprintf("%v-%v-%v", param.houseInfoM[v].BuildingNumber,param.houseInfoM[v].UnitNumber,param.houseInfoM[v].HouseNumber),
  58. }
  59. if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
  60. tmp.CustomFee = req.VehicleFee
  61. }
  62. if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisArea {
  63. tmp.ObjArea = param.houseInfoM[v].HouseArea
  64. }
  65. if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisUsedArea {
  66. tmp.ObjArea = param.houseInfoM[v].HouseUsedArea
  67. }
  68. if !charge_utils.IsOtherChargeType[param.chargeInfo.ChargeType] {
  69. tmp.UniqFlag.Int32 = 1
  70. tmp.UniqFlag.Valid = true
  71. }
  72. if charge_utils.IsPowerChargeType[param.chargeInfo.ChargeType] {
  73. tmp.Start = 0
  74. }
  75. arrayChargeHouse = append(arrayChargeHouse, tmp)
  76. }
  77. if len(arrayChargeHouse) == 0 {
  78. return nil
  79. }
  80. p := dbmodel.NewChargeBind(dbname)
  81. err = p.InsertMulti(db, &arrayChargeHouse)
  82. if err != nil {
  83. return errors.DataBaseError
  84. }
  85. // 一次性费用直接创建账单
  86. if param.chargeInfo.ChargeTimeType == charge_utils.ChargeTimeTypeOne {
  87. for _, v := range arrayChargeHouse {
  88. houseInfo := param.houseInfoM[v.ObjId]
  89. bindInfo := v
  90. bparam := &BillAddParam{
  91. HouseInfo:&houseInfo,
  92. PowerInfo:nil,
  93. VehicleInfo:nil,
  94. SpaceInfo:nil,
  95. ChargeInfo:param.chargeInfo,
  96. BindInfo: &bindInfo,
  97. ObjType: charge_utils.ObjTypeHouse,
  98. }
  99. err = BillAddHouseOnce(bparam, db, dbname)
  100. if err != nil {
  101. return err
  102. }
  103. }
  104. }
  105. return err
  106. }
  107. func addByHouseIds(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
  108. if !charge_utils.IsPowerChargeType[chargeInfo.ChargeType] && req.Start < 1 {
  109. return nil, status.Error(10003, "开始时间不能为空")
  110. }
  111. // 获取房屋信息
  112. houseInfoM, err := charge_utils.GetHouseInfos(req.ObjIds, dbname)
  113. if err != nil {
  114. return nil, err
  115. }
  116. if len(houseInfoM) == 0 {
  117. return nil, status.Error(10003, "房屋未录入")
  118. }
  119. houseIds := []int64{}
  120. for houseId, _ := range houseInfoM {
  121. houseIds = append(houseIds, houseId)
  122. }
  123. length := len(houseIds)
  124. param := houseBindParam{
  125. chargeInfo:chargeInfo,
  126. houseInfoM:houseInfoM,
  127. }
  128. // 分批
  129. for i := 0; i < length; i += 100 {
  130. if i+100 >= length {
  131. param.houseIds = houseIds[i:]
  132. err = bindChargeHouse(req, &param, db, dbname)
  133. } else {
  134. param.houseIds = houseIds[i:i+100]
  135. err = bindChargeHouse(req, &param, db, dbname)
  136. }
  137. if err != nil {
  138. return nil, err
  139. }
  140. }
  141. return &pb_v1.ChargeBindReply{}, nil
  142. }