// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package device import ( "context" "github.com/jaryhe/gopkgs/database" "smart-enterprise-management/consts" "smart-enterprise-management/errors" "smart-enterprise-management/model" "smart-enterprise-management/pb/v1" "github.com/jaryhe/gopkgs/logger" "go.uber.org/zap" ) func DeviceList(ctx context.Context, req *v1.DeviceListRequest) (reply *v1.DeviceListReply, err error) { reply = &v1.DeviceListReply{} if req.Cid < 1 { return nil, errors.DataBaseError } p := &model.TDevice{} if req.Page < 1 { req.Page = 1 } pt := &model.TDeviceTypeItem{} m, _, err := pt.AllMap(database.DB()) if err != nil { logger.Error("DeviceList", zap.String("err", err.Error())) return nil, errors.DataBaseError } dbReq := model.ListRawRequest{ Page:req.Page, ProjectId:req.ProjectId, ProviderId:req.ProviderId, Filter:req.Filter, StatusFilter:req.StatusFilters, IsAll:req.IsAll, CanDel:req.CanDel, TypeCode:req.TypeCode, Cid:req.Cid, } list, total, err := p.ListRaw(database.DB(), dbReq) if err != nil { logger.Error("DeviceList", zap.String("err", err.Error())) return nil, errors.DataBaseError } reply.Page = req.Page reply.Total = total reply.PageSize = int32(model.PageSize) if total == 0 { return reply, nil } reply.List = make([]*v1.DeviceItem, len(list)) for i, v := range list{ item := &v1.DeviceItem{ Id: int64(v.Id), Sn: v.Sn, TypeCode: v.TypeCode, TypeName: m[v.TypeCode], ProjectName: v.ProjectName, SafetyRecordNo: v.SafetyRecordNo, ApplyTime: v.CreatedTime.Format(layout), Status:v.Status, State:v.State, Key:v.Key, ProviderName:v.ProviderName, SocialCode:v.SocialCode, Name:v.Name, } if item.Status == consts.DeviceStatusAddAuditted || item.Status == consts.DeviceStatusAddAudittedFail { item.ApproveTime = v.ProjectApproveTime.Format(layout) } reply.List[i] = item } reply.Page = req.Page reply.Total = total reply.PageSize = int32(model.PageSize) return reply, nil }