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

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

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;
	}

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

	//表体业务类型+税率不重复
	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;
	}

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

	//表体业务类型+税率不重复
	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;
	}

你可能感兴趣的:(nc积累,java)