123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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"
- "property-device/errors"
- dbmodel "property-device/model"
- "property-device/pb"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- )
- func checkGateListParam(req *pb_v1.GateListRequest) error {
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- 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 GateList(ctx context.Context, req *pb_v1.GateListRequest) (reply *pb_v1.GateListReply, err error) {
- reply = &pb_v1.GateListReply{}
- // 捕获各个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 = checkGateListParam(req)
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGate{}
- where := [][2]interface{}{}
- if len(req.UnitIds) > 0 {
- if req.GardenId == 0 {
- return nil, status.Error(10003, "小区不能为空")
- }
- w := dbmodel.TGateUnit{}
- filter := [][2]interface{}{}
- filter = dbmodel.WhereAdd(filter, "garden_id", req.GardenId)
- filter = dbmodel.WhereAdd(filter, "unit_id in", req.UnitIds)
- list, err := w.List(database.DB(), filter, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- ids := []int64{}
- for _, v := range list {
- ids = append(ids, v.DeviceId)
- }
- if len(ids) == 0 {
- return reply, nil
- }
- where = dbmodel.WhereAdd(where, "id in", ids)
- }
- if req.GardenId > 0 {
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- }
- if req.Sn != "" {
- where = dbmodel.WhereAdd(where, "sn like", "%"+req.Sn+"%")
- }
- if req.DeviceId != 0 {
- where = dbmodel.WhereAdd(where, "id", req.DeviceId)
- }
- if req.Out == 1 {
- where = dbmodel.WhereAdd(where, "garden_id >", 0)
- }
- if req.Out == 2 {
- where = dbmodel.WhereAdd(where, "garden_id", 0)
- }
- if req.Manufactor != "" {
- where = dbmodel.WhereAdd(where, "manufactor like", "%"+req.Manufactor+"%")
- }
- if req.DeviceName != "" {
- where = dbmodel.WhereAdd(where, "device_name like", "%"+req.DeviceName+"%")
- }
- if req.QcodeSupport > 0 {
- where = dbmodel.WhereAdd(where, "qcode_support", req.QcodeSupport)
- }
- if req.PicSupport > 0 {
- where = dbmodel.WhereAdd(where, "pic_support", req.PicSupport)
- }
- if req.CardSupport > 0 {
- where = dbmodel.WhereAdd(where, "card_support", req.CardSupport)
- }
- count, err := p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.Total = count
- reply.Page = req.Page
- if count == 0 {
- return reply, nil
- }
- list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- gardenM, err := getGardenName(list)
- if err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.GateItem, len(list))
- for i, v := range list {
- 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: gardenM[v.GardenId],
- // 出库人
- 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 {
- reply.List[i].Enable = true
- }
- }
- return reply, nil
- }
|