package com.ruoyi.inspectiontask.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.inspectiontask.mapper.InspectionItemMapper;
|
import com.ruoyi.inspectiontask.pojo.InspectionItem;
|
import com.ruoyi.inspectiontask.service.InspectionItemService;
|
import lombok.RequiredArgsConstructor;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.List;
|
|
@Service
|
@RequiredArgsConstructor
|
public class InspectionItemServiceImpl implements InspectionItemService {
|
|
private static final List<String> VALID_FREQUENCIES = List.of(
|
"DAILY", "WEEKLY", "MONTHLY", "QUARTERLY", "ALL"
|
);
|
|
private final InspectionItemMapper inspectionItemMapper;
|
|
@Override
|
public List<InspectionItem> list(InspectionItem query, boolean includeCommon) {
|
return inspectionItemMapper.selectList(buildQuery(query, includeCommon)
|
.orderByAsc(InspectionItem::getSortNum)
|
.orderByAsc(InspectionItem::getId));
|
}
|
|
@Override
|
public Page<InspectionItem> page(long pageNum, long pageSize, boolean includeCommon, InspectionItem query) {
|
long safePageNum = Math.max(pageNum, 1);
|
long safePageSize = Math.min(Math.max(pageSize, 1), 100);
|
return inspectionItemMapper.selectPage(new Page<>(safePageNum, safePageSize),
|
buildQuery(query, includeCommon)
|
.orderByAsc(InspectionItem::getSortNum)
|
.orderByAsc(InspectionItem::getId));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void create(InspectionItem item) {
|
validate(item);
|
normalize(item);
|
item.setDeleted(0);
|
if (existsDuplicate(item, null)) {
|
throw new ServiceException("该设备和频率下已存在同名巡检内容");
|
}
|
if (inspectionItemMapper.insert(item) <= 0) {
|
throw new ServiceException("新增巡检内容失败");
|
}
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(InspectionItem item) {
|
if (item == null || item.getId() == null) {
|
throw new ServiceException("巡检内容编号不能为空");
|
}
|
validate(item);
|
InspectionItem existing = inspectionItemMapper.selectById(item.getId());
|
if (existing == null || Integer.valueOf(1).equals(existing.getDeleted())) {
|
throw new ServiceException("巡检内容不存在");
|
}
|
normalize(item);
|
if (existsDuplicate(item, item.getId())) {
|
throw new ServiceException("该设备和频率下已存在同名巡检内容");
|
}
|
|
existing.setItemProject(item.getItemProject());
|
existing.setItemContent(item.getItemContent());
|
existing.setDeviceLedgerId(item.getDeviceLedgerId());
|
existing.setFrequencyType(item.getFrequencyType());
|
existing.setSortNum(item.getSortNum());
|
existing.setIsActive(item.getIsActive());
|
existing.setRemarks(item.getRemarks());
|
if (inspectionItemMapper.updateById(existing) <= 0) {
|
throw new ServiceException("修改巡检内容失败");
|
}
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void delete(Long id) {
|
if (id == null) {
|
throw new ServiceException("巡检内容编号不能为空");
|
}
|
InspectionItem item = inspectionItemMapper.selectById(id);
|
if (item == null || Integer.valueOf(1).equals(item.getDeleted())) {
|
throw new ServiceException("巡检内容不存在");
|
}
|
item.setDeleted(1);
|
item.setIsActive(0);
|
if (inspectionItemMapper.updateById(item) <= 0) {
|
throw new ServiceException("删除巡检内容失败");
|
}
|
}
|
|
private LambdaQueryWrapper<InspectionItem> buildQuery(InspectionItem query, boolean includeCommon) {
|
InspectionItem filter = query == null ? new InspectionItem() : query;
|
LambdaQueryWrapper<InspectionItem> wrapper = new LambdaQueryWrapper<InspectionItem>()
|
.eq(InspectionItem::getDeleted, 0);
|
if (StringUtils.isNotBlank(filter.getItemProject())) {
|
wrapper.like(InspectionItem::getItemProject, filter.getItemProject().trim());
|
}
|
if (StringUtils.isNotBlank(filter.getItemContent())) {
|
wrapper.like(InspectionItem::getItemContent, filter.getItemContent().trim());
|
}
|
if (StringUtils.isNotBlank(filter.getItemKeyword())) {
|
String keyword = filter.getItemKeyword().trim();
|
wrapper.and(condition -> condition
|
.like(InspectionItem::getItemProject, keyword)
|
.or()
|
.like(InspectionItem::getItemContent, keyword));
|
}
|
if (filter.getIsActive() != null) {
|
wrapper.eq(InspectionItem::getIsActive, filter.getIsActive());
|
}
|
if (filter.getDeviceLedgerId() != null) {
|
if (includeCommon) {
|
wrapper.and(condition -> condition
|
.eq(InspectionItem::getDeviceLedgerId, filter.getDeviceLedgerId())
|
.or()
|
.isNull(InspectionItem::getDeviceLedgerId));
|
} else {
|
wrapper.eq(InspectionItem::getDeviceLedgerId, filter.getDeviceLedgerId());
|
}
|
}
|
if (StringUtils.isNotBlank(filter.getFrequencyType())) {
|
if (includeCommon) {
|
wrapper.and(condition -> condition
|
.eq(InspectionItem::getFrequencyType, filter.getFrequencyType())
|
.or()
|
.eq(InspectionItem::getFrequencyType, "ALL"));
|
} else {
|
wrapper.eq(InspectionItem::getFrequencyType, filter.getFrequencyType());
|
}
|
}
|
return wrapper;
|
}
|
|
private void validate(InspectionItem item) {
|
if (item == null || StringUtils.isBlank(item.getItemProject())
|
|| StringUtils.isBlank(item.getItemContent())
|
|| StringUtils.isBlank(item.getFrequencyType())
|
|| !VALID_FREQUENCIES.contains(item.getFrequencyType())) {
|
throw new ServiceException("巡检项目、巡检内容和频率不能为空或无效");
|
}
|
}
|
|
private void normalize(InspectionItem item) {
|
item.setItemProject(item.getItemProject().trim());
|
item.setItemContent(item.getItemContent().trim());
|
item.setSortNum(item.getSortNum() == null ? 0 : item.getSortNum());
|
item.setIsActive(item.getIsActive() == null ? 1 : item.getIsActive());
|
}
|
|
private boolean existsDuplicate(InspectionItem item, Long excludedId) {
|
LambdaQueryWrapper<InspectionItem> wrapper = new LambdaQueryWrapper<InspectionItem>()
|
.eq(InspectionItem::getDeleted, 0)
|
.eq(InspectionItem::getItemProject, item.getItemProject())
|
.eq(InspectionItem::getItemContent, item.getItemContent())
|
.eq(InspectionItem::getFrequencyType, item.getFrequencyType());
|
if (item.getDeviceLedgerId() == null) {
|
wrapper.isNull(InspectionItem::getDeviceLedgerId);
|
} else {
|
wrapper.eq(InspectionItem::getDeviceLedgerId, item.getDeviceLedgerId());
|
}
|
if (excludedId != null) {
|
wrapper.ne(InspectionItem::getId, excludedId);
|
}
|
return inspectionItemMapper.selectCount(wrapper) > 0;
|
}
|
}
|