xiejun
2024-01-29 d1ebae3f837f69af8d7d8f752ebd7b976b56db2a
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
package com.vci.ubcs.code.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.vci.ubcs.code.entity.CodeRuleCharacter;
import com.vci.ubcs.code.mapper.CodeRuleCharacterMapper;
import com.vci.ubcs.code.service.ICodeRuleCharacterService;
import com.vci.ubcs.code.vo.pagemodel.CodeRuleCharacterVO;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.util.DefaultAttrAssimtUtil;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.rmi.ServerException;
import java.util.*;
import java.util.stream.Collectors;
 
/***
 * 编码规则字符集服务
 * @author xj
 * @date 2023-11-30
 */
@Service
@AllArgsConstructor
public class CodeRuleCharacterServiceImpl extends ServiceImpl<CodeRuleCharacterMapper, CodeRuleCharacter> implements ICodeRuleCharacterService {
 
    private final CodeRuleCharacterMapper codeRuleCharacterMapper;
 
    /***
     * 使用编码规则oid获取数据
     * @param codeRuleId
     * @param chartType
     * @return
     * @throws VciBaseException
     */
    @Override
    public List<Map<String, String>> getDataByRuleId(String codeRuleId,String chartType) throws VciBaseException {
        List<Map<String,String>> charValueList=new ArrayList<>();
        CodeRuleCharacter codeRuleCharacter=codeRuleCharacterMapper.selectOne(Wrappers.<CodeRuleCharacter>query().lambda().eq(CodeRuleCharacter::getCodeRuleId,codeRuleId).eq(CodeRuleCharacter::getChartType,chartType));
        if(codeRuleCharacter!=null&&StringUtils.isNotBlank(codeRuleCharacter.getOid())){
            List<Character> characterList=codeRuleCharacter.getChartValue().chars().mapToObj(c -> (char) c).collect(Collectors.toList());
            for (int i = 0; i < characterList.size(); i += 15) {
                final int startIndex = i;
                final int endIndex = Math.min(i + 15, characterList.size());
                List<Character> subList = characterList.subList(startIndex, endIndex);
                Map<String, String> chartMap=new HashMap<>();
                // 调用插入数据库的方法
                for (int j=1;j<subList.size()+1;j++){
                    String characterValue=subList.get(j-1)==null?"":subList.get(j-1).toString();
                    chartMap.put(String.valueOf(j),String.valueOf(characterValue));
                }
                charValueList.add(chartMap);
            }
        }
        return charValueList;
    }
 
    /***
     * 使用编码规则oid获取数据
     * @param codeRuleId
     * @param chartType
     * @return
     * @throws VciBaseException
     */
    @Override
    public List<Map<String, String>> getSelectListByRuleId(String codeRuleId,String chartType) throws ServerException {
        List<Map<String, String>> charValueMap = new ArrayList<>();
        CodeRuleCharacter codeRuleCharacter=codeRuleCharacterMapper.selectOne(Wrappers.<CodeRuleCharacter>query().lambda().eq(CodeRuleCharacter::getCodeRuleId,codeRuleId).eq(CodeRuleCharacter::getChartType,chartType));
        if(codeRuleCharacter!=null&&StringUtils.isNotBlank(codeRuleCharacter.getOid())){
            List<Character> characterList=codeRuleCharacter.getChartValue().chars().mapToObj(c -> (char) c).collect(Collectors.toList());
            characterList.stream().forEach(item->{
                Map<String, String> map = new HashMap<>();
                map.put("lable",item.toString());
                map.put("value",item.toString());
                charValueMap.add(map);
            });
        }
        return charValueMap;
    }
 
    @Override
    public String getRegexStrByCodeRuleId(String codeRuleId, String chartType) throws ServerException {
        StringBuilder regexStr = new StringBuilder();
        CodeRuleCharacter codeRuleCharacter=codeRuleCharacterMapper.selectOne(Wrappers.<CodeRuleCharacter>query().lambda().eq(CodeRuleCharacter::getCodeRuleId,codeRuleId).eq(CodeRuleCharacter::getChartType,chartType));
        if(codeRuleCharacter!=null&&StringUtils.isNotBlank(codeRuleCharacter.getOid())){
            regexStr.append("^[");
            regexStr.append(codeRuleCharacter.getChartValue());
            regexStr.append("]+$");
        }
        return regexStr.toString();
    }
 
    /***
     * 字符集数据保存
     * @param codeRuleCharacterVO
     * @return
     * @throws VciBaseException
     */
    @Override
    public R saveOrUpdate(CodeRuleCharacterVO codeRuleCharacterVO,int operation) throws VciBaseException {
        VciBaseUtil.alertNotNull(codeRuleCharacterVO.getCodeRuleId(),"编码规则id",codeRuleCharacterVO.getChartType(),"字符集类型");
        CodeRuleCharacter codeRuleCharacter=codeRuleCharacterMapper.selectOne(Wrappers.<CodeRuleCharacter>query().lambda().eq(CodeRuleCharacter::getCodeRuleId,codeRuleCharacterVO.getCodeRuleId()).eq(CodeRuleCharacter::getChartType,codeRuleCharacterVO.getChartType()));
        if(codeRuleCharacter!=null&& StringUtils.isNotBlank(codeRuleCharacter.getOid())) {
            List<Character> oldCharacterList =  StringUtils.isBlank(codeRuleCharacter.getChartValue())?new ArrayList<>():codeRuleCharacter.getChartValue().chars().mapToObj(c -> (char) c).collect(Collectors.toList());
            List<Character> newCharacterList = StringUtils.isBlank(codeRuleCharacterVO.getChartValue())?new ArrayList<>():codeRuleCharacterVO.getChartValue().chars().mapToObj(c -> (char) c).collect(Collectors.toList());
            if(operation==1) {//新增时候
                List<Character> intersectList = intersect(oldCharacterList, newCharacterList);
                if (intersectList.size() > 0) {
                    String ss = Joiner.on(",").join(intersectList);
                    throw new VciBaseException("系统中存在相应的字符:【" + ss + "】");
                }
                List<Character> allCharacterList = union(oldCharacterList, newCharacterList);
                String str = allCharacterList.stream().map(integer -> Func.isNotEmpty(integer) ? integer.toString() : "").collect(Collectors.joining());
                codeRuleCharacter.setChartValue(str);
            }else if(operation==2){//修改
                String oldChartValue= codeRuleCharacterVO.getOldChartValue();
                String chartValue= codeRuleCharacterVO.getChartValue();
                Map<String,String> oldChartNewChartMap=new HashMap<>();
                List<Character> chartValueList = StringUtils.isBlank(chartValue)?new ArrayList<>():chartValue.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
                List<Character> oldChartValueList = StringUtils.isBlank(oldChartValue)?new ArrayList<>():oldChartValue.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
 
                if(chartValueList.size()!=oldChartValueList.size()){
                    throw new VciBaseException("替换的值个数与选中值的个数不相等");
                }
                /***
                 * id
                 */
                for (int i=0;i<oldChartValueList.size();i++){
                    Character oldValue=oldChartValueList.get(i);
                    if(oldCharacterList.contains(oldValue)){
                        int index =oldCharacterList.indexOf(oldValue);
                        oldCharacterList.set(index,chartValueList.get(i));
                    }
                }
                String str = oldCharacterList.stream().map(integer -> Func.isNotEmpty(integer) ? integer.toString() : "").collect(Collectors.joining());
                codeRuleCharacter.setChartValue(str);
            }else{//删除
                oldCharacterList.removeAll(newCharacterList);
                String str = oldCharacterList.stream().map(integer -> Func.isNotEmpty(integer) ? integer.toString() : "").collect(Collectors.joining());
                codeRuleCharacter.setChartValue(str);
            }
            codeRuleCharacterMapper.updateById(codeRuleCharacter);
        }else{
            codeRuleCharacter=new CodeRuleCharacter();
            DefaultAttrAssimtUtil.addDefaultAttrAssimt(codeRuleCharacter,"character");
            codeRuleCharacter.setCodeRuleId(codeRuleCharacterVO.getCodeRuleId());
            codeRuleCharacter.setChartType(codeRuleCharacterVO.getChartType());
            codeRuleCharacter.setChartValue(codeRuleCharacterVO.getChartValue());
            codeRuleCharacterMapper.insert(codeRuleCharacter);
        }
        return R.status(true);
    }
    /**
     * 交集
     * @param list1
     * @param list2
     * @return
     */
    private static List<Character> intersect(List<Character> list1, List<Character> list2) {
        List<Character> intersect = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
        return intersect;
    }
 
    /**
     * 并集(去重)
     * @param list1
     * @param list2
     * @return
     */
    private static List<Character> union(List<Character> list1, List<Character> list2) {
        list1.addAll(list2);
        return list1.stream().distinct().collect(Collectors.toList());
    }
 
}