package com.steering.mes.zgmil.coup;

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.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.MilOfflineGroupEntity1;
import com.steering.mes.zgmil.entity.MilOfflineReslutEntity1;

public class FrmFurnace  extends DBCommon{

	/***
	 * 查询下线炉批
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryOffLine(ArrayList<String> parm,String startTim,String endTim,String plinCode)
	{
	
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFurnace.doQueryOffLine");
		if(!startTim.equals(""))
		{
			sql += " AND T.OFFLINE_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND T.OFFLINE_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		sql += " AND T.PLINE_CODE = '"+plinCode+"'";
		sql += " GROUP BY T.JUDGE_STOVE_NO,T.LOT_NO,T.DIAMETER,T.THICKNESS,T.LENGTH,T.PLINE_CODE,S.STEELCODE,S.STEELNAME,S.GRADECODE,S.GRADENAME,S.SIMPLE_LOT_NO,";
		sql += " S.ZP_STANDRADCODE,S.ZP_STANDRAD,S.ZP_LENGTH_MAX,S.ZP_LENGTH_MIN,S.GROUP_FLAG";
		return this.getData(sql, parm.toArray());
	}
	
	/***
	 * 查询组炉实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param plinCode
	 * @return
	 */
	public CoreReturnObject doQueryOffGroup(ArrayList<String> parm,String startTim,String endTim,String plinCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFurnace.doQueryOffGroup");
		if(!startTim.equals(""))
		{
			sql += " AND ZP_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND ZP_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		sql += " AND PLINE_CODE = '"+plinCode+"'";
		sql += " GROUP BY DIAMETER, VARIETIES,PLINE_CODE,QUALIFIED_NUM,ZP_LENGTH_MAX,REMARK, COL_USER,COL_SHIFT,COL_GROUP,ZP_LEVEL,ZP_LENGTH_MIN,ZP_SIZE,";
		sql += " ZP_M_LH,ZP_S_LH,ZP_M_PH,SCRAP_NUM,ZP_STANDRAD,ZP_STEEL,ZP_GRADE,ZP_LENGTH,ZP_JUDGE_STOVE_NO,ZP_LOT_NO,THICKNESS,SIMPLE_LOT_NO";
		return this.getData(sql, parm.toArray());
	}
	
	/***
	 * 组炉
	 * @param parm
	 * @param parm1
	 * @return
	 */
	public CoreReturnObject doFurnace(ArrayList<String> parm,ArrayList<String> parm1,String userName,String userGroup,String userOrder)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        String simPleNo = "";
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertSql = CoreXmlSqlParsersFactory.getSql("doFurnace.insertOffGroup"); //插入履历表
			PreparedStatement insertPsta = connection.prepareStatement(insertSql);
			MilOfflineReslutEntity1 tity = EntityTool.getEntityByJSONStr(parm1.get(0).toString(), MilOfflineReslutEntity1.class);
			simPleNo = getQuerySimpleNo(connection,tity.getNewJudgeStoveNo(),tity.getNewLotNo());
			for(String str:parm)
			{
				MilOfflineReslutEntity1 milOffEntity1 = EntityTool.getEntityByJSONStr(str, MilOfflineReslutEntity1.class);
				insertPsta.setString(1, milOffEntity1.getJudgeStoveNo());
				insertPsta.setString(2, milOffEntity1.getLotNo());
				insertPsta.setString(3, milOffEntity1.getZpLengthMin());
				insertPsta.setString(4, milOffEntity1.getZpLengthMax());
				insertPsta.setString(5, milOffEntity1.getLength());
				insertPsta.setInt(6, Integer.parseInt(milOffEntity1.getOfflineNum())+1);
				
				insertPsta.setString(7, tity.getDiameter());
				insertPsta.setString(8, tity.getThickness());
				insertPsta.setString(9, tity.getVarieties());
				insertPsta.setString(10, tity.getNewJudgeStoveNo());
				insertPsta.setString(11, tity.getNewLotNo());
				
				insertPsta.setString(12, tity.getZpStandrad());
				insertPsta.setString(13, tity.getSteelname());
				insertPsta.setString(14, tity.getGradename());
				insertPsta.setString(15, tity.getLength());
				insertPsta.setString(16, tity.getZpLengthMax());
				insertPsta.setString(17, tity.getZpLengthMin());
				

				insertPsta.setString(18, tity.getJudgeStoveNo());
				insertPsta.setString(19, tity.getLotNo());
				insertPsta.setString(20, tity.getSimpleLotNo());
				insertPsta.setString(21, userName);
				insertPsta.setString(22, userOrder);
				insertPsta.setString(23, userGroup);
				insertPsta.setString(24, simPleNo);
//				insertPsta.setString(25, tity.getNewJudgeStoveNo());
//				insertPsta.setString(26, tity.getNewLotNo());
				insertPsta.setString(25, tity.getNewJudgeStoveNo());
				insertPsta.setString(26, tity.getNewLotNo());
				insertPsta.setString(27, simPleNo);
				insertPsta.addBatch();
			}
			insertPsta.executeBatch();
			connection.commit();
			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 parm
	 * @return
	 */
	public CoreReturnObject doCancelFurnace(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmFurnace.doCancelFurnace");
			PreparedStatement deletePsta = connection.prepareStatement(deleteSql);
			for(String str : parm)
			{
			   MilOfflineGroupEntity1 milOffTity = EntityTool.getEntityByJSONStr(str, MilOfflineGroupEntity1.class);
			   deletePsta.setString(1, milOffTity.getZpJudgeStoveNo());
			   deletePsta.setString(2, milOffTity.getZpLotNo());
			   //deletePsta.setString(3, milOffTity.getZpLengthMin());
			   //deletePsta.setString(4, milOffTity.getZpLengthMax());
			   deletePsta.addBatch();
			}
			deletePsta.executeBatch();
			connection.commit();
			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 conn
	 * @param jugeNo
	 * @param lotNo
	 * @return
	 */
	public String getQuerySimpleNo(Connection conn,String jugeNo,String lotNo)
	throws Exception{
        PreparedStatement psta = null;
        String res = "";
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmFurnace.getQuerySimpleNo");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, jugeNo);
        psta.setString(2, lotNo);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
             res= rs.getString(1);
        }
        rs.close();
        psta.close();
        return res;
	}
}
