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.MchPathResultDEntity;
import com.steering.mes.mcp.entity.MchPathResultEntity;
import com.steering.mes.mcp.entity.MchRemanenceResultDEntity;
import com.steering.mes.mcp.entity.MchRemanenceResultEntity;

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

/**
 * 通径实绩
 * 
 * @author SZL
 *
 */
public class FrmRemanenceResult extends DBCommon {

	/**
	 * 查询通径实绩
	 */
	public CoreReturnObject getQueryExpandResult(String heatNo ,String BatchNo) {
		return this.getDataBySqlID("FrmRemanenceResult.getQueryExpandResult",
				new Object[] { heatNo,BatchNo});
	}


	public CoreReturnObject doQuerypDetail(
			String[] ResultNo) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmRemanenceResult.QueryDetail");
		sql += PlanService.toSqlConditionString("T.RESULT_NO", ResultNo);
		return this.getData(sql);
	}



	public CoreReturnObject DoAdd(String loadStr, String gx, ArrayList<String> Details){
		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); //


			MchRemanenceResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchRemanenceResultEntity.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;
			if(StringTool.isInteger(Load.getProNum()) ){
				count = Integer.parseInt(Load.getProNum()) ;
			}
			if(count<=0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}


			//获取最大实绩编号
			sql =   CoreXmlSqlParsersFactory
					.getSql("FrmRemanenceResult.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("FrmRemanenceResult.AddResult");

			sql = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, Load);
			psta = connection.prepareStatement(sql);
			psta.executeUpdate();
			psta.close();


			sql = CoreXmlSqlParsersFactory
					.getSql("FrmRemanenceResult.AddDetailResult");

			sql = com.steering.common.EntityTool.fillSqlByEntity(sql, Load);
			psta = connection.prepareStatement(sql);


			for (int i = 0; i <Details.size() ; i++) {

				MchRemanenceResultDEntity DEntity = EntityTool.getEntityByJSONStr(Details.get(i),MchRemanenceResultDEntity.class);

				psta.setObject(1,i);
				psta.setObject(2,DEntity.getMeasurement());
				psta.setObject(3,DEntity.getMatNo());
				psta.setObject(4,DEntity.getMeasurementValueA0());
				psta.setObject(5,DEntity.getMeasurementValueA90());
				psta.setObject(6,DEntity.getMeasurementValueA180());
				psta.setObject(7,DEntity.getMeasurementValueA270());
				psta.setObject(8,DEntity.getMeasurementValueAavg());
				psta.setObject(9,DEntity.getMeasurementValueB0());
				psta.setObject(10,DEntity.getMeasurementValueB90());
				psta.setObject(11,DEntity.getMeasurementValueB180());
				psta.setObject(12,DEntity.getMeasurementValueB270());
				psta.setObject(13,DEntity.getMeasurementValueBavg());

				psta.addBatch();
			}

			psta.executeBatch();
			psta.close();


			 sql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.AddComInfoResultNew");// 新增公共信息
			psta = connection
					.prepareStatement(sql);

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




			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;
	}

	/**
	 * 修改
	 * @return
	 */
	public CoreReturnObject DoUpdate(String result,ArrayList<String> Details){
		CoreReturnObject data = new CoreReturnObject();
		MchRemanenceResultEntity batching = EntityTool.getEntityByJSONStr(result, MchRemanenceResultEntity.class);
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false);
			// 修改实绩信息
			String sql = CoreXmlSqlParsersFactory.getSql("FrmRemanenceResult.UpdateResult");
			sql = EntityTool.fillSqlByEntity(sql, batching);
			PreparedStatement patAdd = connection.prepareStatement(sql);

			// 检测记录详情
			String DeleteDetailResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmRemanenceResult.DeleteDetailResult");// 删除记录详情
			PreparedStatement deleteDetailResult = connection
					.prepareStatement(DeleteDetailResultSql);
			deleteDetailResult.setObject(1,batching.getResultNo());
			deleteDetailResult.addBatch();

			//添加
			String sqlAddD = CoreXmlSqlParsersFactory
					.getSql("FrmRemanenceResult.AddDetailResult");

			sqlAddD = EntityTool.fillSqlByEntity(sqlAddD, batching);
			PreparedStatement patAddD = connection.prepareStatement(sqlAddD);
			patAdd.addBatch();

			for (int i = 0; i <Details.size() ; i++) {

				MchRemanenceResultDEntity DEntity = EntityTool.getEntityByJSONStr(Details.get(i),MchRemanenceResultDEntity.class);

				patAddD.setObject(1,i);
				patAddD.setObject(2,DEntity.getMeasurement());
				patAddD.setObject(3,DEntity.getMatNo());
				patAddD.setObject(4,DEntity.getMeasurementValueA0());
				patAddD.setObject(5,DEntity.getMeasurementValueA90());
				patAddD.setObject(6,DEntity.getMeasurementValueA180());
				patAddD.setObject(7,DEntity.getMeasurementValueA270());
				patAddD.setObject(8,DEntity.getMeasurementValueAavg());
				patAddD.setObject(9,DEntity.getMeasurementValueB0());
				patAddD.setObject(10,DEntity.getMeasurementValueB90());
				patAddD.setObject(11,DEntity.getMeasurementValueB180());
				patAddD.setObject(12,DEntity.getMeasurementValueB270());
				patAddD.setObject(13,DEntity.getMeasurementValueBavg());

				patAddD.addBatch();
			}

			patAdd.executeBatch();
			deleteDetailResult.executeBatch();
			patAddD.executeBatch();

			data.setV_errMsg("修改成功！");
			data.setResult(1);
			connection.commit();
		} catch (SQLException e) {
			data.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 data;
	}


	public CoreReturnObject DoDelete(String loadStr,String gx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();


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

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

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

			String DeleteDetailResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmRemanenceResult.DeleteDetailResult");// 删除实绩
			PreparedStatement patsDeleteDetailResult = connection
					.prepareStatement(DeleteDetailResultSql);



			MchPathResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchPathResultEntity.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");// 查询 工序是否是结炉状态
			PreparedStatement 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();

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



			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 UpdatePath(String Path, String ResultNo){
		return	this.setDataBySqlID("FrmRemanenceResult.UpdatePath",new Object[]{Path,ResultNo});
	}


}
