123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- // 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"
- "github.com/jinzhu/gorm"
- "smart-government-management/consts"
- "smart-government-management/errors"
- "smart-government-management/model"
- "smart-government-management/pb/v1"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- )
- func ChannelList(ctx context.Context, req *v1.ChannelListRequest) (reply *v1.ChannelListReply, err error) {
- if req.Sn == "" {
- return nil, errors.ParamsError
- }
- if req.Page < 1 {
- req.Page = 1
- }
- p := model.TDevice{}
- where := map[string]interface{}{
- "sn":req.Sn,
- }
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound{
- logger.Error("ChannelList",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if p.Id == 0 {
- return nil, errors.NoRecordError
- }
- if p.VerifyStatus != consts.DeviceStatusAddAuditted {
- return nil, errors.VssStatusError
- }
- pc := model.Vsschanneltbl{}
- where = map[string]interface{}{
- "DevPubID":req.Sn,
- }
- if req.ChannelNo != "" {
- where["ChanPubID like"] = "%"+req.ChannelNo+"%"
- }
- count, err := pc.Count(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound{
- logger.Error("ChannelList",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- reply = &v1.ChannelListReply{
- Page:req.Page,
- PageSize:int32(model.PageSize),
- Total:count,
- }
- if count == 0 {
- return reply, nil
- }
- list, err := pc.List(database.DB(), where, req.Page)
- if err != nil && err != gorm.ErrRecordNotFound{
- logger.Error("ChannelList",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- reply.List = make([]*v1.ChannelItem, len(list))
- for i, v := range list {
- item := &v1.ChannelItem{
- Name:v.Nickname,
- Id:v.ID,
- ChannelNo:v.Chanpubid,
- State:int32(v.Alive),
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|