package com.ld.igds.three.service.impl; import com.alibaba.fastjson.JSONObject; import com.ld.igds.common.CoreCommonService; import com.ld.igds.common.CoreDeviceService; import com.ld.igds.common.CoreThreeService; import com.ld.igds.common.dto.DepotSerData; import com.ld.igds.models.Device; import com.ld.igds.models.ThreeConf; import com.ld.igds.three.ThreeCodeEnum; import com.ld.igds.three.ThreeConstant; import com.ld.igds.three.data.Data3001; import com.ld.igds.three.data.ThreeResponse; import com.ld.igds.three.param.ThreeCommonParam; import com.ld.igds.three.param.ThreeRequest; import com.ld.igds.three.service.ThreeService; import com.ld.igds.three.util.ThreeRespUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 3001-批量设备信息接口实现 * * @author chen */ @Service public class ThreeServiceImpl3001 implements ThreeService { @Autowired private CoreCommonService commonService; @Autowired private CoreThreeService coreThreeService; @Autowired private CoreDeviceService deviceService; @Override public String getInterfaceId() { return ThreeConstant.API_THREE_3001; } @SuppressWarnings("unchecked") @Override public ThreeResponse execute(ThreeRequest req) { //转化请求参数 ThreeCommonParam param = JSONObject.parseObject(req.getData().toString(), ThreeCommonParam.class); //判断uid if (StringUtils.isEmpty(param.getUid())) { return ThreeRespUtil.error(ThreeCodeEnum.CODE_1005, req); } //获取全部三维配置信息 List threeList = coreThreeService.getCacheThreeAll(req.getCompanyId()); if (threeList == null || threeList.isEmpty()) { return ThreeRespUtil.error(ThreeCodeEnum.CODE_1111, "未查询相关三维配置信息,请联系管理员!", req); } //返回数据定义 List list = new ArrayList<>(); //存放分机获取的设备信息 List deviceList = new ArrayList<>(); //根据uid判断查询所有设备信息还是查询某个仓库设备信息 if (ThreeConstant.REQUEST_UID.equals(param.getUid())) { //从三维配置中获取所有仓库编码集合 List depotIds = threeList.stream().map(ThreeConf::getDepotId).collect(Collectors.toList()); //根据仓库获取对应分机 List depotSerList = commonService.getCacheDepotSerByDepots(req.getCompanyId(), depotIds); //根据分机获取设备,存入deviceList getDevice(depotSerList, deviceList); if(deviceList.isEmpty()){ return ThreeRespUtil.success(list, req); } //筛选返回数据 for (ThreeConf threeConf : threeList) { if (StringUtils.isNotEmpty(threeConf.getDeviceId())) { getData(threeConf, deviceList, list); } } } else { //根据uid获取三维配置信息 ThreeConf three = coreThreeService.getCacheThreeConfById(req.getCompanyId(), param.getUid()); if (three == null) { return ThreeRespUtil.error(ThreeCodeEnum.CODE_1111, "未查询到此uid的配置信息,请联系管理员!", req); } //根据仓库编码获取分机 List depotList = new ArrayList<>(); depotList.add(three.getDepotId()); List depotSerList = commonService.getCacheDepotSerByDepots(req.getCompanyId(), depotList); //根据分机获取设备,存入deviceList getDevice(depotSerList, deviceList); if(deviceList.isEmpty()){ return ThreeRespUtil.success(list, req); } //筛选返回数据 for (ThreeConf threeConf : threeList) { if (three.getDepotId().equals(threeConf.getDepotId()) && StringUtils.isNotEmpty(threeConf.getDeviceId())) { getData(threeConf, deviceList, list); } } } return ThreeRespUtil.success(list, req); } /** * 数据封装 * * @param threeConf * @param deviceList * @return */ private void getData(ThreeConf threeConf, List deviceList, List list) { Data3001 data; for (Device device : deviceList) { if(device.getId().equals(threeConf.getDeviceId())){ data = new Data3001(); data.setUid(threeConf.getUid()); data.setName(device.getName()); data.setType(device.getTypeName()); data.setStatus(device.getStatusName()); list.add(data); } } } /** * 根据分机获取分机下设备 * * @param depotSerList * @param deviceList * @return */ private void getDevice(List depotSerList, List deviceList) { for (DepotSerData depotSerData : depotSerList) { List tempList = deviceService.getCacheDeviceBySerId( depotSerData.getCompanyId(), depotSerData.getSerId()); deviceList.addAll(tempList); } } }