package com.ld.igds.gas.impl; import com.baomidou.mybatisplus.plugins.Page; import com.ld.igds.common.CoreCommonService; import com.ld.igds.constant.BizType; import com.ld.igds.constant.RedisConst; import com.ld.igds.gas.CoreGasService; import com.ld.igds.gas.GasDataBuilder; import com.ld.igds.gas.dto.GasData; import com.ld.igds.gas.dto.GasParam; import com.ld.igds.gas.mapper.GasServiceMapper; import com.ld.igds.io.constant.OrderRespEnum; import com.ld.igds.models.Depot; import com.ld.igds.models.DicSysConf; import com.ld.igds.models.Gas; import com.ld.igds.models.GasInfo; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.RedisUtil; import com.ld.igds.websocket.WebSocketPacket; import com.ld.igds.websocket.WebSocketServer; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.*; @Slf4j @Component(CoreGasService.BEAN_ID) public class CoreGasServiceImpl implements CoreGasService { @Resource private GasServiceMapper gasMapper; @Resource private RedisUtil redisUtil; @Resource private CoreCommonService commonService; @Override public List pageQueryList(GasParam param) { Page page = new Page<>(param.getPage(), param.getLimit()); page.setSearchCount(false); List result; if (null == param.getStart()) {// 表明没有设置时间参数,为了保证正常获取数据 result = gasMapper.pageListGas(page, param); } else {// 如果有参数,将pageSize设置为100 page.setSize(100); result = gasMapper.pageListGas(page, param); } if (null == result || result.isEmpty()) { return result; } // 粮情数据调整 if (param.isTagUpdate()) { // 获取配置信息 DicSysConf conf = commonService.getCacheSysConf(result.get(0).getCompanyId()); for (GasData data : result) { GasDataBuilder.updatePestData(data, conf); } } return result; } @Override public List queryChartList(GasParam param) { if (null == param.getStart()) { param.setLimit(20); } else { param.setLimit(1000); } Page page = new Page(param.getPage(), param.getLimit()); return gasMapper.pageListChart(page, param); } @Override public List getInfoGas(Map parameter) throws Exception { String companyId = (String) parameter.get("companyId"); String depotId = (String) parameter.get("depotId"); String batchId = (String) parameter.get("batchId"); if (StringUtils.isEmpty(companyId) || StringUtils.isEmpty(depotId) || StringUtils.isEmpty(batchId)) return null; Map args = new HashMap<>(); args.put("id", ContextUtil.buildInfoId(companyId, depotId, batchId)); return gasMapper.getInfoGas(args); } @Override public void saveInfoGas(List result) { for (GasInfo gasInfo : result) { try { gasMapper.saveGasInfo(gasInfo); } catch (Exception e) { log.error(e.getMessage(), e); } } } @Override public void saveOrUpdateData(Gas gas) { try { gasMapper.saveGas(gas); //更新缓存中的数据 updateCacGasData(gas); } catch (Exception e) { log.error(e.getMessage(), e); } } @SuppressWarnings("unchecked") @Override public List listCacheData(String companyId, String deptId) { String patten = RedisConst.buildKey(companyId, RedisConst.KEY_GAS_DATA); Set keys = redisUtil.keys(patten); if (null == keys) return null; List result = new ArrayList<>(); GasData gasData; for (String key : keys) { gasData = (GasData) redisUtil.get(key); if (null == gasData) continue; result.add(gasData); } return result; } @Override public Map cacheMapGasData(String companyId, String deptId) { String patten = RedisConst.buildKey(companyId, RedisConst.KEY_GAS_DATA); Set keys = redisUtil.keys(patten); if (null == keys) return null; Map result = new HashMap<>(); GasData gasData; for (String key : keys) { gasData = (GasData) redisUtil.get(key); if (null == gasData) continue; result.put(gasData.getDepotId(), gasData); } return result; } public void updateCacGasData(Gas data) { String key = RedisConst.buildKey(data.getCompanyId(), RedisConst.KEY_GAS_DATA, data.getDepotId()); GasData gasData = new GasData(); BeanUtils.copyProperties(data, gasData); redisUtil.set(key, gasData); //推动大屏 Depot depot = commonService.getCacheDepot(data.getCompanyId(), data.getDepotId()); if (null == depot) { log.error("-----------跟新缓存没获取到当前仓库信息-------"); return; } Map mapData = this.cacheMapGasData(data.getCompanyId(), depot.getDeptId()); // 气体缓存更新后,需要通知到大屏 WebSocketPacket packet = new WebSocketPacket(); packet.setBizType(BizType.SCREEN.getCode()); packet.setCompanyId(ContextUtil.getDefaultCompanyId()); packet.setDeptId(depot.getDeptId()); packet.setBizId(BizType.GAS.getCode()); packet.setOrderResp(OrderRespEnum.MSG_SUCCESS.getCode()); packet.setData(mapData); WebSocketServer.sendByPocket(packet); } }