背景:
需求概述:相同客户id的 销售出库单 数量 – 期初销售出库单 数量>0 时,生成推单。
模拟NC实现方式概述:
Tips:令人头大的NC代码~
模拟SQL:
select distinct id, test,num from (
select distinct 'a' id,3 test,1 num from dual
union all
select distinct 'a' id,2 test,-1 num from dual
)
根据sql查询到的数据集,生成二维数组
/**
* 查询返回二位数组
*/
private String[][] getData(String sqlStr) {
String[][] dataArray = null;
try{
IUAPQueryBS query = NCLocator.getInstance().lookup(IUAPQueryBS.class);
List<Object[]> rsList = new ArrayList<Object[]>();
ArrayListProcessor processor = new ArrayListProcessor();
rsList= (List<Object[]>)query.executeQuery(sqlStr, processor) ;
if (rsList.size()!=0 ){
int lenrow = rsList.size();
Object[] firstrow = (Object[])rsList.get(0);
int lencol = firstrow.length;
dataArray = new String[lenrow][lencol];
for (int i=0;i<lenrow;i++){
Object[] objs = (Object[])rsList.get(i);
for (int j=0;j<lencol;j++){
if (null !=objs[j]){
dataArray[i][j] = objs[j].toString();
}
}
}
}
}catch(Exception e){
e.printStackTrace();
ExceptionUtils.wrappBusinessException(e.getMessage());
}
return dataArray;
}
取值累加:
HashMap<String, UFDouble> numMap = new HashMap<String, UFDouble>();
String key = null;
UFDouble num = UFDouble.ZERO_DBL;
for(int m=0;m<customerlArray.length;m++){
key = customerlArray[m][0];
num = new UFDouble(customerlArray[m][2], 8);
if(numMap.containsKey(key)){
numMap.put(key, numMap.get(key).add(num));
}else{
numMap.put(key, num);
}
}
for(int m=0;m<customerlArray.length;m++){
if(numMap.get(customerlArray[m][0]).doubleValue()<=0)
continue;
//省略生成推单的代码
}
如果其他字段值不需要可直接使用SQL简化实现:
--案例:利用union all 合并去重结果集,相同字段值累加求和
select distinct id,sum(num) num from (
select distinct 'a' id,1 num from dual
union all
select distinct 'a' id,-1 num from dual
)
group by id;
结果集:
评论前必须登录!
注册