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

import com.steering.comm.tools.StringTool;
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.SpecManageDao;
import com.steering.pss.sale.order.craftDesign.model.*;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

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

/**
 * 规格管理
 * 
 * @author Administrator
 * 
 */
public class SpecManage {
	/**
	 * 轧制规格、锻造规格、铸造规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param proDucInfo
	 *            制程
	 * @param grades
	 *            钢种集合
	 * @param design
	 *            质量设计类
	 * @return SpecEntity
	 * @throws SQLException
	 */
	public SpecEntity checkSpec(Connection con,
								CraftOrdDesignMscPlEntity proDucEntity,
								List<CraftOrdDesignMscPlGzEntity> grades, CraftEntity design)
			throws SQLException {
		return checkSpec(con,proDucEntity,grades,design,null);
	}
	public SpecEntity checkSpec(Connection con,
			CraftOrdDesignMscPlEntity proDucEntity,
			List<CraftOrdDesignMscPlGzEntity> grades, CraftEntity design, String supp)
			throws SQLException {
		SpecManageDao specManageDaoImp = new SpecManageDao();
		OperationDao operationDaoImp = new OperationDao();
		LineManageDao LineManageDao = new LineManageDao();
		// 提示信息
		String errMessage = "";
		// 全程途径码
		String wholeBacklog = proDucEntity.getWholeBacklog();
		// 冶金规范码
		String msc = design.getMscOr();
		// 全程产线号
		String mscPline = proDucEntity.getMscPline();
		// 外徑
		String dimater = design.getDimater();
		// 壁厚
		String height = design.getHeight();
		// 质量主键
		String designKey = design.getDesignKey();
		/**
		 * 轧制规格类集合
		 */
		List<CraftOrdDesignMscGzGgEntity> rolledSpecs = new ArrayList<CraftOrdDesignMscGzGgEntity>();
		/**
		 * 合适的钢种集合
		 */
		List<CraftOrdDesignMscPlGzEntity> fiterGrades = new ArrayList<CraftOrdDesignMscPlGzEntity>();
		// 返回信息(0.验证成功(true),失败(false) 1 返回提示信息 2 返回key信息 3 轧管规格 4锻造规格 5 铸造规格)
		SpecEntity specEntity = new SpecEntity();
		/**
		 * 镦拔扩工序
		 */
		if (wholeBacklog.contains("E")) {
			// 查询镦拔扩的二级工序
			CraftOrdDesignMscPlPrcEntity processInfo = operationDaoImp
					.getOperation(con, msc, mscPline, "E");
			for (CraftOrdDesignMscPlGzEntity grade : grades) {
				boolean eFlag = false;
				//获取设定轧管规格
				List<ComMscRollRUpsetEntity> rollSpecs = specManageDaoImp.getRollSpecSet(
						con, design.getOrdLnPk(), mscPline, grade.getGradecode());
				if(rollSpecs.size() == 0)
				{
					// 钢种+镦拔扩规格+二级工序找轧制规格
				    rollSpecs = specManageDaoImp
							.getRollSpec(con, dimater, height,
									grade.getGradecode(),
									processInfo.getProcessCodeC());
				    if(rollSpecs != null && rollSpecs.size() > 1){
				    	errMessage = "制程(" + mscPline + " )/全程途径码(" + wholeBacklog
								+ " )  :外径(" + dimater + " ) 壁厚(" + height
								+ " ) 钢种(" + grade.getGradename() + " ) 镦拔扩二级工序("
								+ processInfo.getProcessDescC() + ") 找到多个轧管规格，请设定轧管规格";
				    	String objKey = proDucEntity.getMscPline() + "," + processInfo.getProcessCodeC();
						specEntity.setObjKey(objKey);
						specEntity.setErrMessage(errMessage);
						specEntity.setFlag(false);
						specEntity.setListItem("镦拔扩");
						return specEntity;
				    }
				}
				
				// 如果镦拔扩找不到轧制规格
				if (rollSpecs == null || rollSpecs.size() == 0) {
					errMessage = "制程(" + mscPline + " )/全程途径码(" + wholeBacklog
							+ " )  :外径(" + dimater + " ) 壁厚(" + height
							+ " ) 钢种(" + grade.getGradename() + " ) 镦拔扩二级工序("
							+ processInfo.getProcessDescC() + ") 找不到轧制规格";
					specEntity.setErrMessage(errMessage);
					specEntity.setFlag(false);
					specEntity.setListItem("镦拔扩");
					return specEntity;
				}

				for (ComMscRollRUpsetEntity rollSpec : rollSpecs) {
					List<ComBaseSpecEntity> baseSpec = specManageDaoImp
							.getBaseSpec(con, rollSpec.getMinD(),
									rollSpec.getMinH());
					// 找不到基础规格
					if (baseSpec == null) {
						errMessage = "制程(" + mscPline + " )/全程途径码("
								+ wholeBacklog + " ):设定的轧制规格 外径("
								+ rollSpec.getMinD() + ")壁厚("
								+ rollSpec.getMinH() + ")在基础规格表不存在!";
						String objKey = proDucEntity.getMscPline() + "," + processInfo.getProcessCodeC();
						specEntity.setObjKey(objKey);
						specEntity.setErrMessage(errMessage);
						specEntity.setFlag(false);
						specEntity.setListItem("镦拔扩");
						return specEntity;
					}
					if (baseSpec.size() > 1) {
						errMessage = "制程(" + mscPline + " )/全程途径码("
								+ wholeBacklog + " ):设定的轧制规格 外径("
								+ rollSpec.getMinD() + ")壁厚("
								+ rollSpec.getMinH() + ")在基础规格存在多条记录!";
						String objKey = proDucEntity.getMscPline() + "," + processInfo.getProcessCodeC();
						specEntity.setObjKey(objKey);
						specEntity.setErrMessage(errMessage);
						specEntity.setFlag(false);
						specEntity.setListItem("镦拔扩");
						return specEntity;
					}
					// 轧制规格
					CraftOrdDesignMscGzGgEntity rolledSpec = SpecManage
							.setRollSpec(designKey, mscPline, grade
									.getGradecode(), grade.getGradename(),
									baseSpec.get(0).getSpecCode(), baseSpec
											.get(0).getSpecName(), rollSpec
											.getMinD(), rollSpec.getMinH());
					rolledSpecs.add(rolledSpec);
					eFlag = true;
				}
				if (eFlag) {
					fiterGrades.add(grade);
				}
			}

		} else {
			for (CraftOrdDesignMscPlGzEntity grade : grades) {
				fiterGrades.add(grade);
			}
			CraftOrdDesignMscGzGgEntity rolledSpec = SpecManage.setRollSpec(
					designKey, mscPline, "", "", design.getSpecCode(),
					design.getSpecName(), dimater, height);
			rolledSpecs.add(rolledSpec);
		}
		
		// 只要有一个钢种能找到轧制规格就通过
//		if (rolledSpecs.size() == 0) {
//			specEntity.setErrMessage(errMessage);
//			specEntity.setFlag(false);
//			specEntity.setListItem("镦拔扩");
//			return specEntity;
//		}

		// 如果合同允许原料管 ，要求轧制工序 只能是外购产线，或者只能是自炼产线
		String messageAll = "";
		List<CraftOrdDesignMscBiltCEntity> blitCSpecsW = new ArrayList<CraftOrdDesignMscBiltCEntity>();// 专门用于外购管的铸造规格
		if (wholeBacklog.contains("D")) {
			for (int i = 0; i < rolledSpecs.size(); i++) {
				CraftOrdDesignMscGzGgEntity rolledSpec = rolledSpecs.get(i);
				messageAll = "D," + rolledSpec.getDimater() + ","
						+ rolledSpec.getHeight() + "," + "," + "," + "," + ","
						+ "1";
				List<String[]> biltSpecArray = specManageDaoImp.getBiltSpec(
						con, rolledSpec.getDimater(), rolledSpec.getHeight(),
						design.getOutsStlPipeFl(), supp);
				if (biltSpecArray == null || biltSpecArray.size() == 0) {
					if (design.getOutsStlPipeFl().equals("121902")
							|| design.getOutsStlPipeFl().equals("121903")) {
						errMessage = "制程("
								+ mscPline
								+ " )/全程途径码("
								+ wholeBacklog
								+ ") 轧制工序:外径("
								+ rolledSpec.getDimater()
								+ ") 壁厚("
								+ rolledSpec.getHeight()
								+ ") 的(外购)产线找不到管坯规格[合同允许(半成品管/原料管),轧制只能为外购产线]  ";
					} else {
						errMessage = "制程(" + mscPline + " )/全程途径码("
								+ wholeBacklog + ") 轧制工序:外径("
								+ rolledSpec.getDimater() + ") 壁厚("
								+ rolledSpec.getHeight() + ")的(自炼)产线找不到管坯规格";
					}
					rolledSpecs.remove(rolledSpec);
					i--;
				}

				// 如果是外购管则不往下验证锻造/铸造规格,但要生成铸造规格
				if (design.getOutsStlPipeFl().equals("121902")
						|| design.getOutsStlPipeFl().equals("121903")) {
					for (String[] strings : biltSpecArray) {
						CraftOrdDesignMscBiltCEntity biltCSpec = new CraftOrdDesignMscBiltCEntity();
						biltCSpec = SpecManage.setBiltCSpec(designKey,
								mscPline, rolledSpec.getSpecCode(), "", "",
								strings[0], strings[1]);
						
						boolean exists = false;
						for(CraftOrdDesignMscBiltCEntity blitCSpecW : blitCSpecsW){
							if(blitCSpecW.getSpecCode().equals(biltCSpec.getSpecCode())
									&& blitCSpecW.getBSpecCode().equals(biltCSpec.getBSpecCode())
									&& blitCSpecW.getCSpecCode().equals(biltCSpec.getCSpecCode())){
								exists = true;
							}
						}
						if(!exists){
							blitCSpecsW.add(biltCSpec);
						}
					}
				}
			}
			if (rolledSpecs == null || rolledSpecs.size() == 0) {
				specEntity.setErrMessage(errMessage);
				specEntity.setFlag(false);
				specEntity.setObjKey(messageAll);
				specEntity.setListItem("适用产线");
				return specEntity;
			}
		}

		// 如果是外购管，不向下计算
		if (design.getOutsStlPipeFl().equals("121902")
				|| design.getOutsStlPipeFl().equals("121903")) {
			specEntity.setErrMessage("");
			specEntity.setFlag(true);
			specEntity.setObjKey("");
			specEntity.setListItem("适用产线");
			specEntity.setRollSpecs(rolledSpecs);
			specEntity.setBillSpecs(null);
			specEntity.setBillSpecCs(blitCSpecsW);
			return specEntity;
		}

		// /锻造比极限值
		String ratio = Compressio.getPscRatio(con, design.getPsc());
		if (ratio.equals("")) {
			specEntity.setErrMessage("请维护产品码(" + design.getPsc() + ")的锻造比极限值!");
			specEntity.setFlag(false);
			specEntity.setObjKey(messageAll);
			specEntity.setListItem("适用产线");
			return specEntity;
		}

		/**
		 * 轧制工序(如果制程包含锻造工序，则对应的是锻造规格，否则是铸造规格)
		 */
		/**
		 * 最终轧制规格类集合
		 */
		List<CraftOrdDesignMscGzGgEntity> rolledSpecsE = new ArrayList<CraftOrdDesignMscGzGgEntity>();
		// 锻造规格
		List<CraftOrdDesignMscBiltEntity> blitSpecs = new ArrayList<CraftOrdDesignMscBiltEntity>();
		// 铸造规格
		List<CraftOrdDesignMscBiltCEntity> blitCSpecs = new ArrayList<CraftOrdDesignMscBiltCEntity>();
		// 定义错误提示信息
		String messageM = "";
		String messageD = "";
		if (wholeBacklog.contains("D")) {
			// 遍历轧管规格
			for (CraftOrdDesignMscGzGgEntity rolledSpec : rolledSpecs) {
				// 通过外径、壁厚找锻造/铸造工序的规格
				List<String[]> biltSpecArray = specManageDaoImp.getBiltSpec(
						con, rolledSpec.getDimater(), rolledSpec.getHeight(),
						design.getOutsStlPipeFl(),supp);
				messageD = "D," + rolledSpec.getDimater() + ","
						+ rolledSpec.getHeight() + "," + "," + "," + "," + ","
						+ "1";
				// 前面验证轧管规格是否外购/自炼，则对应的管坯规格则肯定能够找到
				// 如果制程有锻造工序，则对应的管坯规格是锻造规格，否则一定为铸造规格
				if (wholeBacklog.contains("C")) {
					for (int i = 0; i < biltSpecArray.size(); i++) {
						// 轧制锻造类
						if (!StringTool.isNumeric(biltSpecArray.get(i)[1])) {
							messageM = "制程(" + mscPline + ")/全程途径码("
									+ wholeBacklog + "):轧制规格("
									+ rolledSpec.getSpecName() + ")->锻造规格("
									+ biltSpecArray.get(i)[1] + ")是钢锭规格类型!";
							continue;
						}
						// 如果是合同允许原料坯，则锻造规格必须找到外购产线，否则找自炼产线。
						List<ComMscPlineRSpecEntity> plineCs = LineManageDao
								.getProLine(con, "C",null);
						plineCs = LineManage.fiterProLine(plineCs,
								biltSpecArray.get(i)[0], "", "", "", "");
//						if (design.getOutsBilietFl().equals("121801")) {
//							plineCs = LineManage.fiterOutLine(plineCs, true);
//						} else {
//							plineCs = LineManage.fiterOutLine(plineCs, false);
//						}
						// 如果对应的产线找不到，则继续找下一个规格对应的产线
						if (plineCs == null || plineCs.size() == 0) {
							if (design.getOutsBilietFl().equals("121801")) {
								messageM = "制程(" + mscPline + ")/全程途径码("
										+ wholeBacklog + "): 轧制规格("
										+ rolledSpec.getSpecName() + ")->锻造规格("
										+ biltSpecArray.get(i)[1]
										+ ")  锻造工序不存在(外购)产线";
							} else {
								messageM = "轧制规格(" + rolledSpec.getSpecName()
										+ ")->锻造规格(" + biltSpecArray.get(i)[1]
										+ ")  锻造工序不存在(自炼)产线";
							}
							continue;
						}
						// 转化为锻造规格实体类
						CraftOrdDesignMscBiltEntity blitSpec = SpecManage
								.setBilt(designKey, mscPline,
										rolledSpec.getSpecCode(),
										biltSpecArray.get(i)[0],
										biltSpecArray.get(i)[1]);
						// 如果是锻造工序则必定要有对应连铸坯规格
						String processCodeC = "";
						if (wholeBacklog.contains("B")) {
							CraftOrdDesignMscPlPrcEntity processInfoB = operationDaoImp
									.getOperation(con, msc, mscPline, "B");
							processCodeC = processInfoB.getProcessCodeC();
						} else {
							processCodeC = "B002";
						}
						List<ComMscCastRForgEntity> castRForgs = specManageDaoImp
								.getBiltCSpec(con, blitSpec.getBSpecCode(),
										processCodeC);
						if (castRForgs == null || castRForgs.size() == 0) {
							if (processCodeC.equals("B002")) {
								messageM = "制程(" + mscPline + ")/全程途径码("
										+ wholeBacklog + "):轧制规格("
										+ rolledSpec.getSpecName()
										+ " )->锻造规格(" + blitSpec.getBSpecName()
										+ ")不存在连铸坯规格(二级工序:连铸)";
								continue;
							} else {
								messageM = "制程(" + mscPline + ")/全程途径码("
										+ wholeBacklog + "): 轧制规格("
										+ rolledSpec.getSpecName() + "->锻造规格("
										+ blitSpec.getBSpecName()
										+ ")找不到钢锭规格(二级工序:模铸)";
								continue;
							}
						}
						CraftOrdDesignMscBiltCEntity biltCSpec = null;
						for (ComMscCastRForgEntity castRForg : castRForgs) {
							// 连铸或者模铸
							if (processCodeC.equals("B002")) {
								if (!StringTool.isNumeric(castRForg
										.getSpecName())) {
									messageM = "制程(" + mscPline + ")/全程途径码("
											+ wholeBacklog + "): 轧制规格("
											+ rolledSpec.getSpecName()
											+ ")->锻造规格("
											+ blitSpec.getBSpecName()
											+ "->铸造规格("
											+ castRForg.getSpecName()
											+ ")是钢锭规格!(二级工序:连铸)";
									continue;
								}
								biltCSpec = SpecManage.setBiltCSpec(designKey,
										mscPline, blitSpec.getSpecCode(),
										blitSpec.getBSpecCode(),
										blitSpec.getBSpecName(),
										castRForg.getSpecCode(),
										castRForg.getSpecName());
							} else {
								biltCSpec = SpecManage.setBiltCSpec(designKey,
										mscPline, blitSpec.getSpecCode(),
										blitSpec.getBSpecCode(),
										blitSpec.getBSpecName(),
										castRForg.getSpecCodeIngot(),
										castRForg.getSpecNameIngot());
							}
							if (CraftTool.isNumeric(biltCSpec.getbSpecName())) {
								// 验证压缩比
								String ratioFlag = Compressio.compressioRatio(
										con, dimater, height,
										biltCSpec.getbSpecName(), ratio);
								if (ratioFlag.equals("B")) {
									messageM = "制程(" + mscPline + ")/全程途径码("
											+ wholeBacklog + "): 轧制规格("
											+ rolledSpec.getSpecName()
											+ ")->锻造规格("
											+ blitSpec.getBSpecName()
											+ "->铸造规格("
											+ castRForg.getSpecName()
											+ ",压缩比大于产品码锻造比极限值为" + ratio
											+ ",制程不能包含锻造!";
									continue;
								}
							}
							// 验证产线
							List<ComMscPlineRSpecEntity> plineBs = LineManageDao
									.getProLine(con, "B",null);
							plineBs = LineManage.fiterProLine(plineBs,
									biltCSpec.getCSpecCode(), processCodeC, "",
									"", "");
							plineBs = LineManage.fiterOutLine(plineBs, false);
							if (plineBs == null || plineBs.size() == 0) {
								messageM = "制程(" + mscPline + ")/全程途径码("
										+ wholeBacklog + "):轧制规格("
										+ rolledSpec.getSpecName() + ")->锻造规格("
										+ blitSpec.getBSpecName() + "->铸造规格("
										+ castRForg.getSpecName()
										+ " 铸造工序不存在自炼产线";
								continue;
							}
							// 增加锻造规格与铸造规格
							blitSpecs.add(blitSpec);
							blitCSpecs.add(biltCSpec);
							// 重新获取轧管规格
							if (!rolledSpecsE.contains(rolledSpec)) {
								rolledSpecsE.add(rolledSpec);
							}
						}
					}
				} else {
					// 如果没有锻造工序，则是对应的连铸坯规格
					for (int i = 0; i < biltSpecArray.size(); i++) {
						if (!StringTool.isNumeric(biltSpecArray.get(i)[1])) {
							messageM = "制程(" + mscPline + ")/全程途径码("
									+ wholeBacklog + "): 轧制规格("
									+ rolledSpec.getSpecName() + ")->铸造规格(连铸)"
									+ biltSpecArray.get(i)[1] + "是钢锭规格!";
							continue;
						}
						CraftOrdDesignMscBiltCEntity biltCSpec = new CraftOrdDesignMscBiltCEntity();
						biltCSpec.setCSpecCode(biltSpecArray.get(i)[0]);
						biltCSpec.setCSpecName(biltSpecArray.get(i)[1]);
						biltCSpec.setDesginKey(design.getDesignKey());
						biltCSpec.setMscPline(mscPline);
						biltCSpec.setSpecCode(rolledSpec.getSpecCode());

						// 验证压缩比
						String ratioFlag = Compressio.compressioRatio(con,
								dimater, height, biltCSpec.getCSpecName(),
								ratio);
						if (ratioFlag.equals("C")) {
							messageM = "制程(" + mscPline + ")/全程途径码("
									+ wholeBacklog + "):轧制规格("
									+ rolledSpec.getSpecName() + ")->铸造规格(连铸)"
									+ biltSpecArray.get(i)[1]
									+ ",压缩比小于产品码锻造比极限值为" + ratio + ",制程必须包含锻造!";
							continue;
						}
						String processCodeC = "";
						if (wholeBacklog.contains("B")) {
							CraftOrdDesignMscPlPrcEntity process = operationDaoImp
									.getOperation(con, msc, mscPline, "B");
							processCodeC = process.getProcessCodeC();
						} else {
							processCodeC = "B002";
						}
						List<ComMscPlineRSpecEntity> plineBs = LineManageDao
								.getProLine(con, "B",null);
						plineBs = LineManage.fiterProLine(plineBs,
								biltCSpec.getCSpecCode(), processCodeC, "", "",
								"");
//						if (design.getOutsBilietFl().equals("121801")) {
//							plineBs = LineManage.fiterOutLine(plineBs, true);
//						} else {
//							plineBs = LineManage.fiterOutLine(plineBs, false);
//						}
						if (plineBs == null || plineBs.size() == 0) {
							if (design.getOutsBilietFl().equals("121801")) {
								messageM = "制程(" + mscPline + ")/全程途径码("
										+ wholeBacklog + "):轧制规格("
										+ rolledSpec.getSpecName()
										+ ")->铸造规格(连铸)"
										+ biltSpecArray.get(i)[1]
										+ " 铸造工序找不到(外购)产线";
							} else {
								messageM = "制程(" + mscPline + ")/全程途径码("
										+ wholeBacklog + "): 轧制规格("
										+ rolledSpec.getSpecName()
										+ ")->铸造规格(连铸)"
										+ biltSpecArray.get(i)[1]
										+ " 铸造工序找不到(自炼)产线";
							}
							continue;
						}
						blitCSpecs.add(biltCSpec);
						// 重新获取轧管规格
						if (!rolledSpecsE.contains(rolledSpec)) {
							rolledSpecsE.add(rolledSpec);
						}
					}

				}
			}

		}
		// 如果是管坯(特殊处理)
		if (design.getSpecStyle().equals("B")) {
			messageD = "D," + design.getDimater() + "," + design.getHeight()
					+ "," + "," + "," + "," + "," + "1";
			// 如果包含锻造工序
			if (wholeBacklog.contains("C")) {
				CraftOrdDesignMscBiltEntity blitSpec = new CraftOrdDesignMscBiltEntity();
				blitSpec.setSpecCode("/");
				blitSpec.setBSpecCode(design.getSpecCode());
				blitSpec.setBSpecName(design.getSpecName());
				blitSpec.setDesginKey(design.getDesignKey());
				blitSpec.setMscPline(mscPline);
				String processCodeC = "";
				if (wholeBacklog.contains("B")) {
					CraftOrdDesignMscPlPrcEntity process = operationDaoImp
							.getOperation(con, msc, mscPline, "B");
					processCodeC = process.getProcessCodeC();
				} else {
					processCodeC = "B002";
				}
				List<ComMscCastRForgEntity> castRForgs = specManageDaoImp
						.getBiltCSpec(con, blitSpec.getBSpecCode(),
								processCodeC);
				if (castRForgs == null || castRForgs.size() == 0) {
					if (processCodeC.equals("B002")) {
						messageM = "制程(" + mscPline + ")/全程途径码(" + wholeBacklog
								+ ") :锻造规格(" + blitSpec.getBSpecName()
								+ ")不存在连铸坯规格(二级工序:连铸)";
					} else {
						messageM = "制程(" + mscPline + ")/全程途径码(" + wholeBacklog
								+ ") :锻造规格(" + blitSpec.getBSpecName()
								+ ")找不到钢锭规格(二级工序:模铸)";
					}
				}

				for (ComMscCastRForgEntity castRForg : castRForgs) {
					// 连铸或者模铸
					CraftOrdDesignMscBiltCEntity biltCSpec = null;
					if (processCodeC.equals("B002")) {
						if (!StringTool.isNumeric(castRForg.getSpecName())) {
							messageM = "制程(" + mscPline + ")/全程途径码("
									+ wholeBacklog + ") :锻造规格("
									+ blitSpec.getBSpecName() + "->铸造规格("
									+ castRForg.getSpecName()
									+ ")是钢锭规格!(二级工序:连铸)";
							continue;
						}
						biltCSpec = SpecManage.setBiltCSpec(designKey,
								mscPline, blitSpec.getSpecCode(),
								blitSpec.getBSpecCode(),
								blitSpec.getBSpecName(),
								castRForg.getSpecCode(),
								castRForg.getSpecName());
					} else {
						biltCSpec = SpecManage.setBiltCSpec(designKey,
								mscPline, blitSpec.getSpecCode(),
								blitSpec.getBSpecCode(),
								blitSpec.getBSpecName(),
								castRForg.getSpecCodeIngot(),
								castRForg.getSpecNameIngot());
					}
					blitSpecs.add(blitSpec);
					blitCSpecs.add(biltCSpec);
				}
			} else {
				// 只包含铸造(制程已加限制 管坯只能必须包含铸造或者)
				CraftOrdDesignMscBiltCEntity biltCSpec = new CraftOrdDesignMscBiltCEntity();
				biltCSpec.setCSpecCode(design.getSpecCode());
				biltCSpec.setCSpecName(design.getSpecName());
				biltCSpec.setDesginKey(design.getDesignKey());
				biltCSpec.setBSpecCode("");
				biltCSpec.setbSpecName("");
				biltCSpec.setMscPline(mscPline);
				biltCSpec.setSpecCode("/");
				biltCSpec.setSpecCodeS(design.getSpecCode());
				blitCSpecs.add(biltCSpec);
			}

		}
		if (wholeBacklog.contains("D") || wholeBacklog.contains("B")) {
			// 判断是否找到铸造规格
			if (blitCSpecs == null || blitCSpecs.size() == 0) {
				specEntity.setErrMessage(messageM);
				specEntity.setFlag(false);
				specEntity.setObjKey(messageD);
				specEntity.setListItem("适用产线");
				return specEntity;
			}
		}
		rolledSpecs = SpecManage.fiterRollSpec(rolledSpecs);
		blitSpecs = SpecManage.fiterBiltSpec(blitSpecs);
		blitCSpecs = SpecManage.fiterBiltCSpec(blitCSpecs);
		specEntity.setErrMessage("");
		specEntity.setFlag(true);
		specEntity.setObjKey("");
		specEntity.setListItem("适用产线");
		specEntity.setRollSpecs(rolledSpecs);
		specEntity.setBillSpecs(blitSpecs);
		specEntity.setBillSpecCs(blitCSpecs);
		return specEntity;
	}

	/**
	 * 轧制规格类
	 * 
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @param gradeCode
	 *            钢种
	 * @param gradeName
	 *            钢种名称
	 * @param specCode
	 *            规格代码
	 * @param specName
	 *            规格描述
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return OrdDesignMscGzGgEntity
	 */
	public static CraftOrdDesignMscGzGgEntity setRollSpec(String designKey,
			String mscPline, String gradeCode, String gradeName,
			String specCode, String specName, String dimater, String height) {
		CraftOrdDesignMscGzGgEntity rollSpec = new CraftOrdDesignMscGzGgEntity();
		rollSpec.setDesginKey(designKey);
		rollSpec.setMscPline(mscPline);
		rollSpec.setGradecode(gradeCode);
		rollSpec.setGradename(gradeName);
		rollSpec.setSpecCode(specCode);
		rollSpec.setSpecName(specName);
		rollSpec.setDimater(dimater);
		rollSpec.setHeight(height);
		return rollSpec;
	}

	/**
	 * 锻造规格类
	 * 
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @param specCode
	 *            轧制规格
	 * @param bSpecCode
	 *            锻造规格代码
	 * @param bSpecName
	 *            锻造规格名称
	 * @return OrdDesignMscBiltEntity
	 */
	public static CraftOrdDesignMscBiltEntity setBilt(String designKey,
			String mscPline, String specCode, String bSpecCode, String bSpecName) {
		CraftOrdDesignMscBiltEntity blank = new CraftOrdDesignMscBiltEntity();
		blank.setDesginKey(designKey);
		blank.setMscPline(mscPline);
		blank.setSpecCode(specCode);
		blank.setBSpecCode(bSpecCode);
		blank.setBSpecName(bSpecName);
		return blank;
	}

	/**
	 * 锻造规格转铸造规格
	 * 
	 * @param sections
	 *            锻造规格
	 * @return List<OrdDesignMscBiltCEntity>
	 */
	public static List<CraftOrdDesignMscBiltCEntity> biltToBiltC(
			List<CraftOrdDesignMscBiltEntity> bilts) {
		List<CraftOrdDesignMscBiltCEntity> biltCs = new ArrayList<CraftOrdDesignMscBiltCEntity>();
		for (CraftOrdDesignMscBiltEntity bilt : bilts) {
			CraftOrdDesignMscBiltCEntity biltC = new CraftOrdDesignMscBiltCEntity();
			biltC.setDesginKey(bilt.getDesginKey());
			biltC.setMscPline(bilt.getMscPline());
			biltC.setSpecCode(bilt.getSpecCode());
			biltC.setBSpecCode("");
			biltC.setCSpecCode(bilt.getBSpecCode());
			biltC.setCSpecName(bilt.getBSpecName());
			biltCs.add(biltC);
		}
		return biltCs;
	}

	/**
	 * 铸造规格类
	 * 
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @param specCode
	 *            轧制规格
	 * @param bSpecCode
	 *            锻造规格
	 * @param cSpecCode
	 *            铸造规格代码
	 * @param cSpecName
	 *            铸造规格描述
	 * @return OrdDesignMscBiltCEntity
	 */
	public static CraftOrdDesignMscBiltCEntity setBiltCSpec(String designKey,
			String mscPline, String specCode, String bSpecCode,
			String bSpecName, String cSpecCode, String cSpecName) {
		CraftOrdDesignMscBiltCEntity bilt = new CraftOrdDesignMscBiltCEntity();
		bilt.setDesginKey(designKey);
		bilt.setMscPline(mscPline);
		bilt.setBSpecCode(bSpecCode);
		bilt.setbSpecName(bSpecName);
		bilt.setSpecCode(specCode);
		bilt.setCSpecCode(cSpecCode);
		bilt.setCSpecName(cSpecName);
		return bilt;
	}

	/**
	 * 过滤铸造规格
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<CraftOrdDesignMscBiltCEntity> fiterBiltCSpec(
			List<CraftOrdDesignMscBiltCEntity> biltSpecs) {
		if (biltSpecs == null || biltSpecs.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscBiltCEntity> list = new ArrayList<CraftOrdDesignMscBiltCEntity>();
		List<String> keys = new ArrayList<String>();
		for (CraftOrdDesignMscBiltCEntity biltSpec : biltSpecs) {
			String key = biltSpec.getDesginKey() + biltSpec.getMscPline()
					+ biltSpec.getSpecCode() + biltSpec.getBSpecCode()
					+ biltSpec.getCSpecCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltSpec);
			}
		}
		return list;
	}

	/**
	 * 过滤锻造规格
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<CraftOrdDesignMscBiltEntity> fiterBiltSpec(
			List<CraftOrdDesignMscBiltEntity> biltSpecs) {
		if (biltSpecs == null || biltSpecs.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscBiltEntity> list = new ArrayList<CraftOrdDesignMscBiltEntity>();
		List<String> keys = new ArrayList<String>();
		for (CraftOrdDesignMscBiltEntity biltSpec : biltSpecs) {
			String key = biltSpec.getDesginKey() + biltSpec.getMscPline()
					+ biltSpec.getSpecCode() + biltSpec.getBSpecCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltSpec);
			}
		}
		return list;
	}

	/**
	 * 过滤轧管规格
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<CraftOrdDesignMscGzGgEntity> fiterRollSpec(
			List<CraftOrdDesignMscGzGgEntity> rollSpecs) {
		if (rollSpecs == null || rollSpecs.size() == 0) {
			return null;
		}
		List<CraftOrdDesignMscGzGgEntity> list = new ArrayList<CraftOrdDesignMscGzGgEntity>();
		List<String> keys = new ArrayList<String>();
		for (CraftOrdDesignMscGzGgEntity rollSpec : rollSpecs) {
			String key = rollSpec.getDesginKey() + rollSpec.getMscPline()
					+ rollSpec.getSpecCode() + rollSpec.getGradecode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(rollSpec);
			}
		}
		return list;
	}

}
