123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package gate_unit
- 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"
- "property-device/utils/gate_utils"
- )
- func checkGateUnitDeviceParam(req *pb_v1.GateUnitDeviceRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case len(req.UnitId) == 0:
- return status.Error(10003, "单元不能为空")
- }
- return nil
- }
- func GateUnitDevice(ctx context.Context, req *pb_v1.GateUnitDeviceRequest) (reply *pb_v1.GateUnitDeviceReply, err error) {
- reply = &pb_v1.GateUnitDeviceReply{}
- // 捕获各个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 = checkGateUnitDeviceParam(req)
- if err != nil {
- return nil, err
- }
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGateUnit{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- where = dbmodel.WhereAdd(where, "unit_id in", req.UnitId)
- if req.OnlyHas {
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if p.ID > 0 {
- reply.HasDevice = true
- }
- return reply, nil
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- deviceIds := []int64{}
- dm := map[int64]bool{}
- for _, v := range list {
- if dm[v.DeviceId] {
- continue
- }
- dm[v.DeviceId] = true
- deviceIds = append(deviceIds, v.DeviceId)
- }
- if len(deviceIds) == 0 {
- return reply, nil
- }
- gt := &dbmodel.TGate{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id in", deviceIds)
- glist, err := gt.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if len(glist) == 0 {
- return reply, nil
- }
- reply.List = make([]*pb_v1.GateItem, len(glist))
- for i, v := range glist {
- reply.List[i] = &pb_v1.GateItem{
- // 设备id
- DeviceId: v.ID,
- // 设备名
- DeviceName: v.DeviceName,
- // 序列号
- Sn: v.Sn,
- // 厂商
- Manufactor: v.Manufactor,
- // 授权key
- AuthKey: v.AuthKey,
- // 协议
- Protocol: v.Protocol,
- // 小区id
- GardenId: v.GardenId,
- // 小区名
- GardenName: "",
- // 出库人
- OutUser: v.OutUser,
- // 出库时间
- OutTime: v.OutTime,
- // 1 在线 2 离线
- Status: v.Status,
- Enable: false,
- // 1 进场 2 出场 3 进出场
- Direction: v.Direction,
- Location: v.Location,
- QcodeSupport: v.QcodeSupport,
- PicSupport: v.PicSupport,
- CardSupport: v.CardSupport,
- Ip: v.Ip,
- Mac: v.Mac,
- UserName: v.UserName,
- Password: v.Password,
- ProtocolDesc: gate_utils.GateProtocolNameMap[v.Protocol],
- }
- if v.Enable == 1 {
- reply.List[i].Enable = true
- }
- }
- return reply, nil
- }
|