123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- package com.jjt.biz.service.impl;
- import com.alibaba.fastjson2.JSON;
- import com.jjt.biz.service.IApiYrService;
- import com.jjt.biz.vo.*;
- import com.jjt.calc.domain.TwinCalcDayYhj;
- import com.jjt.calc.domain.TwinCalcHourYhj;
- import com.jjt.calc.service.ITwinCalcDayYhjService;
- import com.jjt.calc.service.ITwinCalcHourYhjService;
- import com.jjt.common.constant.CacheConstants;
- import com.jjt.common.core.redis.RedisCache;
- import com.jjt.common.utils.DateUtils;
- import com.jjt.rz.domain.TwinCalcHourRz;
- import com.jjt.rz.service.ITwinCalcHourRzService;
- import com.jjt.rz.service.ITwinDeviceRzService;
- import com.jjt.rz.service.ITwinDeviceTypeService;
- import com.jjt.rz.vo.CompareVO;
- import com.jjt.ws.domain.TwinRzCalcMonth;
- import com.jjt.ws.service.ITwinRzCalcMonthService;
- 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.time.ZoneOffset;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.stream.Collectors;
- /**
- * 首页统计数据
- *
- * @author wukai
- * @date 2024/5/4 20:35
- */
- @Service
- @Slf4j
- public class ApiYrServiceImpl implements IApiYrService {
- @Resource
- private RedisCache redisCache;
- @Resource
- private ITwinRzCalcMonthService rzCalcMonthService;
- @Resource
- private ITwinCalcDayYhjService calcDayYhjService;
- @Resource
- private ITwinDeviceRzService rzService;
- @Resource
- private ITwinCalcHourRzService hourRzService;
- @Resource
- private ITwinDeviceTypeService typeService;
- /**
- * 获取产线状态
- *
- * @return 结果
- */
- @Override
- public YrProdLineStatusVO status() {
- YrProdLineStatusVO vo = new YrProdLineStatusVO();
- vo.mock();
- return vo;
- }
- /**
- * 获取生产效率
- *
- * @return 结果
- */
- @Override
- public List<YrProdEfficiencyVO> eff() {
- List<YrProdEfficiencyVO> list = new ArrayList<>();
- YrProdEfficiencyVO[] arr = new YrProdEfficiencyVO[9];
- for (int i = 0; i < 9; i++) {
- YrProdEfficiencyVO vo = new YrProdEfficiencyVO(i);
- vo.mock();
- arr[i] = vo;
- }
- YrProdEfficiencyVO line0 = new YrProdEfficiencyVO(0);
- // for (int i = 1; i < 9; i++) {
- // YrProdEfficiencyVO vo = arr[i];
- // line0.setEffA(line0.getEffA().add(vo.getEffA()));
- // line0.setEffB(line0.getEffB().add(vo.getEffB()));
- // line0.setTimeA(line0.getTimeA().add(vo.getTimeA()));
- // line0.setTimeB(line0.getTimeB().add(vo.getTimeB()));
- // line0.setLengthA(line0.getLengthA().add(vo.getLengthA()));
- // line0.setLengthB(line0.getLengthB().add(vo.getLengthB()));
- // line0.setWeightA(line0.getWeightA().add(vo.getWeightA()));
- // line0.setWeightB(line0.getWeightB().add(vo.getWeightB()));
- // line0.setWeight(line0.getWeight().add(vo.getWeight()));
- // line0.setLength(line0.getLength().add(vo.getLength()));
- //
- // }
- List<TwinCalcHourYhj> calcList = redisCache.getCacheObject(CacheConstants.YHJ_TODAY);
- Map<Integer, Map<String, Integer>> sumByDeviceAndTeam = calcList.stream()
- .collect(Collectors.groupingBy(
- TwinCalcHourYhj::getDeviceId,
- Collectors.groupingBy(
- TwinCalcHourYhj::getTeam,
- Collectors.summingInt(TwinCalcHourYhj::getLength)
- )
- ));
- AtomicInteger openTimes = new AtomicInteger(0);
- sumByDeviceAndTeam.forEach((deviceId, teamMap) -> {
- Integer lengthA = teamMap.get("A");
- Integer lengthB = teamMap.get("B");
- if (lengthB == null) {
- lengthB = 0;
- }
- Integer length = lengthA + lengthB;
- YrProdEfficiencyVO vo = arr[deviceId];
- if (length > 0) {
- openTimes.incrementAndGet();
- } else {
- vo.setOpen(0);
- vo.setRatio(BigDecimal.ZERO);
- vo.setTimeA(BigDecimal.ZERO);
- vo.setTimeB(BigDecimal.ZERO);
- vo.setEffA(BigDecimal.ZERO);
- vo.setEffB(BigDecimal.ZERO);
- }
- BigDecimal weightA = BigDecimal.valueOf(lengthA).divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
- BigDecimal weightB = BigDecimal.valueOf(lengthB).divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
- BigDecimal weight = BigDecimal.valueOf(length).divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
- vo.setLengthA(BigDecimal.valueOf(lengthA));
- vo.setLengthB(BigDecimal.valueOf(lengthB));
- vo.setWeightA(weightA);
- vo.setWeightB(weightB);
- vo.setWeight(weight);
- vo.setLength(BigDecimal.valueOf(length));
- line0.setEffA(line0.getEffA().add(vo.getEffA()));
- line0.setEffB(line0.getEffB().add(vo.getEffB()));
- line0.setTimeA(line0.getTimeA().add(vo.getTimeA()));
- line0.setTimeB(line0.getTimeB().add(vo.getTimeB()));
- line0.setLengthA(line0.getLengthA().add(vo.getLengthA()));
- line0.setLengthB(line0.getLengthB().add(vo.getLengthB()));
- line0.setWeightA(line0.getWeightA().add(vo.getWeightA()));
- line0.setWeightB(line0.getWeightB().add(vo.getWeightB()));
- line0.setWeight(line0.getWeight().add(vo.getWeight()));
- line0.setLength(line0.getLength().add(vo.getLength()));
- });
- line0.setEffA(line0.getEffA().divide(BigDecimal.valueOf(8), 2, RoundingMode.HALF_UP));
- line0.setEffB(line0.getEffB().divide(BigDecimal.valueOf(8), 2, RoundingMode.HALF_UP));
- list.add(line0);
- for (int i = 1; i < 9; i++) {
- YrProdEfficiencyVO vo = arr[i];
- list.add(vo);
- }
- return list;
- }
- /**
- * 获取生产趋势
- *
- * @return 结果
- */
- @Override
- public List<YrProdTradeVO> prodTrade() {
- return mockProd();
- }
- /**
- * 获取能耗趋势
- *
- * @return 结果
- */
- @Override
- public List<YrEnergyTradeVO> energyTrade() {
- return mockEnergy();
- }
- /**
- * 获取能耗趋势
- *
- * @return 结果
- */
- public List<YrEnergyTradeVO> energyTrade(List<TwinRzCalcMonth> list) {
- List<YrEnergyTradeVO> result = new ArrayList<>();
- for (TwinRzCalcMonth cm : list) {
- YrEnergyTradeVO vo = new YrEnergyTradeVO();
- LocalDate ldt = DateUtils.toLocalDate(cm.getDataDate());
- vo.setDate(ldt.getDayOfMonth() + "");
- vo.setTips(ldt.toString());
- vo.split(cm.getLengthPrice(), cm.getDUse(), cm.getQLowUse(), cm.getSUse());
- result.add(vo);
- }
- return result;
- }
- /**
- * 获取生产趋势
- *
- * @return 结果
- */
- public List<YrProdTradeVO> prodTrade(List<TwinRzCalcMonth> list, List<TwinCalcDayYhj> yhjList) {
- Map<Date, List<TwinCalcDayYhj>> yhjMap = yhjList.stream().collect(Collectors.groupingBy(TwinCalcDayYhj::getTime));
- List<YrProdTradeVO> result = new ArrayList<>();
- for (TwinRzCalcMonth cm : list) {
- YrProdTradeVO vo = new YrProdTradeVO();
- LocalDate ldt = DateUtils.toLocalDate(cm.getDataDate());
- List<TwinCalcDayYhj> yhjs = yhjMap.get(cm.getDataDate());
- vo.setDate(ldt.getDayOfMonth() + "");
- vo.setTips(ldt.toString());
- vo.split(yhjs, cm.getLength(), cm.getWeight());
- result.add(vo);
- }
- return result;
- }
- /**
- * 模拟数据定时任务
- */
- @Override
- public void mock() {
- YrTwinVO vo = new YrTwinVO();
- YrProdLineStatusVO status = status();
- vo.setStatus(status);
- List<YrProdEfficiencyVO> effList = eff();
- int open = 0;
- for (YrProdEfficiencyVO eff : effList) {
- if (eff.getLine() == 0) {
- status.setWeight(eff.getWeight().setScale(0, RoundingMode.HALF_UP));
- status.setLength(eff.getLength().setScale(0, RoundingMode.HALF_UP));
- Random random = new Random();
- BigDecimal baiPi = eff.getLength().multiply(BigDecimal.valueOf(0.5 + random.nextDouble())).setScale(0, RoundingMode.HALF_UP);
- status.setBaiPei(baiPi);
- } else {
- if (eff.getLength().compareTo(BigDecimal.ZERO) > 0) {
- open++;
- }
- }
- }
- status.setOpenProd(open);
- //白坯投放量处理--start
- Map<String, Object> result = redisCache.getCacheObject(CacheConstants.VMS_STOCK);
- Map<String, Long> lineMap = (Map<String, Long>) result.get("out-rz-line");
- lineMap.remove("@type"); // 手动移除多余字段
- AtomicReference<Long> baiPei = new AtomicReference<>(0L);
- lineMap.forEach((key, value) -> {
- baiPei.updateAndGet(v -> v + value);
- });
- status.setBaiPei(BigDecimal.valueOf(baiPei.get()));
- //白坯投放量处理--end
- vo.setEff(effList);
- TwinRzCalcMonth calcMonth = new TwinRzCalcMonth();
- Map<String, Object> params = new HashMap<>(16);
- params.put("start", LocalDate.now().minusDays(30).toString());
- calcMonth.setParams(params);
- List<TwinRzCalcMonth> list = rzCalcMonthService.selectTwinRzCalcMonthList(calcMonth);
- TwinCalcDayYhj yhj = new TwinCalcDayYhj();
- yhj.setParams(params);
- List<TwinCalcDayYhj> yhjList = calcDayYhjService.selectTwinCalcDayYhjList(yhj);
- calcMonth.setParams(params);
- vo.setProdTrade(prodTrade(list, yhjList));
- vo.setEnergyTrade(energyTrade(list));
- redisCache.setCacheObject(CacheConstants.RZ_MOCK, vo);
- }
- private List<YrProdTradeVO> mockProd() {
- List<YrProdTradeVO> list = new ArrayList<>();
- LocalDate end = LocalDate.now().minusDays(1);
- LocalDate start = end.minusDays(30);
- do {
- YrProdTradeVO vo = new YrProdTradeVO();
- vo.setDate(start.getDayOfMonth() + "");
- vo.setTips(start.toString());
- vo.mock();
- list.add(vo);
- start = start.plusDays(1);
- } while (!start.isAfter(end));
- return list;
- }
- private List<YrEnergyTradeVO> mockEnergy() {
- List<YrEnergyTradeVO> list = new ArrayList<>();
- LocalDate end = LocalDate.now().minusDays(1);
- LocalDate start = end.minusDays(30);
- do {
- YrEnergyTradeVO vo = new YrEnergyTradeVO();
- vo.setDate(start.getDayOfMonth() + "");
- vo.setTips(start.toString());
- vo.mock();
- list.add(vo);
- start = start.plusDays(1);
- } while (!start.isAfter(end));
- return list;
- }
- @Resource
- private ITwinCalcHourYhjService yhjService;
- /**
- * 工艺对比
- *
- * @param reqs 输入参数
- * @return 结果集
- */
- @Override
- public List<CompareVO> compare(List<YrCompareBackReq> reqs, boolean flag) {
- List<CompareVO> result = new ArrayList<>();
- for (YrCompareBackReq req : reqs) {
- CompareVO vo = itemProcessor(req, flag);
- int totalLength = getTotalLength(req);
- vo.setLength(totalLength);
- result.add(vo);
- }
- return result;
- }
- /**
- * 获取印花产量数据
- *
- * @param req 请求
- * @return 结果
- */
- private Integer getTotalLength(YrCompareBackReq req) {
- //获取印花产量数据
- TwinCalcHourYhj yhjSearch = new TwinCalcHourYhj();
- yhjSearch.setDeviceId(req.getLine());
- Map<String, Object> params = new HashMap<>(16);
- params.put("sTime", DateUtils.toDate(req.getStart()));
- params.put("eTime", DateUtils.toDate(req.getEnd()));
- yhjSearch.setParams(params);
- List<TwinCalcHourYhj> yhjList = yhjService.selectTwinCalcHourYhjList(yhjSearch);
- int totalLength = yhjList.stream()
- .mapToInt(TwinCalcHourYhj::getLength)
- .sum();
- return totalLength;
- }
- private CompareVO itemProcessor(YrCompareBackReq req, boolean flag) {
- CompareVO vo = new CompareVO();
- vo.setLine(req.getLine());
- vo.setStart(req.getStart());
- vo.setEnd(req.getEnd());
- vo.setYh(10);
- CompareVO.Qz qz = vo.getQz();
- CompareVO.Hz hz = vo.getHz();
- LocalDateTime ldt = req.getEnd().minusHours(1);
- Date date = Date.from(ldt.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
- TwinCalcHourRz search = new TwinCalcHourRz();
- search.setDataDate(date);
- search.setHour(ldt.getHour());
- search.setLine(req.getLine());
- List<TwinCalcHourRz> list = hourRzService.selectTwinCalcHourRzList(search);
- list.forEach(item -> {
- Map<String, Object> map = JSON.parseObject(item.getData(), Map.class);
- map.put("device", item.getDeviceName());
- map.put("online", item.getOnline());
- if ("Forward".equals(item.getWsName())) {
- qzProcessor(qz, item, flag, map);
- } else if ("Back".equals(item.getWsName())) {
- hzProcessor(hz, item, flag, map);
- } else {
- //留给烘固机
- }
- });
- return vo;
- }
- private void qzProcessor(CompareVO.Qz qz, TwinCalcHourRz item, boolean flag, Map<String, Object> map) {
- //前整
- switch (item.getTypeId()) {
- case 2:
- //四节定型机
- if ("Y".equals(item.getOnline())) {
- qz.setDx(qz.getDx() + 1);
- }
- if (flag) {
- qz.addDxPara(map);
- }
- break;
- case 3:
- //双棍烫光机
- if ("Y".equals(item.getOnline())) {
- qz.setTg(qz.getTg() + 1);
- }
- if (flag) {
- qz.addTgPara(map);
- }
- break;
- case 4:
- //高梳机
- if ("Y".equals(item.getOnline())) {
- qz.setGs(qz.getGs() + 1);
- }
- if (flag) {
- qz.addGsPara(map);
- }
- break;
- case 5:
- //双棍刷毛
- if ("Y".equals(item.getOnline())) {
- qz.setSm(qz.getSm() + 1);
- }
- if (flag) {
- qz.addSmPara(map);
- }
- break;
- default:
- break;
- }
- }
- private void hzProcessor(CompareVO.Hz hz, TwinCalcHourRz item, boolean flag, Map<String, Object> map) {
- //后整
- switch (item.getTypeId()) {
- case 6:
- //六节定型机
- if ("Y".equals(item.getOnline())) {
- hz.setDx(hz.getDx() + 1);
- }
- if (flag) {
- hz.addDxPara(map);
- }
- break;
- case 3:
- //双棍烫光机
- if ("Y".equals(item.getOnline())) {
- hz.setTg(hz.getTg() + 1);
- }
- if (flag) {
- hz.addTgPara(map);
- }
- break;
- case 4:
- //高梳机
- if ("Y".equals(item.getOnline())) {
- hz.setGs(hz.getGs() + 1);
- }
- if (flag) {
- hz.addGsPara(map);
- }
- break;
- case 5:
- //双棍刷毛
- if ("Y".equals(item.getOnline())) {
- hz.setSm(hz.getSm() + 1);
- }
- if (flag) {
- hz.addSmPara(map);
- }
- break;
- case 7:
- //起毛机
- if ("Y".equals(item.getOnline())) {
- hz.setQm(hz.getQm() + 1);
- }
- if (flag) {
- hz.addQmPara(map);
- }
- break;
- case 9:
- //烫剪机
- if ("Y".equals(item.getOnline())) {
- hz.setTj(hz.getTj() + 1);
- }
- if (flag) {
- hz.addTjPara(map);
- }
- break;
- default:
- break;
- }
- }
- }
|