package core.stlmes.server.sale.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.dao.*;
import core.stlmes.server.sale.po.*;

public class YdmZcBillcDao extends BaseService
{
	String sql = " select * from ydm_zc_bill_c a  where 1 = 1  and validflag = '1' ";

	public java.sql.ResultSet getYdmBillCByBillno(String billno)
			throws java.sql.SQLException

	{
		String sqlCondition = super.GetSqlConditionByStringValue("bill_no",
				billno);
		java.sql.ResultSet rs = null;
		String strSql = sql + sqlCondition;
		rs = super.executeForResult(strSql);
		// rs.close();
		return rs;

	}

	public java.util.Hashtable getYdmBillCStorageNoByBillnoWithKeyOrdPkAndLinePkAndDelNoPk(
			String billno) throws java.sql.SQLException

	{
		java.util.Hashtable hs = new java.util.Hashtable();
		java.sql.ResultSet rs = getYdmBillCByBillno(billno);
		String key = "";
		Double price = 0.0;
		String storageNo = "";
		while (rs.next())
		{
			key = rs.getString("ORD_PK") + rs.getString("ORD_LN_PK")
					+ rs.getString("ORD_LN_DLY_PK");
			storageNo = rs.getString("STORAGE_NO");
			if (hs.containsKey(key))
			{
				hs.remove(key);
			}
			hs.put(key, storageNo);

		}
		rs.close();
		return hs;

	}

	public java.util.Hashtable getYdmBillCGradeNameByBillnoWithKeyOrdPkAndLinePkAndDelNoPk(
			String billno) throws java.sql.SQLException

	{
		java.util.Hashtable hs = new java.util.Hashtable();
		java.sql.ResultSet rs = getYdmBillCByBillno(billno);
		String key = "";
		Double price = 0.0;
		String storageNo = "";
		while (rs.next())
		{
			key = rs.getString("ORD_PK") + rs.getString("ORD_LN_PK")
					+ rs.getString("ORD_LN_DLY_PK");
			storageNo = rs.getString("GRADENAME");
			if (storageNo != null)
			{
				if (hs.containsKey(key))
				{
					hs.remove(key);
				}
				hs.put(key, storageNo);
			}

		}
		rs.close();
		return hs;

	}

	public java.util.Hashtable getYdmBillBatchNoByBillnoWithKeyOrdPkAndLinePkAndDelNoPk(
			String billno) throws java.sql.SQLException

	{
		java.util.Hashtable hs = new java.util.Hashtable();
		java.util.Hashtable hsbatch = new java.util.Hashtable();
		java.util.Hashtable hsbatcht = new java.util.Hashtable();
		// java.sql.ResultSet rs = getYdmBillCByBillno(billno);
		java.sql.ResultSet rs = null;
		String sql = "select a.ord_pk,a.ord_ln_pk,a.ord_ln_dly_pk,sum(a.act_count) ACT_COUNT,sum(a.act_weight) ACT_WEIGHT, "
				+ " a.judge_stove_no,a.batch_no,a.group_no "
				+ " from Ydm_Zc_Bill_c a  where a.validflag = '1' and a.bill_no  = ?  "
				+ " group by a.ord_pk,a.ord_ln_pk,a.ord_ln_dly_pk,a.judge_stove_no,a.batch_no,a.group_no ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billno });
		rs = super.executeForResult(sql);
		String key = "";
		Double price = 0.0;
		String storageNo = "";
		String[] str = new String[4];
		double wgt = 0.0;
		double cnt = 0;
		String batchno = "";
		String pno = "";
		String groupNo = "";
		String batchKey = "";
		while (rs.next())
		{
			wgt = 0;
			cnt = 0;
			key = rs.getString("ORD_PK") + rs.getString("ORD_LN_PK")
					+ rs.getString("ORD_LN_DLY_PK") + billno;

			batchno = rs.getString("JUDGE_STOVE_NO");
			pno = rs.getString("BATCH_NO");
			groupNo = rs.getString("GROUP_NO");
			Double actw = rs.getDouble("ACT_WEIGHT");
			Double actc = rs.getDouble("ACT_COUNT");

			String bathKey = batchno + pno + groupNo;
			// 交货行向下,有多个炉批组
			if (hs.containsKey(key))
			{
				hsbatch = ( java.util.Hashtable)hs.get(key);
				if (hsbatch.containsKey(bathKey))
				{
					str = ( String[])hsbatch.get(bathKey);
					wgt = core.stlmes.server.sale.util.Convert
							.getDouble(str[2]);
					cnt = core.stlmes.server.sale.util.Convert
							.getDouble(str[3]);
					wgt = wgt + actw;
					cnt = cnt + actc;
					str = new String[]
					{ batchno, pno, wgt + "", cnt + "", groupNo };
					hsbatch.remove(bathKey);
					hsbatch.put(bathKey, str);
				}
				else
				{

					wgt = actw;
					cnt = actc;
					str = new String[]
					{ batchno, pno, wgt + "", cnt + "", groupNo };
					hsbatch.put(bathKey, str);

				}
				hs.remove(key);
				hs.put(key, hsbatch);
			}
			else
			{
				hsbatcht = new java.util.Hashtable();
				wgt = actw;
				cnt = actc;
				str = new String[]
				{ batchno, pno, wgt + "", cnt + "", groupNo };
				hsbatcht.put(bathKey, str);
				hs.put(key, hsbatcht);
			}

		}
		rs.close();
		return hs;

	}
}
