// Copyright 2019 githup.com. All rights reserved. // Use of this source code is governed by githup.com. package user import ( "context" "github.com/jaryhe/gopkgs/database" "smart-enterprise-management/consts" "smart-enterprise-management/errors" dbmodel "smart-enterprise-management/model" pb_v1 "smart-enterprise-management/pb/v1" "github.com/jaryhe/gopkgs/logger" "go.uber.org/zap" ) func Publicity(ctx context.Context, req *pb_v1.PublicityRequest)(reply *pb_v1.PublicityReply, err error) { // 设备类型map pdt := &dbmodel.TDeviceTypeItem{} m, _, err := pdt.AllMap(database.DB()) if err != nil { logger.Error("publicity", zap.String("err", err.Error())) return nil, errors.DataBaseError } // 供应商公示列表 reply = &pb_v1.PublicityReply{} list, total, err := dbmodel.PublicityList(database.DB(), req.Provider) if err != nil { logger.Error("publicity", zap.String("err", err.Error())) return nil, errors.DataBaseError } if total == 0 { return reply, nil } // 设配类型 pids := make([]int64, len(list)) for i, v := range list { pids[i] = v.Id } pt := dbmodel.TProviderDevice{} where := map[string]interface{}{ "provider_id in":pids, "status":consts.DeviceTypeStatusAddAuditted, } if req.DeviceCode != 0 { where["device_code"] = req.DeviceCode } dlist, err := pt.List(database.DB(), where) if err != nil { logger.Error("publicity", zap.String("err", err.Error())) return nil, errors.DataBaseError } dmap := map[int64][]dbmodel.TProviderDevice{} for _, v := range dlist { dmap[v.ProviderId] = append(dmap[v.ProviderId], v) } // 组装结果 reply.List = make([]*pb_v1.PublicityItem, len(dmap)) k := 0 for _, v := range list { item := &pb_v1.PublicityItem{ Name:v.Name, SocialCode:v.SocialCode, } if len(dmap[v.Id]) == 0 { continue } if dsublist, ok := dmap[v.Id]; ok { for _, value := range dsublist { subItem := &pb_v1.PublicityDeviceTypeItem{ TypeCode:value.DeviceCode, TypeName:m[value.DeviceCode], } item.Devices = append(item.Devices, subItem) } } reply.List[k] = item k++ } return reply, nil }