package core.stlmes.server.sale.dao;

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

import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.po.SlmBalanceInfoBean;

public class SlmBalanceInfoOrgDao extends BaseService
{

	private String FstDraweeUnitGrop = "1";

	private String FstDraweeUnitGuomao = "2";

	private String FstDraweeUnitGangmao = "3";

	private String FstDraweeUnitLastInvoice = "4";

	private List<SlmBalanceInfoBean> query(String sqlCondition)
			throws SQLException
	{
		List<SlmBalanceInfoBean> list = null;
		String sqlStr = this.getQuerySql();
		String sqlConditonOrg = " and (FST_DRAWEE_UNIT = ? or FST_DRAWEE_UNIT = ? or FST_DRAWEE_UNIT = ?)";
		sqlConditonOrg = super.getPreparedSQL(sqlConditonOrg, new Object[]
		{ FstDraweeUnitGrop, FstDraweeUnitGuomao, FstDraweeUnitGangmao });
		sqlStr = sqlStr + sqlCondition + sqlConditonOrg;
		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_ORG", balancestatus);
		return query(sqlCondition);
	}

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

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

	public List<SlmBalanceInfoBean> getAffrimSlmBalanceInfoByAskPlanId(
			String askPlanId) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"BALANCESTATUS_ORG", "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> getAffrimSlmBalanceInfoByListPrimyKey(
			java.util.ArrayList prikey) throws java.sql.SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(
				"BALANCESTATUS_ORG", "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));
		List<SlmBalanceInfoBean> list = query(sqlCondition);
		if (list == null || list.size() == 0) return null;
		return list.get(0);

	}

	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_ORG =  ? ,"
				+ "  AFFRIMPERSON_ORG = ?,AFFIRMTIME_ORG = sysdate  "
				+ "  where ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ balanceStatus, person, updateCondition });
		System.out.print(sqlStr);
		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_ORG =  '0' ,"
				+ "  AFFRIMPERSON_ORG = null,AFFIRMTIME_ORG = null  "
				+ "  where ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ updateCondition });
		System.out.print(sqlStr);
		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_ORG =  ? ,"
				+ "  AFFRIMPERSON_ORG = ?,AFFIRMTIME_ORG = sysdate "
				+ "  where ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ "2", person, updateCondition });
		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 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_ORG =  ? ,"
				+ "  AFFRIMPERSON_ORG = null,AFFIRMTIME_ORG = null  "
				+ "  where ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ "0", updateCondition });
		return sqlStr;
	}

	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_ORG =  nvl(?,0), "
				+ "  MONEY_ORG = round(nvl(?,0) * weight,5), "
				+ "  PRICE_INPUTPERSON_ORG = ? ,PRICE_INPUTPEDATE_ORG = sysdate,PRICE_PRE_ORG = ?   "
				+ "  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 insertBalancePriceSqlStr(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_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, orderNo, orderSeq, orderDlvSeq,
				askPlanId, askPlanIdSeq, spetKey, steelCodeKey });
		return sqlStr;

	}

	public String insertBalancePriceSqlStrForGm(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_ORG =  nvl(?,0), "
				+ "  MONEY_ORG = round(nvl(?,0) *   weight"
				+ "  ,5), "
				+ "  PRICE_INPUTPERSON_ORG = ? ,PRICE_INPUTPEDATE_ORG = sysdate,PRICE_PRE_ORG = ?   "
				+ "  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 });
		return sqlStr;

	}

	public String insertBalancePriceSqlByOrdPkAndLnPk(String orderNo,
			String orderSeq, String price, String person)

	{
		CoreReturnObject obj = new CoreReturnObject();
		java.lang.StringBuilder sb = new StringBuilder();
		String updateCondition = sb.append(orderNo).append(orderSeq).toString();
		String sqlStr = "  update  SLM_BALANCEINFO set PRICE_ORG =  nvl(?,0), "
				+ "  MONEY_ORG = round(nvl(?,0) * (case when order_unit = '吨' then  weight"
				+ "  when order_unit = '支' then quantity else nvl(send_num,weight) end ),5), "
				+ "  PRICE_INPUTPERSON_ORG = ? ,PRICE_INPUTPEDATE_ORG = sysdate,PRICE_PRE_ORG = ?   "
				+ "  where 1= 1 and nvl(balancestatus_org,'0') <> '3' and ord_pk||ord_ln_pk = "
				+ "  ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ price, price, person, price, updateCondition });
		return sqlStr;

	}

	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_ORG =  '3' ,"
				+ "INVOICENO_ORG = ? ,INVOICENO_SEQ_ORG = ?, INVOICEDATE_ORG = sysdate,INVOICEPERSON_ORG = ? "
				+ "   where   ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceno, invoiceno_seq, invoicePer, updateCondition });
		obj = super.ExcuteNonQueryIgnoreTrans(sql);
		return obj;

	}

	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
	{
		CoreReturnObject obj = new CoreReturnObject();

		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 = ? "
				+ "   where BALANCESTATUS_ORG = '2' and   ORD_NO||ORD_SEQ||ORD_LN_DLY||ASKPLAN_ID||ASKPLAN_ID_SEQ||spetKey||steelCodeKey  = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceno, invoiceno_seq, invoicePer, updateCondition });
		return sql;

	}

	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_ORG =  '3' ,"
				+ "INVOICENO_ORG = ? ,INVOICENO_SEQ_ORG = ?, INVOICEDATE_ORG = sysdate,INVOICEPERSON_ORG = ? "
				+ "   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_ORG =  '2' ,"
				+ "INVOICENO_ORG = '' ,REALINVOICENO_ORG = '',INVOICENO_SEQ_ORG = '', INVOICEDATE_ORG = null,PRICE_ORG = PRICE_PRE_ORG"
				+ ", money_org = PRICE_PRE_ORG * weight "
				+ "   where  INVOICENO_ORG = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceno });
		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 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_org = ?,money_org = ? "
				+ "  where invoiceno_org||invoiceno_seq_org" + " = ? ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ price, money, invoiceNoSeq });
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public String getmaxInvoiceNo() throws java.sql.SQLException
	{
		String sqlStr = "select nvl(max(a.INVOICENO_ORG), to_char(sysdate, 'yyyymmdd') || '6500000') inv  from Slm_Balanceinfo a where a.INVOICENO_ORG  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 String updateRealInvoiceDate(String invoiceNo, String invoicedate,
			String person)
	{
		// invoicedate yyyymmddhh24miss
		String sql = "  update Slm_Balanceinfo set realinvoiceno_date_org = "
				+ " to_date(?,'yyyymmddhh24miss')  where invoiceno_org = ? "
				+ "  and realinvoiceno_org is not null ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ invoiceNo, invoicedate });
		return sql;
	}

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

}
