保存校验,根据表体String和UFDouble的两个字段,判断哪些记录重复

方法一 :保存校验,根据业务类型和税率判断哪些记录重复(不推荐)

java 复制代码
import org.apache.commons.collections.CollectionUtils;

	private void ywlxAndSlCheck(List<JsdtbBVO> bvolist) throws BusinessException {
		bvolist = translateBodyVO(bvolist);
		JsdtbBVO[] bvos = bvolist.toArray(new JsdtbBVO[0]);
		if(bvos != null && bvos.length > 0){
			Map<String, List<UFDouble>> map = new HashMap<String, List<UFDouble>>();
			//往map中存值,往list中存值
			for(int i = 0; i < bvos.length; i++){
				String ywlx = NullValueUtils.getNullStringValue(bvos[i].getYwlx());
				UFDouble taxrate = NullValueUtils.getNullUFdoubleValue((bvos[i].getTaxrate()));
				if(map.containsKey(ywlx)){
					List<UFDouble> list = map.get(ywlx);
					list.add(taxrate);
					map.put(ywlx, list);
				}else{
					List<UFDouble> list = new ArrayList<>();
					list.add(taxrate);
					map.put(ywlx, list);
				}
			}
			StringBuilder sb = new StringBuilder();
			for(String ywlx : map.keySet()){
				if(map.containsKey(ywlx)){
					List<UFDouble> list = map.get(ywlx);
					//list去重
					Set<UFDouble> set = new HashSet<>(list);
					//获取重复元素集合
					Collection<UFDouble> rs = CollectionUtils.disjunction(list, set);
					//newList为重复元素集合
					List<UFDouble> newList = new ArrayList<>(rs);  
					if(newList!=null && newList.size()>0){
						sb.append("业务类型为:");
						sb.append(ywlx);
						sb.append(",税率为:");
						for (UFDouble str : newList) {
							sb.append(str).append(",");
						}
						sb.delete(sb.length() - 1, sb.length());
						sb.append(";");
					}
				}
			}
			if(sb!=null&&sb.length()>0){
				throw new BusinessException(sb+ "表体重复,请检查!");
			}
		}
	}
	
	private List<JsdtbBVO> translateBodyVO(List<JsdtbBVO> bvolist) {
		SuperVOUtil.execFormulaWithVOs((SuperVO[]) bvolist.toArray(new JsdtbBVO[0]),new String[] {
				//业务类型
				"ywlx->getcolvalue2(bd_defdoc,name,pk_defdoclist,getcolvalue(bd_defdoclist,pk_defdoclist,code,\"ZK_0001\"),pk_defdoc,ywlx)"
		});
		return bvolist;
	}

方法二 :保存校验,根据业务类型和税率判断哪些记录重复(推荐)

java 复制代码
	//表体业务类型+税率不重复
	private void ywlxAndSlCheck(List<JsdtbBVO> bvolist) throws BusinessException {
		bvolist = translateBodyVO(bvolist);
		JsdtbBVO[] bvos = bvolist.toArray(new JsdtbBVO[0]);
		if(bvos != null && bvos.length > 0){
			List<String> ywlxList = new ArrayList<String>();
			List<UFDouble> taxrateList = new ArrayList<UFDouble>();
			StringBuilder sb = new StringBuilder();
			//往map中存值,往list中存值
			for(int i = 0; i < bvos.length; i++){
				String ywlx = NullValueUtils.getNullStringValue(bvos[i].getYwlx());
				UFDouble taxrate = NullValueUtils.getNullUFdoubleValue((bvos[i].getTaxrate()));
				String rowno = NullValueUtils.getNullStringValue(bvos[i].getRowno().substring(0, bvos[i].getRowno().length()-1));
				String classify = NullValueUtils.getNullStringValue(bvos[i].getClassify());
				
				if(ywlxList.contains(ywlx) && taxrateList.contains(taxrate)){
					sb.append("行号为:").append(rowno);
					sb.append(",业务类型为:").append(ywlx);
					sb.append(",分类为:").append(classify);
					sb.append(",税率为:").append(taxrate);
					sb.append(";");
				}
				ywlxList.add(ywlx);
				taxrateList.add(taxrate);
			}
			if(sb != null && sb.length() > 0){
				throw new BusinessException(sb+ "表体重复,请检查!");
			}
		}
	}

	private List<JsdtbBVO> translateBodyVO(List<JsdtbBVO> bvolist) {
		SuperVOUtil.execFormulaWithVOs((SuperVO[]) bvolist.toArray(new JsdtbBVO[0]),new String[] {
				//业务类型
				"ywlx->getcolvalue2(bd_defdoc,name,pk_defdoclist,getcolvalue(bd_defdoclist,pk_defdoclist,code,\"ZK_0001\"),pk_defdoc,ywlx)"
		});
		return bvolist;
	}

方法三 :保存校验,根据业务类型和税率判断哪些记录重复(极力推荐)

java 复制代码
	//表体业务类型+税率不重复
	private void ywlxAndSlCheck(List<JsdtbBVO> bvolist) throws BusinessException {
		bvolist = translateBodyVO(bvolist);
		JsdtbBVO[] bvos = bvolist.toArray(new JsdtbBVO[0]);
		if(bvos != null && bvos.length > 0){
			Map<String, Map<String,List<String>> >map = new HashMap<String, Map<String,List<String>>>();

			for(int i = 0; i < bvos.length; i++){
				if(bvos[i].getYwlx() != null && !bvos[i].getYwlx().equals("")
						&&bvos[i].getTaxrate() != null){
					String rowno = NullValueUtils.getNullStringValue(i+1);
					String ywlx = NullValueUtils.getNullStringValue(bvos[i].getYwlx());
					String classify = NullValueUtils.getNullStringValue(bvos[i].getClassify());
					UFDouble taxrate = NullValueUtils.getNullUFdoubleValue(bvos[i].getTaxrate()).setScale(2, UFDouble.ROUND_HALF_UP);
					if(map.containsKey(ywlx+"@"+taxrate)){
						Map<String, List<String>> classifymap = map.get(ywlx+"@"+taxrate);
						List<String> list = classifymap.get(classify);
						list.add(rowno);
						classifymap.put(classify, list);
						map.put(ywlx+"@"+taxrate, classifymap);
					}else{
						Map<String, List<String>> classifymap = new HashMap<String,List<String>>();
						List<String> list = new ArrayList<>();
						list.add(rowno);
						classifymap.put(classify, list);
						map.put(ywlx+"@"+taxrate, classifymap);
					}
				}
			}
			
			StringBuilder sb = new StringBuilder();
			for (String ywlxTaxrate : map.keySet()) {
				Map<String, List<String>> classifymap = map.get(ywlxTaxrate);
				for(String classify : classifymap.keySet()){
					List<String> list = classifymap.get(classify);
					if(list != null && list.size() > 1){
						Collections.sort(list);//对list进行升序排序
						for (String str : list) {
							sb.append("第").append(str).append("行").append("和");
						}
						sb.delete(sb.length() - 1, sb.length());
						sb.append("的分类:").append(classify);
						int index = ywlxTaxrate.indexOf("@");
						sb.append(",业务类型:").append(ywlxTaxrate.substring(0,index));
						sb.append(",税率:").append(ywlxTaxrate.substring(index+1,ywlxTaxrate.length()));
						sb.append("重复;");
					}
				}
			}
			
			if(sb != null && sb.length() > 0){
				sb.delete(sb.length() - 3, sb.length());
				throw new BusinessException(sb+"重复,不允许保存,请检查!");
			}
			
		}
	}

	private List<JsdtbBVO> translateBodyVO(List<JsdtbBVO> bvolist) {
		SuperVOUtil.execFormulaWithVOs((SuperVO[]) bvolist.toArray(new JsdtbBVO[0]),new String[] {
				//业务类型
				"ywlx->getcolvalue2(bd_defdoc,name,pk_defdoclist,getcolvalue(bd_defdoclist,pk_defdoclist,code,\"ZK_0001\"),pk_defdoc,ywlx)"
		});
		return bvolist;
	}
相关推荐
憨子周44 分钟前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
霖雨2 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404192 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
我不是星海3 小时前
1.集合体系补充(1)
java·数据结构
P.H. Infinity3 小时前
【RabbitMQ】07-业务幂等处理
java·rabbitmq·java-rabbitmq
爱吃土豆的程序员3 小时前
java XMLStreamConstants.CDATA 无法识别 <![CDATA[]]>
xml·java·cdata
2401_857610034 小时前
多维视角下的知识管理:Spring Boot应用
java·spring boot·后端