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.basic.enums.RecordTypeEnum;
|
import com.ruoyi.basic.utils.FileUtil;
|
import com.ruoyi.common.utils.bean.BeanUtils;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import com.ruoyi.device.dto.DeviceMaintenanceDto;
|
import com.ruoyi.device.execl.DeviceMaintenanceExeclDto;
|
import com.ruoyi.device.mapper.DeviceMaintenanceMapper;
|
import com.ruoyi.device.mapper.DeviceMaintenanceItemMapper;
|
import com.ruoyi.device.mapper.MaintenanceItemMapper;
|
import com.ruoyi.device.pojo.DeviceMaintenance;
|
import com.ruoyi.device.pojo.DeviceMaintenanceItem;
|
import com.ruoyi.device.pojo.MaintenanceItem;
|
import com.ruoyi.device.service.IDeviceMaintenanceService;
|
import com.ruoyi.device.vo.DeviceMaintenanceVo;
|
import com.ruoyi.device.vo.DeviceRepairVo;
|
import com.ruoyi.framework.web.domain.AjaxResult;
|
import com.ruoyi.measuringinstrumentledger.mapper.SparePartsMapper;
|
import com.ruoyi.measuringinstrumentledger.pojo.SpareParts;
|
import com.ruoyi.measuringinstrumentledger.pojo.SparePartsRequisitionRecord;
|
import com.ruoyi.measuringinstrumentledger.service.SparePartsRequisitionRecordService;
|
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
|
import jakarta.servlet.http.HttpServletResponse;
|
import lombok.RequiredArgsConstructor;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.stream.Collectors;
|
import java.time.LocalDateTime;
|
|
|
@Service
|
@RequiredArgsConstructor
|
public class DeviceMaintenanceServiceImpl extends ServiceImpl<DeviceMaintenanceMapper, DeviceMaintenance> implements IDeviceMaintenanceService {
|
|
private final DeviceMaintenanceMapper deviceMaintenanceMapper;
|
private final SparePartsMapper sparePartsMapper;
|
private final SparePartsRequisitionRecordService sparePartsRequisitionRecordService;
|
private final FileUtil fileUtil;
|
private final DeviceMaintenanceItemMapper deviceMaintenanceItemMapper;
|
private final MaintenanceItemMapper maintenanceItemMapper;
|
|
@Override
|
public IPage<DeviceMaintenanceDto> queryPage(Page page, DeviceMaintenanceDto deviceMaintenanceDto) {
|
IPage<DeviceMaintenanceDto> result = deviceMaintenanceMapper.queryPage(page, deviceMaintenanceDto);
|
if (result.getRecords() != null && !result.getRecords().isEmpty()) {
|
List<Long> ids = result.getRecords().stream().map(DeviceMaintenance::getId).toList();
|
List<DeviceMaintenanceItem> items = deviceMaintenanceItemMapper.selectList(new LambdaQueryWrapper<DeviceMaintenanceItem>().in(DeviceMaintenanceItem::getDeviceMaintenanceId, ids).orderByAsc(DeviceMaintenanceItem::getSortNum));
|
Map<Long, List<DeviceMaintenanceItem>> byRecord = items.stream().collect(Collectors.groupingBy(DeviceMaintenanceItem::getDeviceMaintenanceId));
|
result.getRecords().forEach(record -> record.setMaintenanceItems(byRecord.getOrDefault(record.getId(), new ArrayList<>())));
|
}
|
return result;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public AjaxResult saveDeviceRepair(DeviceMaintenanceDto deviceMaintenance) {
|
List<MaintenanceItem> selectedItems = getSelectedItems(deviceMaintenance.getMaintenanceItemIds(), null, deviceMaintenance.getDeviceLedgerId(), deviceMaintenance.getFrequencyType());
|
if (selectedItems == null) return AjaxResult.error("请选择至少一个有效的保养项目");
|
boolean save = this.save(deviceMaintenance);
|
if (save) {
|
saveRecordItems(deviceMaintenance, selectedItems);
|
// 处理图片上传
|
fileUtil.saveStorageAttachmentByRecordTypeAndRecordId("file", RecordTypeEnum.DEVICE_MAINTENANCE, deviceMaintenance.getId(), deviceMaintenance.getStorageBlobDTOs());
|
return AjaxResult.success();
|
}
|
return AjaxResult.error();
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public AjaxResult updateDeviceDeviceMaintenance(DeviceMaintenanceDto deviceMaintenance) {
|
DeviceMaintenance oldDeviceMaintenance = this.getById(deviceMaintenance.getId());
|
if (oldDeviceMaintenance == null) return AjaxResult.error("保养记录不存在");
|
if (deviceMaintenance.getMaintenanceItems() != null) {
|
AjaxResult validation = validateRecordItems(deviceMaintenance, oldDeviceMaintenance);
|
if (validation != null) return validation;
|
}
|
// 处理备件使用情况
|
if (CollectionUtils.isNotEmpty(deviceMaintenance.getSparePartsUseList())) {
|
List<Long> sparePartIds = new ArrayList<>();
|
for (DeviceMaintenance.SparePartUse sparePartUse : deviceMaintenance.getSparePartsUseList()) {
|
// 获取备件信息
|
SpareParts spareParts = sparePartsMapper.selectById(sparePartUse.getId());
|
if (spareParts != null) {
|
// 检查数量是否足够
|
if (spareParts.getQuantity().compareTo(new BigDecimal(sparePartUse.getQuantity())) >= 0) {
|
// 更新数量
|
spareParts.setQuantity(spareParts.getQuantity().subtract(new BigDecimal(sparePartUse.getQuantity())));
|
sparePartsMapper.updateById(spareParts);
|
sparePartIds.add(sparePartUse.getId());
|
|
// 创建备件领用记录
|
SparePartsRequisitionRecord record = new SparePartsRequisitionRecord();
|
record.setSourceType(1); // 1 保养
|
record.setSourceId(deviceMaintenance.getId());
|
record.setDeviceLedgerId(oldDeviceMaintenance.getDeviceLedgerId());
|
record.setSparePartsId(sparePartUse.getId());
|
record.setQuantity(sparePartUse.getQuantity());
|
sparePartsRequisitionRecordService.save(record);
|
} else {
|
return AjaxResult.error("备件 " + spareParts.getName() + " 数量不足");
|
}
|
}
|
}
|
// 更新备件IDs字段
|
if (!sparePartIds.isEmpty()) {
|
deviceMaintenance.setSparePartsIds(StringUtils.join(sparePartIds, ","));
|
}
|
}
|
|
if (deviceMaintenance.getMaintenanceItems() != null) {
|
AjaxResult itemResult = updateRecordItems(deviceMaintenance, oldDeviceMaintenance);
|
if (itemResult != null) return itemResult;
|
} else if (deviceMaintenance.getMaintenanceItemIds() != null) {
|
List<MaintenanceItem> selectedItems = getSelectedItems(deviceMaintenance.getMaintenanceItemIds(), deviceMaintenance.getId(), oldDeviceMaintenance.getDeviceLedgerId(), oldDeviceMaintenance.getFrequencyType());
|
if (selectedItems == null) return AjaxResult.error("请选择至少一个有效的保养项目");
|
deviceMaintenanceItemMapper.delete(new LambdaQueryWrapper<DeviceMaintenanceItem>().eq(DeviceMaintenanceItem::getDeviceMaintenanceId, deviceMaintenance.getId()));
|
saveRecordItems(deviceMaintenance, selectedItems);
|
}
|
|
if (this.updateById(deviceMaintenance)) {
|
// 处理图片上传
|
fileUtil.saveStorageAttachmentByRecordTypeAndRecordId("file", RecordTypeEnum.DEVICE_MAINTENANCE, deviceMaintenance.getId(), deviceMaintenance.getStorageBlobDTOs());
|
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);
|
List<DeviceMaintenanceItem> recordItems = deviceMaintenanceItemMapper.selectList(new LambdaQueryWrapper<DeviceMaintenanceItem>().eq(DeviceMaintenanceItem::getDeviceMaintenanceId, deviceMaintenance.getId()).orderByAsc(DeviceMaintenanceItem::getSortNum));
|
deviceRepairExeclDto.setMachineryCategory(recordItems.stream().map(DeviceMaintenanceItem::getItemName).collect(Collectors.joining("、")));
|
deviceRepairExeclDto.setStatus(deviceMaintenance.getStatus() == 0 ? "待维修" : deviceMaintenance.getStatus() == 1 ? "完结" : "失败");
|
// 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 DeviceMaintenanceVo detailById(Long id) {
|
DeviceMaintenanceVo vo = deviceMaintenanceMapper.detailById(id);
|
if (vo != null) {
|
vo.setMaintenanceItems(deviceMaintenanceItemMapper.selectList(new LambdaQueryWrapper<DeviceMaintenanceItem>().eq(DeviceMaintenanceItem::getDeviceMaintenanceId, id).orderByAsc(DeviceMaintenanceItem::getSortNum)));
|
}
|
vo.setStorageBlobVOs(fileUtil.getStorageBlobVOsByRecordTypeAndRecordId(RecordTypeEnum.DEVICE_MAINTENANCE, id));
|
return vo;
|
}
|
|
private List<MaintenanceItem> getSelectedItems(List<Long> ids, Long recordId, Long deviceId, String frequencyType) {
|
if (deviceId == null || !List.of("DAILY", "WEEKLY", "MONTHLY", "QUARTERLY").contains(frequencyType) || ids == null || ids.isEmpty() || new HashSet<>(ids).size() != ids.size())
|
return null;
|
List<MaintenanceItem> items = maintenanceItemMapper.selectList(new LambdaQueryWrapper<MaintenanceItem>().in(MaintenanceItem::getId, ids));
|
if (items.size() != ids.size()) return null;
|
if (items.stream().anyMatch(item -> (item.getDeviceLedgerId() != null && !deviceId.equals(item.getDeviceLedgerId())) || !(frequencyType.equals(item.getFrequencyType()) || "ALL".equals(item.getFrequencyType()))))
|
return null;
|
Map<Long, MaintenanceItem> itemMap = items.stream().collect(Collectors.toMap(MaintenanceItem::getId, item -> item));
|
Set<Long> existingIds = recordId == null ? Collections.emptySet() : deviceMaintenanceItemMapper.selectList(new LambdaQueryWrapper<DeviceMaintenanceItem>().eq(DeviceMaintenanceItem::getDeviceMaintenanceId, recordId)).stream().map(DeviceMaintenanceItem::getMaintenanceItemId).collect(Collectors.toSet());
|
if (items.stream().anyMatch(item -> (item.getDeleted() != null && item.getDeleted() == 1 || item.getIsActive() == null || item.getIsActive() != 1) && !existingIds.contains(item.getId())))
|
return null;
|
return ids.stream().map(itemMap::get).toList();
|
}
|
|
private void saveRecordItems(DeviceMaintenance record, List<MaintenanceItem> selectedItems) {
|
for (int i = 0; i < selectedItems.size(); i++) {
|
MaintenanceItem source = selectedItems.get(i);
|
DeviceMaintenanceItem item = new DeviceMaintenanceItem();
|
item.setDeviceMaintenanceId(record.getId());
|
item.setMaintenanceItemId(source.getId());
|
item.setItemProject(source.getItemProject());
|
item.setItemName(source.getItemName());
|
item.setItemStatus(0);
|
item.setSortNum(i);
|
item.setTenantId(record.getTenantId());
|
item.setDeptId(record.getDeptId());
|
deviceMaintenanceItemMapper.insert(item);
|
}
|
}
|
|
private AjaxResult updateRecordItems(DeviceMaintenanceDto request, DeviceMaintenance record) {
|
AjaxResult validation = validateRecordItems(request, record);
|
if (validation != null) return validation;
|
List<DeviceMaintenanceItem> current = deviceMaintenanceItemMapper.selectList(new LambdaQueryWrapper<DeviceMaintenanceItem>().eq(DeviceMaintenanceItem::getDeviceMaintenanceId, record.getId()));
|
Map<Long, DeviceMaintenanceItem> currentById = current.stream().collect(Collectors.toMap(DeviceMaintenanceItem::getId, item -> item));
|
for (DeviceMaintenanceItem requested : request.getMaintenanceItems()) {
|
DeviceMaintenanceItem item = currentById.get(requested.getId());
|
int status = requested.getItemStatus() == null ? 0 : requested.getItemStatus();
|
item.setItemStatus(status);
|
if (requested.getRemarks() != null) item.setRemarks(requested.getRemarks());
|
item.setCompletedBy(status == 1 ? request.getMaintenanceActuallyName() : null);
|
item.setCompletedTime(status == 1 ? LocalDateTime.now() : null);
|
deviceMaintenanceItemMapper.updateById(item);
|
}
|
return null;
|
}
|
|
private AjaxResult validateRecordItems(DeviceMaintenanceDto request, DeviceMaintenance record) {
|
List<DeviceMaintenanceItem> requestedItems = request.getMaintenanceItems();
|
if (requestedItems.stream().anyMatch(item -> item == null || item.getId() == null || (item.getItemStatus() != null && (item.getItemStatus() < 0 || item.getItemStatus() > 2)))) {
|
return AjaxResult.error("保养项目状态无效");
|
}
|
List<DeviceMaintenanceItem> current = deviceMaintenanceItemMapper.selectList(new LambdaQueryWrapper<DeviceMaintenanceItem>().eq(DeviceMaintenanceItem::getDeviceMaintenanceId, record.getId()));
|
Set<Long> currentIds = current.stream().map(DeviceMaintenanceItem::getId).collect(Collectors.toSet());
|
Set<Long> requestedIds = requestedItems.stream().map(DeviceMaintenanceItem::getId).collect(Collectors.toSet());
|
if (requestedIds.size() != current.size() || !requestedIds.equals(currentIds)) {
|
return AjaxResult.error("保养项目清单已变化,请刷新后重试");
|
}
|
return null;
|
}
|
}
|