package com.steering.pss.plnsaleord.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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.pss.plnsaleord.order.model.PlnOrdInfo;

/**
 *接箍排产订单补齐
 * 
 * @author 滕建刚
 */
public class FrmBlOrderPln extends DBCommon {
	/**
	 * 查询
	 * 
	 * @param list
	 * @param isConfirm
	 * @return
	 */
	public CoreReturnObject querySaleOrder(String orderNo,
			String[] strExtStrats, String strIfProJgNo) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmBlOrderPln.querySaleOrder");
		if (!orderNo.equals("")) {
			sql += " AND (T.ORDER_NO || '/' || T.ORDER_SEQ) LIKE '%' || NLS_UPPER('"
					+ orderNo + "') || '%' ) E1 WHERE 1=1";
		}
		sql += CoreConverter
				.toSqlConditionString("DECODE((SELECT GET_PROCESS_STATUS(E.ORD_LN_DLY_PK,E.PROCESS_SEQ)FROM DUAL),'0','计划','1','生产中','2','生产完成','3','关闭A')", strExtStrats);
		sql += " )E1";
		if (strIfProJgNo.equals("")) {
			sql += " WHERE E1.If_Pro_Jg_No IS NOT NULL";
		}
		cro = this.getData(sql);
		return cro;
	}

	/**
	 * 合同下发
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject canCelIussed(ArrayList<ArrayList<String>> list,
			String flag) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		ResultSet rsta = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);

			int failNum = 0;
			for (int i = 0; i < list.size(); i++) {
				PlnOrdInfo ordInfo = new PlnOrdInfo();
	
				String iussedMan = list.get(i).get(0);
				String ordLnDlyPk = list.get(i).get(1);
				String plnDivideId = list.get(i).get(2);
				String orderUnit = list.get(i).get(3);// 合同订货单位
				double planWt = Double.parseDouble(list.get(i).get(4));
				int planNum = Integer.parseInt(list.get(i).get(5));
				String orderNoSeq = list.get(i).get(6);// 合同号
				String orderSource = list.get(i).get(7);// 排产分类

				ordInfo.setOrdLnDlyPk(ordLnDlyPk);
				ordInfo.setPlnDivideId(plnDivideId);
				double planPMain = 0;
				String cutHeadDbk = "";
				String cutendDbk = "";
				String longerDbk = "";
				String gradeCode = "";
				// 查询合同信息
				String sql = CoreXmlSqlParsersFactory
						.getSql("CorePlnProcessOrd.querySaleProcStatus");
				psta = conn.prepareStatement(sql);
				psta.setString(1, ordLnDlyPk);
				psta.setString(2, plnDivideId);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					gradeCode = rs.getString("GRADECODE");
					planPMain = rs.getDouble("PLAN_P_MAIN");
					ordInfo.setOrderNo(rs.getString("ORDER_NO"));
					ordInfo.setOrderSeq(rs.getString("ORDER_SEQ"));
					ordInfo.setPlanWtD(rs.getDouble("PLAN_P_MAIN"));
					ordInfo.setPlanNumD(rs.getInt("PLAN_NUM_D"));
					ordInfo.setOutMultD(rs.getInt("OUT_MULT_D"));
					ordInfo.setGradecodeAll(rs.getString("GRADECODE_ALL"));
					if (planPMain == 0) {
						return cro;
					}
					cutHeadDbk = rs.getString("LEN_CUTHEAD_DBK");
					cutendDbk = rs.getString("LEN_CUTEND_DBK");
					longerDbk = rs.getString("LEN_LONGER_DBK");
					ordInfo.setAddDbkLen(rs.getDouble("LEN_CUTHEAD_DBK")
							+ rs.getDouble("LEN_CUTEND_DBK"));
					ordInfo.setAddDcLen(rs.getDouble("LEN_LONGER_DBK"));
					ordInfo.setLenCutheadDbk(cutHeadDbk);
					ordInfo.setLenCutendCbk(cutendDbk);
					ordInfo.setRatioDbk(rs.getDouble("RATIO_DBK"));
					ordInfo.setTotlength(rs.getDouble("TOTLENGTH"));
					ordInfo.setTotlengthUnit(rs.getString("TOTLENGTH_UNIT"));
					ordInfo.setAbc(rs.getString("ABC"));
					ordInfo.setOutputStdNo(rs.getString("OUTPUT_STD_NO"));
					ordInfo.setUseDesc(rs.getString("USE_DESC"));
					ordInfo.setStdName(rs.getString("STD_NAME"));
					ordInfo.setRollMonth(rs.getString("ROLL_MONTH"));
					ordInfo.setEndlenSpecstd(rs.getDouble("ENDLEN_SPECSTD"));
					ordInfo.setPlanPMainMin(rs.getDouble("PLAN_P_MAIN_MIN"));
					ordInfo.setAvgLenMin(rs.getDouble("AVG_LEN_MIN"));
					ordInfo.setMsc(rs.getString("MSC"));
					ordInfo.setIndexSeq(rs.getString("INDEX_SEQ"));
					ordInfo.setDesginKey(rs.getString("DESGIN_KEY"));
					ordInfo.setBom(rs.getString("BOM"));
					ordInfo.setOutdiameter(rs.getDouble("OUTDIAMETER"));
					ordInfo.setWallthick(rs.getDouble("WALLTHICK"));
					ordInfo.setAimLenRoll(rs.getDouble("AIM_LEN_ROLL"));
					ordInfo.setwJ(rs.getDouble("WEIGHT_PER_M"));
					ordInfo.setLengthmax(rs.getDouble("LENGTHMAX"));
					ordInfo.setLengthmin(rs.getDouble("LENGTHMIN"));
					ordInfo.setSiglLen(rs.getDouble("SIGL_LEN"));
					ordInfo.setShortest(rs.getDouble("SHORTEST"));
					ordInfo.setCheckJVal(rs.getDouble("CHECK_J_VAL"));
					ordInfo.setAddInspect(rs.getDouble("INSPECTION_VALUE"));
					ordInfo.setLenRollMin(rs.getDouble("LEN_ROLL_MIN"));
					ordInfo.setLenRollMax(rs.getDouble("LEN_ROLL_MAX"));
					ordInfo.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
					ordInfo.setOrderUnit(rs.getString("ORDER_UNIT"));
					ordInfo.setPlineName(rs.getString("GROUP_ROLL_NAME"));
					ordInfo.setProduceSort(rs.getString("PRODUCE_SORT"));
					ordInfo.setOrderSource(rs.getString("ORDER_SOURCE"));
					ordInfo.setLenScope(rs.getDouble("LEN_SCOPE"));
					if (rs.getString("ORDER_UNIT") != null
							&& (rs.getString("ORDER_UNIT").trim().equals("吨") || rs
									.getString("ORDER_UNIT").trim()
									.equals("英磅"))) {
						if (rs.getString("WEIGH_TYPE") != null
								&& rs.getString("WEIGH_TYPE").contains("理")) {
							ordInfo.setWeighType("0");
						} else {
							ordInfo.setWeighType("1");
						}
					} else {
						ordInfo.setWeighType("-1");
					}
					if (rs.getString("SHRT_STOVE") != null) {
						ordInfo.setShrtStove(rs.getString("SHRT_STOVE"));
					}
					if (rs.getString("PRDCR_NO") != null) {
						ordInfo.setPrdcrNo(rs.getString("PRDCR_NO"));
					}
					if (rs.getString("GROUP_ROLL") != null) {
						ordInfo.setPlineD(rs.getString("GROUP_ROLL"));
					}
					if (rs.getString("MODEL_DESC") != null) {
						ordInfo.setModelDesc(rs.getString("MODEL_DESC"));
					}
					if (rs.getString("OUTS_STL_PIPE_FL") != null) {
						ordInfo.setOutsStlPipeFl(rs
								.getString("OUTS_STL_PIPE_FL"));
					}
					if (rs.getString("OUTS_BILLET_FL") != null) {
						if (rs.getString("OUTS_BILLET_FL").equals("121801")) {
							ordInfo.setOutsBilletFl("1");// 外购
						} else {
							ordInfo.setOutsBilletFl("0");// 自炼
						}
					} else {
						ordInfo.setOutsBilletFl("0");// 自炼
					}
				} else {
					cro.setV_errMsg("未找到合同信息。");
					return cro;
				}
				rs.close();
				psta.close();

				String blType = "";
				if (orderSource.equals("备料需求")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.queryPorderBlReq");
					psta = conn.prepareStatement(sql);
					psta.setString(1, orderNoSeq);
 					rs = psta.executeQuery();
					if (rs.next()) {
						blType = rs.getString("BLTYPE");
					}
					rs.close();
					psta.close();
					// ordInfo.setOutsBilletFl("0");// 自炼
				}
				if (blType.equals("接箍料")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst1");
					psta = conn.prepareStatement(sql);
					psta.setString(1, ordInfo.getPrdcrNo());
					psta.setString(2, ordInfo.getIndexSeq());
					rs = psta.executeQuery();
					if (rs.next()) {
						if (rs.getString("PRDCR_PRC") != null
								&& !rs.getString("PRDCR_PRC").equals("")) {
							ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
						}
					}
					rs.close();
					psta.close();

					// 生成接箍排产订单
					String mess = CorePlnOrderJgS.addOrderJgS(conn, ordInfo,
							gradeCode, planPMain);
					if (!mess.equals("")) {
						cro.setV_errMsg(mess);
						return cro;
					}
				}
			}

			conn.commit();

		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} catch (Exception e) {
			if (conn != null) {
				try {
					conn.rollback();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				}
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(e.getLocalizedMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cro;
	}

}