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

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

import com.steering.pss.sale.order.bomGeneration.dao.CraftDao;
import com.steering.pss.sale.order.bomGeneration.dao.IBomQueryDao;
import com.steering.pss.sale.order.bomGeneration.daoImp.BomCheckDaoImp;
import com.steering.pss.sale.order.bomGeneration.daoImp.BomFactoryDao;
import com.steering.pss.sale.order.bomGeneration.daoImp.BomSaveDaoImp;
import com.steering.pss.sale.order.bomGeneration.model.BaseMaterialEntity;
import com.steering.pss.sale.order.bomGeneration.model.BomEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomAEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomBEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomCEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomDEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomEEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomFEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomKEntity;
import com.steering.pss.sale.order.bomGeneration.model.SlmOrdDesignMscPlPrcEntity;
import com.steering.pss.sale.order.bomGeneration.serverImp.BomProducImp;
import com.steering.pss.sale.order.model.DesignMscPlGxPrlEntity;

public class BomProduc implements BomProducImp {

	private static BomCheckDaoImp bomCheckDaoImp = BomFactoryDao
			.getBomCheckDaoImp();
	private static BomSaveDaoImp bomSaveDaoImp = BomFactoryDao
			.getBomSaveDaoImp();

	/**
	 * 形成BOM数据
	 * 
	 * @param con
	 *            连接字符串
	 * @param bom
	 *            BOM主键集合类（即一套BOM物料编码）
	 * @param bomEntity
	 *            合同行
	 * @throws SQLException
	 */
	public String fromAndSaveBom(Connection con, DesignBomEntity bom,
			BomEntity bomEntity, List<DesignMscPlGxPrlEntity> lines,
			String designKey, String flagCraft) throws SQLException {
		// 如果已经形成BOM数据,则不再形成,直接返回这个BOM
		String bomNo = bomCheckDaoImp.checkBomCommon(con, bom);
		if (bomNo.equals("")) {
			bomSaveDaoImp.saveBom(con, bom);
			// 获取生成的BOM
			bomNo = bomCheckDaoImp.checkBomCommon(con, bom);
		}
		// 如果没有，则生成BOM及相关工序的数据
		bom.setBom(bomNo);
		String wholeBacklog = bom.getWholeBacklog();
		// 电炉
		DesignBomAEntity bomA = null;
		if (wholeBacklog.contains("A")) {
			bomA = BomCode.generationBomA("A", "电炉投入方案", bomNo,
					bom.getGradecode(), bom.getGradename());
		}
		CraftDao craftDao = new CraftDao();
		IBomQueryDao ibomQueryDao = new IBomQueryDao();
		// 铸造
		DesignBomBEntity bomB = null;
		if (wholeBacklog.contains("B")) {
			// 铸造
			SlmOrdDesignMscPlPrcEntity process = null;
			if (flagCraft.equals("1")) {
				process = craftDao.getOperation(con, designKey,
						bom.getMscPline(), "B");
			} else {
				process = ibomQueryDao.getOperation(con, designKey,
						bom.getMscPline(), "B");
			}

			String prefix = "";
			if (process.getProcessCodeC().equals("B002")) {
				prefix = "PA";
			} else {
				prefix = "PD";
			}
			bomB = BomCode.generationBomB(bom.getGradecode(),
					bom.getGradename(), prefix, bomNo, bom.getSpecCodeZ(),
					bom.getSpecNameZ(), bom.getGradecode(), bom.getGradename());
		}
		// 锻造
		DesignBomCEntity bomC = null;
		if (wholeBacklog.contains("C")) {
			SlmOrdDesignMscPlPrcEntity process = null;
			if (flagCraft.equals("1")) {
				process = craftDao.getOperation(con, designKey,
						bom.getMscPline(), "C");
			} else {
				process = ibomQueryDao.getOperation(con, designKey,
						bom.getMscPline(), "C");
			}

			// MscProcessEntity process = operationDaoImp.getOperation(con,
			// bom.getMsc(), bom.getMscPline(), "C");
			String prefix = "PB";
			if (process.getProcessCodeC().equals("B026")) {
				prefix = "PC";
			}
			if (wholeBacklog.contains("B")) {
				bomC = BomCode.generationBomC(prefix, bomB.getOutMaterialNo(),
						bomB.getOutMaterialDesc(), bomNo, bom.getGradecode(),
						bom.getGradename(), bom.getSpecCodeD(),
						bom.getSpecNameD());
			} else {
				String prefixB = "PA";
				if (bom.getSpecNameZ().contains("t")) {
					if (bom.getSpecNameZ().contains("t")) {
						prefixB = "PD";
					}
				}
				bomC = BomCode
						.generationBomC(
								"PB",
								prefixB + bom.getSpecCodeZ()
										+ bom.getGradecode(),
								prefixB + bom.getSpecNameZ() + ","
										+ bom.getGradename(), bomNo,
								bom.getGradecode(), bom.getGradename(),
								bom.getSpecCodeD(), bom.getSpecNameD());
			}

		}
		// 轧管
		// 前缀
		String prefix = BomMaterial.generationPrefix(con,
				bomEntity.getProduccode());
		String prefixD = prefix + "X";
		DesignBomDEntity bomD = null;
		if (wholeBacklog.contains("D")) {
			// 如果制程不包含热处理，镦拔扩，且轧管的产出物料为前缀+规格+钢级；如果并且不包含加工，且轧管产出物料为成品物料
			String outCodeD = bom.getGradecode();
			String outNameD = bom.getGradename();
			String flag = "0";
			if ((!wholeBacklog.contains("E")) && (!wholeBacklog.contains("F"))) {
				outCodeD = bomEntity.getSteelcode();
				outNameD = bomEntity.getSteelname();
				if (!wholeBacklog.contains("G")) {
					outCodeD = bom.getMaterialNo();
					outNameD = bom.getMaterialDesc();
					flag = "1";
				}
			} else {
				// 如果制程包含热处理，但不包含镦拔扩,且二级工序是仅探伤，且轧管的产出物料为前缀+规格+钢级
				if (wholeBacklog.contains("F") && !wholeBacklog.contains("E")) {
					// 热处理二级工序
					SlmOrdDesignMscPlPrcEntity process = null;
					if (flagCraft.equals("1")) {
						process = craftDao.getOperation(con, designKey,
								bom.getMscPline(), "F");
					} else {
						process = ibomQueryDao.getOperation(con, designKey,
								bom.getMscPline(), "F");
					}
//					if (process.getProcessCodeC().equals("B024")) {
//						if (wholeBacklog.contains("G")) {
//							outCodeD = bomEntity.getSteelcode();
//							outNameD = bomEntity.getSteelname();
//						} else {
//							flag = "1";
//							outCodeD = bom.getMaterialNo();
//							outNameD = bom.getMaterialDesc();
//						}
//
//					}
				}
			}
			// 如果包含锻造,轧管的投入物料为锻造的产出物料
			if (wholeBacklog.contains("C")) {
				bomD = BomCode.generationBomD(bomC.getOutMaterialNo(),
						bomC.getOutMaterialDesc(), prefixD, bom.getSpecCodeR(),
						bom.getSpecNameR(), bomNo, outCodeD, outNameD, flag);
			} else {
				// 如果不包含锻造，包含铸造，且投入物料为铸造的产出物料
				if (wholeBacklog.contains("B")) {
					bomD = BomCode.generationBomD(bomB.getOutMaterialNo(),
							bomB.getOutMaterialDesc(), prefixD,
							bom.getSpecCodeR(), bom.getSpecNameR(), bomNo,
							outCodeD, outNameD, flag);
				} else {
					// 如果不含锻造/铸造,则自动生成
					String prefixB = "PA";
					if (bom.getSpecNameZ().contains("t")) {
						prefixB = "PD";
					}
					bomD = BomCode.generationBomD(prefixB + bom.getSpecCodeZ()
							+ bom.getGradecode(), prefixB + bom.getSpecNameZ()
							+ "," + bom.getGradename(), prefixD,
							bom.getSpecCodeR(), bom.getSpecNameR(), bomNo,
							outCodeD, outNameD, flag);
				}
			}
		}

		// 墩拔扩
		DesignBomEEntity bomE = null;

		if (wholeBacklog.contains("E")) {
			SlmOrdDesignMscPlPrcEntity processE = null;
			if (flagCraft.equals("1")) {
				processE = craftDao.getOperation(con, designKey,
						bom.getMscPline(), "E");
			} else {
				processE = ibomQueryDao.getOperation(con, designKey,
						bom.getMscPline(), "E");
			}
			// MscProcessEntity processE = operationDaoImp.getOperation(con,
			// bom.getMsc(), bom.getMscPline(), "E");
			// 如果墩拔扩二级工序经过墩粗
			if (processE.getProcessCodeC().equals("B008")) {
				// 一级工序是鈍拔扩且二级工序是“钝粗”
				String eMaterialCode = BomProduc.getModelMaterialCode(con,
						bomEntity.getModelCode());
				if (!eMaterialCode.equals("")) {
					prefixD = prefix + eMaterialCode;
				} else {
					prefixD = prefix + "D";
				}

			} else {
				prefixD = prefix + "X";
			}
			// 如果制程不包含热处理，且轧管的产出物料为前缀+规格+钢级；如果并且不包含加工，且轧管产出物料为成品物料
			String outCodeE = bom.getGradecode();
			String outNameE = bom.getGradename();
			String flag = "0";
			if (!wholeBacklog.contains("F")) {
				outCodeE = bomEntity.getSteelcode();
				outNameE = bomEntity.getSteelname();
				if (!wholeBacklog.contains("G")) {
					outCodeE = bom.getMaterialNo();
					outNameE = bom.getMaterialDesc();
					flag = "1";
				}
			} else {
				// 热处理二级工序，制程包含热处理，且二级工序是仅探伤,且镦拔扩的产出物料为前缀+规格+钢级,如果不包含加工,则出成品
				SlmOrdDesignMscPlPrcEntity process = null;
				if (flagCraft.equals("1")) {
					process = craftDao.getOperation(con, designKey,
							bom.getMscPline(), "F");
				} else {
					process = ibomQueryDao.getOperation(con, designKey,
							bom.getMscPline(), "F");
				}
//				if (process.getProcessCodeC().equals("B024")) {
//					if (wholeBacklog.contains("G")) {
//						outCodeE = bomEntity.getSteelcode();
//						outNameE = bomEntity.getSteelname();
//					} else {
//						outCodeE = bom.getMaterialNo();
//						outNameE = bom.getMaterialDesc();
//						flag = "1";
//					}
//
//				}
			}
			if (wholeBacklog.contains("D")) {
				bomE = BomCode.generationBomE(prefixD, bomD.getOutMaterialNo(),
						bomD.getOutMaterialDesc(), bomNo, bom.getSpecCodeC(),
						bom.getSpecNameC(), outCodeE, outNameE, flag);
			} else {
				bomE = BomCode.generationBomE(
						prefixD,
						prefixD + bomEntity.getSpecCode() + bom.getGradecode(),
						prefixD + bomEntity.getSpecName() + ","
								+ bom.getGradename(), bomNo,
						bom.getSpecCodeC(), bom.getSpecNameC(), outCodeE,
						outNameE, flag);
			}
		}

		// 热处理
		DesignBomFEntity bomF = null;
		if (wholeBacklog.contains("F")) {
			// 产出物料为前缀+规格+钢级；如果并且不包含加工，且轧管产出物料为成品物料
			String outCodeF = bomEntity.getSteelcode();
			String outNameF = bomEntity.getSteelname();
			String flag = "0";
			if (!wholeBacklog.contains("G")) {
				outCodeF = bom.getMaterialNo();
				outNameF = bom.getMaterialDesc();
				flag = "1";
			}
			// 一级工序包含热处理的，生成产出物料码的时候，物料码的第二位字母根据扣型表中取一下字母
			String eMaterialCode = BomProduc.getModelMaterialCode(con,
					bomEntity.getModelCode());
			if (!eMaterialCode.equals("")) {
				prefixD = prefix + eMaterialCode;
			}
			if (wholeBacklog.contains("E")) {
				bomF = BomCode.generationBomF(prefixD, bomE.getOutMaterialNo(),
						bomE.getOutMaterialDesc(), bomNo,
						bomEntity.getStdStyle(), bomEntity.getStdStyleDesc(),
						bomEntity.getSpecCode(), bomEntity.getSpecName(),
						outCodeF, outNameF, flag);
			} else if (wholeBacklog.contains("D")) {
				bomF = BomCode.generationBomF(prefixD, bomD.getOutMaterialNo(),
						bomD.getOutMaterialDesc(), bomNo,
						bomEntity.getStdStyle(), bomEntity.getStdStyleDesc(),
						bomEntity.getSpecCode(), bomEntity.getSpecName(),
						outCodeF, outNameF, flag);
			} else {
				bomF = BomCode.generationBomF(
						prefixD,
						prefixD + bomEntity.getSpecCode() + bom.getGradecode(),
						prefixD + bomEntity.getSpecName() + ","
								+ bom.getGradename(), bomNo,
						bomEntity.getStdStyle(), bomEntity.getStdStyleDesc(),
						bomEntity.getSpecCode(), bomEntity.getSpecName(),
						outCodeF, outNameF, flag);
			}
		}
		// (这里的加工是形成管体的加工接入物料，针对双工短节，与短节附件这种产品是没有管体，
		// 所以这里不形成加工的物料，在外面的接箍码与备料码里形成投入物料)
		// 加工
		DesignBomKEntity bomK = null;
		if (!bomEntity.getProducType().equals("C")
				&& !bomEntity.getProducType().equals("E")) {
			if (wholeBacklog.contains("G")) {
				bomK = new DesignBomKEntity();
				// 备料组号(不是备料投入默认给0)
				bomK.setMark("1");
				bomK.setGroupNum("0");
				bomK.setBom(bomNo);
				bomK.setOutMaterialNo(bom.getMaterialNo());
				bomK.setOutMaterialDesc(bom.getMaterialDesc());
				if (wholeBacklog.contains("F")) {
					bomK.setInMaterialNo(bomF.getOutMaterialNo());
					bomK.setInMaterialDesc(bomF.getOutMaterialDesc());
				} else {
					if (wholeBacklog.contains("E")) {
						bomK.setInMaterialNo(bomE.getOutMaterialNo());
						bomK.setInMaterialDesc(bomE.getOutMaterialDesc());
					} else {
						if (wholeBacklog.contains("D")) {
							bomK.setInMaterialNo(bomD.getOutMaterialNo());
							bomK.setInMaterialDesc(bomD.getOutMaterialDesc());
						} else {
							bomK.setInMaterialNo(prefixD
									+ bomEntity.getSpecCode()
									+ bom.getGradecode());
							bomK.setInMaterialDesc(prefixD
									+ bomEntity.getSpecName() + ","
									+ bom.getGradename());
						}
					}
				}
			}
		}

		// // 加工
		// DesignBomIEntity bomI = null;
		// if (wholeBacklog.contains("G")) {
		// if (wholeBacklog.contains("F")) {
		// bomI = BomCode.generationBomI(bomNo, bomF.getOutMaterialNo(),
		// bomF.getOutMaterialDesc(), bom.getMaterialNo(),
		// bom.getMaterialDesc());
		// } else {
		// bomI = BomCode.generationBomI(
		// bomNo,
		// prefixD + bomEntity.getSpecCode()
		// + bomEntity.getSteelcode(),
		// prefixD + bomEntity.getSpecName() + ","
		// + bomEntity.getSteelname(),
		// bom.getMaterialNo(), bom.getMaterialDesc());
		// }
		// }

		// 回写BOM类（电炉）
		if (wholeBacklog.contains("A")) {
			bom.setAInMaterialNo(bomA.getInMaterialNo());
			bom.setAInMaterialDesc(bomA.getInMaterialDesc());
			bom.setAOutMaterialNo(bomA.getOutMaterialNo());
			bom.setAOutMaterialDesc(bomA.getOutMaterialDesc());
		}

		// 铸造
		if (wholeBacklog.contains("B")) {
			bom.setBInMaterialNo(bomB.getInMaterialNo());
			bom.setBInMaterialDesc(bomB.getInMaterialDesc());
			bom.setBOutMaterialNo(bomB.getOutMaterialNo());
			bom.setBOutMaterialDesc(bomB.getOutMaterialDesc());
		}

		// 锻造
		if (wholeBacklog.contains("C")) {
			bom.setCInMaterialNo(bomC.getInMaterialNo());
			bom.setCInMaterialDesc(bomC.getInMaterialDesc());
			bom.setCOutMaterialNo(bomC.getOutMaterialNo());
			bom.setCOutMaterialDesc(bomC.getOutMaterialDesc());
		}
		// 轧管
		if (wholeBacklog.contains("D")) {
			bom.setDInMaterialNo(bomD.getInMaterialNo());
			bom.setDInMaterialDesc(bomD.getInMaterialDesc());
			bom.setDOutMaterialNo(bomD.getOutMaterialNo());
			bom.setDOutMaterialDesc(bomD.getOutMaterialDesc());
		}

		// 墩拔扩
		if (wholeBacklog.contains("E")) {
			bom.setEInMaterialNo(bomE.getInMaterialNo());
			bom.setEInMaterialDesc(bomE.getInMaterialDesc());
			bom.setEOutMaterialNo(bomE.getOutMaterialNo());
			bom.setEOutMaterialDesc(bomE.getOutMaterialDesc());
		}
		// 热处理
		if (wholeBacklog.contains("F")) {
			bom.setFInMaterialNo(bomF.getInMaterialNo());
			bom.setFInMaterialDesc(bomF.getInMaterialDesc());
			bom.setFOutMaterialNo(bomF.getOutMaterialNo());
			bom.setFOutMaterialDesc(bomF.getOutMaterialDesc());
		}
		// 加工
		if (wholeBacklog.contains("G")
				&& !bomEntity.getProducType().equals("C")
				&& !bomEntity.getProducType().equals("E")) {
			bom.setIInMaterialNo(bomK.getInMaterialNo());
			bom.setIInMaterialDesc(bomK.getInMaterialDesc());
			bom.setIOutMaterialNo(bomK.getOutMaterialNo());
			bom.setIOutMaterialDesc(bomK.getOutMaterialDesc());
		}
		/**
		 * 保存数据
		 */
		bomSaveDaoImp.updateBom(con, bom);
		// 保存基础物料编码
		if (bomCheckDaoImp.checkBaseMaterial(con, bom.getMaterialNo()) == 0) {
			BaseMaterialEntity baseMaterial = BomMaterial.generBaseMater(con,
					bomEntity, bom.getMaterialNo(), bom.getMaterialDesc());
			bomSaveDaoImp.saveBaseMaterial(con, baseMaterial);
		}
		// 电炉
		if (bomA != null) {
			if (bomCheckDaoImp.checkBomA(con, bomA.getBom(),
					bomA.getInMaterialNo(), bomA.getOutMaterialNo())) {
				bomSaveDaoImp.saveBomA(con, bomA);
			}
			// 基础物料-电炉
			if (bomCheckDaoImp.checkBaseMaterialA(con, bomA.getInMaterialNo(),
					bomA.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialA(con, bomA);
			}
			// 基础数据-电炉投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"A");
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkBaseMaterialAPl(con,
						bomA.getInMaterialNo(), bomA.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp.saveBaseMaterialAPl(con,
							bomA.getInMaterialNo(), bomA.getOutMaterialNo(),
							pline.getPlineCode(), pline.getPlineName());
				}
			}
		}

		if (bomB != null) {
			// 铸造
			if (bomCheckDaoImp.checkBomB(con, bomB.getBom(),
					bomB.getInMaterialNo(), bomB.getOutMaterialNo())) {
				bomSaveDaoImp.saveBomB(con, bomB);
			}
			// 基础物料-铸造
			if (bomCheckDaoImp.checkBaseMaterialB(con, bomB.getInMaterialNo(),
					bomB.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialB(con, bomB);
			}
			// 基础数据-铸造投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"B");
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkBaseMaterialBPl(con,
						bomB.getInMaterialNo(), bomB.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp.saveBaseMaterialBPl(con,
							bomB.getInMaterialNo(), bomB.getOutMaterialNo(),
							pline.getPlineCode(), pline.getPlineName());
				}
			}
		}

		// 锻造
		if (bomC != null) {
			if (bomCheckDaoImp.checkBomC(con, bomC.getBom(),
					bomC.getInMaterialNo(), bomC.getOutMaterialNo())) {
				bomSaveDaoImp.saveBomC(con, bomC);
			}
			// 基础物料-锻造
			if (bomCheckDaoImp.checkBaseMaterialC(con, bomC.getInMaterialNo(),
					bomC.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialC(con, bomC);
			}
			// 基础数据-铸造投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"C");
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkBaseMaterialCPl(con,
						bomC.getInMaterialNo(), bomC.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp.saveBaseMaterialCPl(con,
							bomC.getInMaterialNo(), bomC.getOutMaterialNo(),
							pline.getPlineCode(), pline.getPlineName());
				}
			}
		}

		// 轧管
		if (bomD != null) {
			if (bomCheckDaoImp.checkBomD(con, bomD.getBom(),
					bomD.getInMaterialNo(), bomD.getOutMaterialNo())) {
				bomSaveDaoImp.saveBomD(con, bomD);
			}
			// 基础物料-轧管
			if (bomCheckDaoImp.checkBaseMaterialD(con, bomD.getInMaterialNo(),
					bomD.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialD(con, bomD);
			}
			// 基础数据-轧管投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"D");
			String specCode = "";
			if (!bom.getSpecCodeD().equals("")) {
				specCode = bom.getSpecCodeD();
			} else {
				specCode = bom.getSpecCodeZ();
			}
			String[] dimater = bomCheckDaoImp
					.getSpecDH(con, bom.getSpecCodeR());
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkPlineSpec(con, pline.getPlineCode(),
						specCode, dimater[0], dimater[1]) == 0) {
					continue;
				}
				if (bomCheckDaoImp.checkBaseMaterialDPl(con,
						bomD.getInMaterialNo(), bomD.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp.saveBaseMaterialDPl(con,
							bomD.getInMaterialNo(), bomD.getOutMaterialNo(),
							pline.getPlineCode(), pline.getPlineName());
				}
			}
		}

		// 墩拔扩
		if (bomE != null) {
			if (bomCheckDaoImp.checkBomE(con, bomE.getBom(),
					bomE.getInMaterialNo(), bomE.getOutMaterialNo())) {
				bomSaveDaoImp.saveBomE(con, bomE);
			}
			// 基础物料-墩拔扩
			if (bomCheckDaoImp.checkBaseMaterialE(con, bomE.getInMaterialNo(),
					bomE.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialE(con, bomE);
			}
			// 基础数据- 墩拔扩投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"E");
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkBaseMaterialEPl(con,
						bomE.getInMaterialNo(), bomE.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp.saveBaseMaterialEPl(con,
							bomE.getInMaterialNo(), bomE.getOutMaterialNo(),
							pline.getPlineCode(), pline.getPlineName());
				}
			}
		}

		// 热处理
		if (bomF != null) {
			if (bomCheckDaoImp.checkBomF(con, bomF.getBom(),
					bomF.getInMaterialNo(), bomF.getOutMaterialNo())) {
				bomSaveDaoImp.saveBomF(con, bomF);
			}
			// 基础物料-热处理
			if (bomCheckDaoImp.checkBaseMaterialF(con, bomF.getInMaterialNo(),
					bomF.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialF(con, bomF);
			}
			// 基础数据- 墩拔扩投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"F");
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkBaseMaterialFPl(con,
						bomF.getInMaterialNo(), bomF.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp.saveBaseMaterialFPl(con,
							bomF.getInMaterialNo(), bomF.getOutMaterialNo(),
							pline.getPlineCode(), pline.getPlineName());
				}
			}
		}
		// 加工
		if (bomK != null) {
			if (bomCheckDaoImp.checkBomK(con, bomK.getBom(),
					bomK.getInMaterialNo(), bomK.getOutMaterialNo(),
					bomK.getGroupNum())) {
				bomSaveDaoImp.saveBomK(con, bomK);
			}
			// 基础物料-加工
			if (bomCheckDaoImp.checkBaseMaterialK(con, bomK.getInMaterialNo(),
					bomK.getOutMaterialNo()) == 0) {
				bomSaveDaoImp.saveBaseMaterialK(con, bomK);
			}
			// 基础数据- 墩拔扩投入产出-产线能力
			List<DesignMscPlGxPrlEntity> fitPlines = BomProduc.fiterLine(lines,
					"G");
			for (DesignMscPlGxPrlEntity pline : fitPlines) {
				if (bomCheckDaoImp.checkBaseMaterialKPl(con,
						bomK.getInMaterialNo(), bomK.getOutMaterialNo(),
						pline.getPlineCode()) == 0) {
					bomSaveDaoImp
							.saveBaseMaterialKPl(con, bomK.getInMaterialNo(),
									bomK.getOutMaterialNo(),
									pline.getPlineCode(), pline.getPlineName(),
									bomK.getInMaterialDesc(),
									bomK.getOutMaterialDesc());
				}
			}
		}
		// 加工
		// if (bomI != null) {
		// if (bomCheckDaoImp.checkBomI(con, bomI.getBom(),
		// bomI.getInMaterialNo(), bomI.getOutMaterialNo())) {
		// bomSaveDaoImp.saveBomI(con, bomI);
		// }
		//
		// }
		return bomNo;
	}

	/**
	 * 根据工序过滤产线
	 * 
	 * @param lines
	 *            适用产线
	 * @param processCode
	 *            工序
	 * @return List<DesignMscPlGxPrlEntity>
	 */
	public static List<DesignMscPlGxPrlEntity> fiterLine(
			List<DesignMscPlGxPrlEntity> lines, String processCode) {
		if (lines == null || lines.size() == 0) {
			return null;
		}
		List<DesignMscPlGxPrlEntity> fitLines = new ArrayList<DesignMscPlGxPrlEntity>();
		for (DesignMscPlGxPrlEntity line : lines) {
			if (line.getProcessCode().equals(processCode)) {
				fitLines.add(line);
			}
		}
		return fitLines;
	}

	/**
	 * 查询扣型的镦拔扩字母
	 * 
	 * @param con
	 * @param modelCode
	 * @return
	 * @throws SQLException
	 */
	public static String getModelMaterialCode(Connection con, String modelCode)
			throws SQLException {
		String sql = " select t.E_MATERIAL_CODE from COM_BASE_MODEL t where t.MODEL_CODE=? ";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		ResultSet rs = ps.executeQuery();
		String eMaterialCode = "";
		if (rs.next()) {
			if (rs.getString("E_MATERIAL_CODE") != null)
				eMaterialCode = rs.getString("E_MATERIAL_CODE");
		}
		rs.close();
		ps.close();
		return eMaterialCode;
	}
}
