// 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" "smart-site-management/consts" "smart-site-management/errors" "smart-site-management/model" "smart-site-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.ProjectId == 0 { return nil, errors.ParamsError } where := map[string]interface{}{ "project_id":req.ProjectId, } if len(req.StatusFilters) > 0 { where["verify_status in"] = req.StatusFilters } p := &model.TDevice{} if req.Page < 1 { req.Page = 1 } if req.PageSize < 1 { req.PageSize = 10 } 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, PageSize:req.PageSize, } 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, ChannelNo:v.ChannelNo, ChannelName:v.ChannelName, } 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 }