package com.qianwen.smartman.modules.smis.service.impl;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Optional;
|
import java.util.stream.Collectors;
|
|
import org.springframework.stereotype.Service;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.qianwen.core.log.exception.ServiceException;
|
import com.qianwen.core.mp.service.impl.BladeServiceImpl;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.smartman.common.utils.MessageUtils;
|
import com.qianwen.smartman.modules.smis.convert.CheckItemConvert;
|
import com.qianwen.smartman.modules.smis.dto.CheckItemSubmitDTO;
|
import com.qianwen.smartman.modules.smis.entity.CheckItem;
|
import com.qianwen.smartman.modules.smis.mapper.CheckItemMapper;
|
import com.qianwen.smartman.modules.smis.service.ICheckItemService;
|
import com.qianwen.smartman.modules.smis.vo.CheckItemSubmitVO;
|
import com.qianwen.smartman.modules.smis.vo.CheckItemUpdateVO;
|
import com.qianwen.smartman.modules.smis.vo.CheckItemVO;
|
|
@Service
|
public class CheckItemServiceImpl extends BladeServiceImpl<CheckItemMapper, CheckItem> implements ICheckItemService {
|
|
|
@Override
|
public List<CheckItem> createCheckItem(CheckItemSubmitVO vo) {
|
List<CheckItemSubmitDTO> items = vo.getItems();
|
List<String> names = items.stream().map((v0) -> {
|
return v0.getName();
|
}).distinct().collect(Collectors.toList());
|
if (items.size() != names.size()) {
|
throw new ServiceException(MessageUtils.message("cps.tpm.check.item.exists", new Object[0]));
|
}
|
Long checkProjectId = vo.getCheckProjectId();
|
createNotSameName(checkProjectId, names);
|
List<CheckItem> checkItems = items.stream().map(c -> {
|
return CheckItem.builder().checkProjectId(checkProjectId).name(c.getName()).valueType(c.getValueType()).standardValue(c.getStandardValue()).requirement(c.getRequirement()).build();
|
}).collect(Collectors.toList());
|
saveBatch(checkItems);
|
return checkItems;
|
}
|
|
@Override
|
public boolean updateCheckItem(CheckItemUpdateVO vo) {
|
Long id = vo.getId();
|
String valueType = vo.getValueType();
|
String requirement = vo.getRequirement();
|
String standardValue = vo.getStandardValue();
|
String name = vo.getName();
|
Long checkProjectId = vo.getCheckProjectId();
|
if (Func.isNotBlank(name)) {
|
validSameName(checkProjectId, id, name);
|
}
|
|
return update(Wrappers.<CheckItem>lambdaUpdate()
|
.set(CheckItem::getValueType, valueType)
|
.set(CheckItem::getRequirement, requirement)
|
.set(CheckItem::getStandardValue, standardValue)
|
.set(CheckItem::getName, name)
|
.eq(CheckItem::getId, id));
|
}
|
|
@Override
|
public List<CheckItemVO> listCheckItem(String projectId) {
|
List<CheckItem> list = list(Wrappers.<CheckItem>lambdaQuery().eq(CheckItem::getCheckProjectId, projectId).orderByAsc(CheckItem::getCreateTime));
|
|
return Optional.<List<CheckItem>>ofNullable(list)
|
.map(CheckItemConvert.INSTANCE::convert)
|
.orElse(new ArrayList<>());
|
|
}
|
|
private void validSameName(Long checkProjectId, Long id, String name) {
|
long count = count(Wrappers.<CheckItem>lambdaQuery().eq(CheckItem::getCheckProjectId, checkProjectId).ne(CheckItem::getId, id).eq(CheckItem::getName, name));
|
|
if (count > 0) {
|
throw new ServiceException(MessageUtils.message("cps.tpm.check.item.exists", new Object[0]));
|
}
|
}
|
|
private void createNotSameName(Long id, List<String> names) {
|
long count = count(Wrappers.<CheckItem>lambdaQuery().eq(CheckItem::getCheckProjectId, id).in(CheckItem::getName, names));
|
if (count > 0) {
|
throw new ServiceException(MessageUtils.message("cps.tpm.check.item.exists", new Object[0]));
|
}
|
}
|
}
|