package com.steering.mes.mcp.Mch;

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.comm.tools.StringTool;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgxMEntity;
import com.steering.mes.mcp.entity.MchHreadResultEntity;

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 java.util.Locale;

/**
 * 螺纹检验实绩
 * 
 * @author SZL
 *
 */
public class FrmThreadInspectResult extends DBCommon {
	private String perStuts = "";

	/**
	 * 查询螺纹检验实绩
	 */
	public CoreReturnObject getQueryExpandResult(String heatNo ,String BatchNo) {
		return this.getDataBySqlID("FrmThreadInspectResult.getQueryExpandResult",
				new Object[] { heatNo,BatchNo});
	}
	
	/**
	 * 查询相同炉计划ID下螺纹检验状态
	 */
	public CoreReturnObject getQueryExpandResultStuts(String heatNo,
			String plinCode) {
//		String sql = CoreXmlSqlParsersFactory.getSql("FrmThreadInspectResult.getQueryExpandResultStuts");
//		sql += "AND T.HEAT_PLAN_NO = '"+heatNo+"'";
//		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCode);
//		return this.getData(sql);
		return this.getDataBySqlID(
				"FrmThreadInspectResult.getQueryExpandResultStuts",
				new Object[] { heatNo, plinCode });
	}

	/**
	 * 查询螺纹检验从表实绩
	 */
	public CoreReturnObject getQueryJumDResult(String heatNo) {
		return this.getDataBySqlID(
				"FrmThreadInspectResult.getQueryExpandResult1",
				new Object[] { heatNo });
	}

	/**
	 * 查询螺纹检验实绩合格支数的和
	 * 
	 * @param heatNo
	 * @return
	 */
	public CoreReturnObject getQueryExpandResult1(String heatNo) {
		return this.getDataBySqlID(
				"FrmThreadInspectResult.getQueryExpandResult2",
				new Object[] { heatNo });
	}


	public CoreReturnObject DoAdd(String loadStr, String gx, ArrayList<String> ScrappedList, ArrayList<String> SuspiciousList){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false); //



			String sqlupdatepln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.updatePlnZyRclM2");// 修改加工计划表
			PreparedStatement patupdatepln = connection
					.prepareStatement(sqlupdatepln);

			String addPlnInfo = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.AddComInfoResultNew");// 新增公共信息
			PreparedStatement patsPlan = connection
					.prepareStatement(addPlnInfo);


			MchHreadResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchHreadResultEntity.class);


			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			// 螺纹检验暂时不卡炉计划状态    和生产厂操作互不影响。
		/*	if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}*/


	/*		String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();*/
			//endregion


			String resultNo = "";
			int count = 0;
			int detCount = 0;


			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}
			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}


			if(count<=0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}

			if(count<detCount){
				core.setV_errMsg("废品支数不能大于作业支数！");
				return core;
			}


			//获取最大实绩编号
			String sql =   CoreXmlSqlParsersFactory
					.getSql("FrmThreadInspectResult.queryResultNo");
			psta = connection.prepareStatement(sql);
			psta.setObject(1, pln.getPlineCode() + gx );
			psta.setObject(2, pln.getPlineCode() + gx );
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();


			Load.setResultNo(resultNo);

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmThreadInspectResult.AddResult");// 新增上料实绩

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, Load);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();


			// 新增加工公共信息
			patsPlan.setObject(1, resultNo);
			patsPlan.setObject(2, Load.getPlineCode());
			patsPlan.setObject(3, Load.getPlineName());
			patsPlan.setObject(4, gx);
			patsPlan.setObject(5, gx);
			patsPlan.setObject(6, Load.getBatchNo());
			patsPlan.setObject(7, Load.getProNum());
			patsPlan.setObject(8, Load.getProWt());
			patsPlan.setObject(9, Load.getFailNum());
			patsPlan.setObject(10, Load.getFailWt());
			patsPlan.setObject(11, Load.getProTime());
			patsPlan.setObject(12, Load.getProShift());
			patsPlan.setObject(13, Load.getProGroup());
			patsPlan.setObject(14, Load.getProUser());
			patsPlan.setObject(15, Load.getProUser());
			patsPlan.setObject(16, Load.getHeatPlanNo());
			patsPlan.addBatch();


/*
			patupdatepln.setObject(1,"40");
			patupdatepln.setObject(2,Load.getHeatPlanNo());
			patupdatepln.setObject(3,Load.getBatchNo());
			patupdatepln.addBatch();
*/

			patAdd.executeBatch();
			patsPlan.executeBatch();
			patupdatepln.executeBatch();


			FrmMchBugD.AddMchWaste(connection,Load.getHeatPlanNo(),resultNo,Load.getPlineCode(),Load.getProUser(),gx,SuspiciousList, ScrappedList);

			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoDelete(String loadStr,String gx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(
				"yyyyMM", Locale.CHINA);
		String systime1 = dateFormat1.format(now);
		PreparedStatement psta = null;

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(new Date());

		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			String DeleteComInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.deleteVrpResult");// 删除公共信息
			PreparedStatement patsDeleteComInfo = connection
					.prepareStatement(DeleteComInfoSql);

			String DeleteResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmThreadInspectResult.DeleteResult");// 删除实绩
			PreparedStatement patsDeleteResult = connection
					.prepareStatement(DeleteResultSql);

			MchHreadResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchHreadResultEntity.class);

			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo(),Load.getBatchNo());

			if(!pln.getPlineCode().equals(Load.getPlineCode())){
				core.setV_errMsg("不能删除其他产线生成的实绩！");
				return core;
			}


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			// 螺纹检验暂时不卡炉计划状态    和生产厂操作互不影响。
			/*if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}*/


	/*		String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();*/
			//endregion


			patsDeleteComInfo.setObject(1,Load.getResultNo());
			patsDeleteComInfo.addBatch();
			patsDeleteComInfo.executeBatch();



			patsDeleteResult.setObject(1,Load.getResultNo());
			patsDeleteResult.addBatch();
			patsDeleteResult.executeBatch();

			int detCount =0;
			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}

			FrmMchBugD.DeleteMchWaste(connection, Load.getHeatPlanNo(),Load.getResultNo(), Load.getPlineCode(),gx,detCount);


			core.setV_errMsg("删除成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 每支材料对应的入库单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSqlDoc(Connection conn, String strlist, String memo)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmThreadingResult.getInListDoc");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, memo);
		psta.setString(2, matter2.format(dt));
		psta.setString(3, matter1.format(dt));
		psta.setString(4, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 每支材料对应的入库流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSql(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmThreadingResult.getInListDoc1");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("INSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return seq;
	}
}
