lihang
2023-06-15 b48356e1b39f8e812f85d1627c0fc18e1f4d58ba
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.vci.ubcs.omd.service.impl;
 
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.omd.constant.OmdCacheConstant;
import com.vci.ubcs.omd.dto.AttributeDTO;
import com.vci.ubcs.omd.dto.BtmTypeLinkAttributesDTO;
import com.vci.ubcs.omd.entity.Attribute;
import com.vci.ubcs.omd.entity.BtmType;
import com.vci.ubcs.omd.mapper.AttributeMapper;
import com.vci.ubcs.omd.service.IAttributeService;
import com.vci.ubcs.omd.vo.AttributeVO;
import com.vci.ubcs.omd.vo.BtmTypeVO;
import com.vci.ubcs.omd.wrapper.AttributeWrapper;
import com.vci.ubcs.omd.wrapper.BtmTypeWrapper;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.constant.OmdRegExpConstant;
import com.vci.ubcs.starter.web.enumpck.VciFieldTypeEnum;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
 
/**
 * Description: 元数据(属性)的服务
 *
 * @author LiHang
 * @date 2023/4/3
 */
@Service
public class AttributeServiceImpl extends ServiceImpl<AttributeMapper, Attribute>  implements IAttributeService {
 
    private final String REGEXP = "^[A-Za-z]+$";
 
    @Override
    public boolean deleteLogic(@NotEmpty List<Long> ids) {
        return false;
    }
 
    @Override
    public boolean changeStatus(@NotEmpty List<Long> ids, Integer status) {
        return false;
    }
 
    /**
     * 获取元数据详情
     *
     * @param id 主键
     * @return 元数据显示对象
     */
    @Override
    public AttributeVO getAttributeDetail(Long id) {
        Func.requireNotNull(id,"主键不能为空");
        return AttributeWrapper.build().entityVO(baseMapper.selectById(id));
    }
 
    /**
     * 新增或修改
     *
     * @param dto 页面传输对象
     * @return 执行结果
     */
    @Override
    public boolean submit(AttributeDTO dto) {
        VciBaseUtil.alertNotNull(dto.getId(),"属性编号",dto.getName(),"属性名称",dto.getTypeKey(),"属性类型",dto.getMaxLength(),"属性长度");
        if (!Pattern.compile(REGEXP).matcher(dto.getId()).matches()){
            throw new VciBaseException("属性编号{0}只能是英文",new Object[]{dto.getId()});
        }
        LambdaQueryWrapper<Attribute> wrapper = Wrappers.<Attribute>query().lambda().eq(Attribute::getId, dto.getId());
        Long count = baseMapper.selectCount((Func.isEmpty(dto.getOid())) ? wrapper : wrapper.notIn(Attribute::getOid, dto.getOid()));
        if (count > 0L) {
            throw new ServiceException("属性编号已存在!");
        }
        Attribute attribute = AttributeWrapper.build().copyBeforeSave(dto);
        CacheUtil.clear(OmdCacheConstant.ATTR_CACHE);
        return saveOrUpdate(attribute);
    }
 
    /**
     * 删除
     *
     * @param oids 主键集合
     * @return 执行结果
     */
    @Override
    public boolean removeAttrs(String oids) {
        List<String> oidList = Func.toStrList(",", oids);
        List<Attribute> list = list(Wrappers.<Attribute>query().lambda().in(Attribute::getOid, oidList));
        if (!CollectionUtils.isEmpty(list)){
            baseMapper.delete(Wrappers.<Attribute>query().lambda().in(Attribute::getOid,list.stream().map(Attribute::getOid).collect(Collectors.toList())));
        }
        return true;
    }
 
    /**
     * 查看应用范围
     *
     * @param oid 主键
     * @return 查询已应用的业务类型名称
     */
    @Override
    public List<BtmTypeVO> applyRange(String oid) {
        List<BtmType> btmTypes = baseMapper.selectApplyRange(oid);
        if (CollectionUtils.isEmpty(btmTypes)){
            return null;
        }
        return BtmTypeWrapper.build().listEntityVO(btmTypes);
    }
 
    /**
     * 检查属性是否存在
     *
     * @param keyCollections 编号集合
     * @return true表示都存在,false表示不存在,不存在的时候会抛出异常
     * @throws VciBaseException 不存在的时候会抛出异常
     */
    @Override
    public boolean checkAttributeExists(Collection<String> keyCollections) throws VciBaseException {
        List<AttributeVO> existAttributeVOList = listAttributeByKeyCollection(keyCollections);
        if (CollectionUtils.isEmpty(existAttributeVOList)) {
            throw new VciBaseException("使用的属性都在系统中不存在,请先查证");
        } else {
            Set<String> existAttributeOidSet = (existAttributeVOList.stream().collect(Collectors.toMap(s -> s.getId().toLowerCase().trim(), t -> t))).keySet();
            keyCollections.stream().forEach(s -> {
                if (!existAttributeOidSet.contains(s)) {
                    throw new VciBaseException("使用的属性{0}在系统中不存在,请先查证", new Object[]{s});
                }
            });
        }
        return true;
    }
 
    /**
     * 根据编号集合批量获取属性对象
     *
     * @param attributeIdCollection 编号集合
     * @return 属性对象列表,如果有不存在的不会返回,全部不存在的则返回空列表
     * @throws VciBaseException 参数为空或者查询出错时会抛出错误
     */
    @Override
    public List<AttributeVO> listAttributeByKeyCollection(Collection<String> attributeIdCollection) throws VciBaseException {
        if(!CollectionUtils.isEmpty(attributeIdCollection)){
            List<Attribute> attributeDOList = listAttributeByKeyCollectionDO(attributeIdCollection);
            if(!CollectionUtils.isEmpty(attributeDOList)) {
                return AttributeWrapper.build().listEntityVO(attributeDOList);
            }
        }
        return null;
    }
 
    /**
     * 根据编号集合获取属性数据对象
     * @param attributeIdCollection 属性的编号集合
     * @return 属性数据对象列表,如果有不存在的不会返回,全部不存在的则返回空列表
     * @throws VciBaseException mybatis查询出错的时候会抛出异常
     */
    private List<Attribute> listAttributeByKeyCollectionDO(Collection<String> attributeIdCollection) throws VciBaseException {
        if(!CollectionUtils.isEmpty(attributeIdCollection)){
            List<Attribute> attributeDOList = new ArrayList<>();
            Collection<String> distAttributeIdCollection = attributeIdCollection.stream().distinct().collect(Collectors.toList());
            Collection<Collection<String>> idCollections = VciBaseUtil.switchCollectionForOracleIn(distAttributeIdCollection);
            if(!CollectionUtils.isEmpty(idCollections)) {
                idCollections.stream().forEach(s -> {
                    List<Attribute> attributeDOS = baseMapper.selectList(Wrappers.<Attribute>query().lambda().in(Attribute::getId, s));
                    if(!CollectionUtils.isEmpty(attributeDOS)){
                        attributeDOList.addAll(attributeDOS);
                    }
                });
            }
            return attributeDOList;
        }
        return null;
    }
 
    /**
     * 判断属性的内容是否符合要求
     *
     * @param id              属性的编号
     * @param attrDataType    属性的类型
     * @param attributeLength 属性的长度
     * @param defaultValue    默认值
     * @return true表示符合要求,不符合要求会抛出异常
     * @throws VciBaseException 不符合要求会抛出异常
     */
    @Override
    public boolean checkAttributePass(String id, String attrDataType, Integer attributeLength, String defaultValue) throws VciBaseException {
        String attributeDataTypeText = VciFieldTypeEnum.getTextByValue(attrDataType);
        if(StringUtils.isBlank(attributeDataTypeText)){
            throw new VciBaseException("编号为{0}的属性的类型不符合要求",new Object[]{id});
        }
        //必须要输入长度
        if(VciBaseUtil.inArray(new String[] {VciFieldTypeEnum.VTString.name()},attrDataType)
            && (attributeLength == null ||attributeLength < 1)){
            throw new VciBaseException("编号为{0}的属性的为{1}类型时,必须要输入长度的值",new Object[]{id,attributeDataTypeText});
        }
        //判断默认值
        if(StringUtils.isNotBlank(defaultValue)){
            if(VciFieldTypeEnum.VTDouble.name().equalsIgnoreCase(attrDataType)
                && !defaultValue.matches(OmdRegExpConstant.DOUBLE)){
                throw new VciBaseException("编号为{0}的属性的默认值不是双精度类型",new Object[]{id});
            }
            if(VciFieldTypeEnum.VTInteger.name().equalsIgnoreCase(defaultValue)
                && !defaultValue.matches(OmdRegExpConstant.INT)){
                throw new VciBaseException("编号为{0}的属性的默认值不是整数型",new Object[]{id});
            }
            if(VciFieldTypeEnum.VTLong.name().equalsIgnoreCase(defaultValue)
                && !defaultValue.matches(OmdRegExpConstant.LONG)){
                throw new VciBaseException("编号为{0}的属性的默认值不是长整形",new Object[]{id});
            }
        }
        return true;
    }
 
    /**
     * 校验属性是否符合要求
     *
     * @param attributesDTO 属性数据传输对象
     * @return true 符合 false 不符合
     * @throws VciBaseException 不符合时抛出异常
     */
    @Override
    public boolean checkAttribute(BtmTypeLinkAttributesDTO attributesDTO) throws VciBaseException {
        VciBaseUtil.alertNotNull(attributesDTO.getId(), "属性的编号", attributesDTO.getName(), "属性的名称",
            attributesDTO.getAttrDataType(), "属性的数据类型");
        boolean pass = checkAttributePass(attributesDTO.getId(), attributesDTO.getAttrDataType(), attributesDTO.getAttributeLength(), attributesDTO.getDefaultValue());
        if (!pass){
            throw new VciBaseException("属性不符合要求");
        }
        return pass;
    }
 
    /**
     * 单个删除
     *
     * @param oid 主键
     * @return 执行结果
     */
    @Override
    public R remove(String oid) {
        List<BtmTypeVO> vos = applyRange(oid);
        if (CollectionUtils.isEmpty(vos)){
             return R.status(removeAttrs(oid));
        }else {
            return R.fail("该属性已被使用,不允许删除");
        }
    }
}