package core.stlmes.server.sale.dao.fund;

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

import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.dbhelp.DbTrans;
import core.stlmes.server.sale.po.SlmBalanceInfoBean;
import core.stlmes.server.sale.po.fund.SelFundCustAccountBean;
import core.stlmes.server.sale.po.fund.SelFundCustFundInoutBean;
import core.stlmes.server.sale.po.fund.SelFundCustUsedDetailBean;

public class FundCustAccountDao extends BaseDao
{
	String SQL_SEL_FUND_CUST_ACCOUNT = "select balancesubject,"
			+ "      buyercode,     round(totalmoney,4) totalmoney,"
			+ "round(leavemoney,4) leavemoney,      importor,      importtime,"
			+ "      updatetime from sel_fund_cust_account where 1 = 1  ";

	String SQL_DELETE = "delete from sel_fund_cust_account where"
			+ " 1 = 1 and balancesubject = ?  and buyercode = ?  ";

	private List<SelFundCustAccountBean> query(String sqlCondition)
			throws SQLException
	{
		List<SelFundCustAccountBean> list = null;
		String sqlStr = SQL_SEL_FUND_CUST_ACCOUNT;
		sqlStr = sqlStr + sqlCondition;
		java.sql.ResultSet set = super.executeForResult(sqlStr);
		list = SelFundCustAccountBean.convertResultSetToBeanList(set);
		return list;
	}

	public List<SelFundCustAccountBean> getSelFundCustAccountInfo(
			String balancesubject, String buyerCode) throws SQLException
	{
		String sqlConditioin = super.getSqlCondition(balancesubject, buyerCode);
		return query(sqlConditioin);
	}

	public List<SelFundCustAccountBean> getSelFundCustAccountInfo(
			String balancesubject) throws SQLException
	{
		String sqlConditioin = super.GetSqlConditionByStringValue(
				"balancesubject", balancesubject);

		return query(sqlConditioin);
	}

	public CoreReturnObject insert(SelFundCustAccountBean bean)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String SQL_SEL_FUND_CUST_ACCOUNT_INSERT = "insert into sel_fund_cust_account "
				+ " (balancesubject, buyercode,  totalmoney, leavemoney,"
				+ " importor,  importtime, updatetime) "
				+ " values (?,?,?,?,?,sysdate,sysdate) ";
		Object[] obj = new Object[]
		{ bean.getBalancesubject(), bean.getBuyercode(), bean.getTotalmoney(),
				bean.getLeavemoney(), bean.getImportor() };
		String sql = super
				.getPreparedSQL(SQL_SEL_FUND_CUST_ACCOUNT_INSERT, obj);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;

	}

	public CoreReturnObject addMoneyToCustAccount(SelFundCustAccountBean bean,
			double money) throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (money == 0) return cro;
		String sqlStr = " update  sel_fund_cust_account set totalmoney = totalmoney + ?,  leavemoney = leavemoney + ? ,"
				+ "updatetime = sysdate  where 1 = 1 and  balancesubject = ?   and buyercode = ? ";
		Object[] obj = new Object[]
		{ money, money, bean.getBalancesubject(), bean.getBuyercode() };
		String sql = super.getPreparedSQL(sqlStr, obj);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;
	}

	public CoreReturnObject addLeaveMoneyCustAccount(String balancesubject,
			String buyerCode, double money) throws java.sql.SQLException,
			java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (money == 0) return cro;
		String sqlStr = " update  sel_fund_cust_account set   leavemoney = leavemoney + ? ,"
				+ "updatetime = sysdate  where 1 = 1 and  balancesubject = ?   and buyercode = ? ";
		Object[] obj = new Object[]
		{ money, money, balancesubject, buyerCode };
		String sql = super.getPreparedSQL(sqlStr, obj);
		cro = super.ExcuteNonQueryIgnoreTrans(sql);
		return cro;
	}
}
