package core.stlmes.server.sale.dao;

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

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.judge.model.HttCrackDetectionParaEntity;

import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.po.YdmZcBillMBean;
import core.stlmes.server.sale.po.YdmZcBillMNewEntity;

public class YdmZcBillMDao extends BaseService
{
	private final String Bill_no = "Bill_no";

	private List<YdmZcBillMBean> query(String sqlCondition) throws SQLException
	{
		List<YdmZcBillMBean> list = null;
		String sqlStr = " select * from YDM_ZC_BILL_M where 1 = 1 and VALIDFLAG = '1' ";
		sqlStr = sqlStr + sqlCondition;
		java.sql.ResultSet rs = super.executeForResult(sqlStr);
		list = YdmZcBillMBean.convertResultSetToBeanList(rs);
		rs.close();
		return list;
	}

	public List<YdmZcBillMBean> getYdmZcBillMByBillNo(String billNo)
			throws SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(Bill_no,
				billNo);
		return query(sqlCondition);

	}
	
	public List<YdmZcBillMNewEntity> getYdmZcBillMByBillNoNew(String billNo,java.sql.Connection conn)
			throws SQLException
	{
		SqlBuilder b = null;
		b = new SqlBuilder("SlmBalanceQuery.getYdmZcBillMByBillNoNew");
		b.Fill(new String[]{ billNo });
		List<YdmZcBillMNewEntity> tmpDetects = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), conn, YdmZcBillMNewEntity.class);
		return tmpDetects;
	}

	public List<YdmZcBillMBean> getCloseYdmZcBillMByBillNo(String billNo)
			throws SQLException
	{
		String sqlCondition = super.GetSqlConditionByStringValue(Bill_no,
				billNo);
		sqlCondition += " and bill_status = '4' ";
		return query(sqlCondition);

	}

	public CoreReturnObject updateBillStatusToAccept(String billNo)
			throws SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set Bill_status = '5'  "
				+ " where Bill_no = ? and Bill_status = '4'  and VALIDFLAG = '1'";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		cro = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return cro;

	}

	public CoreReturnObject updateBillStatusToAccept(java.sql.Connection conn,
			String billNo) throws SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set Bill_status = '5'  "
				+ " where Bill_no = ? and Bill_status = '4'  and VALIDFLAG = '1'";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		PreparedStatement ps = conn.prepareStatement(sqlStr);
		ps.executeUpdate();
		ps.close();
		return cro;

	}

	public CoreReturnObject updateVirtualBillStatusToAccept(
			java.sql.Connection conn, String billNo) throws SQLException,
			java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set ispreclose = '2'  "
				+ " where Bill_no = ? and ispreclose = '1'  and VALIDFLAG = '1'";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		PreparedStatement ps = conn.prepareStatement(sqlStr);
		ps.executeUpdate();
		ps.close();
		return cro;

	}

	public CoreReturnObject updateVirtualBillStatusToAccept(String billNo)
			throws SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set ispreclose = '2'  "
				+ " where Bill_no = ? and ispreclose = '1'  and VALIDFLAG = '1'";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		cro = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return cro;

	}

	public String updateAcceptBillStatusToNoAcceptSql(String billNo)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set Bill_status = '4'  "
				+ " where Bill_no = ? and Bill_status = '5' and VALIDFLAG = '1' ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		// cro = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return sqlStr;

	}

	public CoreReturnObject updateVirtualAcceptBillStatusToNoAccept(
			String billNo) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set ispreclose = '1'  "
				+ " where Bill_no = ? and ispreclose = '2' and VALIDFLAG = '1' ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		cro = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return cro;

	}

	public String updateVirtualAcceptBillStatusToNoAcceptSql(String billNo)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set ispreclose = '1'  "
				+ " where Bill_no = ? and ispreclose = '2' and VALIDFLAG = '1' ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ billNo });
		// cro = super.ExcuteNonQueryIgnoreTrans(sqlStr);
		return sqlStr;

	}

	public String updateRealInvoiceToBillBySavingRealInvoiceForVirtualBill(
			String realInvoiceNo, String askPlanId, String ordpk,
			String ordlpk, String ord_ln_dly_pk)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set realinvoiceno = ?,"
				+ " realinvoiceno_date = sysdate   "
				+ " where 1=1 and  "
				+ " Bill_no = ? and VALIDFLAG = '1' "
				+ " and ORD_PK = ? and ORD_LN_PK = ?  and  ORD_LN_DLY_PK = ?   ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ realInvoiceNo, askPlanId, ordpk, ordlpk, ord_ln_dly_pk });

		return sqlStr;

	}

	public String updateRealInvoiceToBillBySavingRealFristInvoiceForVirtualBill(
			String realInvoiceNo, String askPlanId, String ordpk,
			String ordlpk, String ord_ln_dly_pk)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String sqlStr = "update  YDM_ZC_BILL_M set realinvoiceno1 = ?,"
				+ " realinvoiceno_date = sysdate   " + " where 1=1  and  "
				+ " Bill_no = ? and VALIDFLAG = '1' "
				+ " and ORD_PK = ? and ORD_LN_PK = ?  and  ORD_LN_DLY_PK = ?  ";
		sqlStr = super.getPreparedSQL(sqlStr, new Object[]
		{ realInvoiceNo, askPlanId, ordpk, ordlpk, ord_ln_dly_pk });

		return sqlStr;

	}

	public String updateBillTicketStatusToAline(String billNO, String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set BILL_TICKET_STATUS = '1' ,"
				+ "     BILL_TICKET_AFFRIM_NAME = ? ,BILL_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ?  and  VALIDFLAG = '1' and  ( BILL_STATUS = '4' or ISPRECLOSE = '1')"
				+ "     and BILL_TICKET_STATUS = '0' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		return sql;

	}

	public String updateBillTicketStatusToNoAline(String billNO, String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set BILL_TICKET_STATUS = '0' ,"
				+ "     BILL_TICKET_AFFRIM_NAME = null ,BILL_TICKET_AFFRIM_TIME = null  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'  and   BILL_TICKET_STATUS = '1' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		return sql;

	}

	public void updateBillTicketStatusToAccept(String billNO, String person,
			java.sql.Connection conn) throws java.sql.SQLException,
			java.lang.Exception
	{
		String sql = "  update  YDM_ZC_BILL_M set BILL_TICKET_STATUS = '2' ,"
				+ "     BILL_TICKET_AFFRIM_NAME = ? ,BILL_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ?  and  VALIDFLAG = '1' and  ( BILL_STATUS = '4' or BILL_STATUS = '5' or ISPRECLOSE = '1'"
				+ "     or ISPRECLOSE = '2' )"
				+ "     and BILL_TICKET_STATUS = '1' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	public void updateBillTicketStatusToNoAccept(String billNO, String person,
			java.sql.Connection conn) throws java.sql.SQLException,
			java.lang.Exception
	{
		String sql = "  update  YDM_ZC_BILL_M set BILL_TICKET_STATUS = '1' ,"
				+ "     BILL_TICKET_AFFRIM_NAME = ? ,BILL_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ?  and  VALIDFLAG = '1' and  ( BILL_STATUS = '4' or BILL_STATUS = '5' or ISPRECLOSE = '1'"
				+ "     or ISPRECLOSE = '2' ) "
				+ "     and BILL_TICKET_STATUS = '2' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	public String updateBillTicketStatusToNoAccept(String billNO, String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set BILL_TICKET_STATUS = '1' ,"
				+ "     BILL_TICKET_AFFRIM_NAME = ? ,BILL_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ?  and  VALIDFLAG = '1' and  ( BILL_STATUS = '4' or BILL_STATUS = '5' or ISPRECLOSE = '1'"
				+ "     or ISPRECLOSE = '2' ) "
				+ "     and BILL_TICKET_STATUS = '2' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		return sql;

	}

	public String updateRealOutStockTicketStatusToAline(String billNO,
			String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set OUTSTOCK_TICKET_STATUS = '1' ,"
				+ "     OUTSTOCK_TICKET_AFFRIM_NAME = ? ,OUTSTOCK_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and OUTSTOCK_TICKET_STATUS = '0' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		return sql;

	}

	public void updateRealOutStockTicketStatusToAccept(String billNO,
			String person, java.sql.Connection conn)
			throws java.sql.SQLException,java.lang.Exception
	{
		String sql = "  update  YDM_ZC_BILL_M set OUTSTOCK_TICKET_STATUS = '2' ,"
				+ "     OUTSTOCK_TICKET_AFFRIM_NAME = ? ,OUTSTOCK_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and OUTSTOCK_TICKET_STATUS = '1' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	public void updateRealOutStockTicketStatusToNoAccept(String billNO,
			String person, java.sql.Connection conn)
			throws java.sql.SQLException,java.lang.Exception
	{
		String sql = "  update  YDM_ZC_BILL_M set OUTSTOCK_TICKET_STATUS = '1' ,"
				+ "     OUTSTOCK_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and OUTSTOCK_TICKET_STATUS = '2' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	public String updateRealOutStockTicketStatusToNoAcceptSql(String billNO,
			String person)

	{
		String sql = "  update  YDM_ZC_BILL_M set OUTSTOCK_TICKET_STATUS = '1' ,"
				+ "     OUTSTOCK_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and OUTSTOCK_TICKET_STATUS = '2' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		return sql;

	}

	public String updateRealOutStockTicketStatusToNoAline(String billNO,
			String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set OUTSTOCK_TICKET_STATUS = '0' ,"
				+ "     OUTSTOCK_TICKET_AFFRIM_NAME = null ,OUTSTOCK_TICKET_AFFRIM_TIME = null  "
				+ "     where BILL_NO = ?  and  VALIDFLAG = '1' and   OUTSTOCK_TICKET_STATUS = '1' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		return sql;

	}

	public String updateRealTransmnyTicketStatusToAline(String billNO,
			String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set TRANSMNY_TICKET_STATUS = '1' ,"
				+ "     TRANSMNY_TICKET_AFFRIM_NAME = ? ,TRANSMNY_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and TRANSMNY_TICKET_STATUS = '0' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ person, billNO });
		return sql;

	}

	public String updateRealTransmnyTicketStatusToNoAline(String billNO,
			String person)
	{
		String sql = "  update  YDM_ZC_BILL_M set TRANSMNY_TICKET_STATUS = '0' ,"
				+ "     TRANSMNY_TICKET_AFFRIM_NAME = null ,TRANSMNY_TICKET_AFFRIM_TIME = null  "
				+ "     where BILL_NO = ?  and  VALIDFLAG = '1' and  TRANSMNY_TICKET_STATUS = '1' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		return sql;

	}

	public void updateRealTransmnyTicketStatusToAccept(String billNO,
			String person, java.sql.Connection conn)
			throws java.sql.SQLException,java.lang.Exception
	{
		String sql = "  update  YDM_ZC_BILL_M set TRANSMNY_TICKET_STATUS = '2' ,"
				+ "     TRANSMNY_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and TRANSMNY_TICKET_STATUS = '1' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	public void updateRealTransmnyTicketStatusToNoAccept(String billNO,
			String person, java.sql.Connection conn)
			throws java.sql.SQLException,java.lang.Exception
	{
		String sql = "  update  YDM_ZC_BILL_M set TRANSMNY_TICKET_STATUS = '1' ,"
				+ "     TRANSMNY_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and TRANSMNY_TICKET_STATUS = '2' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	public String updateRealTransmnyTicketStatusToNoAcceptSql(String billNO,
			String person)

	{
		String sql = "  update  YDM_ZC_BILL_M set TRANSMNY_TICKET_STATUS = '1' ,"
				+ "     TRANSMNY_TICKET_AFFRIM_TIME = sysdate  "
				+ "     where BILL_NO = ? and  VALIDFLAG = '1'   and TRANSMNY_TICKET_STATUS = '2' ";
		sql = super.getPreparedSQL(sql, new Object[]
		{ billNO });
		return sql;

	}

}
