package core.stlmes.server.sale.dao;

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

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.model.SlmOrderLineEntity;

import core.stlmes.server.sale.dbhelp.DbTrans;
import core.stlmes.server.sale.po.SlmOrderHeadTgBean;
import core.stlmes.server.sale.po.SlmOrderLineNewEntity;
import core.stlmes.server.sale.po.SlmOrderLineTgBean;
import CoreFS.SA06.CoreReturnObject;

public class SlmOrderLineTgDao extends BaseService
{
	private List<SlmOrderLineTgBean> query(String sqlCondition)
			throws SQLException
	{
		List<SlmOrderLineTgBean> list = null;
		String sqlStr = " select * from slm_order_line where 1 = 1"
				+ "  and VALIDFLAG = '1'  ";
		sqlStr = sqlStr + sqlCondition;
		java.sql.ResultSet set = null;
		set = super.executeForResult(sqlStr);
		list = SlmOrderLineTgBean.convertResultSetToBeanList(set);
		set.close();
		return list;
	}

	public SlmOrderLineTgBean getSlmOrderLineTgByOrdPkAndOrdlnPk(String ordPk,
			String ordLnPk) throws SQLException
	{
		List<SlmOrderLineTgBean> list = null;
		String sqlCondition = super.GetSqlConditionByStringValue("ORD_PK",
				ordPk);
		sqlCondition += super
				.GetSqlConditionByStringValue("ORD_LN_PK", ordLnPk);
		list = query(sqlCondition);
		if (list == null || list.size() == 0) return null;
		return list.get(0);
	}

	public List<SlmOrderLineTgBean> getSlmOrderLineTgByOrdPk(String ordPk)
			throws SQLException
	{
		List<SlmOrderLineTgBean> list = null;
		String sqlCondition = super.GetSqlConditionByStringValue("ORD_PK",
				ordPk);

		list = query(sqlCondition);
		if (list == null || list.size() == 0) return null;
		return list;
	}
	
	public List<SlmOrderLineNewEntity> getSlmOrderLineTgByOrdPkNew(String ordPk,java.sql.Connection conn)
			throws SQLException
	{
		SqlBuilder b = null;
		b = new SqlBuilder("SlmBalanceQuery.getSlmOrderLineTgByOrdPkNew");
		b.Fill(new String[]{ ordPk });
		List<SlmOrderLineNewEntity> list = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), conn, SlmOrderLineNewEntity.class);
		return list;
	}

	public CoreReturnObject updateFinallBalPrice(String ordLnPk, String price,
			String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update slm_order_line set  balnce_price = ?  where ORD_LN_PK = ? "
				+ " and nvl(BALNCE_PRICE_STS,'0') = '0' ";
		double dprice = core.stlmes.server.sale.util.Convert.getDouble(price);

		sql = super.getPreparedSQL(sql, new Object[]
		{ dprice, ordLnPk });
		System.out.println(sql);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;
	}

	public CoreReturnObject updateFinallBalPriceOrg(String ordLnPk,
			String price, String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update slm_order_line set  BALNCE_PRICE_ORG = ?  where ORD_LN_PK = ? "
				+ " and nvl(BALNCE_PRICE_ORG_STS,'0') = '0' ";
		double dprice = core.stlmes.server.sale.util.Convert.getDouble(price);

		sql = super.getPreparedSQL(sql, new Object[]
		{ dprice, ordLnPk });
		cro = super.ExcuteNonQueryIgnoreTrans(sql);

		return cro;
	}

	public CoreReturnObject affrimFinallBalPrice(String orderNo, String person)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update slm_order_line set  BALNCE_PRICE_STS = '1'  where ORD_PK = ? ";

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

		return cro;
	}

	public CoreReturnObject unAffrimFinallBalPrice(String orderNo, String person)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update slm_order_line set  BALNCE_PRICE_STS = '0'  where ORD_PK = ? ";

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

		return cro;
	}

	public CoreReturnObject affrimFinallBalPriceOrg(String orderNo,
			String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update slm_order_line set  BALNCE_PRICE_ORG_STS = '1'  where ORD_PK = ? ";

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

		return cro;
	}

	public CoreReturnObject unAffrimFinallBalPriceOrg(String orderNo,
			String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sql = "update slm_order_line set  BALNCE_PRICE_ORG_STS = '0'  where ORD_PK = ? ";

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

		return cro;
	}

}
