123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package visitor
- 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"
- "time"
- )
- func checkGateVisitorListParam(req *pb_v1.GateVisitorListRequest) error {
- if req.Id == 0 {
- if req.DeviceId == 0 && req.Uid == 0 {
- return status.Error(10003, "设备和用户不能同时为空")
- }
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- return nil
- }
- func getGateInfo(ids []int64) (map[int64]dbmodel.TGate, error) {
- ret := map[int64]dbmodel.TGate{}
- if len(ids) == 0 {
- return ret, nil
- }
- p := dbmodel.TGate{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id in", ids)
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- for _, v := range list {
- ret[v.ID] = v
- }
- return ret, nil
- }
- func getGardenName(gardenIds []int64) (map[int64]string, error) {
- gardenM := map[int64]string{}
- 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 GateVisitorList(ctx context.Context, req *pb_v1.GateVisitorListRequest) (reply *pb_v1.GateVisitorListReply, err error) {
- reply = &pb_v1.GateVisitorListReply{}
- // 捕获各个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 = checkGateVisitorListParam(req)
- if err != nil {
- return nil, err
- }
- g := dbmodel.TGateVisitor{}
- where := [][2]interface{}{}
- if req.Id > 0 {
- where = dbmodel.WhereAdd(where, "id", req.Id)
- }
- if req.DeviceId > 0 {
- where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
- }
- if req.End > 0 {
- where = dbmodel.WhereAdd(where, "end <", req.End)
- }
- if req.Start > 0 {
- where = dbmodel.WhereAdd(where, "start >=", req.Start)
- }
- if req.Visitor != "" {
- where = dbmodel.WhereAdd(where, "visitor", req.Visitor)
- }
- if req.Name != "" {
- where = dbmodel.WhereAdd(where, "name", req.Name)
- }
- if req.Uid > 0 {
- where = dbmodel.WhereAdd(where, "uid", req.Uid)
- }
- reply.Total, err = g.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.Page = req.Page
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := g.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- ids := []int64{}
- gardenIds := []int64{}
- if req.DeviceId != 0 {
- ids = []int64{req.DeviceId}
- } else {
- m := map[int64]bool{}
- for _, v := range list {
- if m[v.DeviceId] {
- continue
- }
- ids = append(ids, v.DeviceId)
- m[v.DeviceId] = true
- }
- }
- m := map[int64]bool{}
- for _, v := range list {
- if m[v.GardenId] {
- continue
- }
- gardenIds = append(gardenIds, v.GardenId)
- m[v.GardenId] = true
- }
- gateInfoM, err := getGateInfo(ids)
- if err != nil {
- return nil, err
- }
- gardenInfoM, err := getGardenName(gardenIds)
- if err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.GateVisitorListItem, len(list))
- for i, v := range list {
- item := &pb_v1.GateVisitorListItem{
- Uid: v.Uid,
- DeviceId: v.DeviceId,
- GateName: gateInfoM[v.DeviceId].DeviceName,
- Name: v.Name,
- Visitor: v.Visitor,
- VisitorPhone: v.VisitorPhone,
- Phone: v.Phone,
- OpenTime: v.OpenTime,
- Start: v.Start,
- End: v.End,
- Status: 1,
- Id: v.ID,
- Comment: v.Comment,
- Qcode: v.Qcode,
- Location: gateInfoM[v.DeviceId].Location,
- GardenName: gardenInfoM[v.GardenId],
- }
- if v.OpenTime > 0 {
- item.Status = 2
- } else if v.End < time.Now().Unix() {
- item.Status = 3
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|