package core.stlmes.server.sale.dao;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import core.stlmes.server.sale.po.SlmBalanceInfoBean;

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

public class SlmBalanceInfoDao extends BaseService
{
	private List<SlmBalanceInfoBean> query(String sqlCondition)
			throws SQLException
	{
		List<SlmBalanceInfoBean> list = null;
		String sqlStr = this.getQuerySql();
		sqlStr = sqlStr + sqlCondition;
		java.sql.ResultSet set = super.executeForResult(sqlStr);
		list = SlmBalanceInfoBean.convertResultSetToBeanList(set);
		set.close();
		return list;

	}

	private List<SlmBalanceInfoBean> getSlmBalanceInfoByBalanceStatus(
			String balancestatus) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"BALANCESTATUS", balancestatus);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getWaitForAffrimSlmBalanceInfo()
			throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"TICKETSTATUS", "1");
		sqlCondition += super
				.GetSqlConditionByStringValue("BALANCESTATUS", "0");

		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getAffrimSlmBalanceInfo()
			throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"BALANCESTATUS", "2");
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getAffrimSlmBalanceInfoByAskPlanId(
			String askPlanId) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"BALANCESTATUS", "2");
		sqlCondition += super.GetSqlConditionByStringValue("ASKPLAN_ID",
				askPlanId);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getSlmBalanceInfoByAskPlanId(
			String askPlanId) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue("ASKPLAN_ID",
				askPlanId);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getSlmBalanceInfoByListnumber(
			String listnumber) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue("LISTNUMBER",
				listnumber);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getSlmBalanceInfoByInvoiceNo(
			String invoiceno) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue("invoiceno",
				invoiceno);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getSlmBalanceInfoByRealInvoiceNo(
			String invoiceno) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"REALINVOICENO", invoiceno);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getSlmBalanceInfoByInvoiceNoOrg(
			String invoiceno) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"invoiceno_org", invoiceno);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getSlmBalanceInfoByRealInvoiceNoOrg(
			String invoiceno) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"REALINVOICENO_ORG", invoiceno);
		return query(sqlCondition);
	}

	public List<SlmBalanceInfoBean> getAffrimSlmBalanceInfoByListPrimyKey(
			java.util.ArrayList prikey) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"BALANCESTATUS", "2");
		String primyKey = "ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey";
		sqlCondition += super.getSqlConditionByArrayListStringValue(primyKey,
				prikey);
		return query(sqlCondition);
	}

	public SlmBalanceInfoBean getSlmBalanceInfoByPrimaryKey(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey)
			throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				getPrimayColumn(), this.getPrimaryKeyValue(orderNo, orderSeq,
						orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
						steelCodeKey));
		sqlCondition = getBalKeyCondition(orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey);
		List<SlmBalanceInfoBean> list = query(sqlCondition);
		if (list == null || list.size() == 0) return null;
		return list.get(0);

	}

	private String getBalKeyCondition(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey)
	{
		java.lang.StringBuilder sb = new StringBuilder();
		sb.append(super.GetSqlConditionByStringValue("ORD_NO", orderNo));
		sb.append(super.GetSqlConditionByStringValue("ORD_SEQ", orderSeq));
		sb
				.append(super.GetSqlConditionByStringValue("ORD_LN_DLY",
						orderDlvSeq));
		sb.append(super.GetSqlConditionByStringValue("ASKPLAN_ID", askPlanId));
		sb.append(super.GetSqlConditionByStringValue("ASKPLAN_ID_SEQ",
				askPlanIdSeq));
		sb.append(super.GetSqlConditionByStringValue("SPETKEY", spetKey));
		sb.append(super.GetSqlConditionByStringValue("STEELCODEKEY",
				steelCodeKey));
		return sb.toString();

	}

	private String getPrimaryKeyValue(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey)
	{
		java.lang.StringBuilder sb = new StringBuilder();
		String returnstr = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		return returnstr;

	}

	private String getPrimayColumn()
	{
		return "ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey";
	}

	private CoreReturnObject updateBalanceStatus(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String balanceStatus, String person) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();

		String sqlStr = "   update  SLM_BALANCEINFO set BALANCESTATUS =  ? ,"
				+ "  AFFRIMPERSON = ?,AFFIRMTIME = sysdate  "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ balanceStatus, person, orderNo, orderSeq, orderDlvSeq, askPlanId,
				askPlanIdSeq, spetKey, steelCodeKey });
		obj = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return obj;

	}

	public CoreReturnObject updateBalanceStatusToUnAffrim(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String balanceStatus, String person) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set BALANCESTATUS =  '0' ,"
				+ "  AFFRIMPERSON = null,AFFIRMTIME = null  "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ orderNo, orderSeq, orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
				steelCodeKey });
		obj = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return obj;

	}

	public CoreReturnObject updateBalanceStatusToAffrim(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String person) throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		obj = updateBalanceStatus(orderNo, orderSeq, orderDlvSeq, askPlanId,
				askPlanIdSeq, spetKey, steelCodeKey, "2", person);
		return obj;

	}

	public String updateBalanceStatusToAffrimStr(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String person)
	{
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set BALANCESTATUS =  ? ,"
				+ "  AFFRIMPERSON = ?,AFFIRMTIME = sysdate  "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ "2", person, orderNo, orderSeq, orderDlvSeq, askPlanId, askPlanIdSeq,
				spetKey, steelCodeKey });
		return sqlStr;

	}

	public String updateBalanceStatusToUnAffrimStr(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String person)
	{
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set BALANCESTATUS =  ? ,"
				+ "  AFFRIMPERSON = null,AFFIRMTIME = null  "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ "0", orderNo, orderSeq, orderDlvSeq, askPlanId, askPlanIdSeq,
				spetKey, steelCodeKey });
		return sqlStr;

	}

	public CoreReturnObject updateBalanceStatusTounAffrim(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String person) throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		obj = updateBalanceStatusToUnAffrim(orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey, "0", person);
		return obj;

	}

	public CoreReturnObject updateBalancePrice(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String price,
			String adjustreason, String person) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set price =  nvl(?,0),"
				+ " money = round(nvl(?,0) * (case when order_unit = '吨' then  weight"
				+ "  when order_unit = '支' then quantity else nvl(send_num,weight) end ),5), "
				+ "  ADJUSTPRICEPERSON = ? ,ADJUSTPRICEREASON = ?,PRICE_PRE = ? "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ price, price, person, adjustreason, price, orderNo, orderSeq,
				orderDlvSeq, askPlanId, askPlanIdSeq, spetKey, steelCodeKey });
		obj = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return obj;

	}

	public CoreReturnObject insertBalancePrice(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String price, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set price =  nvl(?,0),"
				+ " money = round(nvl(?,0) * (case when order_unit = '吨' then  weight"
				+ "  when order_unit = '支' then quantity else nvl(send_num,weight) end ),5), "
				+ "  PRICE_INPUTPERSON = ? ,PRICE_INPUTPEDATE = sysdate,PRICE_PRE = ?    "
				+ "  where ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ price, price, person, price, updateCondition });
		obj = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return obj;

	}

	public String insertBalancePriceStr(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String price, String person)

	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set price =  nvl(?,0),"
				+ " money = round(nvl(?,0) * (case when order_unit = '吨' then  weight"
				+ "  when order_unit = '支' then quantity else nvl(send_num,weight) end ),5), "
				+ "  PRICE_INPUTPERSON = ? ,PRICE_INPUTPEDATE = sysdate,PRICE_PRE = ?    "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ price, price, person, price, orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey });

		return sqlStr;

	}

	public String insertBalancePriceStr(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String price, String contax,
			String person)

	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set contain_tax = ?,price =  nvl(?,0),"
//				+ " money = round(nvl(?,0) * (case when order_unit = '吨' then  weight"
//				+ "  when order_unit = '支' then quantity else nvl(send_num,weight) end ),5), "
				+ "money = round(nvl(?,0) * (weight),5),"
				+ "  PRICE_INPUTPERSON = ? ,PRICE_INPUTPEDATE = sysdate,PRICE_PRE = ?    "
				+ "  where 1=1 and ORD_NO = ? and "
				+ "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and "
				+ "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and "
				+ "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ contax, price, price, person, price, orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey });

		return sqlStr;

	}

	public String insertBalancePriceByOrdLnPk(String ordPk, String ordLnPk,
			String price, String person)

	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(ordPk).append(ordLnPk).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set price =  nvl(?,0),"
				+ " money = round(nvl(?,0) * (case when order_unit = '吨' then  weight"
				+ "  when order_unit = '支' then quantity else nvl(send_num,weight) end ),5), "
				+ "  PRICE_INPUTPERSON = ? ,PRICE_INPUTPEDATE = sysdate,PRICE_PRE = ?    "
				+ "  where  1 = 1 and nvl( a.balancestatus,'0') <> '3' and ord_pk||ord_ln_pk = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ price, price, person, price, updateCondition });

		return sqlStr;

	}

	public CoreReturnObject insertBalancePriceIncludeCorpPrice(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String price, String corpPrice, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).append(
				orderDlvSeq).append(askPlanId).append(askPlanIdSeq).append(
				spetKey).append(steelCodeKey).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set price =  nvl(?,0), money = round(nvl(?,0) * weight,5), "
				+ "  PRICE_INPUTPERSON = ? ,PRICE_INPUTPEDATE = sysdate,PRICE_PRE = ?   ,"
				+ "  PRICE_ORG =  nvl(?,0), MONEY_ORG = round(nvl(?,0) * weight,5), "
				+ "  PRICE_INPUTPERSON_ORG = ? ,PRICE_INPUTPEDATE_ORG = sysdate,PRICE_PRE_ORG = ?    "
				+ "  where 1=1 and ORD_NO = ? and "
				+ "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and "
				+ "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and "
				+ "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ price, price, person, price, corpPrice, corpPrice, person, corpPrice,
				orderNo, orderSeq, orderDlvSeq, askPlanId, askPlanIdSeq,
				spetKey, steelCodeKey });
		obj = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return obj;

	}

	public CoreReturnObject makeInvoice(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String invoiceno,
			String invoiceno_seq, String invoicePer)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();

		String updateCondition = this.getPrimaryKeyValue(orderNo, orderSeq,
				orderDlvSeq, askPlanId, askPlanIdSeq, spetKey, steelCodeKey);
		String sql = "";
		sql = " update SLM_BALANCEINFO set  BALANCESTATUS =  '3' ,"
				+ "Invoiceno = ? ,Invoiceno_seq = ?, Invoicedate = sysdate,invoiceperson = ? "
				+ "  where 1=1 and ORD_NO = ? and " + "  ORD_SEQ = ? and "
				+ "  ORD_LN_DLY = ? and " + "  ASKPLAN_ID = ? and "
				+ "  ASKPLAN_ID_SEQ = ? and " + "  spetKey = ? and "
				+ "  steelCodeKey =   ?  ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceno, invoiceno_seq, invoicePer, orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	
	public String upateBillM(String billNo)
	{
		String sql = "";
		sql = " UPDATE YDM_ZC_BILL_M T SET T.WIND_UP_FLAG = '1' WHERE T.BILL_NO = '"+billNo+"'";
		return sql;
		
	}
	
	public String makeInvoiceSqlStr(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String invoiceno,
			String invoiceno_seq, String invoicePer)
			throws java.sql.SQLException,java.lang.Exception
	{

		String sql = "";
		sql = " update SLM_BALANCEINFO set  BALANCESTATUS =  '3' ,"
				+ "Invoiceno = ? ,Invoiceno_seq = ?, Invoicedate = sysdate,"
				+ " INVOICE_MONTH = to_char(sysdate,'yyyymm') ,Invoice_Sts = '0',"
				+ "  invoiceperson = ? "
				+ "   where BALANCESTATUS = '2' and ORD_NO = ? and "
				+ "  ORD_SEQ = ? and " + "  ORD_LN_DLY = ? and "
				+ "  ASKPLAN_ID = ? and " + "  ASKPLAN_ID_SEQ = ? and "
				+ "  spetKey = ? and " + "  steelCodeKey =   ?  ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceno, invoiceno_seq, invoicePer, orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey });
		return sql;

	}

	public CoreReturnObject makeInvoiceWithOrgTickets(String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String invoiceno, String invoiceno_seq, String invoicePer)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		String orgInvoiceNoStartWith = "ORG";
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = this.getPrimaryKeyValue(orderNo, orderSeq,
				orderDlvSeq, askPlanId, askPlanIdSeq, spetKey, steelCodeKey);
		String sql = "";
		sql = " update SLM_BALANCEINFO set  BALANCESTATUS_ORG =  '3' ,"
				+ "INVOICENO_ORG = ? ,INVOICENO_SEQ_ORG = ?, INVOICEDATE_ORG = sysdate,INVOICEPERSON_ORG = ?, "
				+ "  BALANCESTATUS =  '3' ,"
				+ " Invoiceno = ? ,Invoiceno_seq = ?, Invoicedate = sysdate,invoiceperson = ?  "
				+ "   where   ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ orgInvoiceNoStartWith + invoiceno, invoiceno_seq, invoicePer,
				invoiceno, invoiceno_seq, invoicePer, updateCondition });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	public CoreReturnObject makeInvoiceWithOrgTickets(SlmBalanceInfoBean bean)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		String orgInvoiceNoStartWith = "ORG";
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(bean.getOrd_no()).append(
				bean.getOrd_seq()).append(bean.getOrd_ln_dly()).append(
				bean.getAskplan_id()).append(bean.getAskplan_id_seq()).append(
				bean.getSpetkey()).append(bean.getSteelcodekey()).toString();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  "
				+ "  INVOICENO_ORG = ? ,INVOICENO_SEQ_ORG = ?, INVOICEDATE_ORG = sysdate,INVOICEPERSON_ORG = ?, "
				+ "  BALANCESTATUS =  '3' ,"
				+ " Invoiceno = ? ,Invoiceno_seq = ?, Invoicedate = sysdate,invoiceperson = ?  "
				+ "   where   ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ orgInvoiceNoStartWith + bean.getInvoiceno(), bean.getInvoiceno_seq(),
				bean.getInvoiceperson(), bean.getInvoiceno(),
				bean.getInvoiceno_seq(), bean.getInvoiceperson(),
				updateCondition });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	public CoreReturnObject makeInvoice(SlmBalanceInfoBean bean)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();

		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(bean.getOrd_no()).append(
				bean.getOrd_seq()).append(bean.getOrd_ln_dly()).append(
				bean.getAskplan_id()).append(bean.getAskplan_id_seq()).append(
				bean.getSpetkey()).append(bean.getSteelcodekey()).toString();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  BALANCESTATUS =  '3' ,"
				+ "Invoiceno = ? ,Invoiceno_seq = ?, Invoicedate = sysdate,invoiceperson = ? "
				+ "   where   ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ bean.getInvoiceno(), bean.getInvoiceno_seq(),
				bean.getInvoiceperson(), updateCondition });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	public CoreReturnObject unmakeInvoice(String invoiceno)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();

		java.lang.StringBuilder sb = new StringBuilder();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  BALANCESTATUS =  '2' ,isinvoicenolocked = '0', "
				+ " REALINVOICENO = '',REALINVOICENO_DATE = null,"
				+ " Invoiceno = '' ,Invoiceno_seq = '', Invoicedate = null,price = PRICE_PRE,money = PRICE_PRE * weight "
				+ "   where  Invoiceno = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceno });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	public CoreReturnObject rollBackBillM(String invoiceno) throws SQLException, Exception
	{
		CoreReturnObject obj = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory.getSql("SlmBalanceInfoDao.rollBackBillM");
		sql = super.getPreparedSQL(sql, new Object[]{invoiceno});
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;
    }
	public CoreReturnObject affrimTicket(String askPlanId, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();

		java.lang.StringBuilder sb = new StringBuilder();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  TICKETSTATUS =  '1' ,"
				+ "TICKETPERSON = ? ,TICKETDATE = sysdate  "
				+ "   where  ASKPLAN_ID = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, askPlanId });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	public String affrimTicketSqlByListNumber(String listNumber, String person)

	{
		CoreReturnObject obj = new CoreReturnObject();

		java.lang.StringBuilder sb = new StringBuilder();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  TICKETSTATUS =  '1' ,"
				+ "TICKETPERSON = ? ,TICKETDATE = sysdate  "
				+ "   where  LISTNUMBER = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, listNumber });
		return sql;
	}

	public String unaffrimTicketSqlByListNumber(String listNumber, String person)

	{
		CoreReturnObject obj = new CoreReturnObject();

		java.lang.StringBuilder sb = new StringBuilder();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  TICKETSTATUS =  '0' ,"
				+ "TICKETPERSON = null ,TICKETDATE = null  "
				+ "   where  LISTNUMBER = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ listNumber });
		return sql;
	}

	public CoreReturnObject unAffrimTicket(String askPlanId, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject obj = new CoreReturnObject();

		java.lang.StringBuilder sb = new StringBuilder();
		String sql = "";
		sql = " update SLM_BALANCEINFO set  TICKETSTATUS =  '0' ,balancestatus = '0' ,balancestatus_org = '0', "
				+ "TICKETPERSON = '' ,TICKETDATE = null  "
				+ "   where  ASKPLAN_ID = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ askPlanId });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	public CoreReturnObject insert(SlmBalanceInfoBean bean)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = core.stlmes.server.sale.dao.SqlCollection
				.getInsertSlmBalanceInfo(bean);
		cro = super.ExcuteNonQuery(sql);
		return cro;

	}

	public CoreReturnObject insert(java.sql.Connection conn,
			SlmBalanceInfoBean bean) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();

		String sql = core.stlmes.server.sale.dao.SqlCollection
				.getInsertSlmBalanceInfo(bean);
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
		return cro;

	}

	public CoreReturnObject delete(String askPlanId)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "delete from SLM_BALANCEINFO  where ASKPLAN_ID = ?";
		sql = super.getPreparedSQL(sql, new Object[]
		{ askPlanId });
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public String deleteSql(String askPlanId) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "delete from SLM_BALANCEINFO  where ASKPLAN_ID = ?";
		sql = super.getPreparedSQL(sql, new Object[]
		{ askPlanId });

		return sql;

	}

	public CoreReturnObject updateRealInvoiceNo(String invoiceNo,
			String realInvoiceNo, String person) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update SLM_BALANCEINFO  set REALINVOICENO = ?,REALINVOICENO_DATE = sysdate"
				+ "  where INVOICENO = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ realInvoiceNo, invoiceNo });

		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public CoreReturnObject updateRealInvoiceNoOrg(String invoiceNo,
			String realInvoiceNo, String person) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update SLM_BALANCEINFO  set REALINVOICENO_ORG = ?,REALINVOICENO_DATE_ORG = sysdate"
				+ "  where INVOICENO_ORG = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ realInvoiceNo, invoiceNo });
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public CoreReturnObject creditInvoicePrice(String invoiceNoSeq,
			String price, String money, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update SLM_BALANCEINFO  set price = ?,money = weight * ? "
				+ "  where invoiceno||invoiceno_seq = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ price, price, invoiceNoSeq });

		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public CoreReturnObject updateSendNum(String sendNum, String orderNo,
			String orderSeq, String orderDlvSeq, String askPlanId,
			String askPlanIdSeq, String spetKey, String steelCodeKey,
			String person) throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String updateCondition = this.getPrimaryKeyValue(orderNo, orderSeq,
				orderDlvSeq, askPlanId, askPlanIdSeq, spetKey, steelCodeKey);
		String sql = "update SLM_BALANCEINFO  set SEND_NUM = ?  "
				+ "  where ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ sendNum, updateCondition });
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public String updateInvoiceStatusToLocked(String invoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();

		String sql = "update SLM_BALANCEINFO  set isinvoicenolocked = '1'  "
				+ "  where invoiceno  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceNo });
		// cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return sql;

	}

	public String updateInvoiceStatusToUnLocked(String invoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();

		String sql = "update SLM_BALANCEINFO  set isinvoicenolocked = '0'  "
				+ "  where invoiceno  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceNo });
		// cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return sql;

	}

	public CoreReturnObject updateVirtualAskplanIdToRealClose(String askPlanId)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = " update Slm_Balanceinfo set ISREALCLOSE = '是'  where ASKPLAN_ID =  ? and ISREALCLOSE = '否' ";

		Object[] param = new Object[]
		{ askPlanId };
		sql = super.getPreparedSQL(sql, param);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;
	}

	public CoreReturnObject updateVirtualAskplanIdToAffrim(String askPlanId)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = " update Slm_Balanceinfo set ISREALCLOSE = '二次确认'  where ASKPLAN_ID =  ? and ISREALCLOSE = '是' ";

		Object[] param = new Object[]
		{ askPlanId };
		sql = super.getPreparedSQL(sql, param);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;
	}

	public CoreReturnObject updateVirtualAskplanIdWhenCancelAffrimTickets(
			String askPlanId) throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = " update Slm_Balanceinfo set ISREALCLOSE = '是'  where ASKPLAN_ID =  ? and ISREALCLOSE = '二次确认' ";

		Object[] param = new Object[]
		{ askPlanId };
		sql = super.getPreparedSQL(sql, param);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;
	}

	public String getmaxInvoiceNo() throws java.sql.SQLException
	{
		String sqlStr = "select nvl(max(a.invoiceno), to_char(sysdate, 'yyyymmdd') || '000000') inv "
				+ " from Slm_Balanceinfo a where a.invoiceno  like to_char(sysdate,'yyyymmdd')||'%' ";
		java.sql.ResultSet set = super.executeForResult(sqlStr);
		if (set == null) throw new SQLException();
		while (set.next())
		{
			return set.getString("INV");
		}

		return "";
	}

	public void updateTicketListnumber(String billNo, String listNumber)
	{
		String sqlUpdateDetail = " update Slm_Balancedetail_All set listnumber = ? "
				+ "    where   ASKPLAN_ID =  ?   ";
		String sqlUpdateBalInfo = " update Slm_Balanceinfo set listnumber = ? "
				+ "    where   ASKPLAN_ID =  ?   ";

		sqlUpdateDetail = super.getPreparedSQL(sqlUpdateDetail, new Object[]
		{ listNumber, billNo });

		sqlUpdateBalInfo = super.getPreparedSQL(sqlUpdateBalInfo, new Object[]
		{ listNumber, billNo });
		try
		{
			super.ExcuteNonQueryIgnoreTrans(sqlUpdateDetail);
			super.ExcuteNonQueryIgnoreTrans(sqlUpdateBalInfo);

		}
		catch (java.sql.SQLException ex)
		{

		}

		catch (java.lang.Exception ex)
		{

		}

	}

	public void updateTicketListnumber(java.sql.Connection conn, String billNo,
			String listNumber) throws java.sql.SQLException,java.lang.Exception
	{
		String sqlUpdateDetail = " update Slm_Balancedetail_All set listnumber = ? "
				+ "    where   ASKPLAN_ID =  ?   ";
		String sqlUpdateBalInfo = " update Slm_Balanceinfo set listnumber = ? "
				+ "    where   ASKPLAN_ID =  ?   ";

		sqlUpdateDetail = super.getPreparedSQL(sqlUpdateDetail, new Object[]
		{ listNumber, billNo });

		sqlUpdateBalInfo = super.getPreparedSQL(sqlUpdateBalInfo, new Object[]
		{ listNumber, billNo });

		PreparedStatement ps = conn.prepareStatement(sqlUpdateDetail);
		PreparedStatement ps1 = conn.prepareStatement(sqlUpdateBalInfo);
		ps.executeUpdate();
		ps1.executeUpdate();
		ps.close();
		ps1.close();

	}

	public void updateOutStockmneyListNumber(java.sql.Connection conn,
			String billNo, String listNumber) throws java.sql.SQLException,
			java.lang.Exception
	{
		String sqlUpdateTrans = " update slm_balance_transmoney set listnumber = ? "
				+ "    where   ASKPLAN_ID =  ?  and money_type = '出库费'  and ticketstatus = '0' ";
		sqlUpdateTrans = super.getPreparedSQL(sqlUpdateTrans, new Object[]
		{ listNumber, billNo });

		PreparedStatement ps = conn.prepareStatement(sqlUpdateTrans);
		ps.executeUpdate();
		ps.close();

	}

	public void updateTransmneyListNumber(java.sql.Connection conn,
			String billNo, String listNumber) throws java.sql.SQLException,
			java.lang.Exception
	{
		String sqlUpdateOutStock = " update slm_balance_transmoney set listnumber = ? "
				+ "    where   ASKPLAN_ID =  ?  and money_type = '运费' and ticketstatus = '0' ";
		sqlUpdateOutStock = super.getPreparedSQL(sqlUpdateOutStock,
				new Object[]
				{ listNumber, billNo });
		PreparedStatement ps = conn.prepareStatement(sqlUpdateOutStock);
		ps.executeUpdate();
		ps.close();

	}

	public String updateRealInvoiceDate(String invoiceNo, String invoicedate,
			String person)
	{
		// invoicedate yyyymmddhh24miss
		String sql = "  update Slm_Balanceinfo set realinvoiceno_date = "
				+ " to_date(?,'yyyymmddhh24miss')  where invoiceno = ? "
				+ " and realinvoiceno is not null ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceNo, invoicedate });
		return sql;
	}

	public void UpdateProcessOrderMByUpdateRealInvoiceNo(String invoiceNO,
			String realInvoiceNo)
	{
		String sql = " update process_order_m t"
				+ "    set (ACCOUNT_NUM, ACCOUNT_WGT, ACCOUNT_LEN) = "
				+ "    (select sum(nvl(a.quantity, 0)),"
				+ "    sum(nvl(a.weight, 0)),"
				+ "    sum(nvl(a.send_num, 0))"
				+ "    from Slm_Balanceinfo a"
				+ "    where a.ord_ln_dly_pk ="
				+ "    t.ord_ln_dly_pk"
				+ "    and a.balancestatus = '3'  and a.realinvoiceno is not null  and "
				+ "    a.ord_ln_dly_pk in"
				+ "    (select a.ord_ln_dly_pk"
				+ "    from Slm_Balanceinfo a  where a.invoiceno ="
				+ "    ? )   group by "
				+ "    a.ord_ln_dly_pk) where exists (select 1"
				+ "    from Slm_Balanceinfo a where "
				+ "    a.ord_ln_dly_pk =  t.ord_ln_dly_pk"
				+ "    and a.balancestatus = '3'    and a.realinvoiceno is not null"
				+ "    and a.ord_ln_dly_pk in" + "    (select  a.ord_ln_dly_pk"
				+ "    from Slm_Balanceinfo a" + "    where a.invoiceno = ?))";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceNO, invoiceNO });
		try
		{
			super.ExcuteNonQueryIgnoreTrans(sql);

		}
		catch (java.sql.SQLException ex)
		{

		}

		catch (java.lang.Exception ex)
		{

		}

	}

	@Override
	public String getQuerySql()
	{
		return core.stlmes.server.sale.dao.SqlCollection.getSlmBalanceInfo();
	}

	public CoreReturnObject updateBalanceMemo(ArrayList<ArrayList<Object>> list) {
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> params = new ArrayList<Object[]>();
		for (ArrayList<Object> o : list) {
			String sql = "UPDATE SLM_BALANCEINFO SET DELVRY_WHEREABOUTS = ?," +
					"IS_USE = ?," +
					"MEMO = ?" +
					"WHERE ASKPLAN_ID = ?";
			Object[] param = o.toArray();
			sqllist.add(sql);
			params.add(param);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, params);
	}
}
