ludc
2023-04-12 d7a83f1396425c4e47da9b95f287b26cd5bb3344
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
262
package com.vci.ubcs.code.service.impl;
 
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSONObject;
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.dto.CodeBasicSecDTO;
import com.vci.ubcs.code.entity.CodeBasicSec;
import com.vci.ubcs.code.entity.CodeClassifyValue;
import com.vci.ubcs.code.entity.CodeFixedValue;
import com.vci.ubcs.code.enumpack.CodeSecTypeEnum;
import com.vci.ubcs.code.lifecycle.CodeRuleLC;
import com.vci.ubcs.code.mapper.CodeBasicSecMapper;
import com.vci.ubcs.code.mapper.CodeClassifyValueMapper;
import com.vci.ubcs.code.mapper.CodeFixedValueMapper;
import com.vci.ubcs.code.service.ICodeBasicSecService;
import com.vci.ubcs.code.service.ICodeRuleService;
import com.vci.ubcs.code.vo.pagemodel.CodeBasicSecVO;
import com.vci.ubcs.code.vo.pagemodel.CodeRuleVO;
import com.vci.ubcs.code.wrapper.CodeBasicSecWrapper;
import com.vci.ubcs.com.vci.starter.exception.VciBaseException;
import com.vci.ubcs.com.vci.starter.web.enumpck.OsCodeFillTypeEnum;
import com.vci.ubcs.com.vci.starter.web.pagemodel.KeyValue;
import com.vci.ubcs.com.vci.starter.web.pagemodel.SessionInfo;
import com.vci.ubcs.com.vci.starter.web.pagemodel.UIFormReferVO;
import com.vci.ubcs.com.vci.starter.web.util.BeanUtilForVCI;
import com.vci.ubcs.com.vci.starter.web.util.VciBaseUtil;
import com.vci.ubcs.com.vci.starter.web.util.WebUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * 码段基础信息服务接口
 *
 * @author weidy
 * @date 2022-01-24
 */
@Service
public class CodeBasicSecServiceImpl extends ServiceImpl<CodeBasicSecMapper, CodeBasicSec> implements ICodeBasicSecService {
 
    @Resource
    private CodeBasicSecMapper codeBasicSecMapper;
 
    /**
     * 固定码段的码值数据操作层
     */
    @Resource
    private CodeFixedValueMapper fixedValueMapper;
 
    /**
     * 分类码段的码值数据操作层
     */
    @Resource
    private CodeClassifyValueMapper codeClassifyValueMapper;
 
    @Resource
    @Lazy
    private ICodeRuleService codeRuleService;
 
    /**
     * 查询所有的码段基础信息
     *
     * @param page 查询条件
     * @param codeBasicSecVO   分页和排序
     * @return 执行结果
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public IPage<CodeBasicSecVO> gridCodeBasicSec(IPage<CodeBasicSecVO> page, CodeBasicSecVO codeBasicSecVO) throws VciBaseException {
        if(Func.isEmpty(codeBasicSecVO.getPkCodeRule()) || Func.isBlank(codeBasicSecVO.getPkCodeRule())){
            return null;
        }
        List<CodeBasicSec> codeBasicSecs = codeBasicSecMapper.selectCodeBasicSecPage(page, codeBasicSecVO);
        return page.setRecords(CodeBasicSecWrapper.build().listVO(codeBasicSecs));
    }
 
    /**
     * 根据编码规则批量删除码段基本信息
     * @param codeRuleOid 编码规则主键
     * @return 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteSecByCodeRuleOid(String codeRuleOid) {
        VciBaseUtil.alertNotNull(codeRuleOid,"编码规则主键");
        // 1、通过pkcoderule作为条件,先查询要删除基础码段
        List<CodeBasicSec> deleteList = this.codeBasicSecMapper.selectList(Wrappers.<CodeBasicSec>query().eq("pkcoderule", codeRuleOid));
        if (CollectionUtils.isEmpty(deleteList)){
            return true;
        }
        // 2、再删除基础码段
        boolean deletFlag = codeBasicSecMapper.deleteBatchIds(deleteList.stream().map(CodeBasicSec::getOid).collect(Collectors.toSet())) > 0;
        // 3、再根据删除固定码段,丛查询出来的基础码段中过滤出包含固定码段的记录
        List<CodeBasicSec> fixedSecList = deleteList.stream().filter(sec -> {
            return CodeSecTypeEnum.CODE_FIXED_SEC.getValue().equals(sec.getSecType());
        }).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(fixedSecList)){
            // 将要作为删除条件的值放在一个集合里面
            Set<String> fixedSecOidSet = fixedSecList.stream().map(CodeBasicSec::getOid).collect(Collectors.toSet());
            // 通过外键进行查询
            List<CodeFixedValue> fixedValueS = fixedValueMapper.selectList(Wrappers.<CodeFixedValue>query().lambda().in(CodeFixedValue::getCodeFixedSecOid,fixedSecOidSet));
            // 根据查询出来的id执行固定码段执行删除
            deletFlag = fixedValueMapper.deleteBatchIds(fixedValueS.stream().map(CodeFixedValue::getOid).collect(Collectors.toSet()))>0;
        }
        // 4、再删除分类码段
        List<CodeBasicSec> classifySecList = deleteList.stream().filter(sec -> {
            return CodeSecTypeEnum.CODE_CLASSIFY_SEC.getValue().equals(sec.getSecType());
        }).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(classifySecList)){
            // 将要作为删除条件的值放在一个集合里面
            Set<String> classifySecOidSet = classifySecList.stream().map(CodeBasicSec::getOid).collect(Collectors.toSet());
            // 通过外键进行查询
            List<CodeClassifyValue> fixedValueS = codeClassifyValueMapper.selectList(Wrappers.<CodeClassifyValue>query().lambda().in(CodeClassifyValue::getCodeClassifySecOid,classifySecOidSet));
            // 根据查询出来的主键id执行固定码段执行删除
            deletFlag = fixedValueMapper.deleteBatchIds(fixedValueS.stream().map(CodeClassifyValue::getOid).collect(Collectors.toSet()))>0;
        }
        return deletFlag;
    }
 
    /**
     * 根据码段分类的类型判断属性是否是空的
     *
     * @param codeBasicSecDTO 码段基础信息数据传输对象
     * @return 有空的则传key-属性名 value-字段含义,没有空的则传 key-success value-true
     */
    @Override
    public KeyValue checkAttrNullableBySecType(CodeBasicSecDTO codeBasicSecDTO) {
        VciBaseUtil.alertNotNull(codeBasicSecDTO.getSectype(), "码段分类");
        String secType = codeBasicSecDTO.getSectype();
        HashMap<String, String> attrMap = JSONObject.parseObject(JSONObject.toJSONString(codeBasicSecDTO), HashMap.class);
        Map<String, String> notNullableAttr = getNotNullableAttr(secType);
        if (notNullableAttr == null) {
            throw new VciBaseException("码段分类填写出错,请查验后重试");
        }
        for (String key : notNullableAttr.keySet()) {
            if (StringUtils.isBlank(WebUtil.getStringValueFromObject(attrMap.get(key)))) {
                KeyValue kv = new KeyValue();
                kv.setKey(key);
                kv.setValue(notNullableAttr.get(key));
                return kv;
            }
        }
        KeyValue kv = new KeyValue();
        kv.setKey("success");
        kv.setValue("true");
        return kv;
    }
 
    /**
     * 增加码段基础信息
     *
     * @param codeBasicSecDTO 码段基础信息数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public boolean addSave(CodeBasicSecDTO codeBasicSecDTO) throws VciBaseException {
        VciBaseUtil.alertNotNull(codeBasicSecDTO, "需要添加的数据对象",codeBasicSecDTO.getPkCodeRule(),"编码规则的主键");
        CodeRuleVO ruleVO = codeRuleService.getObjectByOid(codeBasicSecDTO.getPkCodeRule());
        if(!CodeRuleLC.EDITING.getValue().equalsIgnoreCase(ruleVO.getLcStatus())){
            throw new VciBaseException("编码规则的状态不是【" + CodeRuleLC.EDITING.getText() + "】!不允许修改");
        }
        KeyValue attrKv = checkAttrNullableBySecType(codeBasicSecDTO);
        if (! "success".equals(attrKv.getKey())){
            throw new VciBaseException(attrKv.getValue() + "不能为空");
        }
 
        //将DTO转换为DO
        CodeBasicSec codeBasicSecDO = new CodeBasicSec();
        BeanUtilForVCI.copyPropertiesIgnoreCase(codeBasicSecDTO, codeBasicSecDO);
        //排序号,默认等于当前已有的数量加1
        Long total = codeBasicSecMapper.selectCount(Wrappers.<CodeBasicSec>query()
            .lambda()
            .eq(CodeBasicSec::getPkCodeRule,codeBasicSecDTO.getPkCodeRule()));
        if(total == null){
            total = 0L;
        }
        codeBasicSecDO.setOrderNum(total.intValue() + 1);
 
        //补位的时候,要控制补位字符
        if((OsCodeFillTypeEnum.LEFT.getValue().equalsIgnoreCase(codeBasicSecDO.getCodeFillType())
            || OsCodeFillTypeEnum.RIGHT.getValue().equalsIgnoreCase(codeBasicSecDO.getCodeFillType()))
            && StringUtils.isBlank(codeBasicSecDO.getCodeFillSeparator())){
            throw new VciBaseException("当补位方式为左补位或者右补位的时候,补位字符不能为空");
        }
        //引用码段的时候,需要判断参照的信息是否正确
        if(CodeSecTypeEnum.CODE_REFER_SEC.getValue().equalsIgnoreCase(codeBasicSecDO.getSecType())){
            if(StringUtils.isBlank(codeBasicSecDO.getReferConfig())){
                throw new VciBaseException("引用码段的时候,需要填写 参照配置 的内容");
            }
            try{
                JSONObject.parseObject(codeBasicSecDO.getReferConfig(), UIFormReferVO.class);
            }catch (Throwable e){
                throw new VciBaseException("引用码段的时候,参照配置的内容的格式不正确,",new String[0],e);
            }
        }
        boolean resBoolean = codeBasicSecMapper.insert(codeBasicSecDO) > 0;
        SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
        /*if(StringUtils.isNotBlank(codeBasicSecDO.getCodeFillSeparator())){
            charService.save(MdmBtmTypeConstant.CODE_BASIC_SEC,"codefileseparator",codeBasicSecDO.getCodeFillSeparator(),sessionInfo);
        }*/
        return resBoolean;
    }
 
    /**
     * 根据码段类型获取不可为空的字段
     *
     * @param secType 码段类型
     * @return 不可为空的字段集合
     */
    private Map<String, String> getNotNullableAttr(String secType) {
        Map<String, String> attrMap = new HashMap<>();
        if (CodeSecTypeEnum.CODE_ATTR_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "属性码段名称");
        } else if (CodeSecTypeEnum.CODE_DATE_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "日期码段名称");
            attrMap.put("codeDateFormatStr", "日期格式");
        } else if (CodeSecTypeEnum.CODE_FIXED_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "固定码段名称");
            attrMap.put("codeSecLengthType", "码段长度类型");
            attrMap.put("codeSecLength", "码段的长度");
        } else if (CodeSecTypeEnum.CODE_LEVEL_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "层级码段名称");
            attrMap.put("codeLevelType", "层级类型");
            attrMap.put("valueCutType", "字符截取类型");
        } else if (CodeSecTypeEnum.CODE_REFER_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "引用码段名称");
        } else if (CodeSecTypeEnum.CODE_SERIAL_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "流水码段名称");
            attrMap.put("codeSecLength", "码段的长度");
            attrMap.put("codeFillType", "编码补位方式");
            attrMap.put("codeFillLength", "填充长度");
            attrMap.put("codeFillLimit", "流水上限");
            attrMap.put("codeFillFlag", "流水是否补码");
        } else if (CodeSecTypeEnum.CODE_VARIABLE_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "可变码段名称");
            attrMap.put("codeSecLength", "码段的长度");
            attrMap.put("codeFillType", "编码补位方式");
        } else if (CodeSecTypeEnum.CODE_CLASSIFY_SEC.getValue().equalsIgnoreCase(secType)) {
            attrMap.put("name", "分类码段名称");
            attrMap.put("codeSecLengthType", "码段长度类型");
            attrMap.put("codeSecLength", "码段的长度");
        } else {
            attrMap = null;
        }
        return attrMap;
    }
 
 
 
}