// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package project import ( "context" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/logger" "go.uber.org/zap" "smart-site-management/errors" "smart-site-management/model" "smart-site-management/pb/v1" ) func ProjectDeviceStatistics(ctx context.Context, req *v1.ProjectDeviceStatisticsRequest) (reply *v1.ProjectDeviceStatisticsReply, err error) { reply = &v1.ProjectDeviceStatisticsReply{} if req.ProjectId == 0 { return nil, errors.ParamsError } pt := &model.TDeviceTypeItem{} m, _, err := pt.AllMap(database.DB()) if err != nil { logger.Error("DeviceList", zap.String("err", err.Error())) return nil, errors.DataBaseError } p := &model.TDevice{} list, err := p.StatisticWithProject(database.DB(), req.ProjectId) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*v1.ProjectDeviceStatistics, len(list)) for i, v := range list { item := &v1.ProjectDeviceStatistics{ DeviceType:m[v.TypeCode], Total:v.Total, Online:v.Online, } reply.List[i] = item } return reply, nil }