123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package charge
- import (
- "git.getensh.com/common/gopkgs/database"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "property-garden/errors"
- "property-garden/impl/v1/charge_utils"
- "property-garden/impl/v1/vehicle"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "time"
- )
- type vehicleBindParam struct {
- chargeInfo *dbmodel.TChargeConf
- vehicleInfo dbmodel.TVehicle
- }
- type vehiclesBindParam struct {
- chargeInfo *dbmodel.TChargeConf
- vehicleIds []int64
- vehicleInfoM map[int64]dbmodel.TVehicle
- }
- func bindChargeVehicles(req *pb_v1.ChargeBindRequest, param *vehiclesBindParam, db *gorm.DB, dbname string) (error) {
- now := time.Now()
- arrayChargeVehicle := []dbmodel.TChargeBind{}
- // 去重
- olds := map[int64]bool{}
- binded := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "obj_id in": param.vehicleIds,
- "obj_type": charge_utils.ObjTypeVehicle,
- }
- 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.vehicleIds {
- // 已存在不重复添加
- if olds[v] {
- continue
- }
- if param.vehicleInfoM[v].BindType == 2 {
- //continue
- return status.Error(10003, "车辆已绑定已售车位,该车不能绑定该费用")
- }
- tmp := dbmodel.TChargeBind{
- ObjId: v,
- ChargeId: req.ChargeId,
- Start: req.Start,
- End: req.End,
- CreatedAt: now,
- UpdatedAt: now,
- ChargeType: param.chargeInfo.ChargeType,
- ObjType: charge_utils.ObjTypeVehicle,
- //ChargeBasis:param.chargeInfo.ChargeBasis,
- ObjName:param.vehicleInfoM[v].PlateNo,
- }
- if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
- tmp.CustomFee = req.VehicleFee
- }
- tmp.UniqFlag.Int32 = 1
- tmp.UniqFlag.Valid = true
- arrayChargeVehicle = append(arrayChargeVehicle, tmp)
- }
- if len(arrayChargeVehicle) == 0 {
- return nil
- }
- p := dbmodel.NewChargeBind(dbname)
- err = p.InsertMulti(db, &arrayChargeVehicle)
- if err != nil {
- return errors.DataBaseError
- }
- return err
- }
- func bindChargeVehicle(req *pb_v1.ChargeBindRequest, param *vehicleBindParam, db *gorm.DB, dbname string) error {
- now := time.Now()
- // 去重
- binded := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "obj_id": param.vehicleInfo.ID,
- "obj_type": charge_utils.ObjTypeVehicle,
- }
- where["charge_type"] = param.chargeInfo.ChargeType
- count, err := binded.Count(database.DB(), where, nil)
- if err != nil {
- return errors.DataBaseError
- }
- if count > 0 {
- return status.Error(10003, "操作失败,该车辆已绑定费用")
- }
- bind := dbmodel.TChargeBind{
- ObjId: param.vehicleInfo.ID,
- ChargeId: req.ChargeId,
- Start: req.Start,
- End: req.End,
- CreatedAt: now,
- UpdatedAt: now,
- ChargeType: param.chargeInfo.ChargeType,
- ObjType: charge_utils.ObjTypeVehicle,
- //ChargeBasis:param.chargeInfo.ChargeBasis,
- ObjName:param.vehicleInfo.PlateNo,
- }
- if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
- bind.CustomFee = req.VehicleFee
- }
- bind.UniqFlag.Int32 = 1
- bind.UniqFlag.Valid = true
- bind.SetTable(dbname)
- err = bind.Insert(db)
- if err != nil {
- return errors.DataBaseError
- }
- return err
- }
- func getVehicleInfo(id int64, dbname string) (dbmodel.TVehicle, error) {
- ret := dbmodel.TVehicle{}
- if id == 0 {
- return ret, nil
- }
- p := dbmodel.NewVehicle(dbname)
- where := map[string]interface{}{
- "id": id,
- }
- err := p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound{
- return ret, errors.DataBaseError
- }
- ret = *p
- return ret, nil
- }
- func getVehicleInfos(ids []int64, dbname string) (map[int64]dbmodel.TVehicle, error) {
- ret := map[int64]dbmodel.TVehicle{}
- if len(ids) == 0 {
- return ret, nil
- }
- p := dbmodel.NewVehicle(dbname)
- where := map[string]interface{}{
- "id in": ids,
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil && err != gorm.ErrRecordNotFound{
- return ret, errors.DataBaseError
- }
- for _, v := range list {
- ret[v.ID] = v
- }
- return ret, nil
- }
- func addByVehicleId(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
- if (req.Start > 0 && req.End == 0) || (req.Start == 0 && req.End > 0) {
- return nil, status.Error(10003, "开始和结束时间只能同时存在或同时为空")
- }
- if len(req.ObjIds) == 0 {
- return &pb_v1.ChargeBindReply{}, nil
- }
- vehicleInfo, err := getVehicleInfo(req.ObjIds[0], dbname)
- if err != nil {
- return nil, err
- }
- if vehicleInfo.ID == 0 {
- return nil, status.Error(10003, "未找到车辆")
- }
- if vehicleInfo.BindType == vehicle.BindTypeSell {
- return nil, status.Error(10003, "该车辆已绑定出售车位,请使用车位费")
- }
- param := vehicleBindParam{
- chargeInfo: chargeInfo,
- vehicleInfo: vehicleInfo,
- }
- err = bindChargeVehicle(req, ¶m, db, dbname)
- return &pb_v1.ChargeBindReply{}, nil
- }
- func addByVehicleIds(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
- if (req.Start > 0 && req.End == 0) || (req.Start == 0 && req.End > 0) {
- return nil, status.Error(10003, "开始和结束时间只能同时存在或同时为空")
- }
- if len(req.ObjIds) == 0 {
- return &pb_v1.ChargeBindReply{}, nil
- }
- vehicleInfoM, err := getVehicleInfos(req.ObjIds, dbname)
- if err != nil {
- return nil, err
- }
- if len(vehicleInfoM) == 0 {
- return nil, status.Error(10003, "未找到车辆")
- }
- param := vehiclesBindParam{
- chargeInfo: chargeInfo,
- vehicleInfoM: vehicleInfoM,
- }
- vehicleIds := []int64{}
- for vehicleId, _ := range vehicleInfoM {
- vehicleIds = append(vehicleIds, vehicleId)
- }
- length := len(vehicleIds)
- // 分批
- for i := 0; i < length; i += 100 {
- if i+100 >= length {
- param.vehicleIds = vehicleIds[i:]
- err = bindChargeVehicles(req, ¶m, db, dbname)
- } else {
- param.vehicleIds = vehicleIds[i:i+100]
- err = bindChargeVehicles(req, ¶m, db, dbname)
- }
- if err != nil {
- return nil, err
- }
- }
- return &pb_v1.ChargeBindReply{}, nil
- }
|