package com.steering.mes.zgmil.report;

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.OfflineProcess;
import com.steering.mes.zgmil.entity.OfflineProcessAuditEntity;

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

public class FrmOfflineProcess extends DBCommon{

	/**
	 * 查产线
	 * 
	 * @param PROCESS_CODE
	 * @return
	 */
	public CoreReturnObject JudgeBaseInfo(String[] plineCodes) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOfflineProcess.JudgeBaseInfo");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCodes);
		return this.getData(sql);
	}
	
	
	public CoreReturnObject QueryOfflineProcess(String jugeNo, String startTim,
			String endTim, String plineCode, String isAudit,String reason,String orderno,String lxTime) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOfflineProcess.QueryOfflineProcess");
		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')";
		}
		if (!"".equals(jugeNo)) {
			sql += " AND T.JUDGE_STOVE_NO LIKE '" + jugeNo + "%'";
		}
		if (!"".equals(plineCode)) {
			sql += " AND T.PLINE_CODE IN ( '" + plineCode + "') ";
		}
		if(!"".equals(isAudit)){
			sql += " AND T.IS_AUDIT = '"+isAudit+"' ";
				}
		if(!"".equals(reason)){
			sql += " AND T.REASON1 LIKE '%"+reason+"%' ";
		}
		if(!"".equals(orderno)){
			sql += " AND T.ORDER_NO LIKE '%"+orderno+"%'";
		}
		if(!"".equals(lxTime))
		{
			sql += "AND to_char(T.OFFLINE_TIME,'YYYY-MM-DD HH24:MI:SS') = '"+lxTime+"'";
		}
		return this.getData(sql);
	}
	
	 @SuppressWarnings("unchecked")
	 public CoreReturnObject doSave(ArrayList<String> parm) {
		 CoreReturnObject cro = new CoreReturnObject();
		 Connection connection = this.getDao(CoreConstantData.sysbeanstr)
					.getConnection();
			PreparedStatement psta = null;
			String sql = "";
			try{
				connection.setAutoCommit(false); // 取消自动提交事务
				for (String json : parm) {
					OfflineProcess entity = EntityTool.getEntityByJSONStr(json, OfflineProcess.class);
					String resultno = entity.getResultNo();
					//String traget = entity.getTarget();
					String reason1 = entity.getReason1();
					String reason2= entity.getReason2();
					String reason3 = entity.getReason3();
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmOfflineProcess.doSave"); // 根据产线、报表名称查询是否已有数据
					psta = connection.prepareStatement(sql);
					//psta.setString(1,traget);
					psta.setString(1,reason1);
					psta.setString(2,reason2);
					psta.setString(3,reason3);
					psta.setString(4,entity.getAnalysisResults());
					psta.setString(5,entity.getPlineNameResponse());
					psta.setString(6,entity.getDisposalOpinion());
					psta.setString(7,entity.getDisposalOpinionSchedule());
					psta.setString(8,entity.getMemo());
					psta.setString(9,resultno);
					psta.addBatch();
					psta.executeBatch();
				}
				cro.setV_errMsg("操作成功");
				//psta.executeBatch();
				connection.commit();
			}
			catch (Exception e)
			{
				cro.setV_errMsg(e.getMessage());
				cro.setV_errCode(-1);
				if (connection != null) {
					try {
						connection.rollback(); // 回滚事务
					} catch (Exception e2) {
					}
				}
			}finally{
				try {
					if (connection != null && !connection.isClosed()) {
						connection.close();
					}
				} catch (Exception e2) {
				}
			}
			return cro;
	}
	@SuppressWarnings("unchecked")
	 public CoreReturnObject doAudit(ArrayList<String> parm, String userName,String department) {
		 CoreReturnObject cro = new CoreReturnObject();
		 Connection connection = this.getDao(CoreConstantData.sysbeanstr)
					.getConnection();
			PreparedStatement psta = null;
			String sql = "";
			String sql2 = "";
			String sql3 = "";
			ResultSet rs = null;
			int acount = 0;
			try{
				connection.setAutoCommit(false); // 取消自动提交事务
				//PreparedStatement pstaaddsql = connection.prepareStatement(addsql);
				for (String json : parm) {
					OfflineProcess entity = EntityTool.getEntityByJSONStr(json, OfflineProcess.class);
					String  resultNo = entity.getResultNo();
					String  disposalOpinion = entity.getDisposalOpinion();
					String  name = userName;
					String  dept = department;
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmOfflineProcess.selectByid"); // 根据产线、报表名称查询是否已有数据
					psta = connection.prepareStatement(sql);
					psta.setString(1, resultNo);
					rs = psta.executeQuery();
					if (rs.next()) {
						acount = Integer.parseInt(rs.getString("acount"));
					}
					if (acount>0) {
						cro.setV_errMsg("编号为："+resultNo+"已经提交审核，不能重新提交");
						rs.close();
						psta.close();
						return cro;
					}
					
					sql2 = CoreXmlSqlParsersFactory
							.getSql("FrmOfflineProcess.doAudit"); // 修改数据
					psta = connection.prepareStatement(sql2);
					psta.setString(1, disposalOpinion);
					psta.setString(2, "1");
					psta.setString(3, entity.getReason1());
					psta.setString(4, entity.getReason2());
					psta.setString(5, entity.getReason3());
					psta.setString(6, resultNo);
					psta.addBatch();
					psta.executeBatch();
					
					sql3= CoreXmlSqlParsersFactory
							.getSql("FrmOfflineProcess。doAddAudit"); // 修改数据
					psta = connection.prepareStatement(sql3);
					psta.setString(1, resultNo);
					psta.setString(2, name);
					psta.setString(3, "0");
					psta.setString(4, dept);
					psta.setString(5, disposalOpinion);
					psta.addBatch();
					psta.executeBatch();
				}
				cro.setV_errMsg("操作成功");
				//psta.executeBatch();
				connection.commit();
			}
			catch (Exception e)
			{
				cro.setV_errMsg(e.getMessage());
				cro.setV_errCode(-1);
				if (connection != null) {
					try {
						connection.rollback(); // 回滚事务
					} catch (Exception e2) {
					}
				}
			}finally{
				try {
					if (connection != null && !connection.isClosed()) {
						connection.close();
					}
				} catch (Exception e2) {
				}
			}
			return cro;
	   }
	
	public CoreReturnObject QueryOfflineAudits(String id) {
		if(id.equals("") || null == id)
			return null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOfflineProcess.QueryOfflineAudits");
		sql+= " and t1.result_no = '"+id+"'";
		return this.getData(sql);
	}
	
}
