123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package gate_card
- 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"
- "property-device/pb"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- )
- func checkGateCardCanBindDevicesParam(req *pb_v1.GateCardCanBindDevicesRequest) error {
- if req.GardenId < 0 {
- return status.Error(10003, "小区不能为空")
- }
- if req.CardNumber == "" {
- return status.Error(10003, "卡号不能为空")
- }
- return nil
- }
- func getGardenName(list []dbmodel.TGate) (map[int64]string, error) {
- gardenM := map[int64]string{}
- gardenIds := []int64{}
- for _, v := range list {
- if v.GardenId == 0 {
- continue
- }
- if _, ok := gardenM[v.GardenId]; ok {
- continue
- }
- gardenM[v.GardenId] = ""
- gardenIds = append(gardenIds, v.GardenId)
- }
- if len(gardenIds) > 0 {
- mreq := pb_v1.GardenInfosRequest{Ids: gardenIds}
- mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
- if err != nil {
- return nil, err
- }
- for _, v := range mreply.List {
- gardenM[v.Id] = v.GardenName
- }
- }
- return gardenM, nil
- }
- func getCardBindDevices(gardenId int64, cardNumber string) (map[int64]bool, error) {
- ucard := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", gardenId)
- where = dbmodel.WhereAdd(where, "card_number", cardNumber)
- err := ucard.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- ret := map[int64]bool{}
- if ucard.ID == 0 {
- return ret, nil
- }
- gcard := dbmodel.TGateCard{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "record_id", ucard.ID)
- list, err := gcard.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- for _, v := range list {
- ret[v.DeviceId] = true
- }
- return ret, nil
- }
- func GateCardCanBindDevices(ctx context.Context, req *pb_v1.GateCardCanBindDevicesRequest) (reply *pb_v1.GateCardCanBindDevicesReply, err error) {
- reply = &pb_v1.GateCardCanBindDevicesReply{}
- // 捕获各个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 = checkGateCardCanBindDevicesParam(req)
- if err != nil {
- return nil, err
- }
- bindedIdM, err := getCardBindDevices(req.GardenId, req.CardNumber)
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGate{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- protocols := []int32{}
- for protocol, array := range gate_utils.GateProtocolFuntionMap {
- if array[2] == 1 {
- protocols = append(protocols, protocol)
- }
- }
- where = dbmodel.WhereAdd(where, "protocol in", protocols)
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = []*pb_v1.GateItem{}
- for _, v := range list {
- if bindedIdM[v.ID] {
- continue
- }
- item := &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],
- Port: v.Port,
- }
- if v.Enable == 1 {
- item.Enable = true
- }
- reply.List = append(reply.List, item)
- }
- return reply, nil
- }
|