maven
2025-12-08 0100359605ba05638c1f683d68a2948cc273b0d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.ruoyi.device.service.impl;
 
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
 
@Service
public class DeviceMaintenanceServiceImpl extends ServiceImpl<DeviceMaintenanceMapper, DeviceMaintenance> implements IDeviceMaintenanceService {
 
 
 
    @Autowired
    private DeviceMaintenanceMapper deviceMaintenanceMapper;
 
    @Override
    public IPage<DeviceMaintenanceDto> queryPage(Page page, DeviceMaintenanceDto deviceMaintenanceDto) {
 
        return deviceMaintenanceMapper.queryPage(page, deviceMaintenanceDto);
    }
 
    @Override
    public AjaxResult saveDeviceRepair(DeviceMaintenance deviceMaintenance) {
        boolean save = this.save(deviceMaintenance);
        if (save){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
 
    @Override
    public AjaxResult updateDeviceDeviceMaintenance(DeviceMaintenance deviceMaintenance) {
        if (this.updateById(deviceMaintenance)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
 
    @Override
    public void export(HttpServletResponse response, Long[] ids) {
        List<DeviceMaintenance> supplierManageList = deviceMaintenanceMapper.selectList(null);
        ArrayList<DeviceMaintenanceExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
        supplierManageList.forEach(deviceMaintenance -> {
            DeviceMaintenanceExeclDto deviceRepairExeclDto = new DeviceMaintenanceExeclDto();
            BeanUtils.copyProperties(deviceMaintenance,deviceRepairExeclDto);
            deviceRepairExeclDto.setStatus(deviceMaintenance.getStatus() == 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;
    }
}