yangys
2024-10-30 c27b939fa5fa6ce4d712f7e9ced2ad811d69d5ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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]));
        }
    }
}