package com.steering.mes.zgmil.report;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.common.StringTool;
import com.steering.mes.zgmil.entity.FeedSawResult;

public class FrmFeedSaw extends DBCommon {

    /**
     *查询上料锯切
     */
    public CoreReturnObject queryMinResult(String date, String endDate,
            String bc, String bz,String JudgeStoveNo , String[] plineCodes) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmFeedSaw.queryMinResult");
        if (!date.equals("")) {
            sql += " AND T.END_TIME BETWEEN TO_DATE('" + date
                    + "' ,'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + endDate
                    + "' ,'YYYY-MM-DD HH24:MI:SS') ";
        }
        if (!bc.equals("")) {
            sql += "  AND T.COL_SHIFT = '" + bc + "' ";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP = '" + bz + "' ";
        }
		if(!JudgeStoveNo.equals("")){
			sql += "   AND T.JUDGE_STOVE_NO  LIKE '%' || '"+JudgeStoveNo+"' || '%' ";
		}
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCodes);
        sql += "  GROUP BY T.JUDGE_STOVE_NO,T.COL_SHIFT, T.COL_GROUP,T.PLINE_CODE, T.FEED_ROW_NO,T.RESULT_NO,"
                + "T.LOT_NO, T.DB_NUM, T3.VARIETIES,T3.GRADENAME, T3.PIPE_SECTION, T2.FEED_LENGTH, T2.DB_LENGTH,"
                + "T1.REMARK, T.COL_USER  ORDER BY START_TIME ASC";
        return this.getData(sql);
    }
    
 
    /**
     * 炉前修改
     * 1-锯切 2-加热炉 3-穿孔 4-连轧 5-再加热炉 6-定径 7-探伤 8-人工质检 9-管排锯 10-矫直 11-热取样
     * @param parm
     * @return
     */
    public CoreReturnObject doSave(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			FeedSawResult matZcTity1 = EntityTool.getEntityByJSONStr(parm.get(0),
					FeedSawResult.class);
			String updateSql = "";
			if(matZcTity1.getRemark().equals("锯切")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult1");
			}else if(matZcTity1.getRemark().equals("环形炉")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult2");
			}else if(matZcTity1.getRemark().equals("穿孔")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult3");
			}else if(matZcTity1.getRemark().equals("连轧")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult4");
			}else if(matZcTity1.getRemark().equals("定径")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult6");
			}else if(matZcTity1.getRemark().equals("热取样")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult11");
			}else if(matZcTity1.getRemark().equals("管排锯")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult9");
			}else if(matZcTity1.getRemark().equals("矫直")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult10");
			}else if(matZcTity1.getRemark().equals("探伤")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult7");
			}else if(matZcTity1.getRemark().equals("人工质检")){
				updateSql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedSaw.updateResult8");
			}
			
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			for (String str : parm) {
				FeedSawResult matZcTity = EntityTool.getEntityByJSONStr(str,
						FeedSawResult.class);
				if(matZcTity1.getRemark().equals("环形炉") && matZcTity1.getRemarkState().equals("入炉修改")){
					updatePsta.setString(1, "");
					updatePsta.setString(2, "");
					updatePsta.setString(3, StringTool.ChangColShift(matZcTity.getColShift()));
					updatePsta.setString(4, StringTool.ChangColShift(matZcTity.getColGroup()));
					updatePsta.setString(5, matZcTity.getStartTime());
					updatePsta.setString(6, matZcTity.getJudgeStoveNo());
					//updatePsta.setString(7, matZcTity.getColUser());
				}
				else if (matZcTity1.getRemark().equals("环形炉") && matZcTity1.getRemarkState().equals("出炉修改")) {
					updatePsta.setString(1, StringTool.ChangColShift(matZcTity.getColShift()));
					updatePsta.setString(2, StringTool.ChangColShift(matZcTity.getColGroup()));
					updatePsta.setString(3, "");
					updatePsta.setString(4, "");
					updatePsta.setString(5, matZcTity.getStartTime());
					updatePsta.setString(6, matZcTity.getJudgeStoveNo());
					//updatePsta.setString(7, matZcTity.getColUser());
				}
				else{
					updatePsta.setString(1, StringTool.ChangColShift(matZcTity.getColShift()));
					updatePsta.setString(2, StringTool.ChangColShift(matZcTity.getColGroup()));
					updatePsta.setString(3, matZcTity.getStartTime());
					updatePsta.setString(4, matZcTity.getJudgeStoveNo());
					updatePsta.setString(5, matZcTity.getColUser());
				}
				updatePsta.addBatch();
				updatePsta.executeBatch();
			}
			cro.setV_errMsg("修改成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
