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.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;
|
import com.ruoyi.device.pojo.DeviceRepair;
|
import com.ruoyi.device.service.DeviceDefectRecordService;
|
import com.ruoyi.device.service.IDeviceLedgerService;
|
import com.ruoyi.device.service.IDeviceRepairService;
|
import com.ruoyi.framework.web.domain.AjaxResult;
|
import lombok.AllArgsConstructor;
|
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.math.BigDecimal;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
|
@Service
|
@AllArgsConstructor
|
@Slf4j
|
public class DeviceRepairServiceImpl extends ServiceImpl<DeviceRepairMapper, DeviceRepair> implements IDeviceRepairService {
|
|
@Autowired
|
private DeviceDefectRecordService deviceDefectRecordService;
|
@Autowired
|
private DeviceRepairMapper deviceRepairMapper;
|
@Autowired
|
private IDeviceLedgerService deviceLedgerService;
|
@Override
|
public IPage<DeviceRepairDto> queryPage(Page page, DeviceRepairDto deviceRepairDto) {
|
|
return deviceRepairMapper.queryPage(page, deviceRepairDto);
|
}
|
|
@Override
|
public AjaxResult saveDeviceRepair(DeviceRepair deviceRepair) {
|
DeviceLedger byId = deviceLedgerService.getById(deviceRepair.getDeviceLedgerId());
|
deviceRepair.setDeviceName(byId.getDeviceName());
|
deviceRepair.setDeviceModel(byId.getDeviceModel());
|
boolean save = this.save(deviceRepair);
|
if (save){
|
return AjaxResult.success();
|
}
|
return AjaxResult.error();
|
}
|
|
@Override
|
public AjaxResult updateDeviceRepair(DeviceRepair deviceRepair) {
|
if (this.updateById(deviceRepair)) {
|
Long id = deviceRepair.getId();
|
//
|
DeviceDefectRecordDto deviceDefectRecordDto = new DeviceDefectRecordDto();
|
deviceDefectRecordDto.setDeviceLedgerId(id);
|
deviceDefectRecordDto.setStatus("严重缺陷");
|
List<DeviceDefectRecordDto> records = deviceDefectRecordService.listPage(new Page<>(1, -1), deviceDefectRecordDto).getRecords();
|
if (!records.isEmpty()){
|
records.forEach(deviceDefectRecord -> {
|
deviceDefectRecord.setStatus("正常");
|
deviceDefectRecordService.updateByDDR(deviceDefectRecord);
|
});
|
}
|
return AjaxResult.success();
|
}
|
return AjaxResult.error();
|
}
|
|
@Override
|
public void export(HttpServletResponse response, Long[] ids) {
|
if (ids == null || ids.length == 0) {
|
List<DeviceRepair> supplierManageList = this.list();
|
ArrayList<DeviceRepairExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
|
supplierManageList.stream().forEach(deviceRepair -> {
|
DeviceRepairExeclDto deviceRepairExeclDto = new DeviceRepairExeclDto();
|
BeanUtils.copyProperties(deviceRepair,deviceRepairExeclDto);
|
deviceRepairExeclDto.setStatusStr(deviceRepair.getStatus() == 0 ? "待维修" : "完结");
|
|
deviceLedgerExeclDtos.add(deviceRepairExeclDto);
|
});
|
ExcelUtil<DeviceRepairExeclDto> util = new ExcelUtil<DeviceRepairExeclDto>(DeviceRepairExeclDto.class);
|
util.exportExcel(response, deviceLedgerExeclDtos, "设备报修导出");
|
}else {
|
ArrayList<Long> arrayList = new ArrayList<>();
|
Arrays.stream(ids).map(id -> {
|
return arrayList.add( id);
|
});
|
List<DeviceRepair> supplierManageList = deviceRepairMapper.selectBatchIds(arrayList);
|
ArrayList<DeviceRepairExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
|
supplierManageList.stream().forEach(deviceRepair -> {
|
DeviceRepairExeclDto deviceRepairExeclDto = new DeviceRepairExeclDto();
|
BeanUtils.copyProperties(deviceRepair,deviceRepairExeclDto);
|
deviceRepairExeclDto.setStatusStr(deviceRepair.getStatus() == 0 ? "待维修" : "完结");
|
|
deviceLedgerExeclDtos.add(deviceRepairExeclDto);
|
});
|
ExcelUtil<DeviceRepairExeclDto> util = new ExcelUtil<DeviceRepairExeclDto>(DeviceRepairExeclDto.class);
|
util.exportExcel(response, deviceLedgerExeclDtos, "设备报修导出");
|
}
|
|
}
|
|
@Override
|
public DeviceRepairDto detailById(Long id) {
|
|
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;
|
}
|
|
}
|