package core.stlmes.server.sale.service.balance;

import java.sql.SQLException;
import core.stlmes.server.sale.po.SlmBusinessOpeInfoBean;
import java.util.*;

import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.dao.DaoFactory;
import core.stlmes.server.sale.dao.SlmBalanceInfoDao;
import core.stlmes.server.sale.dao.SlmOrderLineTgDao;
import core.stlmes.server.sale.dbhelp.DbTrans;
import core.stlmes.server.sale.po.SlmBalanceDetailAllBean;
import core.stlmes.server.sale.po.SlmBalanceInfoBean;
import core.stlmes.server.sale.po.SlmOrderPriceListBean;
import core.stlmes.server.sale.service.baseservice.BaseBll;
import core.stlmes.server.sale.po.SlmBalanceTransMoneyBean;
import core.stlmes.server.sale.dao.*;
import core.stlmes.server.sale.po.*;

public class SlmBalanceMnySubt extends BaseBll
{
	public CoreReturnObject insert(java.util.HashMap hsMap)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceMnySubtDao dao = new SlmBalanceMnySubtDao();
		SlmBalanceMnySubtBean bean = SlmBalanceMnySubtBean
				.convertHashMapToBean(hsMap);
		String sub_seq = bean.getSub_seq();
		try
		{
			SlmBalanceMnySubtBean debean = dao
					.getSlmBalanceMnySubtBySubSeq(sub_seq);
			if (debean != null)
			{
				super.setCroErrMsg(cro, "冲减单号:" + sub_seq + "已经存在");
				return cro;
			}
			bean.setStatus_cd("0");
			bean.setCreate_time(new Date());
			dao.insert(bean);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;
		}

		return cro;
	}

	public CoreReturnObject delete(String subSeq, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceMnySubtDao dao = new SlmBalanceMnySubtDao();

		String sub_seq = subSeq;
		try
		{
			SlmBalanceMnySubtBean debean = dao
					.getSlmBalanceMnySubtBySubSeq(sub_seq);
			if (debean == null)
			{
				return cro;
			}

			if (debean.getStatus_cd().equals("1"))
			{
				super.setCroErrMsg(cro, "冲减单号:" + sub_seq + "已经确认");
				return cro;
			}

			if (debean.getStatus_cd().equals("2"))
			{
				super.setCroErrMsg(cro, "冲减单号:" + sub_seq + "已经冲减");
				return cro;
			}

			dao.delete(sub_seq);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;
		}

		return cro;
	}

	public CoreReturnObject update(java.util.HashMap hsMap)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceMnySubtDao dao = new SlmBalanceMnySubtDao();
		SlmBalanceMnySubtBean bean = SlmBalanceMnySubtBean
				.convertHashMapToBean(hsMap);
		String sub_seq = bean.getSub_seq();

		try
		{
			SlmBalanceMnySubtBean debean = dao
					.getSlmBalanceMnySubtBySubSeq(sub_seq);
			if (debean == null)
			{
				return cro;
			}

			if (debean.getStatus_cd().equals("1"))
			{
				super.setCroErrMsg(cro, "冲减单号:" + sub_seq + "已经确认");
				return cro;
			}

			if (debean.getStatus_cd().equals("2"))
			{
				super.setCroErrMsg(cro, "冲减单号:" + sub_seq + "已经冲减");
				return cro;
			}

			dao.update(bean);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}

		return cro;
	}

	public CoreReturnObject affrim(java.util.ArrayList list, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceMnySubtDao dao = new SlmBalanceMnySubtDao();
		DbTrans trans = new DbTrans();
		String sub_seq = "";
		try
		{
			trans.beginTransaction();
			dao.setTrans(trans);
			for (Object s : list)
			{
				sub_seq = s.toString();
				dao.affrim(sub_seq, person);
			}
			trans.commit();
			dao.setTrans(null);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		return cro;
	}

	public CoreReturnObject unaffrim(java.util.ArrayList list, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceMnySubtDao dao = new SlmBalanceMnySubtDao();
		DbTrans trans = new DbTrans();
		String sub_seq = "";
		try
		{
			trans.beginTransaction();
			dao.setTrans(trans);
			for (Object s : list)
			{
				sub_seq = s.toString();
				dao.unaffrim(sub_seq, person);

			}
			trans.commit();
			dao.setTrans(null);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		return cro;
	}

	public CoreReturnObject credit(String invoiceNo, String person,
			java.util.Hashtable hs) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{
			slmOrderLineTgDao.affrimFinallBalPriceOrg("", person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

}
