package tower import ( "context" "github.com/jaryhe/gopkgs/database" "smart-site-management/errors" "smart-site-management/model" "smart-site-management/pb/v1" ) // iot 设备列表 func IotList(ctx context.Context, req *v1.IotListRequest)(reply *v1.IotListReply, err error) { if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } if req.DeviceCode == 0 { return nil, errors.ParamsError } if req.ProjectId == 0 { return nil, errors.ParamsError } // 总数 reply = &v1.IotListReply{} p := model.TDevice{} where := map[string]interface{}{ "device_code":req.DeviceCode, "verify_status":1, "project_id":req.ProjectId, } total, err := p.Count(database.DB(), where) if err != nil { return nil, errors.DataBaseError } reply.PageSize = req.PageSize reply.Page = req.Page reply.Total = total if total == 0{ return reply, nil } // 在线离线数 where["status"] = 1 p.Id = 0 online, err := p.Count(database.DB(), where) if err != nil { return nil, errors.DataBaseError } reply.Online = online reply.Offline = total - online p.Id = 0 where = map[string]interface{}{ "device_code":req.DeviceCode, "verify_status":1, "project_id":req.ProjectId, } // 列表 list, err := p.List(database.DB(), where, req.Page, req.PageSize) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*v1.IotListItem, len(list)) for i, v := range list { item := &v1.IotListItem{ State:int32(v.Status), Id:v.Id, Name:v.Name, Sn:v.Sn, } reply.List[i] = item } return reply, nil }