package com.steering.ydm.bc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.YdmBcInlistEntity;
import com.steering.mes.mcp.entity.YdmBcOutlistEntity;
import com.steering.mes.mcp.entity.YdmGpOutlistEntity;

public class FrmGateFrim extends DBCommon{

	/***
	 * 查询管坯倒库信息(读卡)
	 * @param outDoc
	 * @return
	 */
	public CoreReturnObject doQueryGpOutList(String outDoc)
	{
		return this.getDataBySqlID("FrmGateFrim.doQueryGpOutList", new Object[]{outDoc});
	}
	/***
	 * 查询钢管倒库信息(读卡)
	 * @param outDoc
	 * @return
	 */
	public CoreReturnObject doQueryBcOutList(String outDoc)
	{
		return this.getDataBySqlID("FrmGateFrim.doQueryBcOutList", new Object[]{outDoc});
	}
	
	
	/***
	 * 查询管坯倒库信息
	 * @param outDoc
	 * @return
	 */
	public CoreReturnObject doQueryGpOutList1(ArrayList<String> parm)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.doQueryGpOutList1");
		if(!parm.get(0).equals(""))
		{
			sql += " AND OUTSTOCK_TIME >= TO_DATE('"
				+ parm.get(0)
				+ "','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME <= TO_DATE('"
				+ parm.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND JUDGE_STOVE_NO = '"+parm.get(2)+"'";
		}
		if(!parm.get(3).equals(""))
		{
			sql += " AND OUTSTOCK_DOC = '"+parm.get(3)+"'";
		}
		sql += " group by OUTSTOCK_PLAN_NO,OUTSTOCK_DOC,OUTSTOCK_TYPE_CODE,OUTSTOCK_TYPE_NAME, VEHICLE_NO,STOVE_NO,FLAG_STOVE_NO,JUDGE_STOVE_NO,";
		sql += " MATERIAL_NO,MATERIAL_NAME,GRADECODE,GRADENAME,STD_CODE,";
		sql += " STD_NAME,DIMATER,ACT_LEN,FIX_NUM,FIX_LEN,PLINE_CODE,PLINE_NAME,";
		sql += " BELONG_CODE,BELONG_NAME,STORAGE_NO,TAR_STORAGE_NO,TAR_REC_FLAG,MEMO,CREATE_NAME,PRO_PLAN_ID_ZG,GX_PLAN_NO_ZG";
		return this.getData(sql);
	}
	/***
	 * 查询钢管倒库信息
	 * @param outDoc
	 * @return
	 */
	public CoreReturnObject doQueryBcOutList1(ArrayList<String> parmList)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.doQueryBcOutList1");
    	if(!parmList.get(0).equals(""))
    	{
    		sql += " AND OUTSTOCK_TIME >= TO_DATE('"
				+ parmList.get(0)
				+ "','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME <= TO_DATE('"
				+ parmList.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
    	}
    	if(!parmList.get(2).equals(""))
    	{
    		sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(2)+"'";
    	}
    	if(!parmList.get(3).equals(""))
    	{
    		sql += " AND T.OUTSTOCK_DOC = '"+parmList.get(3)+"'";
    	}
    	sql += " GROUP BY T.OUTSTOCK_DOC,T.ICCAR_NO,T.VEHICLE_NO,T.LOT_NO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,";
    	sql += " T.PRO_ORDER_NO,T.PLINE_CODE,T.PLINE_NAME,T.BELONG_CODE,T.BELONG_NAME,T.STORAGE_NO,T.TAR_STORAGE_NO,T.DATA_SUORCE,T.QCM_STAUS,T.TAR_REC_FLAG";
    	return this.getData(sql);
	}
	
	/***
	 * 查询管坯倒库信息
	 * @param outDoc
	 * @return
	 */
	public CoreReturnObject doQueryGpOutList2(ArrayList<String> parm,String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.doQueryGpOutList2");
		sql += CoreConverter.toSqlConditionString("LEAVE_MANAGEMENT_NO", arr);
		if(!parm.get(0).equals(""))
		{
			sql += " AND OUTSTOCK_TIME >= TO_DATE('"
				+ parm.get(0)
				+ "','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME <= TO_DATE('"
				+ parm.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!parm.get(2).equals(""))
		{
			sql += " AND JUDGE_STOVE_NO = '"+parm.get(2)+"'";
		}
		if(!parm.get(3).equals(""))
		{
			sql += " AND OUTSTOCK_DOC = '"+parm.get(3)+"'";
		}
		sql += " group by OUTSTOCK_PLAN_NO,OUTSTOCK_DOC,OUTSTOCK_TYPE_CODE,OUTSTOCK_TYPE_NAME, VEHICLE_NO,STOVE_NO,FLAG_STOVE_NO,JUDGE_STOVE_NO,";
		sql += " MATERIAL_NO,MATERIAL_NAME,GRADECODE,GRADENAME,STD_CODE,";
		sql += " STD_NAME,DIMATER,ACT_LEN,FIX_NUM,FIX_LEN,PLINE_CODE,PLINE_NAME,";
		sql += " BELONG_CODE,BELONG_NAME,STORAGE_NO,TAR_STORAGE_NO,TAR_REC_FLAG,MEMO,CREATE_NAME,PRO_PLAN_ID_ZG,GX_PLAN_NO_ZG";
		return this.getData(sql);
	}
	/***
	 * 查询钢管倒库信息
	 * @param outDoc
	 * @return
	 */
	public CoreReturnObject doQueryBcOutList2(ArrayList<String> parmList,String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.doQueryBcOutList2");
		sql += CoreConverter.toSqlConditionString("T.LEAVE_MANAGEMENT_NO", arr);
    	if(!parmList.get(0).equals(""))
    	{
    		sql += " AND OUTSTOCK_TIME >= TO_DATE('"
				+ parmList.get(0)
				+ "','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME <= TO_DATE('"
				+ parmList.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
    	}
    	if(!parmList.get(2).equals(""))
    	{
    		sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(2)+"'";
    	}
    	if(!parmList.get(3).equals(""))
    	{
    		sql += " AND T.OUTSTOCK_DOC = '"+parmList.get(3)+"'";
    	}
    	sql += " GROUP BY T.OUTSTOCK_DOC,T.ICCAR_NO,T.VEHICLE_NO,T.LOT_NO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,";
    	sql += " T.PRO_ORDER_NO,T.PLINE_CODE,T.PLINE_NAME,T.BELONG_CODE,T.BELONG_NAME,T.STORAGE_NO,T.TAR_STORAGE_NO,T.DATA_SUORCE,T.QCM_STAUS,T.TAR_REC_FLAG";
    	return this.getData(sql);
	}
	/***
	 * 确认出厂
	 * @return
	 */
	public CoreReturnObject doComfrim(String ydmBcTity,String ydmGpTity,String userName,String manageNo,String manageMent)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
        try {
        	ArrayList<String> sqllist = new ArrayList<String>();
        	if(!ydmBcTity.equals(""))
        	{
        		YdmBcOutlistEntity ydmBcEntity = EntityTool.getEntityByJSONStr(ydmBcTity, YdmBcOutlistEntity.class);
        		ydmBcEntity.setLeaveFactoryName(userName);
        		ydmBcEntity.setLeaveManagementNo(manageNo);
        		ydmBcEntity.setLeaveManagementName(manageMent);
        		String updateBcOutSql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.updateYdmBcLeaveFlag");
        		updateBcOutSql = EntityTool.fillSqlByEntity(updateBcOutSql, ydmBcEntity);
        		sqllist.add(updateBcOutSql);
        	}
        	if(!ydmGpTity.equals(""))
        	{
        		YdmGpOutlistEntity ydmGpEntity = EntityTool.getEntityByJSONStr(ydmGpTity, YdmGpOutlistEntity.class);
        		ydmGpEntity.setLeaveFactoryName(userName);
        		ydmGpEntity.setLeaveManagementNo(manageNo);
        		ydmGpEntity.setLeaveManagementName(manageMent);
        		String updateGpOutSql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.updateYdmGpLeaveFlag");
        		updateGpOutSql = EntityTool.fillSqlByEntity(updateGpOutSql, ydmGpEntity);
        		sqllist.add(updateGpOutSql);
        	}
        	cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
        	if(cro.getV_errMsg().equals(""))
        	{
        		cro.setV_errMsg("确认出厂成功!");
        	}
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务 
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
	/***
	 * 撤销确认出厂
	 * @return
	 */
	public CoreReturnObject cancelComfrim(String ydmBcTity,String ydmGpTity)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
        try {
        	ArrayList<String> sqllist = new ArrayList<String>();
        	if(!ydmBcTity.equals(""))
        	{
        		YdmBcOutlistEntity ydmBcEntity = EntityTool.getEntityByJSONStr(ydmBcTity, YdmBcOutlistEntity.class);
        		
        		String updateBcOutSql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.cancelUpdateYdmBcLeaveFlag");
        		updateBcOutSql = EntityTool.fillSqlByEntity(updateBcOutSql, ydmBcEntity);
        		sqllist.add(updateBcOutSql);
        	}
        	if(!ydmGpTity.equals(""))
        	{
        		YdmGpOutlistEntity ydmGpEntity = EntityTool.getEntityByJSONStr(ydmGpTity, YdmGpOutlistEntity.class);
        		
        		String updateGpOutSql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim.cancelUpdateYdmGpLeaveFlag");
        		updateGpOutSql = EntityTool.fillSqlByEntity(updateGpOutSql, ydmGpEntity);
        		sqllist.add(updateGpOutSql);
        	}
        	cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
        	if(cro.getV_errMsg().equals(""))
        	{
        		cro.setV_errMsg("撤销确认成功!");
        	}
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务 
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
}
