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.common.StringTool;
import com.steering.mes.mcp.entity.YdmBcScrapfrimEntity;

public class FrmScrapApplyFrim extends DBCommon{

	/***
	 * 查询已申请废品信息
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject doQueryApply(ArrayList<String> parmList,String chkValue,String isFast,String[] validDataPurviewIds)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApplyFrim.doQueryApply");
		if(!parmList.get(0).equals(""))
		{
			sql += " AND T.BAL_YEAR_MONTH = '"+parmList.get(0)+"'";
		}
		if(!parmList.get(1).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(1)+"'";
		}
		if(!parmList.get(2).equals(""))
		{
		    sql += " AND T.FRIM_TIME >=TO_DATE('" + parmList.get(2)
			+ "', 'YYYY-MM-DD HH24:MI:SS')AND T.FRIM_TIME <=TO_DATE('"
			+ parmList.get(3) + "', 'YYYY-MM-DD HH24:MI:SS') ";	
		}
		sql += " AND T.FRIM_FLAG = '"+chkValue+"'";
		if("1".equals(isFast)){//是快速处理----（各部门）
			sql += " AND T.IS_FAST = '"+isFast+"'";
			sql += CoreConverter.toSqlConditionString("T.BELONG_CODE_RESPONSE", validDataPurviewIds);
		}
		return this.getData(sql);
	}
	/***
	 * 审核成功
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject frimScarpApply(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		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 bathYear = df.format(now);
		String bathYear = "";//改25号结转
		String flag = "";
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				YdmBcScrapfrimEntity ydmScrapEntity = EntityTool.getEntityByJSONStr(str, YdmBcScrapfrimEntity.class);
				bathYear = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(ydmScrapEntity.getPlineCode()));//改25号结转
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.getFrimFlag");
				sql = EntityTool.fillSqlByEntity(sql, ydmScrapEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					flag = rs.getString("FRIM_FLAG");
				}
				rs.close();
				psta.close();
				if(!flag.equals("0"))
				{
					cro.setV_errMsg("申请信息已审核,不允许再次审核!");
					return cro;
				}
				ydmScrapEntity.setBalYearMonth(bathYear);
				String updateYdmScrapSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApplyFrim.frimScarpApply");
				updateYdmScrapSql = EntityTool.fillSqlByEntity(updateYdmScrapSql, ydmScrapEntity);
				sqllist.add(updateYdmScrapSql);
			}
			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;
	}
	/***
	 * 审核不通过
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject frimScarpApplyNot(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		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 bathYear = df.format(now);
		String bathYear = "";//改25号结转
		String flag = "";
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				YdmBcScrapfrimEntity ydmScrapEntity = EntityTool.getEntityByJSONStr(str, YdmBcScrapfrimEntity.class);
				bathYear = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(ydmScrapEntity.getPlineCode()));//改25号结转
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.getFrimFlag");
				sql = EntityTool.fillSqlByEntity(sql, ydmScrapEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					flag = rs.getString("FRIM_FLAG");
				}
				rs.close();
				psta.close();
				if(!flag.equals("0"))
				{
					cro.setV_errMsg("申请信息已审核,不允许再次审核!");
					return cro;
				}
				ydmScrapEntity.setBalYearMonth(bathYear);
				String updateYdmScrapSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApplyFrim.frimScarpApplyNot");
				updateYdmScrapSql = EntityTool.fillSqlByEntity(updateYdmScrapSql, ydmScrapEntity);
				sqllist.add(updateYdmScrapSql);
			}
			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 cancelFrimScrapApply(ArrayList<String> parmList,String date)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		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 bathYear = df.format(now);
		String flag = "";
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				YdmBcScrapfrimEntity ydmBsEntity = EntityTool.getEntityByJSONStr(str, YdmBcScrapfrimEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.getFrimFlag");
				sql = EntityTool.fillSqlByEntity(sql, ydmBsEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					flag = rs.getString("FRIM_FLAG");
				}
				rs.close();
				psta.close();
				if(flag.equals("0")||flag.equals("2"))
				{
					cro.setV_errMsg("申请信息未审核或者审核不通过,不允许撤销审核!");
					return cro;
				}
				
				//判断是否为当月
				if(!date.equals(bathYear))
				{
					cro.setV_errMsg("废品审核与撤销必须在当月才允许撤销审核!");
					return cro;
				}
				String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApplyFrim.cancelFrimScrapApply");
				deleteSql = EntityTool.fillSqlByEntity(deleteSql, ydmBsEntity);
				sqllist.add(deleteSql);
			}
			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;
	}
}
