From dadf90a142175590766a8bfdbc2a5486d707fa43 Mon Sep 17 00:00:00 2001
From: yb <1113799@qq.com>
Date: 星期四, 11 九月 2025 11:19:54 +0800
Subject: [PATCH] 倍率统计报表

---
 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentMagnificationServiceImpl.java |  273 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 266 insertions(+), 7 deletions(-)

diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentMagnificationServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentMagnificationServiceImpl.java
index 9237246..0debec7 100644
--- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentMagnificationServiceImpl.java
+++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentMagnificationServiceImpl.java
@@ -1,21 +1,40 @@
 package org.jeecg.modules.mdc.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.apache.commons.lang3.StringUtils;
-import org.jeecg.modules.mdc.entity.*;
+import org.apache.shiro.SecurityUtils;
+import org.jeecg.common.constant.CommonConstant;
+import org.jeecg.common.system.vo.LoginUser;
+import org.jeecg.modules.mdc.dto.MdcEquProDto;
+import org.jeecg.modules.mdc.dto.MdcEquipmentMagnificationDto;
+import org.jeecg.modules.mdc.entity.Equipment;
+import org.jeecg.modules.mdc.entity.MdcDriveTypeParamConfig;
+import org.jeecg.modules.mdc.entity.MdcEquipmentMagnification;
+import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection;
 import org.jeecg.modules.mdc.mapper.MdcEquipmentMagnificationMapper;
 import org.jeecg.modules.mdc.service.*;
 import org.jeecg.modules.mdc.util.DateUtils;
-import org.jeecg.modules.mdc.vo.MdcDeviceCalendarVo;
 import org.jeecg.modules.mdc.vo.MdcEquipmentMagnificationVo;
+import org.jeecg.modules.mdc.vo.MdcMagnificationEquipmentVo;
+import org.jeecg.modules.mdc.vo.MdcMagnificationVo;
+import org.jeecg.modules.system.entity.MdcProduction;
+import org.jeecg.modules.system.service.IMdcProductionService;
+import org.jeecg.modules.system.service.ISysDepartService;
+import org.jeecgframework.poi.excel.def.NormalExcelConstants;
+import org.jeecgframework.poi.excel.entity.ExportParams;
+import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.stereotype.Service;
+import org.springframework.web.servlet.ModelAndView;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 public class MdcEquipmentMagnificationServiceImpl extends ServiceImpl<MdcEquipmentMagnificationMapper, MdcEquipmentMagnification> implements IMdcEquipmentMagnificationService {
@@ -28,17 +47,33 @@
     private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
     @Resource
     private IMdcEquipmentService mdcEquipmentService;
+    @Resource
+    private ISysDepartService sysDepartService;
+    @Resource
+    private IMdcProductionService mdcProductionService;
+
 
     private static String spindlebeilv = "涓昏酱鍊嶇巼";
     private static String feedbeilv = "杩涚粰鍊嶇巼";
     @Override
-    public void savesEquipmentMagnifications() {
+    public void savesEquipmentMagnifications(String dateTime) {
         List<MdcEquipmentMagnification> magnifications = new ArrayList<>();
         //涓昏酱鍊嶇巼
         //杩涚粰鍊嶇巼
        /* Equipment e = equipmentService.getById("12345678");
         List<Equipment> equipmentList = new ArrayList<>();
         equipmentList.add(e);*/
+        if (org.apache.commons.lang.StringUtils.isNotBlank(dateTime)) {
+            try {
+                Date initDate = DateUtils.toDate(dateTime, "yyyyMMdd");
+                String date = DateUtils.format(initDate,DateUtils.STR_DATE);
+                if (initDate != null) {
+                    this.remove(new LambdaQueryWrapper<MdcEquipmentMagnification>().eq(MdcEquipmentMagnification::getTheDate, date));
+                }
+            } catch (Exception ee) {
+                log.error("鍙傛暟鏍煎紡涓嶅", ee);
+            }
+        }
         List<Equipment> equipmentList = equipmentService.list();
         Map<String,List<MdcDriveTypeParamConfig>> map = new HashMap<>();
         Date now =  DateUtils.plusTime(DateUtils.getNow(), -1);
@@ -47,12 +82,12 @@
             //鏌ヨ鏈�鍚庝竴鏉¤褰�
             MdcEquipmentMagnification mdcMagnification = super.baseMapper.getMaxStaticsData(equipment.getEquipmentid());
             if (mdcMagnification == null) {
-                start = DateUtils.plusTime(DateUtils.getNow(), -5);
+                start = DateUtils.plusTime(DateUtils.getNow(), -20);
             } else {
+                start = DateUtils.toDate(mdcMagnification.getTheDate(),DateUtils.STR_DATE );
                 if (now.toString().equals(start.toString())) {
                     continue;
                 }
-                start = DateUtils.toDate(mdcMagnification.getTheDate(),DateUtils.STR_DATE );
             }
             List<String> dates = DateUtils.getDatesStringList(start,now);
             List<MdcDriveTypeParamConfig> list = new ArrayList<>();
@@ -130,6 +165,230 @@
         return this.baseMapper.pageList(pageData, magnificationVo);
     }
 
+    @Override
+    public ModelAndView exportXls(String userId, MdcEquipmentMagnificationVo magnificationVo) {
+        List<String> equipmentIds = new ArrayList<>();
+        if (StringUtils.isNotEmpty(magnificationVo.getParentId()) && StringUtils.isEmpty(magnificationVo.getEquipmentId())) {
+            if ("2".equals(magnificationVo.getTypeTree())) {
+                // 閮ㄩ棬灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, magnificationVo.getParentId());
+            } else {
+                // 浜х嚎灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, magnificationVo.getParentId());
+            }
+        } else if (StringUtils.isNotEmpty(magnificationVo.getEquipmentId())) {
+            // 鍗曞彴璁惧淇℃伅
+            magnificationVo.setEquipmentIdList(Collections.singletonList(magnificationVo.getEquipmentId()));
+        } else {
+            // 鏌ヨ鐢ㄦ埛鎷ユ湁鐨勬墍鏈夎澶囦俊鎭�
+            if ("2".equals(magnificationVo.getTypeTree())) {
+                //閮ㄩ棬灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
+            } else {
+                //浜х嚎灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
+            }
+        }
+        if (magnificationVo.getEquipmentIdList() == null || magnificationVo.getEquipmentIdList().isEmpty()) {
+            magnificationVo.setEquipmentIdList(equipmentIds);
+        }
+        if (magnificationVo.getEquipmentIdList() == null || magnificationVo.getEquipmentIdList().isEmpty()) {
+            return null;
+        }
+        List<MdcEquipmentMagnificationDto> magnificationDtos = super.baseMapper.list(magnificationVo);
+        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
+        // 瀵煎嚭鏂囦欢鍚嶇О
+        mv.addObject(NormalExcelConstants.FILE_NAME, "鍊嶇巼鎶ヨ〃");
+        mv.addObject(NormalExcelConstants.CLASS, MdcEquipmentMagnificationDto.class);
+        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("鍊嶇巼鎶ヨ〃", "瀵煎嚭浜�:" + user.getRealname(), "鍊嶇巼鎶ヨ〃"));
+        mv.addObject(NormalExcelConstants.DATA_LIST, magnificationDtos);
+        return mv;
+    }
+
+    @Override
+    public MdcMagnificationVo beilvsList(MdcEquipmentMagnificationVo vo, String userId) {
+        List<MdcMagnificationEquipmentVo> mdcMagnificationVos = new ArrayList<>();
+        MdcMagnificationVo result = new MdcMagnificationVo();
+        List<String> equipmentIds = new ArrayList<>();
+        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
+            if ("2".equals(vo.getTypeTree())) {
+                // 閮ㄩ棬灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId());
+            } else {
+                // 浜х嚎灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
+            }
+        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
+            // 鍗曞彴璁惧淇℃伅
+            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
+        } else {
+            // 鏌ヨ鐢ㄦ埛鎷ユ湁鐨勬墍鏈夎澶囦俊鎭�
+            if ("2".equals(vo.getTypeTree())) {
+                //閮ㄩ棬灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
+            } else {
+                //浜х嚎灞傜骇
+                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
+            }
+        }
+        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
+            vo.setEquipmentIdList(equipmentIds);
+        }
+
+        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
+        List<String> dates = DateUtils.getDatesStringList(DateUtils.getShortDate(vo.getStartTime()), DateUtils.getShortDate(vo.getEndTime()));
+        result.setDates(dates);
+
+        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
+            result.setMdcMagnificationVos(mdcMagnificationVos);
+        } else {
+            List<String> equipmentIdList = mdcEquipmentService.listEquipmentMagnificationId(vo);
+            if (equipmentIdList != null && !equipmentIdList.isEmpty()) {
+                vo.setEquipmentIdList(equipmentIdList);
+            } else {
+                result.setMdcMagnificationVos(mdcMagnificationVos);
+                return result;
+            }
+            // 鏌ヨ鍒╃敤鐜囨暟鎹�
+            List<MdcEquipmentMagnificationDto> magnificationDtos = super.baseMapper.list(vo);
+            // 灏佽缁撴灉
+            if ("2".equals(vo.getTypeTree())) {
+            } else {
+                // 浜х嚎灞傜骇
+                List<MdcEquProDto> equipmentList = mdcEquipmentService.findEquProList(vo.getEquipmentIdList());
+                // 鏌ヨ鎵�鏈変骇绾夸俊鎭�
+                List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().ne(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_1.toString()).orderByAsc(MdcProduction::getProductionOrder));
+                for (MdcEquProDto mdcEquProDto : equipmentList) {
+                    MdcMagnificationEquipmentVo mdcMgVo = new MdcMagnificationEquipmentVo();
+                    mdcMgVo.setEquipmentId(mdcEquProDto.getEquipmentId());
+                    mdcMgVo.setEquipmentName(mdcEquProDto.getEquipmentName());
+                    mdcMgVo.setEquipmentType(mdcEquProDto.getEquipmentType());
+                    switch (mdcEquProDto.getOrgType()) {
+                        case "1":
+                            mdcMgVo.setLevel1(mdcEquProDto.getProductionName());
+                            break;
+                        case "2":
+                            mdcMgVo.setLevel2(mdcEquProDto.getProductionName());
+                            break;
+                        case "3":
+                            mdcMgVo.setLevel3(mdcEquProDto.getProductionName());
+                            break;
+                        default:
+                    }
+                    MdcProduction mdcProduction = productionList.stream().filter(production -> production.getId().equals(mdcEquProDto.getParentId())).findAny().orElse(null);
+                    if (mdcProduction != null) {
+                        switch (mdcProduction.getOrgType()) {
+                            case "1":
+                                mdcMgVo.setLevel1(mdcProduction.getProductionName());
+                                break;
+                            case "2":
+                                mdcMgVo.setLevel2(mdcProduction.getProductionName());
+                                break;
+                            case "3":
+                                mdcMgVo.setLevel3(mdcProduction.getProductionName());
+                                break;
+                            default:
+                        }
+                        if (StringUtils.isNotEmpty(mdcProduction.getParentId())) {
+                            productionList.stream().filter(production -> production.getId().equals(mdcProduction.getParentId())).findAny().ifPresent(production1 -> {
+                                switch (production1.getOrgType()) {
+                                    case "1":
+                                        mdcMgVo.setLevel1(production1.getProductionName());
+                                        break;
+                                    case "2":
+                                        mdcMgVo.setLevel2(production1.getProductionName());
+                                        break;
+                                    case "3":
+                                        mdcMgVo.setLevel3(production1.getProductionName());
+                                        break;
+                                    default:
+                                }
+                            });
+                        }
+                    }
+                    List<MdcEquipmentMagnificationDto> list = new ArrayList<>();
+                    MdcEquipmentMagnificationDto mdcMagnificationDto = new MdcEquipmentMagnificationDto();
+                    mdcMagnificationDto.setTheDate("骞冲潎鍊�");
+                    for (String date : dates) {
+                        list.add(this.setDataList(magnificationDtos, date, mdcEquProDto.getEquipmentId(),mdcMagnificationDto));
+                    }
+                    mdcMagnificationDto.setFeedbeilv(mdcMagnificationDto.getFeedbeilv().divide(new BigDecimal(dates.size()),2, RoundingMode.HALF_UP));
+                    mdcMagnificationDto.setSpindlebeilv(mdcMagnificationDto.getSpindlebeilv().divide(new BigDecimal(dates.size()),2, RoundingMode.HALF_UP));
+                    list.add(mdcMagnificationDto);
+                    mdcMgVo.setMdcBeilvVoList(list);
+                    mdcMagnificationVos.add(mdcMgVo);
+                }
+            }
+            result.setMdcMagnificationVos(mdcMagnificationVos);
+        }
+        // 娣诲姞鍚堣鍊煎拰骞冲潎鍊�
+
+        result.getDates().add("骞冲潎鍊�");
+        MdcMagnificationEquipmentVo avg = new MdcMagnificationEquipmentVo();
+        avg.setLevel1("骞冲潎鍊�");
+        avg.setLevel2("骞冲潎鍊�");
+        avg.setLevel3("骞冲潎鍊�");
+        avg.setEquipmentId("骞冲潎鍊�");
+        avg.setEquipmentName("骞冲潎鍊�");
+        avg.setEquipmentType("骞冲潎鍊�");
+        //璁$畻骞冲潎鍊�
+        avg.setMdcBeilvVoList(calculateOpenAverage(result.getMdcMagnificationVos()));
+        result.getMdcMagnificationVos().add(avg);
+        return result;
+    }
+
+    /**
+     * 璁$畻骞冲潎鍊�
+     *
+     * @param mdcMagnificationVos
+     * @return
+     */
+    private List<MdcEquipmentMagnificationDto> calculateOpenAverage(List<MdcMagnificationEquipmentVo> mdcMagnificationVos) {
+        List<MdcEquipmentMagnificationDto> result = new ArrayList<>();
+        List<MdcEquipmentMagnificationDto> dataList = new ArrayList<>();
+        for (MdcMagnificationEquipmentVo mdcMagnificationEquipmentVo : mdcMagnificationVos) {
+            dataList.addAll(mdcMagnificationEquipmentVo.getMdcBeilvVoList());
+        }
+        dataList.parallelStream().collect(Collectors.groupingBy(MdcEquipmentMagnificationDto::getTheDate, Collectors.toList()))
+                .forEach((theDate, mert) -> {
+                    mert.stream().reduce((a, b) -> new MdcEquipmentMagnificationDto(
+                            a.getTheDate(),
+                            a.getFeedbeilv().add(b.getFeedbeilv()),
+                            a.getSpindlebeilv().add(b.getSpindlebeilv()))).ifPresent(result::add);
+                });
+        for (MdcEquipmentMagnificationDto mdcEquipmentMagnificationDto : result) {
+            mdcEquipmentMagnificationDto.setSpindlebeilv(mdcEquipmentMagnificationDto.getSpindlebeilv().divide(new BigDecimal(mdcMagnificationVos.size()), 2, RoundingMode.HALF_UP));
+            mdcEquipmentMagnificationDto.setFeedbeilv(mdcEquipmentMagnificationDto.getFeedbeilv().divide(new BigDecimal(mdcMagnificationVos.size()), 2, RoundingMode.HALF_UP));
+        }
+        result.sort(Comparator.comparing(MdcEquipmentMagnificationDto::getTheDate));
+        return result;
+    }
+
+    private MdcEquipmentMagnificationDto setDataList(List<MdcEquipmentMagnificationDto> list,String date,String equipmentId,MdcEquipmentMagnificationDto mdcMagnificationDto) {
+        MdcEquipmentMagnificationDto result = new MdcEquipmentMagnificationDto();
+        if (list != null && list.size() > 0) {
+            for (MdcEquipmentMagnificationDto mdcEquipmentMagnificationDto : list) {
+                if (date.equals(mdcEquipmentMagnificationDto.getTheDate()) && mdcEquipmentMagnificationDto.getEquipmentId().equals(equipmentId)) {
+                    result.setEquipmentId(equipmentId);
+                    result.setSpindlebeilv(mdcEquipmentMagnificationDto.getSpindlebeilv());
+                    result.setFeedbeilv(mdcEquipmentMagnificationDto.getFeedbeilv());
+                    result.setTheDate(date);
+                }
+            }
+        } else {
+            result.setTheDate(date);
+            result.setFeedbeilv(new BigDecimal(0));
+            result.setEquipmentId(equipmentId);
+            result.setSpindlebeilv(new BigDecimal(0));
+            return result;
+        }
+        mdcMagnificationDto.setFeedbeilv(mdcMagnificationDto.getFeedbeilv().add(result.getFeedbeilv()));
+        mdcMagnificationDto.setSpindlebeilv(mdcMagnificationDto.getSpindlebeilv().add(result.getSpindlebeilv()));
+        return result;
+    }
+
+
     private MdcEquipmentMagnification selectMagnification(String equipmentId,String date,String drivetype,List<MdcDriveTypeParamConfig> list) {
         Date startTime = DateUtils.plusTime(DateUtils.getShortDate(date), 0);
         Date endTime = DateUtils.plusTime(DateUtils.getShortDate(date), 1);
@@ -173,14 +432,14 @@
                     info.setSpindlebeilvValue(bigDecimal);
                     info.setSpindlebeilvNumber(numberSizeList);
                     if (bigDecimal.compareTo(BigDecimal.ZERO) >0  && new BigDecimal(numberSizeList).compareTo(BigDecimal.ZERO) > 0) {
-                        info.setSpindlebeilv(bigDecimal.divide(new BigDecimal(numberSizeList)));
+                        info.setSpindlebeilv(bigDecimal.divide(new BigDecimal(numberSizeList),2, RoundingMode.HALF_UP));
                     }
                 }
                 if (config.getChineseName().equals(feedbeilv)) {
                     info.setFeedbeilvValue(bigDecimal);
                     info.setFeedbeilvNumber(numberSizeList);
                     if (bigDecimal.compareTo(BigDecimal.ZERO) >0  && new BigDecimal(numberSizeList).compareTo(BigDecimal.ZERO) > 0) {
-                        info.setFeedbeilv(bigDecimal.divide(new BigDecimal(numberSizeList)));
+                        info.setFeedbeilv(bigDecimal.divide(new BigDecimal(numberSizeList),2, RoundingMode.HALF_UP));
                     }
                 }
             }

--
Gitblit v1.9.3