maven
2025-12-08 0100359605ba05638c1f683d68a2948cc273b0d2
src/main/java/com/ruoyi/device/service/impl/DeviceMaintenanceServiceImpl.java
@@ -7,18 +7,24 @@
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.dto.DeviceMaintenanceDto;
import com.ruoyi.device.dto.DeviceMonthlyRepairTableDTO;
import com.ruoyi.device.dto.RepairAmountGroupDTO;
import com.ruoyi.device.execl.DeviceMaintenanceExeclDto;
import com.ruoyi.device.mapper.DeviceLedgerMapper;
import com.ruoyi.device.mapper.DeviceMaintenanceMapper;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.device.pojo.DeviceMaintenance;
import com.ruoyi.device.service.IDeviceMaintenanceService;
import com.ruoyi.framework.web.domain.AjaxResult;
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
@@ -36,8 +42,8 @@
    }
    @Override
    public AjaxResult saveDeviceRepair(DeviceMaintenance deviceRepair) {
        boolean save = this.save(deviceRepair);
    public AjaxResult saveDeviceRepair(DeviceMaintenance deviceMaintenance) {
        boolean save = this.save(deviceMaintenance);
        if (save){
            return AjaxResult.success();
        }
@@ -45,8 +51,8 @@
    }
    @Override
    public AjaxResult updateDeviceRepair(DeviceMaintenance deviceRepair) {
        if (this.updateById(deviceRepair)) {
    public AjaxResult updateDeviceDeviceMaintenance(DeviceMaintenance deviceMaintenance) {
        if (this.updateById(deviceMaintenance)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
@@ -54,21 +60,115 @@
    @Override
    public void export(HttpServletResponse response, Long[] ids) {
        ArrayList<Long> arrayList = new ArrayList<>();
        Arrays.stream(ids).map(id -> {
            return arrayList.add( id);
        });
        List<DeviceMaintenance> supplierManageList = deviceMaintenanceMapper.selectBatchIds(arrayList);
        List<DeviceMaintenance> supplierManageList = deviceMaintenanceMapper.selectList(null);
        ArrayList<DeviceMaintenanceExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
        supplierManageList.stream().forEach(deviceMaintenance -> {
        supplierManageList.forEach(deviceMaintenance -> {
            DeviceMaintenanceExeclDto deviceRepairExeclDto = new DeviceMaintenanceExeclDto();
            BeanUtils.copyProperties(deviceMaintenance,deviceRepairExeclDto);
            deviceRepairExeclDto.setStatus(deviceMaintenance.getStatus() == 0 ? "待维修" : "完结");
            deviceRepairExeclDto.setMaintenanceResult(deviceMaintenance.getMaintenanceResult() == 0 ? "维修" : "完好");
            deviceRepairExeclDto.setMaintenanceResult(deviceMaintenance.getMaintenanceResult() != null && deviceMaintenance.getMaintenanceResult() == 0 ? "维修" : "完好");
            deviceLedgerExeclDtos.add(deviceRepairExeclDto);
        });
        ExcelUtil<DeviceMaintenanceExeclDto> util = new ExcelUtil<DeviceMaintenanceExeclDto>(DeviceMaintenanceExeclDto.class);
        util.exportExcel(response, deviceLedgerExeclDtos, "设备报修导出");
    }
    @Override
    public DeviceMaintenanceDto detailById(Long id) {
        return deviceMaintenanceMapper.detailById(id);
    }
    @Autowired
    private DeviceLedgerMapper deviceLedgerMapper;
    @Override
    public List<DeviceMonthlyRepairTableDTO> getMonthlyRepairAmountByYear(String year) {
        List<RepairAmountGroupDTO> repairAmountGroupDTOS = deviceMaintenanceMapper.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 = deviceMaintenanceMapper.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;
    }
}