package com.steering.ydm.bc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.QcmZgJugdeApplyEntity;

public class FrmApplyCode extends DBCommon{

	/***
	 * 查询送判信息
	 * @return
	 */
	public CoreReturnObject doQueryApplyJudge(ArrayList<String> list,String startTim,String endTim)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmApplyCode.doQueryApplyJudge");
		if(!list.get(0).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+list.get(0)+"'";
		}
		if(!startTim.equals(""))
		{
			sql += " AND T.APPLY_TIME >= TO_DATE('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND T.APPLY_TIME <= TO_DATE('"+endTim+"','YYYY-MM-DD HH24:MI:SS') ";
		}
		if(!list.get(1).equals(""))
		{
			sql += " AND T.JUDGE_RESULT_CODE = '"+list.get(1)+"'";
		}
		if(!list.get(2).equals(""))
		{
			sql += " AND T.JUDGE_TOLRESULT_CODE = '"+list.get(2)+"'";
		}
		return this.getData(sql);
	}
	/***
	 * 作废
	 * @return
	 */
	public CoreReturnObject scrap(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				QcmZgJugdeApplyEntity qcmEntity = EntityTool.getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
				String updateSql = CoreXmlSqlParsersFactory.getSql("FrmApplyCode.updateQcmFlag");
				updateSql = EntityTool.fillSqlByEntity(updateSql, qcmEntity); 
				sqllist.add(updateSql);
			}
			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 {
				connection.rollback(); // 回滚事务
			} catch (Exception e2) {
			}
		}
	} finally {
		try {
			if (connection != null && !connection.isClosed()) {
				connection.close();
			}
		} catch (Exception e2) {
		}
	}
	return cro;
	}
	/***
	 * 恢复
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject cancelScrap(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				QcmZgJugdeApplyEntity qcmEntity = EntityTool.getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
				String updateSql = CoreXmlSqlParsersFactory.getSql("FrmApplyCode.cancelScrap");
				updateSql = EntityTool.fillSqlByEntity(updateSql, qcmEntity); 
				sqllist.add(updateSql);
			}
			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 {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/***
	 * 获取工序判定结果
	 * @return
	 */
	public CoreReturnObject getJudgeResult()
	{
		return this.getDataBySqlID("FrmApplyCode.getJudgeResult");
	}
	
	
}
