package com.jjt.biz.service.impl; import com.jjt.biz.service.IApiAllService; import com.jjt.biz.vo.*; import com.jjt.common.constant.CacheConstants; import com.jjt.common.core.redis.RedisCache; import com.jjt.common.utils.DateUtils; import com.jjt.ws.domain.TwinWorkshopCalc; import com.jjt.ws.service.ITwinWorkshopCalcService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** * 首页统计数据 * * @author wukai * @date 2024/5/4 20:35 */ @Service @Slf4j public class ApiAllServiceImpl implements IApiAllService { @Resource private RedisCache redisCache; @Resource private ITwinWorkshopCalcService wsCalcService; /** * 获取当前库存数据 * * @return 库存数据 */ @Override public Map stock() { return redisCache.getCacheObject(CacheConstants.VMS_STOCK); } /** * 获取本月平均值 * * @return 结果 */ @Override public AvgMonthVO avgMonth() { AvgMonthVO vo = new AvgMonthVO(); vo.mock(); redisCache.setCacheObject(CacheConstants.AVG_MONTH, vo); return redisCache.getCacheObject(CacheConstants.AVG_MONTH); } /** * 获取生产情况 * * @return 结果 */ @Override public CurrYieldVO currYield() { CurrYieldVO vo = new CurrYieldVO(); vo.mock(); IndexData todayData = redisCache.getCacheObject(CacheConstants.INDEX_CALC); IndexData currData = redisCache.getCacheObject(CacheConstants.INDEX_ALARM); vo.getJb().setYield(BigDecimal.valueOf(todayData.getEfficiency().getTotalLength()).setScale(0, RoundingMode.HALF_UP)); vo.getJb().setYieldMax(BigDecimal.valueOf(50000)); BigDecimal jdl = BigDecimal.valueOf(todayData.getEfficiency().getAEfficiency() + todayData.getEfficiency().getBEfficiency()).divide(BigDecimal.valueOf(2), 0, RoundingMode.HALF_UP); if (todayData.getEfficiency().getBEfficiency() == 0f) { jdl = BigDecimal.valueOf(todayData.getEfficiency().getAEfficiency()).setScale(0, RoundingMode.HALF_UP); } vo.getJb().setJdl(jdl); // vo.getJb().setOpen(currData.getDevice().getOnline(),currData.getDevice().getTotal()); vo.getJb().setOpenNum(currData.getDevice().getOnline()); vo.getJb().setTotalNum(currData.getDevice().getTotal()); vo.getJb().setOpenRatio(BigDecimal.valueOf(currData.getDevice().getRunningRatio()).setScale(0, RoundingMode.HALF_UP)); redisCache.setCacheObject(CacheConstants.CURR_YIELD, vo); return redisCache.getCacheObject(CacheConstants.CURR_YIELD); } /** * 获取生产趋势 * * @return 结果 */ @Override public List prodTrade() { List list = new ArrayList<>(); IndexData id = redisCache.getCacheObject(CacheConstants.INDEX_CALC); List weekDataList = id.getWeekData(); for (WeekData wd : weekDataList) { ProdTradeVO vo = new ProdTradeVO(); vo.mock(); vo.setJbLength(wd.getLength()); vo.setJbWeight(wd.getWeight()); LocalDate localDate = LocalDate.parse(wd.getTime()); vo.setDate(week(localDate)); vo.setTips(localDate.toString()); list.add(vo); } return list; } /** * 获取能耗趋势 * 1 WS02-D 经编车间 经编车间用电 * 2 WS03-D-QZ 前整车间 染整线-前整车间用电 * 3 WS03-D-YR 印染车间 染整线-印染车间用电 * 4 WS03-D-HZ 后整车间 染整线-后整车间用电 * 5 WS03-S-YR 印染工业水 染整线-工业水 * 6 WS03-Q-DY 低压蒸汽 2/3低压蒸汽 * 7 WS03-Q-ZY 中压蒸汽 中压蒸汽 * * @return 结果 */ @Override public List energyTrade() { // redisCache.setCacheObject(CacheConstants.ENERGY_TRADE, mockEnergy()); List list = new ArrayList<>(); LocalDate end = LocalDateTime.now().minusHours(7).toLocalDate().minusDays(1); //一个月数据 LocalDate start = end.minusMonths(1); List wsList = wsCalcService.listByWsCodeDate(null, DateUtils.toDate(start)); Map> wsMap = wsList.stream().collect(Collectors.groupingBy(TwinWorkshopCalc::getDataDate, LinkedHashMap::new, Collectors.toList())); for (Map.Entry> entry : wsMap.entrySet()) { EnergyTradeVO vo = new EnergyTradeVO(); LocalDate ldt = DateUtils.toLocalDate(entry.getKey()); List wss = entry.getValue(); vo.setDate(ldt.getDayOfMonth() + ""); vo.setTips(ldt.toString()); EnergyTradeVO.RZ rz = new EnergyTradeVO.RZ(); rz.setElectricity(BigDecimal.ZERO); rz.setPrice(BigDecimal.ZERO); for (TwinWorkshopCalc ws : wss) { switch (ws.getWsCode()) { case "WS02-D": //经编车间用电 vo.getJb().setElectricity(ws.getTotalValue()); vo.getJb().setPrice(ws.getTotalPrice() != null ? ws.getTotalPrice() : BigDecimal.ZERO); break; case "WS03-D-QZ": //染整线-前整车间用电 case "WS03-D-YR": //染整线-印染车间用电 case "WS03-D-HZ": //染整线-后整车间用电 if (ws.getTotalValue() != null) { rz.setElectricity(rz.getElectricity().add(ws.getTotalValue())); } if (ws.getTotalPrice() != null) { rz.setPrice(rz.getPrice().add(ws.getTotalPrice())); } break; case "WS03-S-YR": rz.setWater(ws.getTotalValue()); if (ws.getTotalPrice() != null) { rz.setPrice(rz.getPrice().add(ws.getTotalPrice())); } //染整线-工业水 break; case "WS03-Q-DY": rz.setSteam(ws.getTotalValue()); if (ws.getTotalPrice() != null) { rz.setPrice(rz.getPrice().add(ws.getTotalPrice())); } //染整线-工业水 //2/3低压蒸汽 break; case "WS03-Q-ZY": //中压蒸汽 break; default: } } rz.setPrice(rz.getPrice().setScale(0, RoundingMode.HALF_UP)); vo.getJb().setPrice(vo.getJb().getPrice().setScale(0, RoundingMode.HALF_UP)); vo.setRz(rz); list.add(vo); } return list; } private List mockProd() { List list = new ArrayList<>(); LocalDate end = LocalDate.now().minusDays(1); LocalDate start = end.minusDays(6); do { ProdTradeVO vo = new ProdTradeVO(); vo.setDate(week(start)); vo.setTips(start.toString()); vo.mock(); list.add(vo); start = start.plusDays(1); } while (!start.isAfter(end)); return list; } private List mockEnergy() { List list = new ArrayList<>(); LocalDate end = LocalDate.now().minusDays(1); LocalDate start = end.minusDays(30); do { EnergyTradeVO vo = new EnergyTradeVO(); vo.setDate(start.getDayOfMonth() + ""); vo.setTips(start.toString()); vo.mock(); list.add(vo); start = start.plusDays(1); } while (!start.isAfter(end)); return list; } /** * 获取周几 * * @param date * @return */ private String week(LocalDate date) { String[] weeks = {"一", "二", "三", "四", "五", "六", "日"}; return "周" + weeks[date.getDayOfWeek().getValue() - 1]; } public static void main(String[] args) { int day = 7; LocalDate end = LocalDate.now().minusDays(1); LocalDate start = end.minusDays(day - 1); do { System.err.println(start + "\t" + start.getDayOfWeek().getValue()); start = start.plusDays(1); } while (!start.isAfter(end)); } /** * 模拟数据 */ @Override public void mock() { YrTwinVO rzVO = redisCache.getCacheObject(CacheConstants.RZ_MOCK); TwinAllVO vo = new TwinAllVO(); vo.setStock(stock()); vo.setAvgMonth(avgMonth()); CurrYieldVO currYieldVO = currYield(); CurrYieldVO.RZ rz = currYieldVO.getRz(); rz.setYield(rzVO.getStatus().getLength()); vo.setCurrYield(currYieldVO); vo.setProdTrade(prodTrade()); vo.setEnergyTrade(energyTrade()); redisCache.setCacheObject(CacheConstants.ALL_MOCK, vo); } }