package com.steering.mes.port.mil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

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

/**
 * 组批取样实绩
 * 
 * @author HC 2015/09/16
 * @version v1.0
 * @modify
 */
public class FrmMilGetBatchAndSample extends DBCommon {

	/**
	 * 获取PCODE
	 */
	public CoreReturnObject GetPCode(String[] arr) {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMilGetBatchAndSample.GetPCode");

		sql += CoreConverter.toSqlConditionString("DEPARTMENT_CODE", arr);
		return this.getData(sql);
	}

	/**
	 * 查询轧管工序合同
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryPln_Order_Zg(ArrayList<Object> arrayList) {
		return this.getDataBySqlID("FrmMilGetBatchAndSample.QueryPln_Order_Zg",
				arrayList.toArray());
	}

	/**
	 * 查询合同轧制制程是否经过成分和理化
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryGetSampleReq_Desgin_Process(
			ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QueryGetSampleReq_Desgin_Process",
				arrayList.toArray());
	}

	/**
	 * 查询取样要求(根据合同头和合同行号) 质量设计数据
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryGetSampleReq_Desgin(ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QueryGetSampleReq_Desgin", arrayList
						.toArray());
	}

	/**
	 * 查询取样要求(根据合同头和合同行号)
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryGetSampleReq_(ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QueryGetSampleReq_", arrayList
						.toArray());
	}

	/**
	 * 查询取样要求(根据轧管工序合同号)
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryGetSampleReq(ArrayList<Object> arrayList) {
		return this.getDataBySqlID("FrmMilGetBatchAndSample.QueryGetSampleReq",
				arrayList.toArray());
	}

	/**
	 * 查询合同成分检验项目明细(根据轧管工序合同号) CRAFT_ORD_DESIGN_STD_PIC
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryGetSampleReqDetail_Chem(
			ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QueryGetSampleReqDetail_Chem",
				arrayList.toArray());
	}

	/**
	 * 查询取样要求明细理化(根据轧管工序合同号) CRAFT_ORD_DESIGN_STD_PIC
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryGetSampleReqDetail_Phy(
			ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QueryGetSampleReqDetail_Phy",
				arrayList.toArray());
	}

	/**
	 * 查询生产批号、炉号队列
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryLotNoAndStoveNo(ArrayList<Object> arrayList,
			String strPlcode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMilGetBatchAndSample.QueryLotNoAndStoveNo");

		sql += " AND T1.PLINE_CODE IN " + strPlcode;

		sql += " ORDER BY T1.START_TIME";
		return this.getData(sql, arrayList.toArray());
	}

	/**
	 * 查询已经剔除支数
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryDeleteNum(ArrayList<Object> arrayList) {
		return this.getDataBySqlID("FrmMilGetBatchAndSample.QueryDeleteNum",
				arrayList.toArray());
	}

	// /**
	// * 查询管号序号队列
	// *
	// * @param arrayList
	// * @return
	// */
	public CoreReturnObject QueryPipeNoRack(ArrayList<Object> arrayList,
			String strGXCode) {
		String strSql = "SELECT LOT_NO,JUDGE_STOVE_NO,PLINE_CODE,DECODE(STATUS, '0', '是', '是') AS ISVALID,MAT_NO PIPE_NO,SEQ FROM ";
		if (strGXCode.equals("7")) {
			strSql += "MIL_SAW_RACK ";// 分切前，分切后，矫直，探伤
		} else if (strGXCode.equals("8")) {// 分切后
			strSql += "MIL_STRAIGH_RACK ";
		} else if (strGXCode.equals("9")) {// 矫直
			strSql += "MIL_TESTING_RACK ";
		} else if (strGXCode.equals("10")) {// 探伤
			strSql += "MIL_QA_RACK ";
		} else {
			strSql += "MIL_QA_RACK ";
		}
		strSql += "WHERE MAT_NO IS NOT NULL ";
		strSql += "AND PLINE_CODE LIKE '%' || ? || '%' ";
		// strSql += "WHERE PLINE_CODE LIKE '%' || ? || '%' ";
		strSql += "AND LOT_NO LIKE '%' || ? || '%' ";
		strSql += "AND JUDGE_STOVE_NO LIKE '%' || ? || '%' ";
		strSql += "ORDER BY LOT_NO,JUDGE_STOVE_NO,SEQ";
		return this.getData(strSql, arrayList.toArray());
	}

	/**
	 * 查询批次号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryBatchInfo(ArrayList<Object> arrayList) {
		return this.getDataBySqlID("FrmMilGetBatchAndSample.QueryBatchInfo",
				arrayList.toArray());
	}

	/**
	 * 查询试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QuerySampleInfo(ArrayList<Object> arrayList) {
		return this.getDataBySqlID("FrmMilGetBatchAndSample.QuerySampleInfo",
				arrayList.toArray());
	}

	/**
	 * 查询不重复检验项目
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QuerySampleInfoPhy_Distinct(
			ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QuerySampleInfoPhy_Distinct",
				arrayList.toArray());
	}

	/**
	 * 查询试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QuerySampleInfo_Distinct(ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QuerySampleInfo_Distinct", arrayList
						.toArray());
	}

	/**
	 * 查询带试样号的批次号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject QueryBatchSampleInfo(ArrayList<Object> arrayList) {
		return this.getDataBySqlID(
				"FrmMilGetBatchAndSample.QueryBatchSampleInfo", arrayList
						.toArray());
	}

	/**
	 * 新增批次号试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject AddBatchSampleInfo(
			ArrayList<ArrayList<ArrayList<Object>>> al) {
		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		for (int i = 0; i < al.get(0).size(); i++) {
			sqlIdList.add("FrmMilGetBatchAndSample.UpdatePlan");
			params.add(al.get(0).get(i).toArray());// 增加加工表参数
		}

		if (al.size() > 1) {
			for (int i = 0; i < al.get(1).size(); i++) {
				sqlIdList.add("FrmMilGetBatchAndSample.AddBatchInfo");
				params.add(al.get(1).get(i).toArray());// 增加加工表参数
			}
		}

		if (al.size() > 2) {
			for (int i = 0; i < al.get(2).size(); i++) {
				sqlIdList.add("FrmMilGetBatchAndSample.AddSampleInfo");
				params.add(al.get(2).get(i).toArray());// 增加热处理表参数
			}
		}
		return this.setDataById(sqlIdList, params);
	}

	/**
	 * 新增批次号试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject AddSampleInfo(
			ArrayList<ArrayList<ArrayList<Object>>> al) {
		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		for (int i = 0; i < al.get(0).size(); i++) {
			sqlIdList.add("FrmMilGetBatchAndSample.UpdatePlan");
			params.add(al.get(0).get(i).toArray());// 增加加工表参数
		}

		// if (al.size() > 1) {
		// for (int i = 0; i < al.get(1).size(); i++) {
		// sqlIdList.add("FrmMilGetBatchAndSample.AddBatchInfo");
		// params.add(al.get(1).get(i).toArray());// 增加加工表参数
		// }
		// }

		if (al.size() > 1) {
			for (int i = 0; i < al.get(1).size(); i++) {
				sqlIdList.add("FrmMilGetBatchAndSample.AddSampleInfo");
				params.add(al.get(1).get(i).toArray());// 增加热处理表参数
			}
		}
		return this.setDataById(sqlIdList, params);
	}

	/**
	 * 删除批号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject DeleteBatchInfo(ArrayList<Object> arrayList) {

		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		sqlIdList.add("FrmMilGetBatchAndSample.DeleteBatchInfo");
		params.add(arrayList.toArray());

		arrayList.add(0, "");
		arrayList.add(1, "0");
		arrayList.add(2, "1");
		sqlIdList.add("FrmMilGetBatchAndSample.UpdatePlan");
		params.add(arrayList.toArray());

		return this.setDataById(sqlIdList, params);
	}

	/**
	 * 删除试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject DeleteSampleInfo(ArrayList<Object> arrayList,
			String strSimple) {

		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		sqlIdList.add("FrmMilGetBatchAndSample.DeleteSampleInfo");
		params.add(arrayList.toArray());

		arrayList.add(0, strSimple);
		arrayList.add(1, "1");
		arrayList.add(2, "2");
		sqlIdList.add("FrmMilGetBatchAndSample.UpdatePlan");
		params.add(arrayList.toArray());

		return this.setDataById(sqlIdList, params);
	}

	/**
	 * 修改批次号试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject UpdateBatchInfo(
			ArrayList<ArrayList<ArrayList<Object>>> al) {
		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		sqlIdList.add("FrmMilGetBatchAndSample.DeleteBatchInfo");
		params.add(al.get(0).get(0).toArray());// 删除参数
		// sqlIdList.add("FrmMilGetBatchAndSample.DeleteSampleInfo");
		// params.add(al.get(0).get(0).toArray());

		for (int i = 0; i < al.get(1).size(); i++) {
			sqlIdList.add("FrmMilGetBatchAndSample.AddBatchInfo");
			params.add(al.get(1).get(i).toArray());// 增加加工表参数
		}

		// if (al.size() > 2) {
		// for (int i = 0; i < al.get(2).size(); i++) {
		// sqlIdList.add("FrmMilGetBatchAndSample.AddSampleInfo");
		// params.add(al.get(2).get(i).toArray());// 增加热处理表参数
		// }
		// }

		return this.setDataById(sqlIdList, params);
	}

	/**
	 * 修改批次号试样号信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject UpdateSampleInfo(
			ArrayList<ArrayList<ArrayList<Object>>> al) {
		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		// sqlIdList.add("FrmMilGetBatchAndSample.DeleteBatchInfo");
		// params.add(al.get(0).get(0).toArray());// 删除参数
		sqlIdList.add("FrmMilGetBatchAndSample.DeleteSampleInfo");
		params.add(al.get(0).get(0).toArray());

		// for (int i = 0; i < al.get(1).size(); i++) {
		// sqlIdList.add("FrmMilGetBatchAndSample.AddBatchInfo");
		// params.add(al.get(1).get(i).toArray());// 增加加工表参数
		// }

		if (al.size() > 1) {
			for (int i = 0; i < al.get(1).size(); i++) {
				sqlIdList.add("FrmMilGetBatchAndSample.AddSampleInfo");
				params.add(al.get(1).get(i).toArray());// 增加热处理表参数
			}
		}

		return this.setDataById(sqlIdList, params);
	}

	/**
	 * 委托确认
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject UpdatePlanInfo(ArrayList<Object> al) {

		return this.setDataBySqlID("FrmMilGetBatchAndSample.UpdatePlan", al
				.toArray());
	}

	/**
	 * 增加人工委托基础信息
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject AddCheckConsign(String strCheckNo,
			ArrayList<ArrayList<ArrayList<Object>>> al) {

		CoreReturnObject cro = new CoreReturnObject();

		ArrayList<String> sqlIdList = new ArrayList<String>();// 存放执行语句
		ArrayList<Object[]> params = new ArrayList<Object[]>();// 存放参数

		String strCheck_No = "";
		try {
			String strSql = "SELECT ? || (LPAD(DECODE(MAX(SUBSTR(CHECK_NO, -1, 1) + 1),'','1',MAX(SUBSTR(CHECK_NO, -1, 1) + 1)),1)) CHECK_NO FROM PIPE_CHECKCONSIGN_BASEINFO WHERE CHECK_NO LIKE '' || ? || '%'";
			ResultSet rs = this.getDao("TgJhyDao").ExceuteQueryForResultSet(
					strSql, new Object[] { strCheckNo, strCheckNo });
			if (rs != null) {
				// 判断是否样管下有试样下发
				while (rs.next()) {
					strCheck_No = rs.getString("CHECK_NO");
					break;
				}
				rs.close();
			} else {
				cro = cro.SetErrInfo("SQL语法错误，请联系编程人员！");
				return cro;
			}

			sqlIdList.add("FrmMilGetBatchAndSample.AddCheckConsignBase");// 一个检验编号只对应一行
			al.get(0).get(0).add(0, strCheck_No);
			params.add(al.get(0).get(0).toArray());// 增加基础表参数

			if (al.size() > 1) {
				for (int i = 0; i < al.get(1).size(); i++) {
					sqlIdList
							.add("FrmMilGetBatchAndSample.AddCheckConsignDetail");
					al.get(1).get(i).add(0, strCheck_No);
					params.add(al.get(1).get(i).toArray());// 增加详细表参数
				}
			}

			if (al.size() > 2) {
				if (al.get(2).size() > 0) {
					for (int i = 0; i < al.get(2).size(); i++) {
						sqlIdList
								.add("FrmMilGetBatchAndSample.AddCheckConsignPhyDetail");
						al.get(2).get(i).add(0, strCheck_No);
						params.add(al.get(2).get(i).toArray());// 增加理化详细表参数
					}
				}
			}

			if (al.size() > 3) {
				if (al.get(3).size() > 0) {
					for (int i = 0; i < al.get(3).size(); i++) {
						sqlIdList
								.add("FrmMilGetBatchAndSample.AddCheckConsignChemDetail");
						al.get(3).get(i).add(0, strCheck_No);
						params.add(al.get(3).get(i).toArray());// 增加化学详细表参数
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			cro = cro.SetErrInfo(e.getMessage());
			return cro;
		}

		cro = this.setDataById("TgJhyDao", sqlIdList, params);
		return cro;
	}

	/**
	 * 检验确认项目
	 * 
	 * @param arrayList
	 * @return
	 */
	public CoreReturnObject ConfirmPhy(ArrayList<Object> al) {

		return this.setDataBySqlID("FrmMilGetBatchAndSample.ConfirmPhy", al
				.toArray());
	}
}
