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 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 gridCodeBasicSec(IPage page, CodeBasicSecVO codeBasicSecVO) throws VciBaseException { if(Func.isEmpty(codeBasicSecVO.getPkCodeRule()) || Func.isBlank(codeBasicSecVO.getPkCodeRule())){ return null; } List 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 deleteList = this.codeBasicSecMapper.selectList(Wrappers.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 fixedSecList = deleteList.stream().filter(sec -> { return CodeSecTypeEnum.CODE_FIXED_SEC.getValue().equals(sec.getSecType()); }).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(fixedSecList)){ // 将要作为删除条件的值放在一个集合里面 Set fixedSecOidSet = fixedSecList.stream().map(CodeBasicSec::getOid).collect(Collectors.toSet()); // 通过外键进行查询 List fixedValueS = fixedValueMapper.selectList(Wrappers.query().lambda().in(CodeFixedValue::getCodeFixedSecOid,fixedSecOidSet)); // 根据查询出来的id执行固定码段执行删除 deletFlag = fixedValueMapper.deleteBatchIds(fixedValueS.stream().map(CodeFixedValue::getOid).collect(Collectors.toSet()))>0; } // 4、再删除分类码段 List classifySecList = deleteList.stream().filter(sec -> { return CodeSecTypeEnum.CODE_CLASSIFY_SEC.getValue().equals(sec.getSecType()); }).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(classifySecList)){ // 将要作为删除条件的值放在一个集合里面 Set classifySecOidSet = classifySecList.stream().map(CodeBasicSec::getOid).collect(Collectors.toSet()); // 通过外键进行查询 List fixedValueS = codeClassifyValueMapper.selectList(Wrappers.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 attrMap = JSONObject.parseObject(JSONObject.toJSONString(codeBasicSecDTO), HashMap.class); Map 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.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 getNotNullableAttr(String secType) { Map 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; } }