// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package charge import ( "context" "encoding/json" "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkChargeBindParam(req *pb_v1.ChargeBindRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.ChargeId == 0: return status.Error(10003, "费用项目不能为空") case len(req.ObjIds) == 0: return status.Error(10003, "绑定对象不能为空") } return nil } func ChargeBind(ctx context.Context, req *pb_v1.ChargeBindRequest) (reply *pb_v1.ChargeBindReply, err error) { reply = &pb_v1.ChargeBindReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() // 参数检查 err = checkChargeBindParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) chargeInfo := dbmodel.NewChargeConf(dbname) where := map[string]interface{}{ "id":req.ChargeId, //"garden_id":req.GardenId, } err = chargeInfo.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if chargeInfo.ID == 0 { return nil, errors.ErrRecordNotFound } if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf && req.VehicleFee == 0 { return nil, status.Error(10003, "车辆自定义费用不能为空") } if req.Start > 0 { t := time.Unix(req.Start, 0) req.Start = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix() } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() switch chargeInfo.ChargeType{ case charge_utils.ChargeTypeSpace: reply, err = addBySpaceIds(req, chargeInfo, db, dbname) case charge_utils.ChargeTypeVehicle: reply, err = addByVehicleIds(req, chargeInfo, db, dbname) default: reply, err = addByHouseIds(req, chargeInfo, db, dbname) } if err != nil { return nil, err } db.Commit() return reply, nil }