123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package charge
- import (
- "fmt"
- "gorm.io/gorm"
- "property-garden/errors"
- "property-garden/impl/v1/charge_utils"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "google.golang.org/grpc/status"
- )
- type spaceBindParam struct {
- chargeInfo *dbmodel.TChargeConf
- spaceIds []int64
- spaceInfoM map[int64]dbmodel.TSpacePark
- }
- func bindChargeSpace(req *pb_v1.ChargeBindRequest, param *spaceBindParam, db *gorm.DB, dbname string) (error) {
- now := time.Now()
- arrayChargeSpace := []dbmodel.TChargeBind{}
- // 去重
- olds := map[int64]bool{}
- binded := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "obj_id in": param.spaceIds,
- "obj_type": charge_utils.ObjTypeSpace,
- }
- where["charge_type"] = param.chargeInfo.ChargeType
- bindedList, err := binded.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return errors.DataBaseError
- }
- for _, v := range bindedList {
- olds[v.ObjId] = true
- }
- for _, v := range param.spaceIds {
- // 已存在不重复添加
- if olds[v] {
- continue
- }
- if param.spaceInfoM[v].HouseId == 0 {
- //continue
- return status.Error(10003, "车位管理费只能绑定已售车位")
- }
- tmp := dbmodel.TChargeBind{
- ObjId: v,
- ChargeId: req.ChargeId,
- Start: req.Start,
- CreatedAt: now,
- UpdatedAt: now,
- ChargeType: param.chargeInfo.ChargeType,
- ObjType: charge_utils.ObjTypeSpace,
- //ChargeBasis:param.chargeInfo.ChargeBasis,
- HouseId:param.spaceInfoM[v].HouseId,
- ObjName:fmt.Sprintf("%v-%v", param.spaceInfoM[v].ParkNumber, param.spaceInfoM[v].SpaceNumber),
- }
- if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSpaceArea {
- tmp.ObjArea = param.spaceInfoM[v].SpaceArea
- }
- tmp.UniqFlag.Int32 = 1
- tmp.UniqFlag.Valid = true
- arrayChargeSpace = append(arrayChargeSpace, tmp)
- }
- if len(arrayChargeSpace) == 0 {
- return nil
- }
- p := dbmodel.NewChargeBind(dbname)
- err = p.InsertMulti(db, &arrayChargeSpace)
- if err != nil {
- return errors.DataBaseError
- }
- return err
- }
- func getSpaceInfos(ids []int64, dbname string) (map[int64]dbmodel.TSpacePark, error) {
- ret := map[int64]dbmodel.TSpacePark{}
- if len(ids) == 0 {
- return ret, nil
- }
- p := dbmodel.NewSpacePark(dbname)
- where := map[string]interface{}{
- "id in":ids,
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- for _, v := range list {
- ret[v.ID] = v
- }
- return ret, nil
- }
- func addBySpaceIds(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
- if req.Start < 1 {
- return nil, status.Error(10003, "开始时间不能为空")
- }
- spaceInfoM, err := getSpaceInfos(req.ObjIds, dbname)
- if err != nil {
- return nil, err
- }
- if len(spaceInfoM) == 0 {
- return nil, status.Error(10003, "车位未录入")
- }
- spaceIds := []int64{}
- for spaceId, _ := range spaceInfoM {
- spaceIds = append(spaceIds, spaceId)
- }
- length := len(spaceIds)
- param := spaceBindParam{
- chargeInfo:chargeInfo,
- spaceInfoM:spaceInfoM,
- }
- // 分批
- for i := 0; i < length; i += 100 {
- if i+100 >= length {
- param.spaceIds = spaceIds[i:]
- err = bindChargeSpace(req, ¶m, db, dbname)
- } else {
- param.spaceIds = spaceIds[i:i+100]
- err = bindChargeSpace(req, ¶m, db, dbname)
- }
- if err != nil {
- return nil, err
- }
- }
- return &pb_v1.ChargeBindReply{}, nil
- }
|