package com.steering.ydm.bc;

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

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;

public class FrmScrapApply extends DBCommon{
	/***
	 * 查询废品库存信息
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject doQuery(ArrayList<String> parmList,String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQuery");
		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)+"'";
		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", arr);
		sql += " GROUP BY T.PRODUCT_FLAG,T.MAT_STATUS,T.LOT_NO,T.JUDGE_STOVE_NO,T.STOVE_NO,T.MEMO,T.FLAG_STOVE_NO_ZG,T1.ORDER_TYP,";
		sql += " T.PRO_ORDER_ID,T.QCM_STAUS,T.PRO_ORDER_NO,T.PRO_PLAN_ID_NEXT,T.GX_PLAN_NO_NEXT,S.STORAGE_NO,T.JUDGE_RESULT,T.MATERIAL_NO,T.MATERIAL_NAME,";
		sql += " T.PROCESS_CDOE,T.PROCESS_DESC,T.PLINE_CODE,T.PLINE_NAME,T.BELONG_CODE,T.BELONG_NAME,T.GX_PLAN_NO,T.SCRAP_FRIM_FLAG,T.SCRAP_REASON,T.SOURE_CODE,T.SOURE_NAME,T.BAL_YEAR_MONTH_FAIL";
		return this.getData(sql);
	}
	
	/***
	 * 查询已申请废品信息
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject doQueryApply(ArrayList<String> parmList,String[] arr,String chkValue)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.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(chkValue.equals("0"))
		{
		    sql += " AND T.FRIM_FLAG = '0'";	
		}else if(chkValue.equals("1"))
		{
			sql += " AND T.FRIM_FLAG IN('1','2')";	
		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", arr);
		return this.getData(sql);
	}
	/***
	 * 申请审核
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject applyFrim(ArrayList<String> parmList,String userName)
	{
		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 acount = "";
		String applyCode = "";//申请单号
		String wgt = "";//重量
		String craftPath = "";
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str:parmList)
			{
				MatBcMEntity matBcEntity = EntityTool.getEntityByJSONStr(str, MatBcMEntity.class);
				bathYear = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(matBcEntity.getPlineCode())); //改25号结转
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryApplyCount");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(matBcEntity.getActCount())> Integer.parseInt(acount))
				{
					cro.setV_errMsg("该库存未申请支数大于库存支数,请刷新页面!");
					return cro;
				}
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryApplyWgt");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					wgt = rs.getString("AWGT");
				}
				rs.close();
				psta.close();
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryCraftPath");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					craftPath = rs.getString("CRAFT_PATH");
				}
				rs.close();
				psta.close();
				
				
				//applyCode = getApplyCode(matBcEntity.getPlineCode(),connection);
				YdmBcScrapfrimEntity ydmBcFrimEntity = new YdmBcScrapfrimEntity();
				//ydmBcFrimEntity.setFailApprovalCode(applyCode);
				ydmBcFrimEntity.setLotNo(matBcEntity.getLotNo());
				ydmBcFrimEntity.setStoveNo(matBcEntity.getStoveNo());
				ydmBcFrimEntity.setJudgeStoveNo(matBcEntity.getJudgeStoveNo());
				ydmBcFrimEntity.setBatchNo(matBcEntity.getBatchNo());
				ydmBcFrimEntity.setBatchGroudNo(matBcEntity.getBatchGroudNo());
				ydmBcFrimEntity.setMaterialNo(matBcEntity.getMaterialNo());
				ydmBcFrimEntity.setMaterialName(matBcEntity.getMaterialName());
				ydmBcFrimEntity.setProduccode(matBcEntity.getProduccode());
				ydmBcFrimEntity.setProducname(matBcEntity.getProducname());
				ydmBcFrimEntity.setSteelcode(matBcEntity.getSteelcode());
				ydmBcFrimEntity.setSteelname(matBcEntity.getSteelname());
				ydmBcFrimEntity.setSpecCode(matBcEntity.getSpecCode());
				ydmBcFrimEntity.setSpecName(matBcEntity.getSpecName());
				ydmBcFrimEntity.setModelCode(matBcEntity.getModelCode());
				ydmBcFrimEntity.setModelDesc(matBcEntity.getModelDesc());
				ydmBcFrimEntity.setStdStyle(matBcEntity.getStdStyle());
				ydmBcFrimEntity.setStdStyleDesc(matBcEntity.getStdStyleDesc());
				ydmBcFrimEntity.setStdCode(matBcEntity.getStdCode());
				ydmBcFrimEntity.setStdName(matBcEntity.getStdName());
				ydmBcFrimEntity.setProOrderNo(matBcEntity.getProOrderNo());
				ydmBcFrimEntity.setProcessCdoe(matBcEntity.getProcessCdoe());
				ydmBcFrimEntity.setProcessDesc(matBcEntity.getProcessDesc());
				ydmBcFrimEntity.setPlineCode(matBcEntity.getPlineCode());
				ydmBcFrimEntity.setPlineName(matBcEntity.getPlineName());
				ydmBcFrimEntity.setActDimater(matBcEntity.getActDimater());
				ydmBcFrimEntity.setActHeight(matBcEntity.getActHeight());
				ydmBcFrimEntity.setActLenMin(matBcEntity.getActLenMin());
				ydmBcFrimEntity.setActLenMax(matBcEntity.getActLenMax());
				ydmBcFrimEntity.setActCount(matBcEntity.getActCount());
				ydmBcFrimEntity.setActWeight(wgt);
				ydmBcFrimEntity.setActLen(matBcEntity.getActLen());
				ydmBcFrimEntity.setBelongCode(matBcEntity.getBelongCode());
				ydmBcFrimEntity.setBelongName(matBcEntity.getBelongName());
				ydmBcFrimEntity.setScrapReason(matBcEntity.getScrapReason());
				ydmBcFrimEntity.setTarget(matBcEntity.getTarget());
				ydmBcFrimEntity.setGradecode(matBcEntity.getGradecode());
				ydmBcFrimEntity.setGradename(matBcEntity.getGradename());
				ydmBcFrimEntity.setCreateName(userName);
				ydmBcFrimEntity.setBalYearMonth(bathYear);
				ydmBcFrimEntity.setCraftNo(matBcEntity.getCrafNo());
				ydmBcFrimEntity.setCraftPath(craftPath);
				ydmBcFrimEntity.setBelongCodeResponse(matBcEntity.getBelongCodeResponse());
				ydmBcFrimEntity.setBelongNameResponse(matBcEntity.getBelongNameResponse());
				ydmBcFrimEntity.setIsFast(matBcEntity.getIsFast());
				ydmBcFrimEntity.setScrapDesc(matBcEntity.getScrapDesc());
				ydmBcFrimEntity.setSoureCode(matBcEntity.getSoureCode());
				ydmBcFrimEntity.setSoureName(matBcEntity.getSoureName());

				String insertFrimSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.applyFrim");
				insertFrimSql = EntityTool.fillSqlByEntity(insertFrimSql, ydmBcFrimEntity);
				sqllist.add(insertFrimSql);
			}
			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 cancelApplyFrim(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 flag = "";
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str: parmList)
			{
				YdmBcScrapfrimEntity ydmBcEntity = EntityTool.getEntityByJSONStr(str, YdmBcScrapfrimEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.getFrimFlag");
				sql = EntityTool.fillSqlByEntity(sql, ydmBcEntity);
				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;
				}
				String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.cancelApplyFrim");
				deleteSql = EntityTool.fillSqlByEntity(deleteSql, ydmBcEntity);
				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;
	}
	/***
	 * 审核
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject frimScarp(ArrayList<String> parmList,String userName)
	{
		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 = "";
		String acount = "";
		String belongCodeReason = "";
		String belongNameReason = "";
		String wgt = "";//重量
		String craftPath = "";
		String applyCode = "";//申请单号
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				MatBcMEntity matBcEntity = EntityTool.getEntityByJSONStr(str, MatBcMEntity.class);
				bathYear = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(matBcEntity.getPlineCode())); //改25号结转
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.getApplyCode2");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					applyCode = rs.getString("FAIL_APPROVAL_CODE");
				}
				rs.close();
				psta.close();
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryApplyCount");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(matBcEntity.getActCount())> Integer.parseInt(acount))
				{
					cro.setV_errMsg("该库存未申请支数大于库存支数,请刷新页面!");
					return cro;
				}
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryApplyWgt");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					wgt = rs.getString("AWGT");
				}
				rs.close();
				psta.close();
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryCraftPath");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					craftPath = rs.getString("CRAFT_PATH");
				}
				rs.close();
				psta.close();
				
				
				//applyCode = getApplyCode(matBcEntity.getPlineCode(),connection);
				YdmBcScrapfrimEntity ydmBcFrimEntity = new YdmBcScrapfrimEntity();
				//ydmBcFrimEntity.setFailApprovalCode(applyCode);
				ydmBcFrimEntity.setLotNo(matBcEntity.getLotNo());
				ydmBcFrimEntity.setStoveNo(matBcEntity.getStoveNo());
				ydmBcFrimEntity.setJudgeStoveNo(matBcEntity.getJudgeStoveNo());
				ydmBcFrimEntity.setBatchNo(matBcEntity.getBatchNo());
				ydmBcFrimEntity.setBatchGroudNo(matBcEntity.getBatchGroudNo());
				ydmBcFrimEntity.setMaterialNo(matBcEntity.getMaterialNo());
				ydmBcFrimEntity.setMaterialName(matBcEntity.getMaterialName());
				ydmBcFrimEntity.setProduccode(matBcEntity.getProduccode());
				ydmBcFrimEntity.setProducname(matBcEntity.getProducname());
				ydmBcFrimEntity.setSteelcode(matBcEntity.getSteelcode());
				ydmBcFrimEntity.setSteelname(matBcEntity.getSteelname());
				ydmBcFrimEntity.setSpecCode(matBcEntity.getSpecCode());
				ydmBcFrimEntity.setSpecName(matBcEntity.getSpecName());
				ydmBcFrimEntity.setModelCode(matBcEntity.getModelCode());
				ydmBcFrimEntity.setModelDesc(matBcEntity.getModelDesc());
				ydmBcFrimEntity.setStdStyle(matBcEntity.getStdStyle());
				ydmBcFrimEntity.setStdStyleDesc(matBcEntity.getStdStyleDesc());
				ydmBcFrimEntity.setStdCode(matBcEntity.getStdCode());
				ydmBcFrimEntity.setStdName(matBcEntity.getStdName());
				ydmBcFrimEntity.setProOrderNo(matBcEntity.getProOrderNo());
				ydmBcFrimEntity.setProcessCdoe(matBcEntity.getProcessCdoe());
				ydmBcFrimEntity.setProcessDesc(matBcEntity.getProcessDesc());
				ydmBcFrimEntity.setPlineCode(matBcEntity.getPlineCode());
				ydmBcFrimEntity.setPlineName(matBcEntity.getPlineName());
				ydmBcFrimEntity.setActDimater(matBcEntity.getActDimater());
				ydmBcFrimEntity.setActHeight(matBcEntity.getActHeight());
				ydmBcFrimEntity.setActLenMin(matBcEntity.getActLenMin());
				ydmBcFrimEntity.setActLenMax(matBcEntity.getActLenMax());
				ydmBcFrimEntity.setActCount(matBcEntity.getActCount());
				ydmBcFrimEntity.setActWeight(wgt);
				ydmBcFrimEntity.setActLen(matBcEntity.getActLen());
				ydmBcFrimEntity.setBelongCode(matBcEntity.getBelongCode());
				ydmBcFrimEntity.setBelongName(matBcEntity.getBelongName());
				ydmBcFrimEntity.setScrapReason(matBcEntity.getScrapReason());
				ydmBcFrimEntity.setTarget(matBcEntity.getTarget());
				ydmBcFrimEntity.setGradecode(matBcEntity.getGradecode());
				ydmBcFrimEntity.setGradename(matBcEntity.getGradename());
				ydmBcFrimEntity.setCreateName(userName);
				ydmBcFrimEntity.setBalYearMonth(bathYear);
				ydmBcFrimEntity.setCraftNo(matBcEntity.getCrafNo());
				ydmBcFrimEntity.setCraftPath(craftPath);
				ydmBcFrimEntity.setBelongCodeResponse(matBcEntity.getPlineCode()); //责任单位是产线
				ydmBcFrimEntity.setBelongNameResponse(matBcEntity.getPlineName());
				ydmBcFrimEntity.setIsFast("0");
				ydmBcFrimEntity.setScrapDesc(matBcEntity.getScrapDesc());
				ydmBcFrimEntity.setFailApprovalCode(applyCode);
				ydmBcFrimEntity.setFrimFlag("1");
				ydmBcFrimEntity.setFrimName(userName);
				ydmBcFrimEntity.setSoureCode(matBcEntity.getSoureCode());
				ydmBcFrimEntity.setSoureName(matBcEntity.getSoureName());

				
				//厂内审核写入管材废品申请审核表
				String insertFrimSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.addApplyFrim");
				insertFrimSql = EntityTool.fillSqlByEntity(insertFrimSql, ydmBcFrimEntity);
				//sqllist.add(insertFrimSql);
				PreparedStatement pstaupdateOff = connection.prepareStatement(insertFrimSql);
				pstaupdateOff.addBatch();
				pstaupdateOff.executeBatch();
				
				matBcEntity.setScrapFrimFlag("0");
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryMatCount");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(matBcEntity.getActCount())>Integer.parseInt(acount))
				{
					cro.setV_errMsg("库存废品支数发生了变化,请刷新页面!");
					return cro;
				}
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryDepartMent");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCodeReason = rs.getString("DEPARTMENT_CODE_P");
					belongNameReason = rs.getString("DEPARTMENT_DESC_P");
				}
				rs.close();
				psta.close();
				
				matBcEntity.setBelongCodeResponse(belongCodeReason);
				matBcEntity.setBelongNameResponse(belongNameReason);
				matBcEntity.setBalYearMonth(bathYear);
				matBcEntity.setScrapName(userName);
				matBcEntity.setScrapTime(systime);
				matBcEntity.setFailApprovalCode(applyCode);
				
				matBcEntity.setScrapFrimFlag("2");
				String updateMatSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.frimScarp");
				updateMatSql = EntityTool.fillSqlByEntity(updateMatSql, matBcEntity);
				//sqllist.add(updateMatSql);
				PreparedStatement pstaupdateMat = connection.prepareStatement(updateMatSql);
				pstaupdateMat.addBatch();
				pstaupdateMat.executeBatch();
			}
			//cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("审核成功!");
			}
			connection.commit();
		} 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 cancelFrimScrap(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 = "";
		String acount = "";
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				MatBcMEntity matBcEntity = EntityTool.getEntityByJSONStr(str, MatBcMEntity.class);
				bathYear = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(matBcEntity.getPlineCode()));//改25号结转
				matBcEntity.setScrapFrimFlag("2");
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.doQueryMatCount");
				sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(matBcEntity.getActCount())>Integer.parseInt(acount))
				{
					cro.setV_errMsg("库存废品已审核支数发生了变化,请刷新页面!");
					return cro;
				}
				if(!bathYear.equals(matBcEntity.getBalYearMonthFail()))
				{
					cro.setV_errMsg("跨月的废品审核信息,不允许撤销审核!");
					return cro;
				}
				matBcEntity.setScrapFrimFlag("0");
				String updateMatSql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.cancelFrimScrap");
				updateMatSql = EntityTool.fillSqlByEntity(updateMatSql, matBcEntity);
				sqllist.add(updateMatSql);
			}
			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 plineCode
	 * @param connection
	 * @return
	 * @throws SQLException 
	 */
	private String getApplyCode(String plineCode, Connection connection) throws SQLException {
		String sql = "";
		String applyCode = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		sql = CoreXmlSqlParsersFactory.getSql("FrmScrapApply.getApplyCode");
		psta = connection.prepareStatement(sql);
		psta.setString(1, plineCode);
		psta.setString(2, plineCode);
		rs = psta.executeQuery();
		if(rs.next())
		{
			applyCode = rs.getString("FAIL_APPROVAL_CODE");
		}
		rs.close();
		psta.close();
		return applyCode;
	}

}
