package com.steering.pss.sale.order.craftDesign.server;

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.dao.DeleteLineDao;
import com.steering.pss.sale.order.craftDesign.dao.LineManageDao;
import com.steering.pss.sale.order.craftDesign.dao.OperationDao;
import com.steering.pss.sale.order.craftDesign.dao.OperationPointDao;
import com.steering.pss.sale.order.craftDesign.model.*;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.SlmOrderHeadEntity;
import com.steering.pss.sale.order.model.SlmOrderLineEntity;
import com.steering.pss.sale.order.qualityDesign.server.iserver.CoreCastWell;

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

/**
 * 工艺评审-产线
 * 
 * @author Administrator
 * 
 */
public class LineManage {
	// 适用产线验证流程:
	// 根据各工序查询适用产线
	// 1、电炉 查询条件:工序
	// 2、轧管与镦拔扩 查询条件: 工序 、外径 、壁厚
	// 3、铸造、锻造 查询条件：工序、规格
	// 4、加工与接箍加工 查询条件：工序、扣型、外径、壁厚
	// 5、热处理 查询条件：工序、二级工序、外径、壁厚
	/**
	 * 适用产线
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @param proDucInfo
	 *            制程
	 * @param rollSpecs
	 *            轧管规格
	 * @param blitSpecs
	 *            断面规格
	 * @param blitCSpecs
	 *            坯料规格
	 * @return ProLineEntity
	 * @throws SQLException
	 */
	public LineEntity analysisProLine(Connection con, CraftEntity design, CraftOrdDesignMscPlEntity proDucInfo,
			List<CraftOrdDesignMscGzGgEntity> rollSpecs, List<CraftOrdDesignMscBiltEntity> blitSpecs,
			List<CraftOrdDesignMscBiltCEntity> blitCSpecs, InnerStdEntity innerStdEntity) throws SQLException {
		LineManageDao lineManageDao = new LineManageDao();
		OperationDao operationDaoImp = new OperationDao();

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

		// 客户认证产线
		List<ComBaseCustomRPlineEntity> identificLines = lineManageDao.getLineByCustAndProduc(con,
				design.getFinalUser(), design.getProducCode());
		
		//销售禁用产线
		List<String> disablePlines = (new DeleteLineDao()).getQualityDesignLine2(con, design.getOrdLnPk());


		//区分产线
		// 获取销售组织
		CoreCastWell coreCastWell = new CoreCastWell();
		String [] saleInfo = coreCastWell.getSaleInfo(con, design.getOrdLnPk().substring(0,design.getOrdLnPk().length()-3));
		String supp = "120501";

		if (saleInfo != null && saleInfo.length > 0 && "100105".equals(saleInfo[0])) {	// 天淮合同
			supp = "120504";
		}
		SqlBuilder b = new SqlBuilder("CoreOrderCopy.queryHead");
		b.Fill(new String[]{ design.getOrdLnPk().substring(0,design.getOrdLnPk().length()-3) });
		SlmOrderHeadEntity orderHeadEntity = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderHeadEntity.class);
		if (orderHeadEntity != null && "120504".equals(orderHeadEntity.getSupplyUnit()) && "120607".equals(orderHeadEntity.getFstDraweeUnit())) {
			supp = "120504";
		}
		if(orderHeadEntity != null && "120504".equals(orderHeadEntity.getSupplyUnit())){
			supp = "120504";
		}
		if (design.getOrdLnPk().startsWith("BL")) {
			supp = "";
		}
		b = new SqlBuilder("CoreOrderCopy.queryLineByLnPk");
		b.Fill(new String[]{ design.getOrdLnPk() });
		SlmOrderLineEntity orderLineEntity = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderLineEntity.class);
		if (orderLineEntity != null && orderLineEntity.getOutsStlPipeFl().equals("121903") && !"120504".equals(orderHeadEntity.getSupplyUnit())) {
			supp = "";
		}
		//区分产线

		/**
		 * 电炉
		 */
		if (wholeBacklog.contains("A")) {
			List<ComBasePlineEntity> basePlines = lineManageDao.getBaseLine(con, "A");
		    
			if (basePlines == null || basePlines.size() == 0) {
				String message = "A," + "," + "," + "," + "," + "," + "," + "1";
				String errMessage = "制程(" + mscPline + ")不可用！(电炉工序找不到适用产线)";
				lineEntity.setFlag(false);
				lineEntity.setErrMessage(errMessage);
				lineEntity.setObjKey(message);
				return lineEntity;
			} else {
				//删除禁用产线
				for(int i = 0; i < basePlines.size(); i++){
					if(disablePlines.contains(basePlines.get(i).getPlineCode())){
						String plineName = basePlines.get(i).getPlineName();
						basePlines.remove(basePlines.get(i));
						i--;
						if(basePlines.size() == 0){
							String message = "A," + "," + "," + "," + "," + "," + "," + "1";
							String errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")不可用！(电炉工序找不到适用产线)";
							lineEntity.setFlag(false);
							lineEntity.setErrMessage(errMessage);
							lineEntity.setObjKey(message);
							return lineEntity;
						}
					}
				}
				
				// 工序
				CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "A");
				for (ComBasePlineEntity basePline : basePlines) {
					String warnFl = "0";
					String warnDesc = "";
					if (LineManage.checkStation(con, msc, mscPline, "A", basePline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					CraftOrdDesignMscPlGxPrlEntity lineA = LineManage.setProLine(designKey, mscPline,
							basePline.getPlineCode(), basePline.getPlineName(), "A", process.getBacklogSeq(), warnFl,
							warnDesc, "", "", "0");
					proLines.add(lineA);
				}
			}
		}
		
		//炼钢工序是否存在自炼索引码
		boolean existOwnIndexA = false;
		boolean existPurchaseIndexA = false;
		if(innerStdEntity != null){
			List<CraftOrdDesignStdCicEntity> chems = innerStdEntity
					.getChems();
			List<String> cics = new ArrayList<String>();
			for(CraftOrdDesignStdCicEntity chem : chems){
				if(existOwnIndexA && existPurchaseIndexA){
					break;
				}
				if(!chem.getMscPline().equals(mscPline)) continue;
				if(!chem.getProcessCode().equals("A")) continue;
				if(!cics.contains(chem.getCic())){
					cics.add(chem.getCic());
					if(!existOwnIndexA){ //判断是否存在自炼索引码
						String ownIndexA = lineManageDao.getOwnIndexA(con, chem.getCic());
						if(!ownIndexA.equals("0")){
							existOwnIndexA = true;
						}
					}
					
					if(!existPurchaseIndexA){ //判断是否存在外购索引码
						String purchaseIndexA = lineManageDao.getPurchaseIndexA(con, chem.getCic());
						if(!purchaseIndexA.equals("0")){
							existPurchaseIndexA = true;
						}
					}
				}
			}
		}
		

		/**
		 * 铸造
		 */
		List<CraftOrdDesignMscBiltCPlEntity> biltCSpecLines = new ArrayList<CraftOrdDesignMscBiltCPlEntity>();
		if (wholeBacklog.contains("B")) {
			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "B");
			List<ComMscPlineRSpecEntity> plineBs = lineManageDao.getProLine(con, "B",null);
			boolean deleteOwnPline = true;
			boolean deletePurchasePline = true;
			for (CraftOrdDesignMscBiltCEntity blitCSpec : blitCSpecs) {
				List<ComMscPlineRSpecEntity> plines = LineManage.fiterProLine(plineBs, blitCSpec.getCSpecCode(),
						process.getProcessCodeC(), "", "", "");
				// 没有找到适用产线
				String message = "B," + "," + "," + "," + blitCSpec.getCSpecCode() + "," + "," + "," + "1";
				String errMessage = "";
//				// 如果制程没有锻造工序，并且合同允许外购坯，则产线只能是外购产线
//				if (!wholeBacklog.contains("C") && design.getOutsBilietFl().equals("121801")) {
//					plines = LineManage.fiterOutLine(plines, true);
//				} else {
//					plines = LineManage.fiterOutLine(plines, false);
//				}
				
				//如果合同是外购坯，那么铸造和锻造都必须拿到自炼产线以及外购产线。
				//proDucInfo.getMscPline()
				if(design.getOutsBilietFl().equals("121801")){
					deletePurchasePline = false;
					List<ComMscPlineRSpecEntity> plines2 = LineManage.fiterOutLine(plines, true);
					// 251断面的坯子都是自炼产线，不卡外购产线。
					if(!blitCSpec.getCSpecCode().equals("C005425") && (plines2 == null || plines2.size() == 0)){
						errMessage = "制程(" + mscPline + ")不可用！(铸造工序,铸造规格("
								+ blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC()
								+ ") )找不到(外购)适用产线![合同允许外购坯,铸造必须找到外购产线])";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
					plines2 = LineManage.fiterOutLine(plines, false);
					if(plines2 != null && plines2.size() > 0){
						if(existOwnIndexA){
							deleteOwnPline = false;
						}else{
							plines = LineManage.fiterOutLine(plines, true);
						}
					}
				}else{
					deleteOwnPline = false;
					List<ComMscPlineRSpecEntity> plines2 = LineManage.fiterOutLine(plines, false);
					if (!blitCSpec.getCSpecCode().equals("C002551") && (plines2 == null || plines2.size() == 0)) {
						errMessage = "制程(" + mscPline + ")不可用！(铸造工序,管坯规格("
								+ blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC() + "))找不到(自炼)适用产线!)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
					
					plines2 = LineManage.fiterOutLine(plines, true);
					if(plines2 != null && plines2.size() > 0){
						if(existPurchaseIndexA){
							deletePurchasePline = false;
						}else{
							plines = LineManage.fiterOutLine(plines, false);
						}
					}
				}
				
//				if (plines == null || plines.size() == 0) {
//					errMessage = "";
//					if (!wholeBacklog.contains("C") && design.getOutsBilietFl().equals("121801")) {
//						errMessage = "制程(" + mscPline + ")不可用！(铸造工序,铸造规格("
//								+ blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC()
//								+ ") )找不到(外购)适用产线![合同允许外购坯,铸造只能找外购产线])";
//					} else {
//						errMessage = "制程(" + mscPline + ")不可用！(铸造工序,管坯规格("
//								+ blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC() + "))找不到(自炼)适用产线!)";
//					}
//					lineEntity.setFlag(false);
//					lineEntity.setErrMessage(errMessage);
//					lineEntity.setObjKey(message);
//					return lineEntity;
//				}
				
				//删除禁用产线
				for(int i = 0; i < plines.size(); i++){
					if(disablePlines.contains(plines.get(i).getPlineCode())){
						String plineName = plines.get(i).getPlineName();
						plines.remove(plines.get(i));
						i--;
						if(plines.size() == 0){
							errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")"
									+ "不可用！(铸造工序找不到适用产线)";
							lineEntity.setFlag(false);
							lineEntity.setErrMessage(errMessage);
							lineEntity.setObjKey(message);
							return lineEntity;
						}
					}
				}
				
				// 客户认证产线
				List<ComBaseCustomRPlineEntity> customLineBs = LineManage.fiterIdentificLines(identificLines, "B");
				if (customLineBs != null && customLineBs.size() > 0) {
					String prolinesStr = "";
					for (ComBaseCustomRPlineEntity customLineB : customLineBs) {
						prolinesStr += customLineB.getPlineName() + " ";
					}
					plines = LineManage.checkLines(customLineBs, plines);
					if (plines == null) {
						errMessage = "制程(" + mscPline + ")不可用！(铸造工序:管坯规格("
								+ blitCSpec.getCSpecName() + ")、二级工序(" + process.getProcessDescC() + ") 找不到客户认证的铸造适用产线("
								+ prolinesStr + "))";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}
//				String wellCode = plines.get(0).getWellCode();
//				String plineName = plines.get(0).getPlineName();
				for (ComMscPlineRSpecEntity pline : plines) {
//					if (!wellCode.equals(pline.getWellCode())) {
//						errMessage = "制程(" + mscPline + ")不可用！(铸造工序 产线(" + plineName + ")与产线("
//								+ pline.getPlineName() + ")产能井不一致)";
//						lineEntity.setFlag(false);
//						lineEntity.setErrMessage(errMessage);
//						lineEntity.setObjKey(message);
//						return lineEntity;
//					}
					String warnFl = "0";
					String warnDesc = "";
					if (LineManage.checkStation(con, msc, mscPline, "B", pline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					if (pline.getIdentFlag().equals("")) {
						pline.setIdentFlag("0");
					}
					CraftOrdDesignMscPlGxPrlEntity line = LineManage.setProLine(designKey, mscPline,
							pline.getPlineCode(), pline.getPlineName(), "B", process.getBacklogSeq(), warnFl, warnDesc,
							pline.getWellCode(), pline.getWellName(), pline.getIdentFlag());
					proLines.add(line);
					CraftOrdDesignMscBiltCPlEntity biltcProLine = LineManage.createBlank(line,
							blitCSpec.getCSpecCode());
					biltCSpecLines.add(biltcProLine);
				}

			}
			
			if(deleteOwnPline && blitCSpecs.size() > 0){
				deleteOwnCic(innerStdEntity.getChems(), innerStdEntity.getProcessList(),
						lineManageDao, mscPline, con);
			}
			if(deletePurchasePline  && blitCSpecs.size() > 0){
				deletePurchaseCic(innerStdEntity.getChems(), innerStdEntity.getProcessList(),
						lineManageDao, mscPline, con);
			}

		}

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

			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "C");
			List<ComMscPlineRSpecEntity> plineCs = lineManageDao.getProLine(con, "C",null);
			boolean deleteOwnPline = true;
			boolean deletePurchasePline = true;
			for (CraftOrdDesignMscBiltEntity blitSpec : blitSpecs) {
				String message = "C," + "," + "," + blitSpec.getBSpecCode() + "," + "," + "," + "," + "1";
				List<ComMscPlineRSpecEntity> plines = LineManage.fiterProLine(plineCs, blitSpec.getBSpecCode(), "", "",
						"", "");
				String errMessage = "";
				
				//锻造
				if(design.getOutsBilietFl().equals("121801")){
					deletePurchasePline = false;
					List<ComMscPlineRSpecEntity> plines2 = LineManage.fiterOutLine(plines, true);
					if(!blitSpec.getBSpecCode().equals("C005425") && (plines2 == null || plines2.size() == 0)){
						errMessage = "制程(" + mscPline + ")不可用！(铸造工序,锻造规格("
								+ blitSpec.getBSpecName() + ")、二级工序(" + process.getProcessDescC()
								+ ") )找不到(外购)适用产线![合同允许外购坯,锻造必须找到外购产线])";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
					plines2 = LineManage.fiterOutLine(plines, false);
					if(plines2 != null && plines2.size() > 0){
						if(existOwnIndexA){
							deleteOwnPline = false;
						}else{
							plines = LineManage.fiterOutLine(plines, true);
						}
					}
				}else{
					deleteOwnPline = false;
					List<ComMscPlineRSpecEntity> plines2 = LineManage.fiterOutLine(plines, false);
					if (!blitSpec.getBSpecCode().equals("C002551") && (plines2 == null || plines2.size() == 0)) {
						errMessage = "制程(" + mscPline + ")不可用！(锻造工序,管坯规格("
								+ blitSpec.getBSpecName() + ")、二级工序(" + process.getProcessDescC() + "))找不到(自炼)适用产线!)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
					
					plines2 = LineManage.fiterOutLine(plines, true);
					if(plines2 != null && plines2.size() > 0){
						if(existPurchaseIndexA){
							deletePurchasePline = false;
						}else{
							plines = LineManage.fiterOutLine(plines, false);
						}
					}
				}
				
				// 合同允许外购坯，则锻造只能找外购产线，否则找自炼产线
//				if (design.getOutsBilietFl().equals("121801")) {
//					plines = LineManage.fiterOutLine(plines, true);
//				} else {
//					plines = LineManage.fiterOutLine(plines, false);
//				}
//				if (plines == null || plines.size() == 0) {
//					if (design.getOutsBilietFl().equals("121801")) {
//						errMessage = "制程(" + mscPline + " )不可用！(锻造工序:锻造规格("
//								+ blitSpec.getBSpecName() + ") 找不到(外购)适用产线![合同允许外购坯,锻造只允许外购产线])";
//					} else {
//						errMessage = "制程(" + mscPline + ")不可用！(锻造工序:锻造规格("
//								+ blitSpec.getBSpecName() + ") 找不到(自炼)适用产线!)";
//					}
//					lineEntity.setFlag(false);
//					lineEntity.setErrMessage(errMessage);
//					lineEntity.setObjKey(message);
//					return lineEntity;
//				}
				
				//删除禁用产线
				for(int i = 0; i < plines.size(); i++){
					if(disablePlines.contains(plines.get(i).getPlineCode())){
						String plineName = plines.get(i).getPlineName();
						plines.remove(plines.get(i));
						i--;
						if(plines.size() == 0){
							errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")"
									+ "不可用！(锻造工序找不到适用产线)";
							lineEntity.setFlag(false);
							lineEntity.setErrMessage(errMessage);
							lineEntity.setObjKey(message);
							return lineEntity;
						}
					}
				}

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

					plines = LineManage.checkLines(customLineCs, plines);
					if (plines == null) {
						errMessage = "制程(" + mscPline + ")不可用！(锻造工序:锻造规格("
								+ blitSpec.getBSpecName() + ") 找不到客户认证的锻造适用产线(" + prolinesStr + ")!)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}

//				String wellCode = plines.get(0).getWellCode();
//				String plineName = plines.get(0).getPlineName();
				for (ComMscPlineRSpecEntity pline : plines) {
//					if (!wellCode.equals(pline.getWellCode())) {
//						errMessage = "制程(" + mscPline + ")不可用！(锻造工序:产线(" + plineName + ")与产线("
//								+ pline.getPlineName() + ")产能井不一致)";
//						lineEntity.setFlag(false);
//						lineEntity.setErrMessage(errMessage);
//						lineEntity.setObjKey(message);
//						return lineEntity;
//					}
					String warnFl = "0";
					String warnDesc = "";
					if (LineManage.checkStation(con, msc, mscPline, "C", pline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					if (pline.getIdentFlag().equals("")) {
						pline.setIdentFlag("0");
					}
					CraftOrdDesignMscPlGxPrlEntity line = LineManage.setProLine(designKey, mscPline,
							pline.getPlineCode(), pline.getPlineName(), "C", process.getBacklogSeq(), warnFl, warnDesc,
							pline.getWellCode(), pline.getWellName(), pline.getIdentFlag());
					proLines.add(line);
					CraftOrdDesignMscBiltPlEntity biltProLine = LineManage.createBiltLine(line,
							blitSpec.getBSpecCode());
					biltSpecLines.add(biltProLine);
				}

			}
			
			if(deleteOwnPline && blitSpecs.size() > 0){
				deleteOwnCic(innerStdEntity.getChems(), innerStdEntity.getProcessList(),
						lineManageDao, mscPline, con);
			}
			if(deletePurchasePline && blitSpecs.size() > 0){
				deletePurchaseCic(innerStdEntity.getChems(), innerStdEntity.getProcessList(),
						lineManageDao, mscPline, con);
			}
		}

		/**
		 * 轧管
		 */
		List<CraftOrdDesignMscGzGgPlEntity> rollSpecLines = new ArrayList<CraftOrdDesignMscGzGgPlEntity>();
		if (wholeBacklog.contains("D")) {
			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "D");
			List<ComMscPlineRSpecEntity> plineDs = lineManageDao.getProLine(con, "D",supp);
			for (CraftOrdDesignMscGzGgEntity rollSpec : rollSpecs) {
				// 轧管产线
				String message = "D," + rollSpec.getDimater() + "," + rollSpec.getHeight() + "," + "," + "," + "," + ","
						+ "1";
				List<ComMscPlineRSpecEntity> plines = LineManage.fiterProLine(plineDs, "", "", rollSpec.getDimater(),
						rollSpec.getHeight(), "");
				// 如果合同允许原料管,则轧管只能外购产线，否则自炼产线
				if (design.getOutsStlPipeFl().equals("121902") || design.getOutsStlPipeFl().equals("121903")) {
					plines = LineManage.fiterOutLine(plines, true);
				} else {
					plines = LineManage.fiterOutLine(plines, false);
				}
				String errMessage = "";
				if (plines == null || plines.size() == 0) {
					if (design.getOutsStlPipeFl().equals("121902") || design.getOutsStlPipeFl().equals("121903")) {
						errMessage = "制程(" + mscPline + " )不可用！(轧制工序:外径(" + rollSpec.getDimater()
								+ ")、壁厚(" + rollSpec.getHeight() + ") 找不到(外购)适用产线![合同允许外购管(原料管),轧管工序只允许外购产线])";
					} else {
						errMessage = "制程(" + mscPline + ")不可用！(的轧制工序: 外径("
								+ rollSpec.getDimater() + ")、壁厚(" + rollSpec.getHeight() + ") 找不到(自炼)适用产线!)";
					}
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
				
				//删除禁用产线
				for(int i = 0; i < plines.size(); i++){
					if(disablePlines.contains(plines.get(i).getPlineCode())){
						String plineName = plines.get(i).getPlineName();
						plines.remove(plines.get(i));
						i--;
						if(plines.size() == 0){
							errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")不可用！(轧制工序找不到适用产线)";
							lineEntity.setFlag(false);
							lineEntity.setErrMessage(errMessage);
							lineEntity.setObjKey(message);
							return lineEntity;
						}
					}
				}

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

					plines = LineManage.checkLines(customLineDs, plines);
					if (plines == null) {
						errMessage = "制程(" + mscPline + " )不可用！(轧制工序:外径("
								+ rollSpec.getDimater() + ")、壁厚(" + rollSpec.getHeight() + ") 找不到客户认证的轧制适用产线("
								+ prolinesStr + "))";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}

				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())) {
						errMessage = "制程(" + mscPline + ")不可用！(轧制工序:产线(" + plineName + ")与产线("
								+ pline.getPlineName() + ")产能井不一致!)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
					if (pline.getPlineCode().equals("C012")) {
						flag = true;
					}
					String warnFl = "0";
					String warnDesc = "";
					if (LineManage.checkStation(con, msc, mscPline, "D", pline.getPlineCode())) {
						warnFl = "1";
						warnDesc = "工序点警告";
					}
					if (pline.getIdentFlag().equals("")) {
						pline.setIdentFlag("0");
					}
					CraftOrdDesignMscPlGxPrlEntity line = LineManage.setProLine(designKey, mscPline,
							pline.getPlineCode(), pline.getPlineName(), "D", process.getBacklogSeq(), warnFl, warnDesc,
							pline.getWellCode(), pline.getWellName(), pline.getIdentFlag());
					proLines.add(line);
					// 轧制适用产线与规格组成轧制产线
					CraftOrdDesignMscGzGgPlEntity rollSpecLine = LineManage.createSpecLine(line,
							rollSpec.getSpecCode());
					rollSpecLines.add(rollSpecLine);
				}
				if (design.getAsselFl().equals("1") && !flag) {
					errMessage = "制程(" + mscPline + " )不可用！(轧制工序 :外径(" + rollSpec.getDimater()
							+ ")、壁厚(" + rollSpec.getHeight() + ")没有找到Assel机组![合同对Assel有要求])";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
			}
		}

		/**
		 * 镦拔扩
		 */
		if (wholeBacklog.contains("E")) {
			// 分析镦拔扩产线
			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "E");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "E," + dimater + "," + height + "," + "," + "," + "," + "," + "1";
			List<ComMscPlineRSpecEntity> plines = lineManageDao.getProLine(con, "E",supp);
			plines = LineManage.fiterProLine(plines, "", process.getProcessCodeC(), dimater, height, "");
			String errMessage = "";
			if (plines == null || plines.size() == 0) {
				errMessage = "制程(" + mscPline + ")不可用！(镦拔扩工序:外径(" + dimater + ")壁厚(" + height
						+ ")二级工序(" + process.getProcessDescC() + ")没有找到适用产线!)";
				lineEntity.setFlag(false);
				lineEntity.setErrMessage(errMessage);
				lineEntity.setObjKey(message);
				return lineEntity;
			}
			
			//删除禁用产线
			for(int i = 0; i < plines.size(); i++){
				if(disablePlines.contains(plines.get(i).getPlineCode())){
					String plineName = plines.get(i).getPlineName();
					plines.remove(plines.get(i));
					i--;
					if(plines.size() == 0){
						errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")"
								+ "不可用！(镦拔扩工序找不到适用产线)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}
			}

			// 客户认证热处理产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineEs = LineManage.fiterIdentificLines(identificLines, "E");
			if (identificLineEs != null && identificLineEs.size() > 0) {
				plines = LineManage.checkLines(identificLineEs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineEs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					errMessage = "制程(" + mscPline + ")不可用！(镦拔扩工序:外径(" + dimater + ") 壁厚("
							+ height + ")二级工序(" + process.getProcessDescC() + ")没有找到客户认证的镦拔扩适用产线(" + plinesStr + "))";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					errMessage = "制程(" + mscPline + ")不可用！(镦拔扩工序:产线(" + plineName + ")与产线("
							+ pline.getPlineName() + ")产能井不一致)";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (LineManage.checkStation(con, msc, mscPline, "E", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				CraftOrdDesignMscPlGxPrlEntity line = LineManage.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")) {
			// 分析热处理产线
			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "F");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "F," + dimater + "," + height + "," + "," + "," + design.getModelCode() + ","
					+ process.getProcessCodeC() + "," + "1";
			List<ComMscPlineRSpecEntity> plines = lineManageDao.getProLine(con, "F",supp);
			plines = LineManage.fiterProLine(plines, "", process.getProcessCodeC(), dimater, height, "");
			String errMessage = "";
			// if (design.getOutsStlPipeFl().equals("121903")) {
			// plines = LineManage.fiterOutLine(plines, true);
			// } else {
			// plines = LineManage.fiterOutLine(plines, false);
			// }
			if (plines == null || plines.size() == 0) {
				if (design.getOutsStlPipeFl().equals("121903")) {
					errMessage = "制程(" + mscPline + ")不可用！(热处理工序:外径(" + dimater + " )壁厚("
							+ height + ")二级工序(" + process.getProcessDescC()
							+ ")没有找到(外购)适用产线[合同允许外购管,并且是半成品管,热处理只允许外购产线])";
				} else {
					errMessage = "制程(" + mscPline + ")不可用！(热处理工序:外径(" + dimater + ") 壁厚("
							+ height + ")二级工序(" + process.getProcessDescC() + ")没有找到(自炼)适用产线)";
				}
				lineEntity.setFlag(false);
				lineEntity.setErrMessage(errMessage);
				lineEntity.setObjKey(message);
				return lineEntity;
			}
			
			//删除禁用产线
			for(int i = 0; i < plines.size(); i++){
				if(disablePlines.contains(plines.get(i).getPlineCode())){
					String plineName = plines.get(i).getPlineName();
					plines.remove(plines.get(i));
					i--;
					if(plines.size() == 0){
						errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")不可用！(热处理工序找不到适用产线)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}
			}

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

			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					errMessage = "制程(" + mscPline + ")不可用！( 热处理工序:产线(" + plineName + ")与产线("
							+ pline.getPlineName() + ")产能井不一致)";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (LineManage.checkStation(con, msc, mscPline, "F", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				CraftOrdDesignMscPlGxPrlEntity line = LineManage.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")) {
			// 分析加工产线
			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "G");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "G," + dimater + "," + height + "," + "," + "," + design.getModelCode() + "," + "," + "1";
			List<ComMscPlineRSpecEntity> plineGs = lineManageDao.getProLine(con, "G",supp);
			List<ComMscPlineRSpecEntity> plines = LineManage.fiterProLine(plineGs, "", process.getProcessCodeC(), dimater, height,
					design.getModelCode());
			// 如果过滤外径、壁厚没有产线则过滤产品规格
			String errMessage = "";
			if (plines == null || plines.size() == 0) {
				plines = lineManageDao.getLineBySpec(con, design.getModelCode(), design.getSpecCode(), "G");
			}
			if (plines == null || plines.size() == 0) {
				errMessage = "制程(" + mscPline + ")不可用！(加工工序 : 扣型(" + design.getModelDesc()
						+ ") 外径(" + dimater + ")壁厚(" + height + ") 二级工序("+ process.getProcessDescC()
						+")/产品规格(" + design.getSpecName() + ")没有找到适用产线)";
				lineEntity.setFlag(false);
				lineEntity.setErrMessage(errMessage);
				lineEntity.setObjKey(message);
				return lineEntity;
			}
			plines = LineManage.fiterLineG(plines, design.getMscOrStyle());
			if (plines == null || plines.size() == 0) {

				if (design.getMscOrStyle().equals("C") || design.getMscOrStyle().equals("E")) {
					errMessage = "制程(" + mscPline + ")不可用！(加工工序: 扣型(" + design.getModelDesc()
							+ ")  外径(" + dimater + ") 壁厚(" + height + ") /产品规格(" + design.getSpecName()
							+ ")没有找到产能井(加工（支）)的适用产线[注:冶金规范码类型为" + 
							"短节+附件 或者 双工短节 使用 加工（支）产能井,其它使用 加工 产能井])";
				} else {
					errMessage = "制程(" + mscPline + ")不可用！(加工工序: 扣型(" + design.getModelDesc()
							+ ") 外径(" + dimater + ") 壁厚(" + height + ") /产品规格(" + design.getSpecName()
							+ ")没有找到产能井(加工)的适用产线[注:冶金规范码类型为短节+附件 或者 " 
							+ "双工短节 使用 加工（支）产能井,其它使用 加工 产能井])";
				}
				lineEntity.setFlag(false);
				lineEntity.setErrMessage(errMessage);
				lineEntity.setObjKey(message);
				return lineEntity;
			}
			
			//删除禁用产线
			for(int i = 0; i < plines.size(); i++){
				if(disablePlines.contains(plines.get(i).getPlineCode())){
					String plineName = plines.get(i).getPlineName();
					plines.remove(plines.get(i));
					i--;
					if(plines.size() == 0){
						errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")"
								+ "不可用！(加工工序找不到适用产线)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}
			}
			
			// 客户认证加工产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineGs = LineManage.fiterIdentificLines(identificLines, "G");
			if (identificLineGs != null && identificLineGs.size() > 0) {
				plines = LineManage.checkLines(identificLineGs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineGs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					errMessage = "制程(" + mscPline + ")不可用！(加工工序: 扣型(" + design.getModelDesc()
							+ ") 外径(" + dimater + ") 壁厚(" + height + ") /产品规格(" + design.getSpecName()
							+ ")没有找到客户认证的加工适用产线(" + plinesStr + "))";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					errMessage = "制程(" + mscPline + ")不可用！(加工工序:产线(" + plineName + ")与产线("
							+ pline.getPlineName() + ")产能井不一致!)";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (LineManage.checkStation(con, msc, mscPline, "G", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				CraftOrdDesignMscPlGxPrlEntity line = LineManage.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")) {
			// 分析加工产线
			// 工序
			CraftOrdDesignMscPlPrcEntity process = operationDaoImp.getOperation(con, msc, mscPline, "H");
			// key组成：工序，外径， 壁厚 ，断面，坯料规格，扣型，二级工序，1或者2
			String message = "H," + dimater + "," + height + "," + "," + "," + design.getModelCode() + "," + "," + "1";
			String errMessage = "";
			List<ComMscPlineRSpecEntity> plineHs = lineManageDao.getProLine(con, "H",supp);
			// 如果过滤外径、壁厚没有产线则过滤产品规格
			List<ComMscPlineRSpecEntity> plines = LineManage.fiterProLine(plineHs, "", "", dimater, height,
					design.getModelCode());
			if (plines == null || plines.size() == 0) {
				plines = lineManageDao.getLineBySpec(con, design.getModelCode(), design.getSpecCode(), "H");
			}
			if (plines == null || plines.size() == 0) {
				errMessage = "制程(" + mscPline + ")不可用！(接箍加工工序: 扣型(" + design.getModelDesc()
						+ ") 外径(" + dimater + ") 壁厚(" + height + ") /产品规格(" + design.getSpecName() + ")没有找到适用产线!)";
				lineEntity.setFlag(false);
				lineEntity.setErrMessage(errMessage);
				lineEntity.setObjKey(message);
				return lineEntity;
			}
			
			//删除禁用产线
			for(int i = 0; i < plines.size(); i++){
				if(disablePlines.contains(plines.get(i).getPlineCode())){
					String plineName = plines.get(i).getPlineName();
					plines.remove(plines.get(i));
					i--;
					if(plines.size() == 0){
						errMessage = "产线:" + plineName + "被合同禁用 制程(" + mscPline + ")不可用！(接箍加工工序找不到适用产线)";
						lineEntity.setFlag(false);
						lineEntity.setErrMessage(errMessage);
						lineEntity.setObjKey(message);
						return lineEntity;
					}
				}
			}

			// 客户认证接箍加工产线 2015/07/20
			List<ComBaseCustomRPlineEntity> identificLineHs = LineManage.fiterIdentificLines(identificLines, "H");
			if (identificLineHs != null && identificLineHs.size() > 0) {
				plines = LineManage.checkLines(identificLineHs, plines);
				if (plines == null) {
					String plinesStr = "";
					for (ComBaseCustomRPlineEntity comBaseCustomRPlineEntity : identificLineHs) {
						plinesStr += comBaseCustomRPlineEntity.getPlineName() + "  ";
					}
					errMessage = "制程(" + mscPline + ")不可用！(接箍加工工序: 扣型(" + design.getModelDesc()
							+ ") 外径(" + dimater + ") 壁厚(" + height + ") /产品规格(" + design.getSpecName()
							+ ")没有找到客户认证的接箍加工产线(" + plinesStr + "))";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
			}

			// 产能井判断
			String wellCode = plines.get(0).getWellCode();
			String plineName = plines.get(0).getPlineName();
			for (ComMscPlineRSpecEntity pline : plines) {
				if (!pline.getWellCode().equals(wellCode)) {
					errMessage = "制程(" + mscPline + ")不可用！(接箍加工工序:产线(" + plineName + ")与产线("
							+ pline.getPlineName() + ")产能井不一致)";
					lineEntity.setFlag(false);
					lineEntity.setErrMessage(errMessage);
					lineEntity.setObjKey(message);
					return lineEntity;
				}
				String warnFl = "0";
				String warnDesc = "";
				if (LineManage.checkStation(con, msc, mscPline, "H", pline.getPlineCode())) {
					warnFl = "1";
					warnDesc = "工序点警告";
				}
				// 如果不是认证产线,则赋值0
				if (pline.getIdentFlag().equals("")) {
					pline.setIdentFlag("0");
				}
				CraftOrdDesignMscPlGxPrlEntity line = LineManage.setProLine(designKey, mscPline, pline.getPlineCode(),
						pline.getPlineName(), "H", process.getBacklogSeq(), warnFl, warnDesc, pline.getWellCode(),
						pline.getWellName(), pline.getIdentFlag());
				proLines.add(line);
			}
		}
		// 过滤重复的产线
		proLines = LineManage.fiterProLine(proLines);
		biltCSpecLines = LineManage.fiterBiltCSpecLine(biltCSpecLines);
		rollSpecLines = LineManage.checkComSpecPline(rollSpecLines);
		biltSpecLines = LineManage.fiterBiltSpecLine(biltSpecLines);
		lineEntity.setFlag(true);
		lineEntity.setErrMessage("适用产线验证");
		lineEntity.setObjKey("");
		lineEntity.setBiltCLines(biltCSpecLines);
		lineEntity.setBiltLines(biltSpecLines);
		lineEntity.setRollLines(rollSpecLines);
		lineEntity.setLines(proLines);
		return lineEntity;
	}
	
	/**
	 * 删除炼钢工序自炼成分索引号
	 * @param chems
	 * @param processList
	 * @param lineManageDao
	 * @param mscPline
	 * @param processCode
	 * @param con
	 */
	private void deleteOwnCic(List<CraftOrdDesignStdCicEntity> chems, List<ProcessEntity> processList, 
			LineManageDao lineManageDao, String mscPline, Connection con){
		ArrayList<String> listCic = new ArrayList<String>();
		for(ProcessEntity process : processList){
			if(process.getProcessCode().equals("A")){
				String[] cics = process.getCic().split(",");
				String strCic = "";
				for(String cic : cics){
					cic = cic.trim();
					String ownIdexA = lineManageDao.getOwnIndexA(con, cic);
					if(!ownIdexA.equals("0")){
						listCic.add(cic);
					}else{
						if(strCic.equals("")){
							strCic = cic;
						}else{
							strCic += "," + cic;
						}
					}
				}
				process.setCic(strCic);
				
				//清除炼钢自炼索引号
				for(int i = chems.size() - 1; i >= 0; i--){
					if(!chems.get(i).getMscPline().equals(mscPline)) continue;
					if(!chems.get(i).getProcessCode().equals("A")) continue;
					if(listCic.contains(chems.get(i).getCic())){
						chems.remove(i);
					}
				}
			}
		}
	}
	
	/**
	 * 删除炼钢工序外购成分索引号
	 * @param chems
	 * @param processList
	 * @param lineManageDao
	 * @param mscPline
	 * @param processCode
	 * @param con
	 */
	private void deletePurchaseCic(List<CraftOrdDesignStdCicEntity> chems, List<ProcessEntity> processList, 
			LineManageDao lineManageDao, String mscPline, Connection con){
		ArrayList<String> listCic = new ArrayList<String>();
		for(ProcessEntity process : processList){
			if(process.getProcessCode().equals("A")){
				String[] cics = process.getCic().split(",");
				String strCic = "";
				for(String cic : cics){
					cic = cic.trim();
					String purchaseIdexA = lineManageDao.getPurchaseIndexA(con, cic);
					if(!purchaseIdexA.equals("0")){
						listCic.add(cic);
					}else{
						if(strCic.equals("")){
							strCic = cic;
						}else{
							strCic += "," + cic;
						}
					}
				}
				process.setCic(strCic);
				
				//清除炼钢自炼索引号
				for(int i = chems.size() - 1; i >= 0; i--){
					if(!chems.get(i).getMscPline().equals(mscPline)) continue;
					if(!chems.get(i).getProcessCode().equals("A")) continue;
					if(listCic.contains(chems.get(i).getCic())){
						chems.remove(i);
					}
				}
			}
		}
	}

	/**
	 * 过滤适用产线通过(外径、壁厚、二级工序、规格代码、扣型)
	 * 
	 * @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 CraftOrdDesignMscPlGxPrlEntity setProLine(String designKey, String mscPline, String plineCode,
			String plineName, String processCode, String backlogSeq, String warnFl, String warnDesc, String wellCode,
			String wellName, String identFlag) {
		CraftOrdDesignMscPlGxPrlEntity proLine = new CraftOrdDesignMscPlGxPrlEntity();
		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 {
		OperationPointDao operationPointDaoImp = new OperationPointDao();
		LineManageDao lineManageDao = new LineManageDao();
		// 通过工序，冶金规范码，制程找工序点集合
		List<CraftOrdDesignMscPlStiEntity> stations = operationPointDaoImp.getOperationPoint(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<ComMscStationRPlineEntity> plines = lineManageDao.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 = CraftTool.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;
			}

		}
	}

	/**
	 * 轧管产线转化为轧管规格-适用产线
	 * 
	 * @param specProLine
	 * @param specCode
	 * @return
	 */
	public static CraftOrdDesignMscGzGgPlEntity createSpecLine(CraftOrdDesignMscPlGxPrlEntity line, String specCode) {
		if (line == null) {
			return null;
		}
		CraftOrdDesignMscGzGgPlEntity spec = new CraftOrdDesignMscGzGgPlEntity();
		spec.setDesginKey(line.getDesginKey());
		spec.setMscPline(line.getMscPline());
		spec.setPlineCode(line.getPlineCode());
		spec.setPlineName(line.getPlineName());
		spec.setSpecCode(specCode);
		return spec;
	}

	/**
	 * 锻造产线
	 * 
	 * @param line
	 * @param specCode
	 * @return
	 */
	public static CraftOrdDesignMscBiltPlEntity createBiltLine(CraftOrdDesignMscPlGxPrlEntity line, String specCode) {
		if (line == null) {
			return null;
		}
		CraftOrdDesignMscBiltPlEntity spec = new CraftOrdDesignMscBiltPlEntity();
		spec.setDesginKey(line.getDesginKey());
		spec.setBSpecCode(specCode);
		spec.setMscPline(line.getMscPline());
		spec.setPlineCode(line.getPlineCode());
		spec.setPlineName(line.getPlineName());
		return spec;
	}

	/**
	 * 铸造产线
	 * 
	 * @param line
	 * @param specCode
	 * @return
	 */
	public static CraftOrdDesignMscBiltCPlEntity createBlank(CraftOrdDesignMscPlGxPrlEntity line, String specCode) {
		if (line == null) {
			return null;
		}
		CraftOrdDesignMscBiltCPlEntity spec = new CraftOrdDesignMscBiltCPlEntity();
		spec.setDesginKey(line.getDesginKey());
		spec.setMscPline(line.getMscPline());
		spec.setCSpecCode(specCode);
		spec.setPlineCode(line.getPlineCode());
		spec.setPlineName(line.getPlineName());
		return spec;
	}

	/**
	 * 过滤轧管适用产线
	 * 
	 * @param proLines
	 * @return
	 */
	public static List<CraftOrdDesignMscGzGgPlEntity> checkComSpecPline(List<CraftOrdDesignMscGzGgPlEntity> proLines) {
		if (proLines == null || proLines.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscGzGgPlEntity> list = new ArrayList<CraftOrdDesignMscGzGgPlEntity>();
		List<String> strs = new ArrayList<String>();
		for (CraftOrdDesignMscGzGgPlEntity proLine : proLines) {
			String str = proLine.getDesginKey() + proLine.getMscPline() + proLine.getPlineCode()
					+ proLine.getSpecCode();
			if (!strs.contains(str)) {
				strs.add(str);
				list.add(proLine);
			}
		}
		return list;
	}

	/**
	 * 过滤适用产线
	 * 
	 * @param proLines
	 * @return
	 */
	public static List<CraftOrdDesignMscPlGxPrlEntity> fiterProLine(List<CraftOrdDesignMscPlGxPrlEntity> proLines) {
		if (proLines == null || proLines.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscPlGxPrlEntity> list = new ArrayList<CraftOrdDesignMscPlGxPrlEntity>();
		List<String> strs = new ArrayList<String>();
		for (CraftOrdDesignMscPlGxPrlEntity proLine : proLines) {
			String str = proLine.getDesginKey() + proLine.getBacklogSeq() + proLine.getMscPline()
					+ proLine.getPlineCode() + proLine.getProcessCode();
			if (!strs.contains(str)) {
				strs.add(str);
				list.add(proLine);
			}
		}
		return list;
	}

	/**
	 * 过滤铸造产线
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<CraftOrdDesignMscBiltCPlEntity> fiterBiltCSpecLine(
			List<CraftOrdDesignMscBiltCPlEntity> biltsCSpecLines) {
		if (biltsCSpecLines == null || biltsCSpecLines.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscBiltCPlEntity> list = new ArrayList<CraftOrdDesignMscBiltCPlEntity>();
		List<String> keys = new ArrayList<String>();
		for (CraftOrdDesignMscBiltCPlEntity biltsCSpecLine : biltsCSpecLines) {
			String key = biltsCSpecLine.getDesginKey() + biltsCSpecLine.getMscPline() + biltsCSpecLine.getCSpecCode()
					+ biltsCSpecLine.getPlineCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltsCSpecLine);
			}
		}
		return list;
	}

	/**
	 * 过滤锻造产线
	 * 
	 * @param biltSpecLines
	 *            锻造产线集合
	 * @return List<DesignMscBiltPlEntity>
	 */
	public static List<CraftOrdDesignMscBiltPlEntity> fiterBiltSpecLine(
			List<CraftOrdDesignMscBiltPlEntity> biltSpecLines) {
		if (biltSpecLines == null || biltSpecLines.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscBiltPlEntity> list = new ArrayList<CraftOrdDesignMscBiltPlEntity>();
		List<String> keys = new ArrayList<String>();
		for (CraftOrdDesignMscBiltPlEntity biltSpecLine : biltSpecLines) {
			String key = biltSpecLine.getDesginKey() + biltSpecLine.getMscPline() + biltSpecLine.getBSpecCode()
					+ biltSpecLine.getPlineCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltSpecLine);
			}
		}
		return list;
	}

	/**
	 * 加工产线特殊处理(如果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 外购
	 * @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) {
				// 如果管理部门代码为002001004，则代表这条产线是外购产线,否则都是自炼产线
				if (line.getPlineName().contains("外购")) {
					fiterLines.add(line);
				}
			} else {
				if (!line.getPlineName().contains("外购")) {
					fiterLines.add(line);
				}
			}
		}
		return fiterLines;
	}
}
