maven
2025-11-19 72a372cb26c4ba489efae6b65dbf9fdfea1b5815
src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java
@@ -6,9 +6,12 @@
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.dto.DeviceDefectRecordDto;
import com.ruoyi.device.dto.DeviceMonthlyRepairTableDTO;
import com.ruoyi.device.dto.DeviceRepairDto;
import com.ruoyi.device.dto.RepairAmountGroupDTO;
import com.ruoyi.device.execl.DeviceRepairExeclDto;
import com.ruoyi.device.mapper.DeviceDefectRecordMapper;
import com.ruoyi.device.mapper.DeviceLedgerMapper;
import com.ruoyi.device.mapper.DeviceRepairMapper;
import com.ruoyi.device.pojo.DeviceDefectRecord;
import com.ruoyi.device.pojo.DeviceLedger;
@@ -21,11 +24,13 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
@AllArgsConstructor
@@ -116,4 +121,96 @@
        return deviceRepairMapper.detailById(id);
    }
    @Autowired
    private DeviceLedgerMapper deviceLedgerMapper;
    @Override
    public List<DeviceMonthlyRepairTableDTO> getMonthlyRepairAmountByYear(String year) {
        List<RepairAmountGroupDTO> repairAmountGroupDTOS = deviceRepairMapper.groupByMonthAndDeviceLedger(year);
        // 1. 先通过设备台账id关联设备名称(如果RepairAmountGroupDTO没有deviceName,需先查设备台账表)
        // 这里假设你能通过deviceLedgerId获取到deviceName,比如:
        Map<Long, String> deviceNameMap = new HashMap<>(); // key:deviceLedgerId, value:deviceName
        // (实际需调用设备台账的Mapper查询:deviceNameMap = deviceLedgerMapper.listAll().stream().collect(Collectors.toMap(DeviceLedger::getId, DeviceLedger::getDeviceName)))
        deviceNameMap = deviceLedgerMapper.selectList(null)
                .stream()
                .collect(Collectors.toMap(DeviceLedger::getId, DeviceLedger::getDeviceName));
        if(CollectionUtils.isEmpty(deviceNameMap)){
            return Collections.emptyList();
        }
        // 2. 按设备名称分组,存储每个设备的各月金额
        Map<String, DeviceMonthlyRepairTableDTO> deviceTableMap = new HashMap<>();
        for (RepairAmountGroupDTO dto : repairAmountGroupDTOS) {
            // 拆分repairYearMonth为月份(如"2025-01" → "01" → 1)
            String yearMonth = dto.getRepairYearMonth(); // 格式:yyyy-MM
            int month = Integer.parseInt(yearMonth); // 提取MM并转成数字(1-12)
            // 获取设备名称
            String deviceName = deviceNameMap.get(dto.getDeviceLedgerId());
            if (deviceName == null) {
                deviceName = "未知设备"; // 兜底
            }
            // 从Map中获取该设备的表格DTO,不存在则初始化
            DeviceMonthlyRepairTableDTO tableDTO = deviceTableMap.getOrDefault(deviceName, new DeviceMonthlyRepairTableDTO());
            tableDTO.setDeviceName(deviceName);
            // 根据月份填充金额(BigDecimal默认0,避免null)
            BigDecimal amount = dto.getTotalRepairPrice() == null ? BigDecimal.ZERO : dto.getTotalRepairPrice();
            switch (month) {
                case 1: tableDTO.setMonth1(amount); break;
                case 2: tableDTO.setMonth2(amount); break;
                case 3: tableDTO.setMonth3(amount); break;
                case 4: tableDTO.setMonth4(amount); break;
                case 5: tableDTO.setMonth5(amount); break;
                case 6: tableDTO.setMonth6(amount); break;
                case 7: tableDTO.setMonth7(amount); break;
                case 8: tableDTO.setMonth8(amount); break;
                case 9: tableDTO.setMonth9(amount); break;
                case 10: tableDTO.setMonth10(amount); break;
                case 11: tableDTO.setMonth11(amount); break;
                case 12: tableDTO.setMonth12(amount); break;
            }
            // 重新放入Map
            deviceTableMap.put(deviceName, tableDTO);
        }
        // 3. 计算每个设备的总计,并补充序号
        List<DeviceMonthlyRepairTableDTO> resultList = new ArrayList<>();
        for (DeviceMonthlyRepairTableDTO tableDTO : deviceTableMap.values()) {
            // 计算总计:1-12月金额相加
            BigDecimal total = Stream.of(
                            tableDTO.getMonth1(), tableDTO.getMonth2(), tableDTO.getMonth3(),
                            tableDTO.getMonth4(), tableDTO.getMonth5(), tableDTO.getMonth6(),
                            tableDTO.getMonth7(), tableDTO.getMonth8(), tableDTO.getMonth9(),
                            tableDTO.getMonth10(), tableDTO.getMonth11(), tableDTO.getMonth12()
                    )
                    .map(amt -> amt == null ? BigDecimal.ZERO : amt)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            tableDTO.setTotal(total);
            resultList.add(tableDTO);
        }
        return resultList;
    }
    @Override
    public List<RepairAmountGroupDTO> getRepairAmountByYear(String year) {
        List<RepairAmountGroupDTO> repairAmountGroupDTOS = deviceRepairMapper.groupByDeviceLedger(year);
        Map<Long, String> deviceNameMap = new HashMap<>(); // key:deviceLedgerId, value:deviceName
        // (实际需调用设备台账的Mapper查询:deviceNameMap = deviceLedgerMapper.listAll().stream().collect(Collectors.toMap(DeviceLedger::getId, DeviceLedger::getDeviceName)))
        deviceNameMap = deviceLedgerMapper.selectList(null)
                .stream()
                .collect(Collectors.toMap(DeviceLedger::getId, DeviceLedger::getDeviceName));
        if(CollectionUtils.isEmpty(deviceNameMap)){
            return Collections.emptyList();
        }
        Map<Long, String> finalDeviceNameMap = deviceNameMap;
        repairAmountGroupDTOS.forEach(dto -> {
            dto.setDeviceName(finalDeviceNameMap.get(dto.getDeviceLedgerId()));
        });
        return repairAmountGroupDTOS;
    }
}