12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package charge
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "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"
- )
- func checkChargeTimeSetParam(req *pb_v1.ChargeTimeSetRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Start == 0:
- return status.Error(10003, "开始时间不能为空")
- case req.BindId == 0:
- return status.Error(10003, "关系id不能为空")
- }
- return nil
- }
- func ChargeTimeSet(ctx context.Context, req *pb_v1.ChargeTimeSetRequest) (reply *pb_v1.ChargeTimeSetReply, err error) {
- reply = &pb_v1.ChargeTimeSetReply{}
- // 捕获各个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 = checkChargeTimeSetParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- bind := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "id":req.BindId,
- }
- err = bind.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound{
- return nil, errors.DataBaseError
- }
- if bind.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if bind.Start > req.Start {
- return nil, status.Error(10003, "开始时间只能后延")
- }
- if bind.ChargeType == charge_utils.ChargeTypeVehicle && bind.End <= bind.Start {
- return nil, status.Error(10003, "结束时间不能大于开始时间")
- }
- if bind.ChargeType != charge_utils.ChargeTypeVehicle {
- req.End = 0
- }
- values := map[string]interface{}{
- "start":req.Start,
- "end":req.End,
- }
- err = bind.Update(database.DB(), where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- return reply, nil
- }
|