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 implements ICheckItemService { @Override public List createCheckItem(CheckItemSubmitVO vo) { List items = vo.getItems(); List 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 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.lambdaUpdate() .set(CheckItem::getValueType, valueType) .set(CheckItem::getRequirement, requirement) .set(CheckItem::getStandardValue, standardValue) .set(CheckItem::getName, name) .eq(CheckItem::getId, id)); } @Override public List listCheckItem(String projectId) { List list = list(Wrappers.lambdaQuery().eq(CheckItem::getCheckProjectId, projectId).orderByAsc(CheckItem::getCreateTime)); return Optional.>ofNullable(list) .map(CheckItemConvert.INSTANCE::convert) .orElse(new ArrayList<>()); } private void validSameName(Long checkProjectId, Long id, String name) { long count = count(Wrappers.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 names) { long count = count(Wrappers.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])); } } }