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

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.SlmBalanceInfoOrgDao;
import core.stlmes.server.sale.dao.SlmBalanceTransMoneyDao;
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.SlmOrderLineTgBean;
import core.stlmes.server.sale.po.SlmOrderPriceListBean;
import core.stlmes.server.sale.po.YdmZcBillMBean;
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 YdmZcBillMgt extends BaseBll
{
	public YdmZcBillMgt()
	{

	}

	public CoreReturnObject makeTicketAline(java.util.ArrayList list,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new DbTrans();
		if (list == null) return cro;
		if (list.size() == 0) return cro;
		String[] temp = null;
		String billno = "";
		String billticketsStatus = "";
		String outstockstatus = "";
		String transmnyStatus = "";
		String sql = "";
		java.util.ArrayList listSql = new java.util.ArrayList();
		try
		{
			trans.beginTransaction();
			ydmZcBillMDao.setTrans(trans);
			for (Object obj : list)
			{
				temp = ( String[])obj;
				billno = temp[0];
				billticketsStatus = temp[1];
				outstockstatus = temp[2];
				transmnyStatus = temp[3];

				List<YdmZcBillMBean> listbill = ydmZcBillMDao
						.getYdmZcBillMByBillNo(billno);
				if (listbill == null || listbill.size() == 0) continue;
				if ("0".equals(billticketsStatus) && "0".equals(outstockstatus)
						&& "0".equals(transmnyStatus))
				{
					continue;

				}
				String billStatus = listbill.get(0).getBill_status();

				// 实单未关闭，不允许操作
				if ( ("0".equals(billStatus) || "1".equals(billStatus)
						|| "2".equals(billStatus) || "3".equals(billStatus) )
						&& "1".equals(listbill.get(0).getLoadvehicle_type()))
				{
					super.setCroErrMsg(cro, "提单" + billno + "未关闭,不允许操作");
					trans.commit();
					return cro;

				}
				// 虚单
				if ("0".equals(listbill.get(0).getIspreclose())
						&& "2".equals(listbill.get(0).getLoadvehicle_type()))
				{
					super.setCroErrMsg(cro, "提单" + billno + "未关闭,不允许操作");
					trans.commit();
					return cro;

				}

				java.util.ArrayList sqlList = getSqlStrByBillnoAndEveryStatus(
						billno, billticketsStatus, outstockstatus,
						transmnyStatus, person);
				listSql.addAll(sqlList);

			}
			if (listSql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(listSql);
			}

		}
		catch (java.sql.SQLException ex)
		{

		}
		finally
		{
			trans.commit();

		}

		return cro;

	}

	public CoreReturnObject makeTicketNoAline(java.util.ArrayList list,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new DbTrans();
		if (list == null) return cro;
		if (list.size() == 0) return cro;
		String[] temp = null;
		String billno = "";
		String billticketsStatus = "";
		String outstockstatus = "";
		String transmnyStatus = "";
		String sql = "";
		java.util.ArrayList listSql = new java.util.ArrayList();
		try
		{
			trans.beginTransaction();
			ydmZcBillMDao.setTrans(trans);
			for (Object obj : list)
			{
				temp = ( String[])obj;
				billno = temp[0];
				billticketsStatus = temp[1];
				outstockstatus = temp[2];
				transmnyStatus = temp[3];

				if ("0".equals(billticketsStatus) && "0".equals(outstockstatus)
						&& "0".equals(transmnyStatus))
				{
					continue;

				}

				List<YdmZcBillMBean> listbill = ydmZcBillMDao
						.getYdmZcBillMByBillNo(billno);
				if (listbill == null || listbill.size() == 0) continue;

				YdmZcBillMBean bean = listbill.get(0);

				if ("1".equals(billticketsStatus)
						&& "2".equals(bean.getBill_ticket_status()))
				{
					super.setCroErrMsg(cro, "提单" + billno + "货款已经结算接收，不允许取消");
					trans.rollback();
					return cro;

				}

				if ("1".equals(outstockstatus)
						&& "2".equals(bean.getOutstock_ticket_status()))
				{
					super.setCroErrMsg(cro, "提单" + billno + "出库费已经结算接收，不允许取消");
					trans.rollback();
					return cro;

				}

				if ("1".equals(transmnyStatus)
						&& "2".equals(bean.getTransmny_ticket_status()))
				{
					super.setCroErrMsg(cro, "提单" + billno + "运费已经结算接收，不允许取消");
					trans.rollback();
					return cro;

				}

				java.util.ArrayList sqlList = getSqlStrByBillnoAndEveryStatusNoAligh(
						billno, billticketsStatus, outstockstatus,
						transmnyStatus, person);
				listSql.addAll(sqlList);

			}
			if (listSql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(listSql);
			}

		}
		catch (java.sql.SQLException ex)
		{

		}
		finally
		{
			trans.commit();

		}

		return cro;

	}

	private java.util.ArrayList getSqlStrByBillnoAndEveryStatus(String billNo,
			String billticketsStatus, String outstockstatus,
			String transmnyStatus, String person)
	{
		java.util.ArrayList listSql = new java.util.ArrayList();
		YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
		String sql = "";
		if ("1".equals(billticketsStatus))
		{
			sql = ydmZcBillMDao.updateBillTicketStatusToAline(billNo, person);
			listSql.add(sql);

		}
		if ("1".equals(outstockstatus))
		{
			sql = ydmZcBillMDao.updateRealOutStockTicketStatusToAline(billNo,
					person);
			listSql.add(sql);

		}
		if ("1".equals(transmnyStatus))
		{
			sql = ydmZcBillMDao.updateRealTransmnyTicketStatusToAline(billNo,
					person);
			listSql.add(sql);

		}
		return listSql;
	}

	private java.util.ArrayList getSqlStrByBillnoAndEveryStatusNoAligh(
			String billNo, String billticketsStatus, String outstockstatus,
			String transmnyStatus, String person)
	{
		java.util.ArrayList listSql = new java.util.ArrayList();
		YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
		String sql = "";
		if ("1".equals(billticketsStatus))
		{
			sql = ydmZcBillMDao.updateBillTicketStatusToNoAline(billNo, person);
			listSql.add(sql);

		}
		if ("1".equals(outstockstatus))
		{
			sql = ydmZcBillMDao.updateRealOutStockTicketStatusToNoAline(billNo,
					person);
			listSql.add(sql);

		}
		if ("1".equals(transmnyStatus))
		{
			sql = ydmZcBillMDao.updateRealTransmnyTicketStatusToNoAline(billNo,
					person);
			listSql.add(sql);

		}
		return listSql;
	}
}
