123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package gate
- 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-device/errors"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- "time"
- )
- func checkGateSetParam(req *pb_v1.GateSetRequest) error {
- switch {
- case req.DeviceId == 0:
- return status.Error(10003, "设备id不能为空")
- case req.GardenId == 0:
- return status.Error(10003, "小区id不能为空")
- case req.Location == "":
- return status.Error(10003, "位置不能为空")
- case req.Direction == 0:
- return status.Error(10003, "进场出场类型不能为空")
- }
- return nil
- }
- func GateSet(ctx context.Context, req *pb_v1.GateSetRequest) (reply *pb_v1.GateSetReply, err error) {
- reply = &pb_v1.GateSetReply{}
- // 捕获各个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 = checkGateSetParam(req)
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGate{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", req.DeviceId)
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if p.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- now := time.Now()
- values := map[string]interface{}{
- "updated_at": now,
- "location": req.Location,
- "direction": req.Direction,
- "ip": req.Ip,
- "user_name": req.UserName,
- "password": req.Password,
- "mac": req.Mac,
- "port": req.Port,
- }
- if req.GateName != "" {
- values["device_name"] = req.GateName
- }
- err = p.Update(database.DB(), where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- delGateInfoFromRedis(req.DeviceId, "", 0)
- delGateInfoFromRedis(0, p.Sn, p.Protocol)
- return reply, nil
- }
|