bind_space.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 spaceBindParam struct {
  14. chargeInfo *dbmodel.TChargeConf
  15. spaceIds []int64
  16. spaceInfoM map[int64]dbmodel.TSpacePark
  17. }
  18. func bindChargeSpace(req *pb_v1.ChargeBindRequest, param *spaceBindParam, db *gorm.DB, dbname string) (error) {
  19. now := time.Now()
  20. arrayChargeSpace := []dbmodel.TChargeBind{}
  21. // 去重
  22. olds := map[int64]bool{}
  23. binded := dbmodel.NewChargeBind(dbname)
  24. where := map[string]interface{}{
  25. "obj_id in": param.spaceIds,
  26. "obj_type": charge_utils.ObjTypeSpace,
  27. }
  28. where["charge_type"] = param.chargeInfo.ChargeType
  29. bindedList, err := binded.List(database.DB(), where, nil, -1, -1)
  30. if err != nil {
  31. return errors.DataBaseError
  32. }
  33. for _, v := range bindedList {
  34. olds[v.ObjId] = true
  35. }
  36. for _, v := range param.spaceIds {
  37. // 已存在不重复添加
  38. if olds[v] {
  39. continue
  40. }
  41. if param.spaceInfoM[v].HouseId == 0 {
  42. //continue
  43. return status.Error(10003, "车位管理费只能绑定已售车位")
  44. }
  45. tmp := dbmodel.TChargeBind{
  46. ObjId: v,
  47. ChargeId: req.ChargeId,
  48. Start: req.Start,
  49. CreatedAt: now,
  50. UpdatedAt: now,
  51. ChargeType: param.chargeInfo.ChargeType,
  52. ObjType: charge_utils.ObjTypeSpace,
  53. //ChargeBasis:param.chargeInfo.ChargeBasis,
  54. HouseId:param.spaceInfoM[v].HouseId,
  55. ObjName:fmt.Sprintf("%v-%v", param.spaceInfoM[v].ParkNumber, param.spaceInfoM[v].SpaceNumber),
  56. }
  57. if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSpaceArea {
  58. tmp.ObjArea = param.spaceInfoM[v].SpaceArea
  59. }
  60. tmp.UniqFlag.Int32 = 1
  61. tmp.UniqFlag.Valid = true
  62. arrayChargeSpace = append(arrayChargeSpace, tmp)
  63. }
  64. if len(arrayChargeSpace) == 0 {
  65. return nil
  66. }
  67. p := dbmodel.NewChargeBind(dbname)
  68. err = p.InsertMulti(db, &arrayChargeSpace)
  69. if err != nil {
  70. return errors.DataBaseError
  71. }
  72. return err
  73. }
  74. func getSpaceInfos(ids []int64, dbname string) (map[int64]dbmodel.TSpacePark, error) {
  75. ret := map[int64]dbmodel.TSpacePark{}
  76. if len(ids) == 0 {
  77. return ret, nil
  78. }
  79. p := dbmodel.NewSpacePark(dbname)
  80. where := map[string]interface{}{
  81. "id in":ids,
  82. }
  83. list, err := p.List(database.DB(), where, nil, -1, -1)
  84. if err != nil {
  85. return nil, errors.DataBaseError
  86. }
  87. for _, v := range list {
  88. ret[v.ID] = v
  89. }
  90. return ret, nil
  91. }
  92. func addBySpaceIds(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
  93. if req.Start < 1 {
  94. return nil, status.Error(10003, "开始时间不能为空")
  95. }
  96. spaceInfoM, err := getSpaceInfos(req.ObjIds, dbname)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if len(spaceInfoM) == 0 {
  101. return nil, status.Error(10003, "车位未录入")
  102. }
  103. spaceIds := []int64{}
  104. for spaceId, _ := range spaceInfoM {
  105. spaceIds = append(spaceIds, spaceId)
  106. }
  107. length := len(spaceIds)
  108. param := spaceBindParam{
  109. chargeInfo:chargeInfo,
  110. spaceInfoM:spaceInfoM,
  111. }
  112. // 分批
  113. for i := 0; i < length; i += 100 {
  114. if i+100 >= length {
  115. param.spaceIds = spaceIds[i:]
  116. err = bindChargeSpace(req, &param, db, dbname)
  117. } else {
  118. param.spaceIds = spaceIds[i:i+100]
  119. err = bindChargeSpace(req, &param, db, dbname)
  120. }
  121. if err != nil {
  122. return nil, err
  123. }
  124. }
  125. return &pb_v1.ChargeBindReply{}, nil
  126. }