田源
2024-04-18 85c3bbd94ff95a8666436d225bb232441f4e4615
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
package com.vci.ubcs.code.applyjtcodeservice.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.code.applyjtcodeservice.entity.DockingPreMetaAttr;
import com.vci.ubcs.code.applyjtcodeservice.entity.GroupAttrPoolMapping;
import com.vci.ubcs.code.applyjtcodeservice.mapper.GroupAttrPoolMappingMapper;
import com.vci.ubcs.code.applyjtcodeservice.service.IDockingPreAttrMappingService;
import com.vci.ubcs.code.applyjtcodeservice.service.IGroupAttrPoolMappingService;
import com.vci.ubcs.code.applyjtcodeservice.vo.GroupAttrPoolMappingVO;
import com.vci.ubcs.code.applyjtcodeservice.wrapper.GroupAttrPoolMappingWrapper;
import com.vci.ubcs.code.feign.ICodeClassifyClient;
import com.vci.ubcs.code.vo.pagemodel.CodeClassifyTemplateAttrVO;
import com.vci.ubcs.omd.feign.IEnumClient;
import com.vci.ubcs.omd.vo.EnumVO;
import com.vci.ubcs.starter.util.DefaultAttrAssimtUtil;
import com.vci.ubcs.starter.util.MdmBtmTypeConstant;
import com.vci.ubcs.starter.util.UBCSCondition;
import com.vci.ubcs.starter.web.pagemodel.BladeQueryObject;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
 
import javax.sql.rowset.serial.SerialException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 集团属性池映射服务层
 *
 * @author ludc
 * @date 2024/4/15 16:52
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GroupAttrPoolMappingServiceImpl extends ServiceImpl<GroupAttrPoolMappingMapper, GroupAttrPoolMapping> implements IGroupAttrPoolMappingService {
 
    private final GroupAttrPoolMappingMapper groupAttrPoolMappingMapper;
 
    private final ICodeClassifyClient codeClassifyClient;
 
    private final IDockingPreAttrMappingService dockingPreAttrMappingService;
 
    private final IEnumClient enumClient;
 
    /**
     * 查询全部集团属性池映射的属性
     * @param bladeQueryObject
     * @return
     * @throws SerialException
     */
    @Override
    public IPage<GroupAttrPoolMappingVO> getGroupAttrPoolALlList(BladeQueryObject bladeQueryObject) throws ServiceException {
        QueryWrapper<GroupAttrPoolMapping> queryWrapper = UBCSCondition.getQueryWrapper(bladeQueryObject.getConditionMap(), GroupAttrPoolMapping.class);
        IPage<GroupAttrPoolMapping> groupAttrPoolMappingIPage = this.groupAttrPoolMappingMapper.selectPage(Condition.getPage(bladeQueryObject.getQuery().setDescs("groupAttrKey")), queryWrapper);
        return GroupAttrPoolMappingWrapper.build().pageVO(groupAttrPoolMappingIPage);
    }
 
    /**
     * 根据集团属性编号查询集团属性池映射
     *
     * @param groupAttrPoolMappingVO
     * @return
     */
    @Override
    public List<GroupAttrPoolMappingVO> getByGroupAttrKeyList(GroupAttrPoolMappingVO groupAttrPoolMappingVO) {
        // 查询条件为空
        if (Func.isEmpty(groupAttrPoolMappingVO.getGroupAttrKeyList())) {
            throw new ServiceException("查询条件,要查询的集团编号列表不能为空!");
        }
        if (Func.isBlank(groupAttrPoolMappingVO.getClassifyId())) {
            throw new ServiceException("查询条件中,分类id不能为空!");
        }
        //集团属性池数据
        List<GroupAttrPoolMapping> groupAttrPoolMappings = groupAttrPoolMappingMapper.selectList(
            Wrappers.<GroupAttrPoolMapping>query()
                .lambda().in(GroupAttrPoolMapping::getGroupAttrKey, groupAttrPoolMappingVO.getGroupAttrKeyList())
        );
        //根据分类id查询出,对应的模板上配置的属性
        List<CodeClassifyTemplateAttrVO> attrVOS = codeClassifyClient.listCodeAttributeByClassId(groupAttrPoolMappingVO.getClassifyId());
        List<GroupAttrPoolMappingVO> groupAttrPoolMappingVOS = GroupAttrPoolMappingWrapper.build().entityVOs(groupAttrPoolMappings);
 
        List<GroupAttrPoolMappingVO> finalGroupAttrPoolMappingVOS;
        //根据集团属性池中配置id元数据属性id找到当前所属分类下的模板属性中对应属性的oid
        finalGroupAttrPoolMappingVOS = groupAttrPoolMappingVOS.stream().map(item -> {
            CodeClassifyTemplateAttrVO codeClassifyTemplateAttrVO = attrVOS.stream().filter(attr -> attr.getId().equals(item.getCodeMetaAttrKey())).findFirst().orElse(new CodeClassifyTemplateAttrVO());
            item.setTargetAttrId(codeClassifyTemplateAttrVO.getOid());
            return item;
        }).collect(Collectors.toList());
 
        return finalGroupAttrPoolMappingVOS;
    }
 
    /**
     * 点击同步模型时调用该方法,
     * 实现将集团获取的属性去重放到集团属性池映射表中
     * @param dockingPreMetaAttrList
     * @return
     * @throws SerialException
     */
    @Override
    public boolean saveDistinctGroupAttr(List<DockingPreMetaAttr> dockingPreMetaAttrList) throws ServiceException {
        // 为空直接返回
        if(Func.isEmpty(dockingPreMetaAttrList)){
            return true;
        }
        List<GroupAttrPoolMapping> groupAttrPoolMappings = new ArrayList<>();
        Map<String,String> distinctMap = new HashMap<>();
        log.info("开始将集团属性同步到集团属性池中");
        // 将dockingPreMetaAttrList集合转换为集团属性池属性对象
        dockingPreMetaAttrList.stream().forEach(item->{
            GroupAttrPoolMapping groupAttrPoolMapping = new GroupAttrPoolMapping();
            groupAttrPoolMapping.setGroupAttrKey(item.getEnglishName());
            groupAttrPoolMapping.setGroupAttrName(item.getChineseName());
            groupAttrPoolMappings.add(groupAttrPoolMapping);
            // 去重查询条件集团属性英文名称
            distinctMap.put(item.getEnglishName(),item.getChineseName());
        });
        // 构造查重sql(根据集团属性中文和英文作为一组查询条件)
        LambdaQueryWrapper<GroupAttrPoolMapping> lambdaQueryWrapper = Wrappers.<GroupAttrPoolMapping>query().lambda();
        distinctMap.forEach((key, value)->{
            lambdaQueryWrapper.eq(GroupAttrPoolMapping::getGroupAttrKey,key).and(wrapper->wrapper.eq(GroupAttrPoolMapping::getGroupAttrName,value)).or(true);
        });
        // 去重查询结果
        List<GroupAttrPoolMapping> dbGroupAttrPoolMappings = groupAttrPoolMappingMapper.selectList(lambdaQueryWrapper);
        List<GroupAttrPoolMapping> newGroupAttrPoolMappings = null;
        // 将已存在的集团属性从groupAttrPoolMappings中移除
        if(Func.isNotEmpty(dbGroupAttrPoolMappings)){
            newGroupAttrPoolMappings = groupAttrPoolMappings.stream()
                .filter(mapping -> !dbGroupAttrPoolMappings.stream()
                    .map(GroupAttrPoolMapping::getGroupAttrKey)
                    .collect(Collectors.toList())
                    .contains(mapping.getGroupAttrKey()))
                .collect(Collectors.toList());
        }else{
            newGroupAttrPoolMappings = groupAttrPoolMappings;
        }
        List<GroupAttrPoolMapping> finalGroupAttrPoolMappings = newGroupAttrPoolMappings.stream().map(item -> {
            DefaultAttrAssimtUtil.addDefaultAttrAssimt(item, MdmBtmTypeConstant.GROUP_ATTR_POOL);
            return item;
        }).collect(Collectors.toList());
        log.info("本次同步的数据为:"+ JSON.toJSONString(finalGroupAttrPoolMappings));
        try {
            this.saveBatch(finalGroupAttrPoolMappings);
            log.info("集团属性同步到集团属性池,同步完毕");
            return true;
        }catch (Exception e){
            log.info("集团属性同步到集团属性池,同步失败!,原因:"+e.getMessage());
            throw new ServiceException("集团属性同步到集团属性池,同步失败!,原因:"+e.getMessage());
        }
    }
 
    /**
     * 集团属性映射界面修改的映射配置保存
     * @param groupAttrPoolMappingVO
     * @return
     * @throws SerialException
     */
    @Override
    public boolean editGroupAttr(List<GroupAttrPoolMappingVO> groupAttrPoolMappingVO) throws ServiceException {
        if(Func.isEmpty(groupAttrPoolMappingVO)){
            return true;
        }
        List<GroupAttrPoolMapping> groupAttrPoolMappings = GroupAttrPoolMappingWrapper.build().VOsEntity(groupAttrPoolMappingVO);
        List<GroupAttrPoolMapping> finalGroupAttrPoolMappings = groupAttrPoolMappings.stream().map(item -> {
            DefaultAttrAssimtUtil.updateDefaultAttrAssimt(item);
            return item;
        }).collect(Collectors.toList());
        return this.updateBatchById(finalGroupAttrPoolMappings);
    }
 
    /**
     * 同步集团属性池中的映射配置到,所有应用了该集团属性的具体某个分类上去
     * @param groupAttrPoolMappingVOS
     * @return
     * @throws SerialException
     */
    @Override
    public R syncGroupAttrMapping(List<GroupAttrPoolMappingVO> groupAttrPoolMappingVOS) throws ServiceException {
        List<String> groupKeyList = groupAttrPoolMappingVOS.stream().map(GroupAttrPoolMappingVO::getGroupAttrKey).collect(Collectors.toList());
        List<GroupAttrPoolMapping> groupAttrPoolMappings = this.groupAttrPoolMappingMapper.selectList(
            Wrappers.<GroupAttrPoolMapping>query().lambda().in(GroupAttrPoolMapping::getGroupAttrKey, groupKeyList)
        );
        if(Func.isEmpty(groupAttrPoolMappings) || groupKeyList.size() != groupAttrPoolMappings.size()){
            return R.fail("勾选的要同步的集团属性中有在库中不存在的数据,请刷新后重试!");
        }
        return dockingPreAttrMappingService.syncGroupAttrMapping(groupAttrPoolMappings);
    }
 
    /**
     * 根据分类OID获取到模板再根据属性id获取到模板下的属性id的enum值,然后查询出枚举
     * @param groupAttrPoolMappingVOS
     * @return
     */
    @Override
    public R getEnumAttrByClsOIdAndAttrId(GroupAttrPoolMappingVO groupAttrPoolMappingVOS) {
        if(Func.isEmpty(groupAttrPoolMappingVOS.getClassifyId())){
            throw new ServiceException("必传参数分类oid不能为空!");
        }
        if(Func.isEmpty(groupAttrPoolMappingVOS.getCodeMetaAttrKey())){
            throw new ServiceException("必传参数,模板属性id不能为空");
        }
        // 根据分类oid查询所使用的模板和模板属性
        List<CodeClassifyTemplateAttrVO> attrVOS = codeClassifyClient.listCodeAttributeByClassId(groupAttrPoolMappingVOS.getClassifyId());
        // 筛选出模板属性中id等于codeMetaAttrKey的,并取出enumId
        if(attrVOS.isEmpty()){
            return R.fail("当前分类所使用模板的模板属性中未查询到相关的属性配置,请排查模板配置是否正确。");
        }
        List<CodeClassifyTemplateAttrVO> codeClassifyTemplateAttrVOS = attrVOS.stream().filter(item -> item.getId().equalsIgnoreCase(groupAttrPoolMappingVOS.getCodeMetaAttrKey())).collect(Collectors.toList());
        // 根据上一步取出的enumId调用枚举服务查询出枚举并返回
        String enumId = codeClassifyTemplateAttrVOS.get(0).getEnumId();
        if(Func.isBlank(enumId)){
            return R.fail("在系统中根据当前配置的模板属性未查询到关于枚举的配置,请排查元数据上是否存在枚举属性!");
        }
        R<List<EnumVO>> list = enumClient.getList(enumId);
        return R.data(list.getData());
    }
 
}