搞了一上午,终于发现原因了

上午,在开发汇总数据时,总是发现最后结果的值翻了好几倍.采用多种方式测试.都没有达到预期效果.
最终,发现了.原来是没有初始化累加的值造成的.
//根据条件进行导出
             String []accountids;
            int t=0;
            accountids=proname.split(",");
            for (int i = 0; i < accountids.length; i++)
            {
                try{
                double totalAllMoney=0d;//初始化合计总金额 
                  db = Database.getInstance();
                  String sql="select DISTINCT(storageid) from dbo.HZ_STORAGE_STORAGE_INFO  where accountid ='"+accountids[i]+"'";
                  if(storagename!="" && !storagename.equals("不限"))
                  {
                      storageid=StorageUtil.returnStorageid(storagename, accountids[i]); 
                      sql+=" and storageid='"+storageid+"'";
                  }
                 
                  rs = db.getInfo(sql);
                 
                  while(rs.next()){
                      double totalMoney=0d;//初始化总金额
                          storageid=rs.getString("storageid");
                      String sqlC="SELECT MAX(DISTINCT hz.ID) AS ID, hz.AccountID, hz.ProductID,hs.ProName,hs.ProUnit,hs.ProSpec,hs.ProTypeID,hz.StorageID, SUM(hz.Amount) AS Amount, SUM(hz.Price * hz.Amount) AS BillSum,hz.note FROM dbo.HZ_STORAGE_STORAGE_INFO hz left JOIN dbo.HZ_SETTING_PRO_INFO hs on hz.ProductID=hs.ID where 1=1 ";
                      if(storageid!="")
                      {
                          sqlC+="and hz.storageid='"+storageid+"'";
                      }
                      if(accountids[i]!="")
                      {
                          sqlC+="and hz.accountid='"+accountids[i]+"'";
                      }
                      if(protypeid!="" && !protypeid.equals("0") )
                      {
                          //sqlC+="and hz.ID in ("+selectAccs+")";
                          sqlC+="and hs.ProTypeID in ("+protypeid+")";
                      }
                      sqlC+="GROUP BY hz.AccountID, hz.ProductID, hz.StorageID,hz.Note,hs.ProName,hs.ProUnit,hs.ProSpec,hs.ProTypeID";
                      rs2 = db.getInfo(sqlC);
                      while(rs2.next()){
                          t++;
                          pronumber = rs2.getString("ProductID");//材料编号
                          pronamer = rs2.getString("proname");//材料名称
                          prospec = rs2.getString("prospec");//材料规格
                          prounit = rs2.getString("prounit");//材料单位
                          storagenames =rs2.getString("storageid");//所有仓库
                          sumAmount = rs2.getDouble("Amount");//数量
                          sumMoney = rs2.getDouble("BillSum");//金额
                          note = rs2.getString("note");//备注
                          //StorageUtil storageUtil = new StorageUtil();
                          storageName=StorageUtil.returnStorage(storagenames);//获取仓库名称
                          accountName=StorageUtil.returnAccountid(accountids[i]);//获取帐套名称
                          Map linkmap = new LinkedHashMap();
                          linkmap.put("numbers", t);
                          linkmap.put("HzStorageStorageView_productid_pronumber", pronumber);
                          linkmap.put("HzStorageStorageView_productid_proname", pronamer);
                          linkmap.put("HzStorageStorageView_productid_prospec", prospec);
                          linkmap.put("HzStorageStorageView_productid_prounit", prounit);
                          linkmap.put("HzStorageStorageView_storageid_storagename", storageName);
                          linkmap.put("HzStorageStorageView_amount", sumAmount);
                          linkmap.put("HzStorageStorageView_billsum", sumMoney);
                          linkmap.put("HzStorageStorageView_productid_pronote", note);
                          linkmap.put("accountidName", accountName);
                          linkmap.put("param", 0);
                          totalMoney+=sumMoney;
                          //linkmap.put("HzStorageStorageView_productid_pronote", 1);
                          list.add(linkmap);
                      }
                      //仓库合计
                      Map linkmapSt = new LinkedHashMap();
                      linkmapSt.put("numbers", "");
                      linkmapSt.put("HzStorageStorageView_productid_pronumber", "");
                      linkmapSt.put("HzStorageStorageView_productid_proname", "");
                      linkmapSt.put("HzStorageStorageView_productid_prospec", "");
                      linkmapSt.put("HzStorageStorageView_productid_prounit", "");
                      linkmapSt.put("HzStorageStorageView_storageid_storagename", "");
                      linkmapSt.put("HzStorageStorageView_amount", "");
                      linkmapSt.put("HzStorageStorageView_billsum", totalMoney);
                      linkmapSt.put("accountidName", "");
                      linkmapSt.put("param", 1);
                      totalAllMoney=totalMoney;
                      linkmapSt.put("HzStorageStorageView_productid_pronote", "");
                      list.add(linkmapSt);  
                  }
                  //帐套合计
                  Map linkmapAccount = new LinkedHashMap();
                  linkmapAccount.put("numbers", "");
                  linkmapAccount.put("HzStorageStorageView_productid_pronumber", "");
                  linkmapAccount.put("HzStorageStorageView_productid_proname", "");
                  linkmapAccount.put("HzStorageStorageView_productid_prospec", "");
                  linkmapAccount.put("HzStorageStorageView_productid_prounit", "");
                  linkmapAccount.put("HzStorageStorageView_storageid_storagename", "");
                  linkmapAccount.put("HzStorageStorageView_amount", "");
                  linkmapAccount.put("HzStorageStorageView_billsum", totalAllMoney);
                  linkmapAccount.put("accountidName", "");
                  linkmapAccount.put("param", 2);
                  linkmapAccount.put("HzStorageStorageView_productid_pronote", "");
                  totalAllMoneys+=totalAllMoney;
                  list.add(linkmapAccount); 
                 
               }
              catch(Exception e){
                  e.printStackTrace();
              }
              finally{
                  db.close(rs);
                  db.close(rs2);
                  db = null;
              }
                 
            }

你可能感兴趣的:(java,初始化,我爱编程网,累加值,Bcw52)