package com.steering.pss.sale.order.qualityDesign.server.iserver;

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

import com.steering.pss.sale.order.model.ComBaseCustomRPlineEntity;
import com.steering.pss.sale.order.model.ComBasePlineEntity;
import com.steering.pss.sale.order.model.ComMscPlineRSpecEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCPlEntity;
import com.steering.pss.sale.order.model.DesignMscBiltPlEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGxPrlEntity;
import com.steering.pss.sale.order.model.DesignMscPlStiEntity;
import com.steering.pss.sale.order.model.DesignStatusEntity;
import com.steering.pss.sale.order.model.MscProcessEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;
import com.steering.pss.sale.order.model.MscStationRPlineEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.OperationDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.OperationPointDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.ProLineManageDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.StatusDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.ProLineEntity;
import com.steering.pss.sale.order.qualityDesign.server.factory.FactoryServer;
import com.steering.pss.sale.order.qualityDesign.server.imp.CreateSpecLineImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.ProLineManageImp;

/**
 * 适用产线管理类
 * 
 * @author Administrator
 */
public class IProLineManage implements ProLineManageImp {

	// 适用产线验证流程:
	// 根据各工序查询适用产线
	// 1、电炉 查询条件:工序
	// 2、轧管与镦拔扩 查询条件: 工序 、外径 、壁厚
	// 3、铸造、锻造 查询条件：工序、规格
	// 4、加工与接箍加工 查询条件：工序、扣型、外径、壁厚
	// 5、热处理 查询条件：工序、二级工序、外径、壁厚
	/**
	 * 适用产线
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @param proDucInfo
	 *            制程
	 * @param rollSpecs
	 *            轧管规格
	 * @param blitSpecs
	 *            断面规格
	 * @param blitCSpecs
	 *            坯料规格
	 * @return ProLineEntity
	 * @throws SQLException
	 */
	public ProLineEntity analysisProLine(Connection con, DesignEntity design, DesignMscPlEntity proDucInfo,
			List<DesignMscGzGgEntity> rollSpecs, List<DesignMscBiltEntity> blitSpecs,
			List<DesignMscBiltCEntity> blitCSpecs) throws SQLException {
		DesignStatusEntity status = null;
		StatusDaoImp statusDaoImp = FactoryDao.getStatusDaoImp();
		CreateSpecLineImp specLineImp = FactoryServer.getCreateSpecLineImp();
		ProLineManageDaoImp proLineManageDaoImp = FactoryDao.getProLineManageDaoImp();
		OperationDaoImp operationDaoImp = FactoryDao.getOperationDaoImp();

		/**
		 * 产线集合
		 */
		List<DesignMscPlGxPrlEntity> proLines = new ArrayList<DesignMscPlGxPrlEntity>();
		// 全程途径码
		String wholeBacklog = proDucInfo.getWholeBacklog();
		// 冶金规范码
		String msc = design.getOrderLineMsc();
		// 全程产线号
		String mscPline = proDucInfo.getMscPline();
		// 外徑
		String dimater = design.getDimater();
		// 壁厚
		String height = design.getHeight();
		// 质量主键
		String designKey = design.getDesignKey();

		ProLineEntity lineInfo = new ProLineEntity();

		// 客户认证产线
		List<ComBaseCustomRPlineEntity> identificLines = proLineManageDaoImp.getLineByCustAndProduc(con,
				design.getFinalUser(), design.getProducCode());
		/**
		 * 电炉
		 */
		if (wholeBacklog.contains("A")) {
			List<ComBasePlineEntity> basePlines = proLineManageDaoImp.getBaseLine(con, "A");
			if (basePlines == null || basePlines.size() == 0) {
				String message = "A," + "," + "," + "," + "," + "," + "," + "1";
				status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
						"制程(" + mscPline + ")不可用！(电炉工序找不到产线)", message,
						design.getMscUnitCode(), design.getMscUnitDesc());
				lineInfo.setStatus(status);
				return lineInfo;
			} else {
				// 工序
				MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "A");
				for (ComBasePlineEntity basePline : basePlines) {
					String warnFl = "0";
					String warnDesc = "";
					if (IProLineManage.checkStation(con, msc, mscPline, "A", basePline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					DesignMscPlGxPrlEntity lineA = IProLineManage.setProLine(designKey, mscPline,
							basePline.getPlineCode(), basePline.getPlineName(), "A", process.getBacklogSeq(), warnFl,
							warnDesc, "", "", "0");
					proLines.add(lineA);
				}
			}
		}

		/**
		 * 铸造
		 */
		List<DesignMscBiltCPlEntity> biltCSpecLines = new ArrayList<DesignMscBiltCPlEntity>();
		if (wholeBacklog.contains("B")) {
			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "B");
			List<ComMscPlineRSpecEntity> plineBs = proLineManageDaoImp.getProLine(con, "B");
			for (DesignMscBiltCEntity blitCSpec : blitCSpecs) {
				List<ComMscPlineRSpecEntity> plines = IProLineManage.fiterProLine(plineBs, blitCSpec.getCSpecCode(),
						process.getProcessCodeC(), "", "", "");
				// 没有找到适用产线
				String message = "B," + "," + "," + "," + blitCSpec.getCSpecCode() + "," + "," + "," + "1";

				// 外购产线处理(如果合同对外购坯有要求，且制程有铸造与锻造工序，那么锻造只能找外购产线，如果制程只有铸造，那么铸造只能有外购产线)
				if (wholeBacklog.contains("C")) {
					plines = IProLineManage.fiterOutLine(plines, false);
				} else {
					if (design.getOutsBilietFl().equals("121801")) {
						plines = IProLineManage.fiterOutLine(plines, true);
					} else {
						plines = IProLineManage.fiterOutLine(plines, false);
					}
				}
				if (plines == null || plines.size() == 0) {
					String errMessage = "制程(" + mscPline + ")不可用！"
							+ "(铸造工序:铸造规格(" + blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC() + ") 找不到(自炼)产线!)";
					if (!wholeBacklog.contains("C") && design.getOutsBilietFl().equals("121801")) {
						errMessage = "制程(" + mscPline + ")不可用！(铸造工序:铸造规格("
								+ blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC() + ") 找不到(外购)产线!)";
					}
					status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
							errMessage, message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}

				// 客户认证产线
				List<ComBaseCustomRPlineEntity> customLineBs = IProLineManage.fiterIdentificLines(identificLines, "B");
				if (customLineBs != null && customLineBs.size() > 0) {
					String prolinesStr = "";
					for (ComBaseCustomRPlineEntity customLineB : customLineBs) {
						prolinesStr += customLineB.getPlineName() + " ";
					}
					plines = IProLineManage.checkLines(customLineBs, plines);
					if (plines == null) {
						status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
								"制程(" + mscPline + ")不可用！(铸造工序:管坯规格(" + blitCSpec.getCSpecName()
										+ ")、二级工序(" + process.getProcessDescC() + ") 找不到客户认证的铸造适用产线(" + prolinesStr
										+ "))",
								message, design.getMscUnitCode(), design.getMscUnitDesc());
						lineInfo.setStatus(status);
						return lineInfo;
					}
				}

				String wellCode = plines.get(0).getWellCode();
				String plineName = plines.get(0).getPlineName();
				for (ComMscPlineRSpecEntity pline : plines) {
					if (!wellCode.equals(pline.getWellCode())) {
						status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
								"制程(" + mscPline + ")不可用！(铸造工序:产线(" + plineName + ")与产线("
										+ pline.getPlineName() + ")产能井不一致!)",
								message, design.getMscUnitCode(), design.getMscUnitDesc());
						lineInfo.setStatus(status);
						return lineInfo;
					}
					String warnFl = "0";
					String warnDesc = "";
					if (IProLineManage.checkStation(con, msc, mscPline, "B", pline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					if (pline.getIdentFlag().equals("")) {
						pline.setIdentFlag("0");
					}
					DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
							pline.getPlineName(), "B", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
							pline.getWellName(), pline.getIdentFlag());
					proLines.add(line);
					DesignMscBiltCPlEntity biltcProLine = specLineImp.createBlank(line, blitCSpec.getCSpecCode());
					biltCSpecLines.add(biltcProLine);
				}

			}

		}

		/**
		 * 锻造
		 */
		List<DesignMscBiltPlEntity> biltSpecLines = new ArrayList<DesignMscBiltPlEntity>();
		if (wholeBacklog.contains("C")) {

			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "C");
			List<ComMscPlineRSpecEntity> plineCs = proLineManageDaoImp.getProLine(con, "C");
			for (DesignMscBiltEntity blitSpec : blitSpecs) {
				String message = "C," + "," + "," + blitSpec.getbSpecCode() + "," + "," + "," + "," + "1";
				List<ComMscPlineRSpecEntity> plines = IProLineManage.fiterProLine(plineCs, blitSpec.getbSpecCode(), "",
						"", "", "");
				// 外购与自炼
				String errMessage = "";
				if (design.getOutsBilietFl().equals("121801")) {
					plines = IProLineManage.fiterOutLine(plines, true);
					errMessage = "(外购)";
				} else {
					plines = IProLineManage.fiterOutLine(plines, false);
					errMessage = "(自炼)";
				}
				if (plines == null || plines.size() == 0) {
					status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！( 锻造工序: 锻造规格(" + blitSpec.getbSpecName()
									+ ") 找不到产线!)" + errMessage,
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}

				// 客户认证产线
				List<ComBaseCustomRPlineEntity> customLineCs = IProLineManage.fiterIdentificLines(identificLines, "C");
				if (customLineCs != null && customLineCs.size() > 0) {
					String prolinesStr = "";
					for (ComBaseCustomRPlineEntity customLineC : customLineCs) {
						prolinesStr += customLineC.getPlineName() + " ";
					}

					plines = IProLineManage.checkLines(customLineCs, plines);
					if (plines == null) {
						status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
								"制程(" + mscPline + ")不可用! (锻造工序:锻造规格(" + blitSpec.getbSpecName()
										+ ") 找不到客户认证的锻造产线(" + prolinesStr + "))",
								message, design.getMscUnitCode(), design.getMscUnitDesc());
						lineInfo.setStatus(status);
						return lineInfo;
					}
				}

				String wellCode = plines.get(0).getWellCode();
				String plineName = plines.get(0).getPlineName();
				for (ComMscPlineRSpecEntity pline : plines) {
					if (!wellCode.equals(pline.getWellCode())) {
						status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
								"制程(" + mscPline + ")不可用！(锻造工序:产线(" + plineName + ")与产线("
										+ pline.getPlineName() + ")产能井不一致!)",
								message, design.getMscUnitCode(), design.getMscUnitDesc());
						lineInfo.setStatus(status);
						return lineInfo;
					}
					String warnFl = "0";
					String warnDesc = "";
					if (IProLineManage.checkStation(con, msc, mscPline, "C", pline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					if (pline.getIdentFlag().equals("")) {
						pline.setIdentFlag("0");
					}
					DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
							pline.getPlineName(), "C", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
							pline.getWellName(), pline.getIdentFlag());
					proLines.add(line);
					DesignMscBiltPlEntity biltProLine = specLineImp.createBiltLine(line, blitSpec.getbSpecCode());
					biltSpecLines.add(biltProLine);
				}

			}

		}

		/**
		 * 轧管
		 */
		List<DesignMscGzGgPlEntity> rollSpecLines = new ArrayList<DesignMscGzGgPlEntity>();
		if (wholeBacklog.contains("D")) {
			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "D");
			List<ComMscPlineRSpecEntity> plineDs = proLineManageDaoImp.getProLine(con, "D");
			for (DesignMscGzGgEntity rollSpec : rollSpecs) {
				// 轧管产线
				String message = "D," + rollSpec.getDimater() + "," + rollSpec.getHeight() + "," + "," + "," + "," + ","
						+ "1";
				List<ComMscPlineRSpecEntity> plines = IProLineManage.fiterProLine(plineDs, "", "",
						rollSpec.getDimater(), rollSpec.getHeight(), "");
				String errMessage = "";
				if (design.getOutsStlPileFl().equals("121902") || design.getOutsStlPileFl().equals("121903")) {
					plines = IProLineManage.fiterOutLine(plines, true);
					errMessage = "(外购)";
				} else {
					plines = IProLineManage.fiterOutLine(plines, false);
					errMessage = "(自炼)";
				}
				if (plines == null || plines.size() == 0) {
					status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用! (轧制工序:外径(" + rollSpec.getDimater()
									+ ")壁厚(" + rollSpec.getHeight() + ") 找不到产线!)" + errMessage,
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}

				// 客户认证产线
				List<ComBaseCustomRPlineEntity> customLineDs = IProLineManage.fiterIdentificLines(identificLines, "D");
				if (customLineDs != null && customLineDs.size() > 0) {
					String prolinesStr = "";
					for (ComBaseCustomRPlineEntity customLineD : customLineDs) {
						prolinesStr += customLineD.getPlineName() + " ";
					}

					plines = IProLineManage.checkLines(customLineDs, plines);
					if (plines == null) {
						status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
								"制程(" + mscPline + ")不可用！( 轧制工序:外径(" + rollSpec.getDimater()
										+ ")壁厚(" + rollSpec.getHeight() + ") 找不到客户认证的轧制适用产线(" + prolinesStr + ")!)",
								message, design.getMscUnitCode(), design.getMscUnitDesc());
						lineInfo.setStatus(status);
						return lineInfo;
					}
				}

				String wellCode = plines.get(0).getWellCode();
				String plineName = plines.get(0).getPlineName();
				// 针对ASSEL做处理
				boolean flag = false;
				for (ComMscPlineRSpecEntity pline : plines) {
					if (!wellCode.equals(pline.getWellCode())) {
						status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
								"制程(" + mscPline + ")不可用！(轧制工序: 产线(" + plineName + ")与产线("
										+ pline.getPlineName() + ")产能井不一致!)",
								message, design.getMscUnitCode(), design.getMscUnitDesc());
						lineInfo.setStatus(status);
						return lineInfo;
					}
					if (pline.getPlineCode().equals("C012")) {
						flag = true;
					}
					String warnFl = "0";
					String warnDesc = "";
					if (IProLineManage.checkStation(con, msc, mscPline, "D", pline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					if (pline.getIdentFlag().equals("")) {
						pline.setIdentFlag("0");
					}
					DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
							pline.getPlineName(), "D", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
							pline.getWellName(), pline.getIdentFlag());
					proLines.add(line);
					// 轧制适用产线与规格组成轧制产线
					DesignMscGzGgPlEntity rollSpecLine = specLineImp.createSpecLine(line, rollSpec.getSpecCode());
					rollSpecLines.add(rollSpecLine);
				}
				if (design.getAsselFl().equals("1") && !flag) {
					status = statusDaoImp.setDesignStatus(design, design.getOrderLineMsc(), "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(轧制工序 :外径(" + rollSpec.getDimater()
									+ ")壁厚(" + rollSpec.getHeight() + ")没有找到Assel机组![合同对Assel有要求])",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
			}
		}

		/**
		 * 镦拔扩
		 */
		if (wholeBacklog.contains("E")) {
			// 分析镦拔扩产线
			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "E");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "E," + dimater + "," + height + "," + "," + "," + "," + "," + "1";
			List<ComMscPlineRSpecEntity> plines = proLineManageDaoImp.getProLine(con, "E");
			plines = IProLineManage.fiterProLine(plines, "", process.getProcessCodeC(), dimater, height, "");
			if (plines == null || plines.size() == 0) {
				status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
						"制程(" + mscPline + ")不可用！(镦拔扩工序 :外径(" + dimater + ") 壁厚(" + height
								+ ") 二级工序(" + process.getProcessDescC() + ")没有找到适用产线!)",
						message, design.getMscUnitCode(), design.getMscUnitDesc());
				lineInfo.setStatus(status);
				return lineInfo;
			}

			// 客户认证热处理产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineEs = IProLineManage.fiterIdentificLines(identificLines, "E");
			if (identificLineEs != null && identificLineEs.size() > 0) {
				plines = IProLineManage.checkLines(identificLineEs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineEs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(镦拔扩工序 :外径(" + dimater + " )壁厚(" + height
									+ ")二级工序(" + process.getProcessDescC() + ")没有找到客户认证的产线(" + plinesStr + "))",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(镦拔扩工序:产线(" + plineName + ")与产线("
									+ pline.getPlineName() + ")产能井不一致!)",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (IProLineManage.checkStation(con, msc, mscPline, "E", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
						pline.getPlineName(), "E", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
						pline.getWellName(), pline.getIdentFlag());
				proLines.add(line);
			}
		}

		/**
		 * 热处理
		 */
		if (wholeBacklog.contains("F")) {
			// 分析热处理产线
			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "F");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "F," + dimater + "," + height + "," + "," + "," + design.getModelCode() + ","
					+ process.getProcessCodeC() + "," + "1";
			List<ComMscPlineRSpecEntity> plines = proLineManageDaoImp.getProLine(con, "F");
			plines = IProLineManage.fiterProLine(plines, "", process.getProcessCodeC(), dimater, height, "");
			String errMessage = "";
			// 热处理是否加外购产线过滤,待确认
			// if (design.getOutsStlPileFl().equals("121903")) {
			// plines = IProLineManage.fiterOutLine(plines, true);
			// errMessage = "(外购)";
			// } else {
			// plines = IProLineManage.fiterOutLine(plines, false);
			// errMessage = "(自炼)";
			// }
			if (plines == null || plines.size() == 0) {
				status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
						"制程(" + mscPline + ")不可用！(热处理工序:外径(" + dimater + ") 壁厚(" + height
								+ ")二级工序(" + process.getProcessDescC() + ")没有找到产线)" + errMessage,
						message, design.getMscUnitCode(), design.getMscUnitDesc());
				lineInfo.setStatus(status);
				return lineInfo;
			}

			// 客户认证热处理产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineFs = IProLineManage.fiterIdentificLines(identificLines, "F");
			if (identificLineFs != null && identificLineFs.size() > 0) {
				plines = IProLineManage.checkLines(identificLineFs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineFs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(热处理工序:外径(" + dimater + ") 壁厚(" + height
									+ ")二级工序(" + process.getProcessDescC() + ")没有找到客户认证的热处理适用产线(" + plinesStr + "))",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}

			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(热处理工序:产线(" + plineName + ")与产线("
									+ pline.getPlineName() + ")产能井不一致!)",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (IProLineManage.checkStation(con, msc, mscPline, "F", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
						pline.getPlineName(), "F", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
						pline.getWellName(), pline.getIdentFlag());
				proLines.add(line);
			}
		}

		/**
		 * 
		 */
		if (wholeBacklog.contains("G")) {
			// 分析加工产线
			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "G");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "G," + dimater + "," + height + "," + "," + "," + design.getModelCode() + "," + "," + "1";
			List<ComMscPlineRSpecEntity> plineGs = proLineManageDaoImp.getProLine(con, "G");
			List<ComMscPlineRSpecEntity> plines = IProLineManage.fiterProLine(plineGs, "", process.getProcessCodeC(),
					dimater, height, design.getModelCode());
			// 如果过滤外径、壁厚没有产线则过滤产品规格
			if (plines == null || plines.size() == 0) {
				plines = proLineManageDaoImp.getLineBySpec(con, design.getModelCode(), design.getSpecCode(), "G");
			}
			if (plines == null || plines.size() == 0) {
				status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
						"制程(" + mscPline + ")不可用！(加工工序:扣型(" + design.getModelDesc() + ")、外径("
								+ dimater + ")壁厚(" + height + ") 二级工序(" + process.getProcessDescC() + ")或者 扣型("
								+ design.getModelDesc() + ") 产品规格(" + design.getSpecName() + ")没有找到产线!)",
						message, design.getMscUnitCode(), design.getMscUnitDesc());
				lineInfo.setStatus(status);
				return lineInfo;
			}
			// 加工产线特殊处理
			plines = IProLineManage.fiterLineG(plines, design.getOrderLineMscStyle());
			if (plines == null || plines.size() == 0) {
				String errMessageG = "";
				if (design.getMscStyle().equals("C") || design.getMscStyle().equals("E")) {
					errMessageG = "制程(" + mscPline + ")不可用！(加工工序 :扣型(" + design.getModelDesc()
							+ ")、外径(" + dimater + ")壁厚(" + height + ")或者 扣型(" + design.getModelDesc() + ")产品规格("
							+ design.getSpecName() + ")没有找到产能井为(加工（支）)的适用产线[MSC类型:短节+附件/双工短节])";
				} else {
					errMessageG = "制程(" + mscPline + ")不可用！(加工工序:扣型(" + design.getModelDesc()
							+ ")、外径(" + dimater + ")壁厚(" + height + ")或者 扣型(" + design.getModelDesc() + ")产品规格("
							+ design.getSpecName() + ")没有找到产能井为(加工)的产线!)";
				}
				status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91", errMessageG, message,
						design.getMscUnitCode(), design.getMscUnitDesc());
				lineInfo.setStatus(status);
				return lineInfo;
			}
			// 客户认证加工产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineGs = IProLineManage.fiterIdentificLines(identificLines, "G");
			if (identificLineGs != null && identificLineGs.size() > 0) {
				plines = IProLineManage.checkLines(identificLineGs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineGs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(加工工序: 扣型(" + design.getModelDesc()
									+ ")、外径(" + dimater + ")壁厚(" + height + ")或者 扣型(" + design.getModelDesc() + ")产品规格("
									+ design.getSpecName() + ")没有找到客户认证的产线(" + plinesStr + ")!)",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(加工工序 :产线(" + plineName + ")与产线("
									+ pline.getPlineName() + ")产能井不一致!)",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (IProLineManage.checkStation(con, msc, mscPline, "G", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
						pline.getPlineName(), "G", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
						pline.getWellName(), pline.getIdentFlag());
				proLines.add(line);
			}
		}
		/**
		 * 接箍加工
		 */
		if (wholeBacklog.contains("H")) {
			// 分析加工产线
			// 工序
			MscProcessEntity process = operationDaoImp.getOperation(con, msc, mscPline, "H");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "H," + dimater + "," + height + "," + "," + "," + design.getModelCode() + "," + "," + "1";
			List<ComMscPlineRSpecEntity> plineHs = proLineManageDaoImp.getProLine(con, "H");
			// 如果过滤外径、壁厚没有产线则过滤产品规格
			List<ComMscPlineRSpecEntity> plines = IProLineManage.fiterProLine(plineHs, "", "", dimater, height,
					design.getModelCode());
			if (plines == null || plines.size() == 0) {
				plines = proLineManageDaoImp.getLineBySpec(con, design.getModelCode(), design.getSpecCode(), "H");
			}
			if (plines == null || plines.size() == 0) {
				status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
						"制程(" + mscPline + ")不可用！(接箍加工工序 : 扣型(" + design.getModelDesc()
								+ ")、产品规格(" + design.getSpecName() + ")没有找到产线!)",
						message, design.getMscUnitCode(), design.getMscUnitDesc());
				lineInfo.setStatus(status);
				return lineInfo;
			}

			// 客户认证接箍加工产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineHs = IProLineManage.fiterIdentificLines(identificLines, "H");
			if (identificLineHs != null && identificLineHs.size() > 0) {
				plines = IProLineManage.checkLines(identificLineHs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineHs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(接箍加工工序 : 扣型(" + design.getModelDesc()
									+ ")、产品规格(" + design.getSpecName() + ")没有找到客户认证的产线(" + plinesStr + ")!)",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					status = statusDaoImp.setDesignStatus(design, msc, "适用产线", "F", "91",
							"制程(" + mscPline + ")不可用！(接箍加工工序:产线(" + plineName + ")与产线("
									+ pline.getPlineName() + ")产能井不一致!)",
							message, design.getMscUnitCode(), design.getMscUnitDesc());
					lineInfo.setStatus(status);
					return lineInfo;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (IProLineManage.checkStation(con, msc, mscPline, "H", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				// 如果不是认证产线,则赋值0
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				DesignMscPlGxPrlEntity line = IProLineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
						pline.getPlineName(), "H", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
						pline.getWellName(), pline.getIdentFlag());
				proLines.add(line);
			}
		}
		// 过滤重复的产线
		proLines = ServerDesignTool.fiterProLine(proLines);
		biltCSpecLines = ServerDesignTool.fiterBiltCSpecLine(biltCSpecLines);
		rollSpecLines = ServerDesignTool.checkComSpecPline(rollSpecLines);
		biltSpecLines = ServerDesignTool.fiterBiltSpecLine(biltSpecLines);
		lineInfo.setStatus(null);
		lineInfo.setProLines(proLines);
		lineInfo.setRollSpecLines(rollSpecLines);
		lineInfo.setBiltCSpecLines(biltCSpecLines);
		lineInfo.setBiltSpecLines(biltSpecLines);
		return lineInfo;
	}

	/**
	 * 过滤适用产线通过(外径、壁厚、二级工序、规格代码、扣型)
	 * 
	 * @param proLines
	 *            适用产线
	 * @param specCode
	 *            规格代码
	 * @param processCodeC
	 *            二级工序
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param height
	 *            扣型
	 * @return
	 */
	public static List<ComMscPlineRSpecEntity> fiterProLine(List<ComMscPlineRSpecEntity> proLines, String specCode,
			String processCodeC, String dimater, String height, String modelCode) {
		List<ComMscPlineRSpecEntity> lines = new ArrayList<ComMscPlineRSpecEntity>();
		if (proLines == null || proLines.size() == 0) {
			return null;
		} else {
			for (int i = 0; i < proLines.size(); i++) {
				ComMscPlineRSpecEntity proLine = proLines.get(i);
				// 规格代码
				if (specCode.length() == 0) {

				} else {
					if (!proLine.getSpecCode().equals(specCode)) {
						continue;
					}
				}
				// 二级工序
				if (processCodeC.length() == 0) {

				} else {
					if (!proLine.getProcessCdoeC().contains(processCodeC)) {
						continue;
					}
				}
				// 外径
				if (dimater.length() == 0) {

				} else {
					if (!proLine.getMaxD().equals("")
							&& Double.parseDouble(dimater) > Double.parseDouble(proLine.getMaxD())) {
						continue;
					}
					if (!proLine.getMinD().equals("")
							&& Double.parseDouble(dimater) < Double.parseDouble(proLine.getMinD())) {
						continue;
					}
					if (proLine.getMaxD().equals("") && proLine.getMinD().equals("")) {
						continue;
					}
				}
				// 壁厚
				if (height.length() == 0) {

				} else {
					if (!proLine.getMaxH().equals("")
							&& Double.parseDouble(height) > Double.parseDouble(proLine.getMaxH())) {
						continue;
					}
					if (!proLine.getMinH().equals("")
							&& Double.parseDouble(height) < Double.parseDouble(proLine.getMinH())) {
						continue;
					}
					if (proLine.getMaxH().equals("") && proLine.getMaxH().equals("")) {
						continue;
					}
				}
				// 扣型
				if (modelCode.length() == 0) {

				} else {
					if (!proLine.getModelCode().contains(modelCode)) {
						continue;
					}
				}
				lines.add(proLine);
			}
		}
		return lines;
	}

	/**
	 * 生成适用产线实体类
	 * 
	 * @param designKey
	 *            designKey
	 * @param mscPline
	 *            全程产线号
	 * @param plineCode
	 *            产线代码
	 * @param plineName
	 *            产线描述
	 * @param processCode
	 *            工序
	 * @param backlogSeq
	 *            全程工序顺序号
	 * @param warnFl
	 *            是否工序点警告
	 * @param warnDesc
	 *            警告内容
	 * @return DesignMscPlGxPrlEntity
	 */
	public static DesignMscPlGxPrlEntity setProLine(String designKey, String mscPline, String plineCode,
			String plineName, String processCode, String backlogSeq, String warnFl, String warnDesc, String wellCode,
			String wellName, String identFlag) {
		DesignMscPlGxPrlEntity proLine = new DesignMscPlGxPrlEntity();
		proLine.setDesginKey(designKey);
		proLine.setMscPline(mscPline);
		proLine.setPlineCode(plineCode);
		proLine.setPlineName(plineName);
		proLine.setProcessCode(processCode);
		proLine.setBacklogSeq(backlogSeq);
		proLine.setWarnFl(warnFl);
		proLine.setWarnDesc(warnDesc);
		proLine.setWellCode(wellCode);
		proLine.setWellName(wellName);
		proLine.setIdentFlag(identFlag);
		return proLine;

	}

	/**
	 * 工序点警告分析
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            合同行冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @param processCode
	 *            工序代码
	 * @param proLine
	 *            产线
	 * @return boolean
	 * @throws SQLException
	 */
	public static boolean checkStation(Connection con, String msc, String mscPline, String processCode, String proLine)
			throws SQLException {
		OperationPointDaoImp operationPointDaoImp = FactoryDao.getOperationPointDaoImp();
		ProLineManageDaoImp proLineManageDaoImp = FactoryDao.getProLineManageDaoImp();
		// 通过工序，冶金规范码，全程产线号找工序点集合
		List<DesignMscPlStiEntity> stations = operationPointDaoImp.getDesignPoint(con, msc, mscPline, processCode);
		// 将工序点代码组成一个字符串数组
		String[] stationCodes = new String[stations.size()];
		for (int i = 0; i < stations.size(); i++) {
			stationCodes[i] = stations.get(i).getStationCode();
		}
		// 通过产线找工序点集合
		List<MscStationRPlineEntity> plines = proLineManageDaoImp.getStationRPline(con, proLine);
		// 将工序点代码组成一个字符串数组
		String[] plineCodes = new String[plines.size()];
		for (int i = 0; i < plines.size(); i++) {
			plineCodes[i] = plines.get(i).getStationCode();
		}
		// 比较两个字符串数组是否相同，如果相同返回true,否则返回false
		boolean flag = QualityDesignTool.checkStringArraysCommon(stationCodes, plineCodes);
		return flag;

	}

	/**
	 * 过滤各工序认证产线
	 * 
	 * @param identificLines
	 *            客户认证产线
	 * @param processCode
	 *            工序
	 * @return List<ComBaseCustomRPlineEntity>
	 */
	public static List<ComBaseCustomRPlineEntity> fiterIdentificLines(List<ComBaseCustomRPlineEntity> identificLines,
			String processCode) {
		if (identificLines == null) {
			return null;
		} else {
			List<ComBaseCustomRPlineEntity> identificLineAs = new ArrayList<ComBaseCustomRPlineEntity>();
			for (ComBaseCustomRPlineEntity identificLine : identificLines) {
				if (identificLine.getProcessCdoe().equals(processCode)) {
					identificLineAs.add(identificLine);
				}
			}
			if (identificLineAs.size() == 0) {
				return null;
			} else {
				return identificLineAs;
			}
		}
	}

	/**
	 * 验证客户认证产线
	 * 
	 * @param identificLines
	 * @param lines
	 * @return
	 */
	public static List<ComMscPlineRSpecEntity> checkLines(List<ComBaseCustomRPlineEntity> identificLines,
			List<ComMscPlineRSpecEntity> lines) {
		if (identificLines == null) {
			return lines;
		} else {
			List<ComMscPlineRSpecEntity> plines = new ArrayList<ComMscPlineRSpecEntity>();
			for (ComBaseCustomRPlineEntity identificLine : identificLines) {
				for (ComMscPlineRSpecEntity line : lines) {
					if (identificLine.getPlineCode().equals(line.getPlineCode())) {
						line.setIdentFlag("1");
						plines.add(line);
						continue;
					}
				}
			}
			if (plines.size() == 0) {
				return null;
			} else {
				return plines;
			}

		}
	}

	/**
	 * 加工产线特殊处理(如果MSC规格类型：短节+附件,双工短节则产能井为J014，否则为J007)
	 * 
	 * @param proLines
	 * @param mscStyle
	 * @return
	 */
	public static List<ComMscPlineRSpecEntity> fiterLineG(List<ComMscPlineRSpecEntity> proLines, String mscStyle) {
		if (proLines == null) {
			return null;
		}
		List<ComMscPlineRSpecEntity> list = new ArrayList<ComMscPlineRSpecEntity>();
		for (ComMscPlineRSpecEntity proLine : proLines) {
			if (mscStyle.equals("C") || mscStyle.equals("E")) {
				if (proLine.getWellCode().equals("J014")) {
					list.add(proLine);
				}
			} else {
				if (!proLine.getWellCode().equals("J014")) {
					list.add(proLine);
				}
			}
		}
		return list;
	}

	/**
	 * 过滤外购与自炼产线
	 * 
	 * @param lines
	 *            产线
	 * @param flag
	 *            true 外购 false 自炼
	 * @return
	 */
	public static List<ComMscPlineRSpecEntity> fiterOutLine(List<ComMscPlineRSpecEntity> lines, boolean flag) {
		if (lines == null || lines.size() == 0) {
			return null;
		}
		List<ComMscPlineRSpecEntity> fiterLines = new ArrayList<ComMscPlineRSpecEntity>();
		for (ComMscPlineRSpecEntity line : lines) {
			if (flag) {
				if (line.getPlineName().contains("外购")) {
					fiterLines.add(line);
				}
			} else {
				if (!line.getPlineName().contains("外购")) {
					fiterLines.add(line);
				}
			}
		}
		return fiterLines;
	}

}
