package com.vci.ubcs.code.service.impl; import com.alibaba.nacos.common.utils.CollectionUtils; 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.poi.bo.SheetRowData; import com.vci.ubcs.starter.util.DefaultAttrAssimtUtil; import com.vci.ubcs.starter.web.util.VciBaseUtil; 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.util.*; import java.util.stream.Collectors; /*** * 编码规则字符集服务 * @author xj * @date 2023-11-30 */ @Service public class CodeRuleCharacterServiceImpl extends ServiceImpl implements ICodeRuleCharacterService { @Autowired private CodeRuleCharacterMapper codeRuleCharacterMapper; /*** * 使用编码规则oid获取数据 * @param codeRuleId * @param chartType * @return * @throws VciBaseException */ @Override public List> getDataByRuleId(String codeRuleId,String chartType) throws VciBaseException { List> charValueList=new ArrayList<>(); CodeRuleCharacter codeRuleCharacter=codeRuleCharacterMapper.selectOne(Wrappers.query().lambda().eq(CodeRuleCharacter::getCodeRuleId,codeRuleId).eq(CodeRuleCharacter::getChartType,chartType)); List 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 subList = characterList.subList(startIndex, endIndex); Map chartMap=new HashMap<>(); // 调用插入数据库的方法 for (int j=1;jquery().lambda().eq(CodeRuleCharacter::getCodeRuleId,codeRuleCharacterVO.getCodeRuleId()).eq(CodeRuleCharacter::getChartType,codeRuleCharacterVO.getChartType())); if(codeRuleCharacter!=null&& StringUtils.isNotBlank(codeRuleCharacter.getOid())) { List oldCharacterList = StringUtils.isBlank(codeRuleCharacter.getChartValue())?new ArrayList<>():codeRuleCharacter.getChartValue().chars().mapToObj(c -> (char) c).collect(Collectors.toList()); List newCharacterList = StringUtils.isBlank(codeRuleCharacterVO.getChartValue())?new ArrayList<>():codeRuleCharacterVO.getChartValue().chars().mapToObj(c -> (char) c).collect(Collectors.toList()); if(operation==1) {//新增时候 List intersectList = intersect(oldCharacterList, newCharacterList); if (intersectList.size() > 0) { String ss = Joiner.on(",").join(intersectList); throw new VciBaseException("系统中存在相应的字符:【" + ss + "】"); } List 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 oldChartNewChartMap=new HashMap<>(); List chartValueList = StringUtils.isBlank(chartValue)?new ArrayList<>():chartValue.chars().mapToObj(c -> (char) c).collect(Collectors.toList()); List 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 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 intersect(List list1, List list2) { List intersect = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList()); return intersect; } /** * 并集(去重) * @param list1 * @param list2 * @return */ private static List union(List list1, List list2) { list1.addAll(list2); return list1.stream().distinct().collect(Collectors.toList()); } }