package com.steering.mes.mcp.coup;

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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.CouplingSandblastResultEntity;

/**
 * 接箍实绩业务类
 * @author siy 2015/05/28
 * @version v1.0
 */
public class FrmPerformanceResult extends DBCommon {
    /**
     * 查询切割实绩
     * @param planNO
     * @return
     */
    public CoreReturnObject getCutResultByPlanNo(String planNO, String woId) {
        Object[] pamar = new Object[] { planNO, woId };
        return this.getDataBySqlID("FrmPerformanceResult.GetCutResultByPlanNo",
                pamar);
    }

    /**
     * 查询切割实绩
     * @param planNO
     * @return
     */
    public CoreReturnObject getCoupSandBlast(String planNO, String woId) {
        Object[] pamar = new Object[] { planNO, woId };
        return this.getDataBySqlID("FrmPerformanceResult.getCoupSandBlast",
                pamar);
    }

    /**
     * 查询切割实绩
     * @param planNO
     * @return
     */
    public CoreReturnObject getThreadingResult(String planNO, String woId) {
        Object[] pamar = new Object[] { planNO, woId };
        return this.getDataBySqlID("FrmPerformanceResult.getThreadingResult",
                pamar);
    }

    /**
     * 查询编号
     * @param plineCode
     * @param connection
     * @return
     */
    public String doAddResultNo(String plineCode, Connection connection) {
        String resultNo = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        String sqlloading = CoreXmlSqlParsersFactory
                .getSql("FrmPerformanceResult.queryLoadRESULT");// 查询最大实绩编号
        try {
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, plineCode);
            psta.setObject(2, matter1.format(dt));
            psta.setObject(3, plineCode);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("RESULT_NO");
            }
            rs1.close();
            psta.close();
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultNo;
    }

    /**
	 * 离线多工序实绩
	 * 
	 * @param result
	 * 多工序实绩
	 * @return
	 */
	public CoreReturnObject lixianThreadingResult(ArrayList<String> list, String latGx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		String systime = dateFormat.format(now);
		String last = date.format(ca.getTime()) + " 21:00:00";
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String insertResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmPerformanceResult.ProcessPlan.lixianThreadingResult");// 离线多工序实绩
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultSql);
			 String insertComInfoSql = CoreXmlSqlParsersFactory
			 .getSql("FrmPerformanceResult.lixianComInfoResult");// 离线ComInfo表
			 PreparedStatement insertComInfoPsta = con
			 .prepareStatement(insertComInfoSql);
			 String insertofflineSql = CoreXmlSqlParsersFactory
					 .getSql("ThreadInspectResultService.lixianofflineResult");// 离线表
					 PreparedStatement insertofflinePsta = con
					 .prepareStatement(insertofflineSql);
			String lutime = systime;

			String lixian = list.get(0);
			String WoId =  list.get(1);
			String JudgeNo = list.get(2);
			String BatchNo = list.get(3);
			String PlineCode =  list.get(4);
			String PlanId =  list.get(5);
			String PlanName =  list.get(6);
			String UserName =  list.get(7);
			String bc = list.get(8);
			String bz =  list.get(9);
			String time =  list.get(10);

			if (PlanService.compareDate(systime, last) == true) {
				if (bc.equals("3")) {
					ca.add(Calendar.DAY_OF_MONTH, 1);
					lutime = date.format(ca.getTime()) + " 00:00:0 31";
				}
			}
			// 离线车丝
			String resultNo = doAddResultNo(PlineCode, con);
			insertResultPsta.setString(1, resultNo);
			insertResultPsta.setString(2, PlineCode);
			insertResultPsta.setString(3, PlanId);
			insertResultPsta.setString(4, WoId);
			insertResultPsta.setString(5, UserName);
			insertResultPsta.setString(6, bc);
			insertResultPsta.setString(7, bz);
			insertResultPsta.setString(8, systime);
			insertResultPsta.setString(9, "1");
			insertResultPsta.setString(10, lixian);
			insertResultPsta.addBatch();
			// 离线公共信息并修改计划车丝
			 insertComInfoPsta.setString(1, PlanId);
			 insertComInfoPsta.setString(2, "");
			 insertComInfoPsta.setString(3, "");
			 insertComInfoPsta.setString(4, "");
			 insertComInfoPsta.setString(5, "");
			 insertComInfoPsta.setString(6, "");
			 insertComInfoPsta.setString(7, "");
			 insertComInfoPsta.setString(8, "");
			 insertComInfoPsta.setString(9, lixian);
			 insertComInfoPsta.setString(10, lutime);
			 insertComInfoPsta.setString(11, UserName);
			 insertComInfoPsta.setString(12, bc);
			 insertComInfoPsta.setString(13, bz);
			 insertComInfoPsta.setString(14, resultNo);
			 insertComInfoPsta.setString(15, "1");
			 insertComInfoPsta.setString(16, latGx);
			 insertComInfoPsta.setString(17, systime);
			 insertComInfoPsta.setString(18, "");
			 insertComInfoPsta.setString(19, WoId);
			 insertComInfoPsta.setString(20, "");
			 insertComInfoPsta.setString(21, "");
			 insertComInfoPsta.setString(22, PlanId);
			 insertComInfoPsta.setString(23, WoId);
			 insertComInfoPsta.addBatch();
			//新增离线表信息
			 insertofflinePsta.setString(1, resultNo);
			 insertofflinePsta.setString(2, PlineCode);
			 insertofflinePsta.setString(3, PlanId);
			 insertofflinePsta.setString(4, WoId);
			 insertofflinePsta.setString(5, latGx);
			 insertofflinePsta.setString(6, JudgeNo);
			 insertofflinePsta.setString(7, BatchNo);
			 insertofflinePsta.setString(8, lixian);
			 insertofflinePsta.setString(9, "");
			 insertofflinePsta.setString(10, "1");
			 insertofflinePsta.setString(11, "");
			 insertofflinePsta.setString(12, "");
			 insertofflinePsta.setString(13,lutime);
			 insertofflinePsta.setString(14, UserName);
			 insertofflinePsta.setString(15,bc);
			 insertofflinePsta.setString(16,bz);
			 insertofflinePsta.setString(17, lutime);
			 insertofflinePsta.addBatch();

			insertResultPsta.executeBatch();
			insertComInfoPsta.executeBatch();
			insertofflinePsta.executeBatch();
			core.setV_errMsg("离线成功!");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					core.setV_errMsg(e.getMessage());
					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}
    /**
     * 修改切割实绩
     * @param result 切割实绩
     * @return
     */
    public CoreReturnObject updateCutResult(ArrayList<String> list,
            String cutGx, String emGX, String plnNo) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String cutResultsql = CoreXmlSqlParsersFactory
                    .getSql("CoupCutService.CutPlan.UpdateCutResult");// 修改切割实绩
            PreparedStatement cutResultPsta = con
                    .prepareStatement(cutResultsql);
            String comInfoSql = CoreXmlSqlParsersFactory
                    .getSql("CoupCutService.UpdateComInfoResult");// 修改comInfo表
            PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
            String planJgSql = CoreXmlSqlParsersFactory
                    .getSql("CoupCutService.UpdatePlanResult");// 修改工单表
            PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
            // 查询计划状态
            int falg = 0;
            int cutfalg = 0;
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("PhosphateResultService.queryPlanJg");
            PreparedStatement psta = con.prepareStatement(sqlloading);
            psta.setObject(1, plnNo);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                falg = Integer.parseInt(rs1.getString("ZY_BILL_STATUS"));
                cutfalg = Integer.parseInt(rs1.getString("CUT_STATUS"));
            }
            rs1.close();
            psta.close();

            if (falg != 2) {
                cor.setV_errMsg("该炉次计划号不是生产中的状态,不允许此操作！");
                return cor;
            }
            if (cutfalg == 2) {
                cor.setV_errMsg("该炉次计划号切管状态已生产完成,不允许此操作,如要修改请先撤销结炉！");
                return cor;
            }

            String resNo = list.get(0);
            // String planNo = list.get(1);
            String quaCount = list.get(2);
            String cutPies = list.get(3);
            String rejCount = list.get(4);
            String reason = list.get(5);
            String bc = list.get(6);
            String bz = list.get(7);
            String jcNo = list.get(8);
            String lvaLen = list.get(9);
            String colName = list.get(10);
            String emContent = list.get(11);
            String emCount = list.get(12);
            String woid = list.get(13);
            // 修改切管实绩
            cutResultPsta.setObject(1, quaCount);
            cutResultPsta.setObject(2, cutPies);
            cutResultPsta.setObject(3, rejCount);
            cutResultPsta.setObject(4, reason);
            cutResultPsta.setObject(5, bc);
            cutResultPsta.setObject(6, bz);
            cutResultPsta.setObject(7, jcNo);
            cutResultPsta.setObject(8, lvaLen);
            cutResultPsta.setObject(9, colName);
            cutResultPsta.setObject(10, emContent);
            cutResultPsta.setObject(11, emCount);
            cutResultPsta.setObject(12, resNo);
            cutResultPsta.addBatch();
            // 修改公共表切管
            comInfoPsta.setObject(1, Integer.parseInt(quaCount)
                    - Integer.parseInt(rejCount));
            comInfoPsta.setObject(2, rejCount);
            comInfoPsta.setObject(3, bc);
            comInfoPsta.setObject(4, bz);
            comInfoPsta.setObject(5, colName);
            comInfoPsta.setObject(6, resNo);
            comInfoPsta.setObject(7, plnNo);
            comInfoPsta.setObject(8, cutGx);
            comInfoPsta.setObject(9, woid);
            comInfoPsta.addBatch();
            // 修改公共表压印
            comInfoPsta.setObject(1, emCount);
            comInfoPsta.setObject(2, "");
            comInfoPsta.setObject(3, bc);
            comInfoPsta.setObject(4, bz);
            comInfoPsta.setObject(5, colName);
            comInfoPsta.setObject(6, resNo);
            comInfoPsta.setObject(7, plnNo);
            comInfoPsta.setObject(8, emGX);
            comInfoPsta.addBatch();

            cutResultPsta.executeBatch();
            comInfoPsta.executeBatch();
            // 查询切管总数
            int countQual = 0;
            int countPcs = 0;
            int emNum = 0;
            sqlloading = CoreXmlSqlParsersFactory
                    .getSql("CoupCutService.queryCutCou");
            psta = con.prepareStatement(sqlloading);
            psta.setObject(1, plnNo);
            psta.setObject(2, woid);
            rs1 = psta.executeQuery();
            if (rs1.next()) {
                countQual = Integer.parseInt(rs1.getString("QUALIFIED_NUMBER"));
                countPcs = Integer.parseInt(rs1.getString("REJECTED_PCS"));
                emNum = Integer.parseInt(rs1.getString("EMB_COUNT"));
            }
            rs1.close();
            psta.close();

            planJgPsta.setObject(1, countQual - countPcs);
            planJgPsta.setObject(2, countPcs);
            planJgPsta.setObject(3, emNum);
            planJgPsta.setObject(4, plnNo);
            planJgPsta.setObject(5, woid);
            planJgPsta.addBatch();

            planJgPsta.executeBatch();
            cor.setV_errMsg("修改切管实绩成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }

    /**
     * 修改车丝实绩
     * @param result 车丝实绩
     * @return
     */
    public CoreReturnObject updateThreadingResult(ArrayList<String> list,
            String latGx, String plnNo) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String cutResultsql = CoreXmlSqlParsersFactory
                    .getSql("CoupThreadingService.UpdateCutResult");// 修改车丝实绩
            PreparedStatement cutResultPsta = con
                    .prepareStatement(cutResultsql);
            String comInfoSql = CoreXmlSqlParsersFactory
                    .getSql("CoupThreadingService.UpdateComInfoResult");// 修改comInfo表
            PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
            String planJgSql = CoreXmlSqlParsersFactory
                    .getSql("CoupThreadingService.UpdatePlanResult");// 修改工单表
            PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
            // 查询工单状态
            int falg = 0;
            int cutfalg = 0;
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("PhosphateResultService.queryPlanJg");
            PreparedStatement psta = con.prepareStatement(sqlloading);
            psta.setObject(1, plnNo);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                falg = Integer.parseInt(rs1.getString("ZY_BILL_STATUS"));
                cutfalg = Integer.parseInt(rs1.getString("LATHE_STATUS"));
            }
            rs1.close();
            psta.close();

            if (falg != 2) {
                cor.setV_errMsg("该炉次计划号不是生产中的状态,不允许此操作！");
                return cor;
            }
            if (cutfalg == 2) {
                cor.setV_errMsg("该炉次计划号切管状态已生产完成,不允许此操作,如要修改请先撤销结炉！");
                return cor;
            }

            String resNo = list.get(0);
            String latPies = list.get(2);
            String quaCount = list.get(3);// 合格个数
            String rejCount = list.get(4);
            String reason = list.get(5);
            String bc = list.get(6);
            String bz = list.get(7);
            String jcNo = list.get(8);
            String colName = list.get(9);
            String colName1 = list.get(10);
            String jzPro = list.get(11);
            String jzTime = list.get(12);
            String woId = list.get(13);
            // 修改车丝实绩
            cutResultPsta.setObject(1, latPies);
            cutResultPsta.setObject(2, quaCount);
            cutResultPsta.setObject(3, rejCount);
            cutResultPsta.setObject(4, reason);
            cutResultPsta.setObject(5, bc);
            cutResultPsta.setObject(6, bz);
            cutResultPsta.setObject(7, jcNo);
            cutResultPsta.setObject(8, jzPro);
            cutResultPsta.setObject(9, jzTime);
            cutResultPsta.setObject(10, colName);
            cutResultPsta.setObject(11, colName1);
            cutResultPsta.setObject(12, resNo);
            cutResultPsta.addBatch();
            // 修改公共
            comInfoPsta.setObject(1, quaCount);
            comInfoPsta.setObject(2, rejCount);
            comInfoPsta.setObject(3, bc);
            comInfoPsta.setObject(4, bz);
            comInfoPsta.setObject(5, colName);
            comInfoPsta.setObject(6, colName1);
            comInfoPsta.setObject(7, jzPro);
            comInfoPsta.setObject(8, jzTime);
            comInfoPsta.setObject(9, resNo);
            comInfoPsta.setObject(10, plnNo);
            comInfoPsta.setObject(11, latGx);
            comInfoPsta.setObject(12, woId);
            comInfoPsta.addBatch();

            cutResultPsta.executeBatch();
            comInfoPsta.executeBatch();
            // 查询车丝总数
            int countQual = 0;
            int countPcs = 0;
            sqlloading = CoreXmlSqlParsersFactory
                    .getSql("CoupThreadingService.queryCutCou");
            psta = con.prepareStatement(sqlloading);
            psta.setObject(1, plnNo);
            psta.setObject(2, woId);
            rs1 = psta.executeQuery();
            if (rs1.next()) {
                countQual = Integer.parseInt(rs1.getString("QUALIFIED_NUMBER"));
                countPcs = Integer.parseInt(rs1.getString("REJECTED_PCS"));
            }
            rs1.close();
            psta.close();

            planJgPsta.setObject(1, countQual);
            planJgPsta.setObject(2, countPcs);
            planJgPsta.setObject(3, plnNo);
            planJgPsta.setObject(4, woId);
            planJgPsta.addBatch();

            planJgPsta.executeBatch();
            cor.setV_errMsg("修改车丝实绩成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }
}

