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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.bomGeneration.BOMGeneration;
import com.steering.pss.sale.order.craftDesign.dao.*;
import com.steering.pss.sale.order.craftDesign.model.*;
import com.steering.pss.sale.order.craftDesign.server.*;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.SlmOrderHeadEntity;
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;

public class CraftDesignMain extends DBCommon {
	/**
	 * 工艺评审
	 * 
	 * @param ordLnPk
	 *            合同行主键(备料码代码)
	 * @param userName
	 *            操作人
	 * @param designKeyFlag
	 *            (工艺评审主键)
	 * @param flag
	 *            (0 销售合同 1 备料码)
	 * @param operateType
	 *            操作类型 0工艺设计1刷新工艺
	 * @param designStyle
	 *            操作类型 0不做内控1做内控
	 * @return CoreReturnObject
	 * 
	 */
	public CoreReturnObject craftDesign(String ordLnPk, String userName,
			String designKeyFlag, String flag, String operateType,
			String designStyle) {
		CoreReturnObject core = new CoreReturnObject();
		// flag = "1";
		// ordLnPk = "BL00270";
		core.setResult(false);
		/* 先判断传入参数是否满足要求 */
		if (ordLnPk.length() == 0 || userName.length() == 0) {
			core.setV_errMsg("请传入正确的合同行主键与操作人!");
			return core;
		}
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			// con.setAutoCommit(false);
			// 获取合同数据
			CraftOrdDesignMscEntity craftOrdDesign = null;
			String[] saleInfo = null;
			String supp = ""; // 制造商
			//0 销售合同 1 备料码
			if (flag.equals("0")) {
				// 合同信息
				PretreatmentDao pretreatmentDao = new PretreatmentDao();
				craftOrdDesign = pretreatmentDao.getOrderInfo(con, ordLnPk);
				// 获取销售组织
				CoreCastWell coreCastWell = new CoreCastWell();
				saleInfo = coreCastWell.getSaleInfo(con, craftOrdDesign.getOrdPk());

				SqlBuilder b = new SqlBuilder("CoreOrderCopy.queryHead");
				b.Fill(new String[]{ craftOrdDesign.getOrdPk() });
				SlmOrderHeadEntity orderHeadEntity = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderHeadEntity.class);
				if (orderHeadEntity != null) {
					supp = orderHeadEntity.getSupplyUnit();
				}
			} else {
				// 备料信息
				CraftBlDao craftBlDao = new CraftBlDao();
				craftOrdDesign = craftBlDao.getBl(con, ordLnPk);
			}

			if (craftOrdDesign == null) {
				core.setV_errMsg("通过合同行主键找不到唯一合同行,请确认合同行主键是否有误!");
				return core;
			}

			// 给备料码赋值
			if (flag.equals("1")) {
				CraftBl craftBl = new CraftBl();
				craftOrdDesign = craftBl.innerCraftBl(con, craftOrdDesign);
			}
			/**
			 * 1.验证合同行数据是否符合工艺评审要求
			 */
			Pretreatment pretreatment = new Pretreatment();
			String message = pretreatment.checkcraftLine(con, craftOrdDesign);
			if (message.length() > 0) {
				core.setV_errMsg(message);
				return core;
			}

			/**
			 * 2.工艺评审预处理
			 */
			String[] pretreantmentResults = pretreatment.pretreatment(con, craftOrdDesign,
					designKeyFlag, operateType, ordLnPk);
			if(!pretreantmentResults[0].equals("")){
				core.setV_errMsg(pretreantmentResults[0]);
				return core;
			}
		    String designKey = pretreantmentResults[1];
			if (designKey.length() == 0) {
				core.setV_errMsg("工艺评审预处理失败!");
				return core;
			}

			/**
			 * 3.初始化明细与状态
			 */
			CraftStatus craftStatus = new CraftStatus();
			CraftStatusDao statusDaoImp = new CraftStatusDao();
			craftStatus.initStatus(con, designKey, ordLnPk,
					craftOrdDesign.getPsc(), craftOrdDesign.getMsc(),
					craftOrdDesign.getFinalUser(), craftOrdDesign.getUseCode(),
					userName, operateType, designStyle,saleInfo);

			/**
			 * 4. 验证基础冶金规范码MSC
			 */
			Msc0 msc0imp = new Msc0();
			CraftEntity design = msc0imp.checkMsc0(con, designKey, userName,
					craftOrdDesign,saleInfo);
			if (design == null) {
				statusDaoImp.updateDesignStsTklg(con, designKey, userName, "F",
						"W");

				core.setV_errMsg("该记录的MSC0不可用");
				return core;
			}
			
			// -------------------保存附加要求全项
			String ordAddAskMsg = OrderAddAskDao.SaveOrderAddAsk(designKey, ordLnPk, con);
			if(!ordAddAskMsg.equals("")){
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "F", "91",
						ordAddAskMsg, "", designKey, "MSC(0)");
				statusDaoImp.updateDesignStsTklg(con, designKey, "",
						"F", "W");
				return core;
			}

			// 获取cic用于成分偏差标准验证
			String biasCic = "";
			List<CraftOrdDesignStdCicEntity> chemBiasList = null;
			ComBaseSpecEntity baseSpec = new MscSplineDao().getSpecInfoBySpecCode(design.getSpecCode(), con);
			/**
			 * 5. 验证交付标准
			 */
			MscChemDao chemDao = new MscChemDao();
			MscPhyDao mscPhyDao = new MscPhyDao();
			MscSpecDao mscSpecDao = new MscSpecDao();
			MscDeteDao mscDeteDao = new MscDeteDao();
			MscWaterDao mscWaterDao = new MscWaterDao();
			MscGProcessDao mscGProcessDao = new MscGProcessDao();
			String errMessageC = "";
			String errMessageP = "";
			String errMessageS = "";
			String errMessageD = "";
			String errMessageW = "";
			String errMessageG = "";
			if (design.getMscOrStyle().equals("C")
					|| design.getMscOrStyle().equals("D")
					|| design.getMscOrStyle().equals("E")) {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "S", "00", "成分标准无要求!", "", designKey,
						"成分标准");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "S", "00", "理化标准无要求!", "", designKey,
						"理化标准");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "S", "00", "公差标准无要求!", "", designKey,
						"公差标准");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "S", "00", "探伤标准无要求!", "", designKey,
						"探伤标准");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "S", "00", "水压标准无要求!", "", designKey,
						"水压标准");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "S", "00", "加工标准无要求!", "", designKey,
						"加工标准");
			} else {
				/**
				 * 成分标准
				 */
				MscChem chemImp = new MscChem();
				// 查询成分索引号
				List<String> cics = chemImp.getCicByMscAndModel(con,
						design.getMsc0(), design.getModelCode(),
						design.getMsc0Valig());
				// 如果没有成分索引号,则代表成分无要求
				if (cics == null || cics.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "成分标准无要求", "",
							designKey, "成分标准");
					errMessageC = design.getMsc0() + "找不到成分索引号";
				} else {
					// 否则验证成分标准
					
					StdEntity stdEntity = chemImp.checkChem(con, design, baseSpec, "0",
							cics);
					// 验证失败
					if (!stdEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "F", "91",
								stdEntity.getMessage(), "", designKey, "成分标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						// 通过,则修改明细,并保存成分标准
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "S", "11", "成分标准通过验证!", "",
								designKey, "成分标准");
						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdCicEntity> chems = (List<CraftOrdDesignStdCicEntity>) stdEntity
								.getObjM();
						for (CraftOrdDesignStdCicEntity chem : chems) {
							chemDao.addChem(con, chem);
						}
						// 修改索引号
						chemDao.updateDesignStd(con, stdEntity.getObjKey(),
								stdEntity.getSmeltingCnt(), stdEntity.getProducCnt(), 
								stdEntity.getRate(), designKey);
						biasCic = stdEntity.getObjKey();
						chemBiasList = chems;
					}

				}

				/**
				 * 理化标准
				 */
				MscPhy mscPhyImp = new MscPhy();
				// 查询理化索引号
				List<String> pics = mscPhyImp.getPicByMscAndModelCode(con,
						design.getMsc0(), design.getModelCode(),
						design.getMsc0Valig());
				// 如果找不到理化索引号,则认为理化标准无要求
				if (pics == null || pics.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "理化标准无要求", "",
							designKey, "理化标准");
					errMessageP = design.getMsc0() + "找不到理化索引号";
				} else {
					StdEntity stdEntity = mscPhyImp.checkPhy(con, design, "0",
							pics, baseSpec, "/", "/", "/");
					if (!stdEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "F", "91",
								stdEntity.getMessage(), "", designKey, "理化标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "S", "11", "理化标准通过验证", "",
								designKey, "理化标准");
						// 理化保存放在验证里面， 水压要用到。
						// 保存理化标准与取样要求
						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdPicEntity> phys = (List<CraftOrdDesignStdPicEntity>) stdEntity
								.getObjM();
						for (CraftOrdDesignStdPicEntity phy : phys) {
							mscPhyDao.savePhy(con, phy);
						}
						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdPicSmpEntity> smps = (List<CraftOrdDesignStdPicSmpEntity>) stdEntity
								.getObjC();
						for (CraftOrdDesignStdPicSmpEntity smp : smps) {
							if (!mscPhyDao.checkSmp(con, smp)) {
								mscPhyDao.savePicSample(con, smp);
							}

						}
						// 修改理化索引号
						mscPhyDao.updateDesignStd(con, stdEntity.getObjKey(),
								designKey);
					}
				}

				/**
				 * 公差标准
				 */
				MscSpec mscSpecImp = new MscSpec();
				// 查询公差索引号
				List<String> sics = mscSpecImp.getSicByMscAndModel(con,
						design.getMsc0(), design.getModelCode(),
						design.getMsc0Valig());
				if (sics == null || sics.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "公差标准无要求", "",
							designKey, "公差标准");
					errMessageS = design.getMsc0() + "找不到公差索引号";
				} else {
					StdEntity stdEntity = mscSpecImp.checkSpec(con, design,
							"0", sics, "", baseSpec);
					if (!stdEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "F", "91",
								stdEntity.getMessage(), "", designKey, "公差标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "S", "11",
								stdEntity.getMessage(), "", designKey, "公差标准");
						// 保存公差标准与索引号
						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdSicEntity> specs = (List<CraftOrdDesignStdSicEntity>) stdEntity
								.getObjM();

						for (CraftOrdDesignStdSicEntity spec : specs) {
							mscSpecDao.saveSpec(con, spec);
						}
						mscSpecDao.updateDesignStd(con, stdEntity.getObjKey(),
								designKey);
					}
				}

				/**
				 * 探伤标准
				 */
				MscDete mscDeteImp = new MscDete();
				List<String> dics = mscDeteImp.getDicByMscAndModel(con,
						design.getMsc0(), design.getModelCode(),
						design.getMsc0Valig());
				if (dics == null || dics.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "探伤标准无要求!", "",
							designKey, "探伤标准");
					errMessageD = design.getMsc0() + "找不到探伤索引号";
				} else {
					StdEntity stdEntity = mscDeteImp.checkDete(con, design,
							"0", dics, baseSpec);
					if (!stdEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "F", "91",
								stdEntity.getMessage(), "", designKey, "探伤标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "S", "11",
								stdEntity.getMessage(), "", designKey, "探伤标准");
						// 保存探伤标准
						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdDicEntity> detes = (List<CraftOrdDesignStdDicEntity>) stdEntity
								.getObjM();
						for (CraftOrdDesignStdDicEntity dete : detes) {
							mscDeteDao.saveDete(con, dete);
						}
						mscDeteDao.updateDesignStd(con, stdEntity.getObjKey(),
								designKey);
					}
				}

				/**
				 * 水压标准
				 */
				MscWater mscWaterImp = new MscWater();
				List<String> wics = mscWaterImp.getWicByMscAndModel(con,
						design.getMsc0(), design.getModelCode(),
						design.getMsc0Valig(), design.getDimater(),
						design.getHeight());
				
				List<String> mscnWics = null;
				if(!design.getMsc0().equals(design.getMscOr())){
					mscnWics = mscWaterImp.getWicByMscAndModel(con,
							design.getMscOr(), design.getModelCode(),
							"", design.getDimater(),
							design.getHeight());
				}
				
				if (wics == null || wics.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "水压标准无要求", "",
							designKey, "水压标准");
					errMessageW = design.getMsc0() + "找不到水压索引号";
				} else {
					StdEntity stdEntity = mscWaterImp.checkWater(con, design,
							"0", wics, mscnWics);
					if (!stdEntity.isFlag()) {
						if(stdEntity.getListItem().equals("MSC(n)")){
							statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
									design.getMscN(), "F", "91",
									stdEntity.getMessage(), "", designKey,
									"MSC(n)");
						}else{
							statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
									design.getMsc0(), "F", "91",
									stdEntity.getMessage(), "", designKey, "水压标准");
						}
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "S", "11",
								stdEntity.getMessage(), "", designKey, "水压标准");
						// 保存水压标准

						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdWicEntity> waters = (List<CraftOrdDesignStdWicEntity>) stdEntity
								.getObjM();
						for (CraftOrdDesignStdWicEntity water : waters) {
							if(water.getPressure().equals("")) continue;
							mscWaterDao.saveWater(con, water);
						}
						mscWaterDao.updateDesignStd(con, stdEntity.getObjKey(),
								designKey);
					}
				}

				/**
				 * 加工标准
				 */
				MscGprocess mscProcessingImp = new MscGprocess();
				List<String> gics = mscProcessingImp.getGicByMscAndModel(con,
						design.getMsc0(), design.getModelCode(),
						design.getMsc0Valig());
				if (gics == null || gics.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "加工标准无要求", "",
							designKey, "加工标准");
					errMessageG = design.getMsc0() + "找不到加工索引号";
				} else {
					StdEntity stdEntity = mscProcessingImp.checkProcess(con,
							design, "0", gics);
					if (!stdEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "F", "91",
								stdEntity.getMessage(), "", designKey, "加工标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMsc0(), "S", "11",
								stdEntity.getMessage(), "", designKey, "加工标准");
						@SuppressWarnings("unchecked")
						List<CraftOrdDesignStdGicEntity> processes = (List<CraftOrdDesignStdGicEntity>) stdEntity
								.getObjM();
						for (CraftOrdDesignStdGicEntity process : processes) {
							mscGProcessDao.saveProcess(con, process);
						}
						mscGProcessDao.updateDesignStd(con,
								stdEntity.getObjKey(), designKey);
					}
				}
			}

			// 用于成分偏差标准客户
			String biasCicN = "";
			List<CraftOrdDesignStdCicEntity> chemNBiasList = null;
			/**
			 * 6.MSC(n)
			 */
			if (!design.getMscOrStyle().equals("C")
					&& !design.getMscOrStyle().equals("D")
					&& !design.getMscOrStyle().equals("E")) {
				MscN mscN = new MscN();
				if (!design.getMscN().equals("")) {
					MscNEntity mscNEntity = mscN.checkMscN(con, design,
							design.getMscN());
					if (!mscNEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscN(), "F", "91",
								mscNEntity.getMessage(), "", designKey,
								"MSC(n)");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscN(), "S", "11", "MSC(n)通过验证", "",
								designKey, "MSC(n)");
						// 保存特殊要求交付标准
						// 成分
						List<CraftOrdDesignStdCicEntity> chems = mscNEntity
								.getChems();
						if (chems != null && chems.size() > 0) {
							chemNBiasList = chems;
							for (CraftOrdDesignStdCicEntity chem : chems) {
								chemDao.addChem(con, chem);
								biasCicN = chem.getCic();
							}
						}
						// 理化
						
						//理化在验证的逻辑中保存， 因为水压要用到。
						
						// 取样要求
						List<CraftOrdDesignStdPicSmpEntity> smps = mscNEntity
								.getSmps();
						if (smps != null && smps.size() > 0) {
							for (CraftOrdDesignStdPicSmpEntity comPicSmp : smps) {
								if (!mscPhyDao.checkSmp(con, comPicSmp)) {
									mscPhyDao.savePicSample(con, comPicSmp);
								}

							}
						}
						// 公差
						List<CraftOrdDesignStdSicEntity> specs = mscNEntity
								.getSpecs();
						if (specs != null && specs.size() > 0) {
							for (CraftOrdDesignStdSicEntity spec : specs) {
								mscSpecDao.saveSpec(con, spec);
							}
						}
						// 探伤
						List<CraftOrdDesignStdDicEntity> detes = mscNEntity
								.getDetes();
						if (detes != null && detes.size() > 0) {

							for (CraftOrdDesignStdDicEntity dete : detes) {
								mscDeteDao.saveDete(con, dete);
							}
						}
						// 水压
						List<CraftOrdDesignStdWicEntity> waters = mscNEntity
								.getWaters();
						if (waters != null && waters.size() > 0) {
							for (CraftOrdDesignStdWicEntity water : waters) {
								mscWaterDao.saveWater(con, water);
							}
						}
						// 加工
						List<CraftOrdDesignStdGicEntity> processes = mscNEntity
								.getProesses();
						if (processes != null && processes.size() > 0) {

							for (CraftOrdDesignStdGicEntity process : processes) {
								mscGProcessDao.saveProcess(con, process);
							}
						}
						// 索引号
						CraftOrdDesignStdEntity ordStdEntity = mscNEntity
								.getStdEntity();
						ordStdEntity.setDesginKey(design.getDesignKey());
						if (ordStdEntity != null) {
							MscNDao mscNDao = new MscNDao();
							mscNDao.updateMscNStd(con, ordStdEntity);
						}
					}
				} else {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscN(), "S", "00", "Msc(n)无要求", "",
							designKey, "MSC(n)");
				}
			} else {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscN(), "S", "00", "Msc(n)无要求", "",
						designKey, "MSC(n)");
			}

			/**
			 * 7.接箍码
			 */
			if (design.getMscOrStyle().equals("B")
					|| design.getMscOrStyle().equals("C")
					|| design.getMscOrStyle().equals("D")) {
				MscJG mscJGimp = new MscJG();
				MscJGDao mscJGDao = new MscJGDao();
				// 查询设定接箍
				List<CraftOrdDesignMscJgEntity> setJgEntitys = mscJGDao
						.getSetJG(con, designKey);
				Object[] jgInfo = mscJGimp.checkMscJG(con, design.getMscOr(),
						design.getDimater(), design.getHeight(),
						design.getSteelCode(), design.getSteelName(),
						design.getModelCode(), design.getModelDesc(),
						design.getSpecCode(), design.getSpecName(),
						design.getSpecStyle(), designKey,
						design.getOrdLnPk(), setJgEntitys, userName,
						designKeyFlag, flag, operateType, designStyle);
				String jgMessage = (String) jgInfo[0];
				String jgFlagSet = (String) jgInfo[1];
				// 如果验证返回结果信息不为空
				if (jgMessage.length() > 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", jgMessage, "" + ","
									+ jgFlagSet, designKey, "接箍");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return core;
				}

				// 加1是为了回退功能准备
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", "接箍码通过验证", "" + ",1",
						designKey, "接箍");
				// 接箍码信息
				CraftOrdDesignMscJgEntity jgEntity = (CraftOrdDesignMscJgEntity) jgInfo[2];
				// 保存接箍或者修改设定接箍
				
				//复制接箍文件
				if(!Pretreatment.CopyJgFile(jgEntity)){
					String jgFileMsg = "接箍码" + jgEntity.getCodeJg() + "接箍KEY" + jgEntity.getKeyJg()
							+ "对应的接箍文件获取失败，请重新工艺制定！";
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", jgFileMsg, "" + ","
									+ jgFlagSet, designKey, "接箍");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return core;
				}
				
				if (setJgEntitys == null || setJgEntitys.size() == 0) {
					mscJGDao.saveJG(con, jgEntity);
				} else {
					mscJGDao.updateSetJG(con, jgEntity);
				}
				@SuppressWarnings("unchecked")
				// 接箍备料关系
				List<CraftOrdDesignMscJgBlEntity> jgblEntitys = (List<CraftOrdDesignMscJgBlEntity>) jgInfo[3];
				for (CraftOrdDesignMscJgBlEntity jgblEntity : jgblEntitys) {
					mscJGDao.saveJgBl(con, jgblEntity);
				}

				/**
				 * 7.1接箍制程
				 */
				MscJGPl mscJGPl = new MscJGPl();
				CraftOrdDesignMscJgPlEntity jgplEntity = mscJGPl.getJGPl(con,
						jgEntity.getKeyJg(), designKey, ordLnPk);
				// 如果找不到接箍制程
				if (jgplEntity == null) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91",
							"接箍码:" + jgEntity.getCodeJg() + "缺少制程!", "",
							designKey, "接箍制程");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return core;
				}
				/**
				 * 7.2接箍工序点
				 */
				List<CraftOrdDesignMscJgPlStiEntity> mscPlStiEntitys = mscJGPl
						.checkJGPlSti(con, jgEntity.getKeyJg(),
								jgplEntity.getMscPline(), designKey);
				// 如果找不到工序序
				if (mscPlStiEntitys == null) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91",
							"接箍码:" + jgEntity.getCodeJg() + "缺少工序点!", "",
							designKey, "接箍制程");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return core;
				}
				/**
				 * 7.3 验证接箍加工的产线
				 */
				MscJGDao mscJGDaoImp = new MscJGDao();
				LineManageDao lineManageDao = new LineManageDao();
				// 查询接箍基础信息
				ComMscJgEntity comMscJgEntity = mscJGDaoImp.getJGBaseInfo(con,
						jgEntity.getCodeJg());
				// 查询接箍加工产线
				List<ComMscPlineRSpecEntity> plines = lineManageDao
						.getLineBySpec(con, comMscJgEntity.getModelCode(),
								comMscJgEntity.getSpecJg(), "H");
				
//				ProDuc proDucimp = new ProDuc();
//				ProDucDao proDucDao = new ProDucDao();
//				 proDucDao.getCraftDesignGrade(designKey, jgplEntity.getMscPline(), con);
//				List<CraftOrdDesignMscCftEntity> cftEntitys =  proDucimp.checkCraft(
//						designKey, design.getMscOr(), jgplEntity.getMscPline(), grades,
//						design.getDimater(), design.getHeight(), design.getSpecCode(), con);
				
				// 找不到接箍加工产线
				if (plines == null || plines.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91",
							"接箍码:" + jgEntity.getCodeJg() + "/接箍主键("+ jgEntity.getKeyJg() +")的接箍制程("
							+ jgplEntity.getMscPline() +")不可用！(接箍加工工序找不到产线![扣型:"
									+ comMscJgEntity.getModelDesc() + ",接箍规格:"
									+ comMscJgEntity.getSpecJgDesc() + "])", "",
							designKey, "接箍制程");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return core;
				}
				/**
				 * 全部验证成功
				 */
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", "接箍制程通过验证", "",
						designKey, "接箍制程");
				MscJGPlDao mscJGPlDao = new MscJGPlDao();
				mscJGPlDao.saveJGpl(con, jgplEntity);
				for (CraftOrdDesignMscJgPlStiEntity mscPlStiEntity : mscPlStiEntitys) {
					mscJGPlDao.saveJGPlSti(con, mscPlStiEntity);
				}
				// 保存接箍加工产线
				for (ComMscPlineRSpecEntity pline : plines) {
					mscJGPlDao.saveJGLine(con, designKey,
							jgplEntity.getMscPline(), pline.getPlineCode(),
							pline.getPlineName(), pline.getWellCode(),
							pline.getWellName());
				}

			} else {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "00", "接箍码无要求", "", designKey,
						"接箍");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "00", "接箍制程无要求", "", designKey,
						"接箍制程");
			}

			/**
			 * 8.备料码
			 */
			if (design.getMscOrStyle().equals("C")
					|| design.getMscOrStyle().equals("E")) {
				MscBL mscBL = new MscBL();
				Object[] blInfo = mscBL.checkBL(con, design.getMscOr(),
						design.getDimater(), design.getHeight(),
						design.getSteelCode(), design.getSteelName(),
						design.getSpecCode(), design.getSpecName(),
						design.getSpecStyle(), designKey, userName,
						designKeyFlag, flag, operateType, designStyle,
						design.getModelCode(), design.getModelDesc());
				String blMessage = (String) blInfo[0];
				@SuppressWarnings("unchecked")
				List<String[]> mscBLs = (List<String[]>) blInfo[1];
				if (mscBLs == null || mscBLs.size() == 0) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", blMessage, "",
							designKey, "备料");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return core;
				} else {
					for (String[] str : mscBLs) {
						MscBLDao mscBlDao = new MscBLDao();
						mscBlDao.saveBL(con, str[0], str[1], str[2], str[3]);
					}
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "S", "11", "备料码通过验", "",
							designKey, "备料");
				}
			} else {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "00", "备料码无要求", "", designKey,
						"备料");
			}
			
			/**
			 * 附件图片处理
			 */
			if(design.getMscOrStyle().equals("C")){
				String fjMsg = Pretreatment.CheckFjImg(craftOrdDesign, con);
				if(!fjMsg.equals("")){
					String objKey = "";
					if(fjMsg.contains("设定附件图片")){
						objKey = "1";
					}
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", fjMsg, objKey,
							designKey, "附件图片");
					statusDaoImp.updateDesignStsTklg(con, design.getDesignKey(), "", "F",
							"W");
					return core;
				}else{
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "S", "11", "附件图片通过验证", "",
							designKey, "附件图片");
				}
			}else{
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "00", "附件图片无要求", "", designKey,
						"附件图片");
			}
			
			

			// 用于成分偏差标准钢种
			String biasGradeCode = "";
			String biasGradeName = "";
			/**
			 * 9.制程
			 */
			// 9.1查询设定制程
			ProDucDao proDucDao = new ProDucDao();
			List<CraftOrdDesignMscPlEntity> setMscPlEntitys = proDucDao
					.getMscPlSetFl(con, designKey);
			// 9.2验证制程
			List<String> errMessages = new ArrayList<String>();
			errMessages.add(errMessageC);
			errMessages.add(errMessageP);
			errMessages.add(errMessageS);
			errMessages.add(errMessageD);
			errMessages.add(errMessageW);
			errMessages.add(errMessageG);
			ProDuc proDucimp = new ProDuc();
			Object[] mscPlInfo = proDucimp.checkProDuc(con, design,
					errMessages, setMscPlEntitys);
			boolean flagProDuc = (Boolean) mscPlInfo[0];
			String messageProDuc = (String) mscPlInfo[1];
			@SuppressWarnings("unchecked")
			List<CraftOrdDesignMscPlEntity> mscPlEntitys = (List<CraftOrdDesignMscPlEntity>) mscPlInfo[2];
			String litItem = (String) mscPlInfo[3];
			if (flagProDuc == false) {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "F", "90", messageProDuc, "",
						designKey, litItem);
				statusDaoImp.updateDesignStsTklg(con, designKey, "", "F", "W");

				return core;
			} else {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", messageProDuc, "",
						designKey, litItem);
			}

			/**
			 * 10.验证制程里的钢种、内控标准、规格、产线
			 */
			// String innerMessage = "内控标准通过验证";
			// 制程验证通过标识 true通过 flase失败
			boolean proDucFlag = false;
			Grade grade = new Grade();
			GradeDao gradeDao = new GradeDao();
			SpecManage specManage = new SpecManage();
			LineManage lineManage = new LineManage();
			OperationDao operationDao = new OperationDao();
			ProDucSaveDao proDucSaveDao = new ProDucSaveDao();
			OperationPointDao operationPointDao = new OperationPointDao();
			String errMessage = "";
			String innerMessage = "";
			// 内控标识(暂时)
			boolean innerFlag = true;
			for (CraftOrdDesignMscPlEntity mscPlEntity : mscPlEntitys) {
				/* 10.1 钢种 */
				Object[] gradeInfo = grade.checkGrade(con,
						mscPlEntity.getMscPline(),
						mscPlEntity.getWholeBacklog(), design.getMscOr(),
						design.getDimater(), design.getHeight(),
						design.getSpecStyle(), design.getSpecName(),
						design.getOutsBilietFl(), design.getMscOrStyle(),
						designKey);
				// 钢种标识
				boolean flagGrade = (Boolean) gradeInfo[0];
				// 钢种提示信息
				String gradeMessage = (String) gradeInfo[1];
				/* 验证失败, 如果有设定制程,则直接终止程序,否则验证下一个制程 */
				if (!flagGrade) {
					if (mscPlEntity.getSetFl().equals("1")) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "F", "91", gradeMessage, "",
								designKey, "钢种");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						errMessage += "{" + gradeMessage + "}";
						continue;
					}
				} else {
					if (mscPlEntity.getSetFl().equals("1")) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "S", "11", gradeMessage, "",
								designKey, "钢种");
					}
				}
				// 钢种集合
				@SuppressWarnings("unchecked")
				List<CraftOrdDesignMscPlGzEntity> grades = (List<CraftOrdDesignMscPlGzEntity>) gradeInfo[2];
				
				
				
				/* 10.2 规格 */
				SpecEntity specEntity = specManage.checkSpec(con, mscPlEntity,
						grades, design,supp);
				/* 验证失败, 如果有设定制程,则终止工艺评审,否则验证下一个制程 */
				if (!specEntity.isFlag()) {
					if (mscPlEntity.getSetFl().equals("1")) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "F", "91",
								specEntity.getErrMessage(),
								specEntity.getObjKey(), designKey,
								specEntity.getListItem());
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");
						return core;
					} else {
						errMessage += "{" + specEntity.getErrMessage() + "}";
						continue;
					}
				} else {
					if (mscPlEntity.getSetFl().equals("1")) {
						if (mscPlEntity.getWholeBacklog().contains("E")) {
							statusDaoImp.updateCraftOrdDesignStsTklgLst3(con,
									design.getMscOr(), "S", "11", "镦拔扩通过验证",
									designKey, "镦拔扩");
						} else {
							statusDaoImp.updateCraftOrdDesignStsTklgLst3(con,
									design.getMscOr(), "S", "00", "镦拔扩无要求",
									designKey, "镦拔扩");
						}

					}

				}
				
				// 合同原始的外径与壁厚
				String dimater = design.getDimater();
				String height = design.getHeight();
				String specStyle = design.getSpecStyle();
				/* 10.4内控标准 */
				InnerStd innerStd = new InnerStd();
				InnerStdEntity innerStdEntity = null;
				// 排产提报不考虑内控
				//if (designStyle.equals("1")) {  内控现在都要考虑 （下面保存成分内控有特殊处理。）
//					if (design.getAllowNk().equals("1")) {
						// 初始状态
						innerFlag = true;
						innerStdEntity = innerStd.checkInnerStd(con,
								design.getMscOr(), mscPlEntity.getMscPline(),
								mscPlEntity.getWholeBacklog(), design, grades,
								specEntity);
						design.setDimater(dimater);
						design.setHeight(height);
						design.setSpecStyle(specStyle);
						innerMessage += innerStdEntity.getMessage();
						/* 验证失败, 如果有设定制程,则终止工艺评审,否则验证下一个制程 */
						if (!innerStdEntity.isFlag()) {
							// innerMessage = innerStdEntity.getMessage();
							if (mscPlEntity.getSetFl().equals("1")) {
								statusDaoImp.updateCraftOrdDesignStsTklgLst(
										con, design.getMscOr(), "F", "91",
										innerStdEntity.getMessage(), "",
										designKey, "内控标准");
								statusDaoImp.updateDesignStsTklg(con,
										designKey, "", "F", "W");
								return core;
							} else {
								errMessage += "{" + innerStdEntity.getMessage()
										+ "}";
								continue;
							}
						} else {
							if (innerStdEntity.isFlag()) {
								innerFlag = true;
							} else {
								innerFlag = false;
							}
						}
					//}
				//}

				// 工艺参数
				Object[] craftResults =  proDucimp.checkCraft(
						designKey, design.getMscOr(), mscPlEntity.getMscPline(), grades,
						design.getDimater(), design.getHeight(), design.getSpecCode(),
						specEntity.getRollSpecs(), specEntity.getBillSpecs(), specEntity.getBillSpecCs(), con);
				@SuppressWarnings("unchecked")
				List<CraftOrdDesignMscCftEntity> cftEntitys = (List<CraftOrdDesignMscCftEntity>)craftResults[1];
				if(!craftResults[0].equals("")){
					 statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
					 design.getMscOr(), "F", "91",
					 craftResults[0].toString(), "", designKey, "内控标准");
					 statusDaoImp.updateDesignStsTklg(con, designKey, "",
					 "F", "W");
					 return core;
				}
				
				/* 10.3适用产线 移动到保存工序之后*/
				LineEntity lineEntity = lineManage.analysisProLine(con, design,
						mscPlEntity, specEntity.getRollSpecs(), specEntity.getBillSpecs(),
						specEntity.getBillSpecCs(), innerStdEntity);
				/* 验证失败, 如果有设定制程,则终止工艺评审,否则验证下一个制程 */
				if (!lineEntity.isFlag()) {
					if (mscPlEntity.getSetFl().equals("1")) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "F", "91",
								lineEntity.getErrMessage(),
								lineEntity.getObjKey(), designKey, "适用产线");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");

						return core;
					} else {
						errMessage += "{" + lineEntity.getErrMessage() + "}";
						continue;
					}
				}
				
				// if (design.getMps().equals("1")
				// && (cftEntitys == null || cftEntitys.size() == 0)) {
				// if (setMscPlEntitys != null && setMscPlEntitys.size() > 0) {
				// statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
				// design.getMscOr(), "F", "91",
				// "合同对MPS有要求,但制程:" + mscPlEntity.getMscPline()
				// + "没有工艺参数!", "", designKey, "内控标准");
				// statusDaoImp.updateDesignStsTklg(con, designKey, "",
				// "F", "W");
				//
				// return core;
				// } else {
				// errMessage += "** 合同对MPS有要求,但制程:"
				// + mscPlEntity.getMscPline() + "没有工艺参数!";
				// continue;
				// }
				// }

				/* **********验证通过,制程标识改为true***************** */
				proDucFlag = true;

				/* 10.5 查询工序与工序点并且保存 */
				String[] processCodes = mscPlEntity.getWholeBacklog()
						.split("-");
				for (String processCode : processCodes) {
					// 工序
					CraftOrdDesignMscPlPrcEntity mscPlPrcEntity = operationDao
							.getOperation(con, design.getMscOr(),
									mscPlEntity.getMscPline(), processCode);
					mscPlPrcEntity.setWholeBacklog(mscPlEntity
							.getWholeBacklog());
					mscPlPrcEntity.setWholeBacklogDesc(mscPlEntity
							.getWholeBacklogDesc());
					mscPlPrcEntity.setDesginKey(designKey);
					//复制工艺贴图
					String prcFile = "Qcm/Msc/ProcessFile/" + design.getMscOr()+mscPlPrcEntity.getMscPline()+mscPlPrcEntity.getProcessCode() + "/";
					String[] results = FTPTool.CopyFile(mscPlPrcEntity.getProcessFile(), prcFile);
					if(results[0].equals("False")){
						String prcFileMsc = "制程" + mscPlEntity.getMscPline() + mscPlPrcEntity.getProcessDesc()
								+ "工序对应的工艺贴图获取失败，请重新工艺制定！"+results[1];
						statusDaoImp.updateCraftOrdDesignStsTklgLst(
								con, design.getMscOr(), "F", "91",
								prcFileMsc, "",
								designKey, "内控标准");
						statusDaoImp.updateDesignStsTklg(con,
								designKey, "", "F", "W");
						return core;
					}
					prcFile = results[1];
					mscPlPrcEntity.setProcessFile(prcFile);
					operationDao.addOperation(con, mscPlPrcEntity);
					// 工序点
					List<CraftOrdDesignMscPlStiEntity> mscPlStiEntitys = operationPointDao
							.getOperationPoint(con, design.getMscOr(),
									mscPlEntity.getMscPline(), processCode);
					for (CraftOrdDesignMscPlStiEntity mscPlStiEntity : mscPlStiEntitys) {
						mscPlStiEntity.setDesginKey(designKey);
						operationPointDao.saveOperationPoint(con,
								mscPlStiEntity);
					}
				}				
				
				// 内控标准(对内控标准有要求) (MSC对内控无要求时，成分内控同样需要)
				List<CraftOrdDesignStdCicEntity> chems = innerStdEntity
						.getChems();
				if (chems != null && chems.size() > 0) {
					chems = CraftTool.fiterCommon(chems, new String[] {
							"desginKey", "cic", "speclFl", "indexSeq",
							"mscPline", "processCode" });
					for (CraftOrdDesignStdCicEntity chem : chems) {
						chemDao.addChem(con, chem);
					}
				}
				
//				if (design.getAllowNk().equals("1")) {
					
				    // 理化保存放在验证中，水压标准要用到。
//					List<CraftOrdDesignStdPicEntity> phys = innerStdEntity
//							.getPhys();
//					if (phys != null && phys.size() > 0) {
//						phys = CraftTool.fiterCommon(phys, new String[] {
//								"desginKey", "pic", "speclFl", "indexSeq",
//								"mscPline", "processCode" });
//						for (CraftOrdDesignStdPicEntity phy : phys) {
//							mscPhyDao.savePhy(con, phy);
//						}
//					}
				List<CraftOrdDesignStdPicSmpEntity> smps = innerStdEntity
						.getSmps();
				if (smps != null && smps.size() > 0) {
					for (CraftOrdDesignStdPicSmpEntity smp : smps) {
						if (!InnerStdDao.checkInnerPicSmp(con, smp)) {
							mscPhyDao.savePicSample(con, smp);
						}
					}
				}
				List<CraftOrdDesignStdSicEntity> specs = innerStdEntity
						.getSpecs();
				if (specs != null && specs.size() > 0) {
					specs = CraftTool.fiterCommon(specs, new String[] {
							"desginKey", "sic", "speclFl", "indexSeq",
							"mscPline", "processCode" });
					for (CraftOrdDesignStdSicEntity spec : specs) {
						mscSpecDao.saveSpec(con, spec);
					}
				}
				List<CraftOrdDesignStdDicEntity> detes = innerStdEntity
						.getDetes();
				if (detes != null && detes.size() > 0) {
					detes = CraftTool.fiterCommon(detes, new String[] {
							"desginKey", "dic", "speclFl", "xh",
							"mscPline", "processCode" });
					for (CraftOrdDesignStdDicEntity dete : detes) {
						mscDeteDao.saveDete(con, dete);
					}
				}
				List<CraftOrdDesignStdWicEntity> waters = innerStdEntity
						.getWaters();
				if (waters != null && waters.size() > 0) {
					waters = CraftTool.fiterCommon(waters, new String[] {
							"desginKey", "wic", "speclFl", "indexSeq",
							"mscPline", "processCode" });
					for (CraftOrdDesignStdWicEntity water : waters) {
						mscWaterDao.saveWater(con, water);
					}
				}
				List<CraftOrdDesignStdGicEntity> processes = innerStdEntity
						.getProesses();
				if (processes != null && processes.size() > 0) {
					processes = CraftTool.fiterCommon(processes,
							new String[] { "desginKey", "gic", "speclFl",
									"xh", "mscPline", "processCode" });
					for (CraftOrdDesignStdGicEntity process : processes) {
						mscGProcessDao.saveProcess(con, process);
					}
				}
				//修改标准索引号
				List<ProcessEntity> processList = innerStdEntity
						.getProcessList();
				if (processList != null && processList.size() > 0) {
					for (ProcessEntity processEntity : processList) {
						InnerStdDao.updateInnerSeq(con, processEntity,
								designKey, mscPlEntity.getMscPline());
					}
				}
				//}
					
				/* 10.6 规格、产线、内控标准 */
				
				// 规格(轧管规格、锻造规格、铸造规格)
				List<CraftOrdDesignMscGzGgEntity> rollSpecs = specEntity
						.getRollSpecs();
				if (rollSpecs != null && rollSpecs.size() > 0) {
					for (CraftOrdDesignMscGzGgEntity rollSpec : rollSpecs) {
						proDucSaveDao.saveRollSpec(con, rollSpec);
					}
				}
				List<CraftOrdDesignMscBiltEntity> billSpecs = specEntity
						.getBillSpecs();
				if (billSpecs != null && billSpecs.size() > 0) {
					for (CraftOrdDesignMscBiltEntity billSpec : billSpecs) {
						proDucSaveDao.savebiltSpec(con, billSpec);
					}
				}
				List<CraftOrdDesignMscBiltCEntity> billSpecCs = specEntity
						.getBillSpecCs();
				if (billSpecCs != null && billSpecCs.size() > 0) {
					for (CraftOrdDesignMscBiltCEntity billSpecC : billSpecCs) {
						proDucSaveDao.saveBiltcSpec(con, billSpecC);
					}
				}
				// 适用产线
				List<CraftOrdDesignMscPlGxPrlEntity> lines = lineEntity
						.getLines();
				if (lines != null && lines.size() > 0) {
					for (CraftOrdDesignMscPlGxPrlEntity line : lines) {
						proDucSaveDao.saveProLine(con, line);
					}
				}
				List<CraftOrdDesignMscGzGgPlEntity> rollLines = lineEntity
						.getRollLines();
				if (rollLines != null && rollLines.size() > 0) {
					for (CraftOrdDesignMscGzGgPlEntity rollLine : rollLines) {
						proDucSaveDao.saveRollLine(con, rollLine);
					}
				}
				List<CraftOrdDesignMscBiltPlEntity> biltLines = lineEntity
						.getBiltLines();
				if (biltLines != null && biltLines.size() > 0) {
					for (CraftOrdDesignMscBiltPlEntity biltLine : biltLines) {
						proDucSaveDao.saveBiltLine(con, biltLine);
					}
				}
				List<CraftOrdDesignMscBiltCPlEntity> biltCLines = lineEntity
						.getBiltCLines();
				if (biltCLines != null && biltCLines.size() > 0) {
					for (CraftOrdDesignMscBiltCPlEntity biltCLine : biltCLines) {
						proDucSaveDao.saveBlitcLine(con, biltCLine);
					}
				}

				/* 10.7验证压缩比 */
				if (!mscPlEntity.getWholeBacklog().contains("C")) {
					if (!CraftTool.compressioRatio(rollSpecs, billSpecCs)) {
						mscPlEntity.setWarnDesc("压缩比为≤3，但制程不经过锻造");
					}
				}
				
				// 保存钢种
				for (CraftOrdDesignMscPlGzEntity gradeEntity : grades) {
					gradeEntity.setDesginKey(designKey);
					gradeDao.saveGrade(con, gradeEntity);
					if (biasGradeCode.equals("")) {
						biasGradeCode = gradeEntity.getGradecode();
						biasGradeName = gradeEntity.getGradename();
					}
				}
				
				if (!mscPlEntity.getSetFl().equals("1")) {
					/* 10.8保存制程 */
					mscPlEntity.setDesginKey(designKey);
					if (!proDucDao.checkProDucIsExist(con, designKey,
							mscPlEntity.getMscPline())) {
						proDucDao.saveProDuc(con, mscPlEntity);
					} else {
						proDucDao.updateProDuc(con, mscPlEntity);
					}
				}
				// 工艺参数
				if (cftEntitys != null && cftEntitys.size() > 0) {
					for (CraftOrdDesignMscCftEntity cftEntity : cftEntitys) {
						cftEntity.setDesginKey(designKey);
						proDucDao.saveCft(con, cftEntity);
					}
				}
			}
			// 如果制程验证通过
			if (proDucFlag) {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", "钢种通过验证", "", designKey,
						"钢种");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", "镦拔扩通过验证", "", designKey,
						"镦拔扩");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", "适用产线通过验证", "",
						designKey, "适用产线");
				if (design.getAllowNk().equals("1")) {
					if (innerFlag) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "S", "11", "内控标准通过验证", "",
								designKey, "内控标准");
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "S", "91", innerMessage, "",
								designKey, "内控标准");
					}

				} else {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "S", "00", "内控标准无要求", "",
							designKey, "内控标准");
				}

			} else {
				if (setMscPlEntitys == null || setMscPlEntitys.size() == 0) {
					String mscPline = "";
					for (CraftOrdDesignMscPlEntity proDuc : mscPlEntitys) {
						mscPline += proDuc.getMscPline() + ",";
					}
					statusDaoImp
							.updateCraftOrdDesignStsTklgLst(
									con,
									design.getMscOr(),
									"F",
									"91",
									"制程"
											+ mscPline.substring(0,
													mscPline.length() - 1)
											+ "交付标准与制程完整性通过验证,但(镦拔扩、规格、内控、适用产线)某项未通过验证[未通过原因:"
											+ errMessage + "],请设定制程!", "1",
									designKey, "制程");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");

					return core;

				} else {
					/* 有设定制程已处理 */
				}
			}
			
			
			
		    //验证成分取样频率
			String chemRateMsg = MscChem.checkChemRate(designKey, con);
			if(!chemRateMsg.equals("")){
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMsc0(), "F", "91",
						chemRateMsg, "", designKey, "成分标准");
				statusDaoImp.updateDesignStsTklg(con, designKey, "",
						"F", "W");

				return core;
			}
			
			// 成分偏差标准验证(先不卡住,只提示原因)
			DeviationChem deviationChem = new DeviationChem();
			if (!biasCic.equals("") || !biasCicN.equals("")) {
				// 验证msc0的成分偏差标准
				if (!biasCic.equals("")) {
					boolean cicFlag = deviationChem.checkDeviationChem(con,
							designKey, biasCic, biasGradeCode, biasGradeName,
							design.getSteelCode(), design.getSteelName(), "G",
							chemBiasList);
					if (cicFlag) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "S", "11",
								deviationChem.errMessage, biasCic, designKey,
								"成分偏差标准");
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "F", "91",
								deviationChem.errMessage, biasCic, designKey,
								"成分偏差标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");
						return core;
					}
				}
				// 验证mscn的成分偏差标准
				if (!biasCicN.equals("")) {
					boolean cicFlag = deviationChem.checkDeviationChem(con,
							designKey, biasCicN, biasGradeCode, biasGradeName,
							design.getSteelCode(), design.getSteelName(), "N",
							chemNBiasList);
					if (cicFlag) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "S", "11",
								deviationChem.errMessage, biasCicN, designKey,
								"成分偏差标准");
					} else {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "F", "91",
								deviationChem.errMessage, biasCicN, designKey,
								"成分偏差标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");
						return core;
					}
				}
			} else {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "00", "成分偏差标准无要求", "",
						designKey, "成分偏差标准");
			}

			// 在线机探盲区切长
			ManInspectionDao ManInspectionDao = new ManInspectionDao();
			boolean inspectFlag = ManInspectionDao.checkManInspection(con,
					designKey);
			if (design.getManInspection().equals("0") && inspectFlag) {
				if (design.getInspectionValue().equals("")) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", "请确认在线机探盲区切长", "",
							designKey, "在线机探盲区切长");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");

					return core;
				} else {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "S", "11", "在线机探盲区切长通过验证", "",
							designKey, "在线机探盲区切长");
				}
			} else {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "00", "在线机探盲区切长无要求", "",
						designKey, "在线机探盲区切长");
			}

			// 投入/产出物料
			/**
			 * 十五、物料码
			 */
			try {
				BOMGeneration.generationBomMain(con, designKey, "2", "1");
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "S", "11", "投入/产出物料通过验证", "",
						designKey, "投入/产出物料");
			} catch (Exception e) {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "F", "91", "投入/产出物料物料编码生成异常,请确认",
						"", designKey, "投入/产出物料");
				statusDaoImp.updateDesignStsTklg(con, designKey, "", "F", "W");
				return core;
			}

			// ---------------------试样码------------0-MSC0 1-MSCN 2-内控
			MscSplineDao mscSplineDao = new MscSplineDao();
			
			//内控试样码
			// 9.1查询设定制程
			List<CraftOrdDesignMscPlEntity> choiceMscPls = mscSplineDao.getCraftMscPl(design.getDesignKey(), con);
			for (CraftOrdDesignMscPlEntity mscPlEntity : choiceMscPls) {
				/* 10.1 钢种 */
				Object[] gradeInfo = grade.checkGrade(con,
						mscPlEntity.getMscPline(),
						mscPlEntity.getWholeBacklog(), design.getMscOr(),
						design.getDimater(), design.getHeight(),
						design.getSpecStyle(), design.getSpecName(),
						design.getOutsBilietFl(),design.getMscOrStyle(),
						designKey);
				// 钢种集合
				@SuppressWarnings("unchecked")
				List<CraftOrdDesignMscPlGzEntity> grades = (List<CraftOrdDesignMscPlGzEntity>) gradeInfo[2];
				/* 10.2 规格 */
				SpecEntity specEntity = specManage.checkSpec(con, mscPlEntity,
						grades, design, supp);
				
				String[] splineResults = MscSplineDao.CheckSpline(con, design, "2", mscPlEntity, specEntity);
				if (splineResults[0].equals("")) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMsc0(), "S", "11", "试样码验证通过", "", designKey,
							"试样码");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "S", "S");
				} else {
					if(design.getAllowNk().equals("1")){
						 statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
									design.getMsc0(), "F", "91", splineResults[0],
									splineResults[4], designKey, "试样码");
						 statusDaoImp.updateDesignStsTklg(con, designKey, "", "F","W");
						 return core;
					 }else{
						 statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
									design.getMsc0(), "F", "91", splineResults[0],
									splineResults[4], designKey, "试样码");
					 }
				}
			}

			//*******************首日检***********************
			try{
				String[] craftOrdFdResults = CraftOrdFd.checkCraftOrdFd(con, designKey, ordLnPk);
				if(craftOrdFdResults[0].equals("")){
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "S", "00", "首日检无要求！", "",
							designKey, "首日检");
				}else if(craftOrdFdResults[0].equals("True")){
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "S", "11", "首日检验证通过！", "",
							designKey, "首日检");
				}else{
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", craftOrdFdResults[0],
							craftOrdFdResults[1], designKey, "首日检");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F", "W");
					return core;
				}
			}catch(Exception ex){
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "F", "91", "首日检评审异常,请确认!",
						"Pic", designKey, "首日检");
				statusDaoImp.updateDesignStsTklg(con, designKey, "", "F", "W");
				return core;
			}

			//计算拉伸断后伸长率公式
			MscPhy.calculateLaShen(con, designKey, design.getHeight());
			
			//验证是否为技术评审通过制程
			Object[] mscProInTec = proDucimp.checkProInTec(con, design,
					errMessages);
			boolean flagProInTec = (Boolean) mscProInTec[0];
			String messageProInTec = (String) mscProInTec[1];
			if (flagProInTec == false) {
				statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
						design.getMscOr(), "F", "90", messageProInTec, "",
						designKey, "制程");
				statusDaoImp.updateDesignStsTklg(con, designKey, "", "F", "W");
				return core;
			}
			
			// 最终执行成功
			statusDaoImp.updateDesignStsTklg(con, designKey, "", "S", "S");
            
			// 删除产线
			//DeleteLine deleteLine = new DeleteLine();
			//deleteLine.deleteLine(con, ordLnPk, designKey);

			// 如果是备料码，工艺制定成功后，把工艺主表置为生效状态。CraftOrdDesignDAL.setCraftOrDesignValid
//			if (flag.equals("1")) {
//				CraftOrdDesignDAL craftOrdDesignDAL = new CraftOrdDesignDAL();
//				craftOrdDesignDAL.setCraftOrDesignValid(ordLnPk, con);
//			}
			
			core.setResult(true);

		} catch (Exception e) {
			e.printStackTrace();
			core.SetErrInfo(e.getMessage());
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return core;
	}
}
