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

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

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.BomQueryDaoImp;
import com.steering.pss.sale.order.bomGeneration.daoImp.BomFactoryDao;
import com.steering.pss.sale.order.bomGeneration.model.BomEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomEntity;
import com.steering.pss.sale.order.bomGeneration.model.SlmOrdDesignMscPlPrcEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGzEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;

/**
 * 形成产品物料编码 集合
 * 
 * @author Administrator
 * 
 */
public class BomFromKey {
	private static BomQueryDaoImp bomQueryDaoImp = BomFactoryDao
			.getBomQueryDaoImp();
	private static IBomQueryDao ibomQueryDao = new IBomQueryDao();

	/**
	 * 产品物料编码
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @return List<DesignBomEntity>
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static List<DesignBomEntity> generationKey(Connection con,
			String designKey, BomEntity bomEntity, String flag)
			throws SQLException, IllegalAccessException,
			InvocationTargetException {
		List<DesignBomEntity> boms = new ArrayList<DesignBomEntity>();
		List<DesignMscPlEntity> mscPls = null;
		CraftDao craftDao = new CraftDao();
		if (flag.equals("1")) {
			mscPls = craftDao.getProDuc(con, designKey);
		} else {
			mscPls = bomQueryDaoImp.getProcess(con, designKey);
		}
		for (DesignMscPlEntity mscPl : mscPls) {
			// 钢种
			List<DesignMscPlGzEntity> grades = null;
			// 轧管规格
			List<DesignMscGzGgEntity> rollSpecs = null;
			// 断面规格
			List<DesignMscBiltEntity> blitSpecs = null;
			// 坯料规格
			List<DesignMscBiltCEntity> biltCSpecs = null;
			if (flag.equals("1")) {
				grades = craftDao.getGrade(con, designKey, mscPl.getMscPline());
				rollSpecs = craftDao.getRollSpec(con, designKey,
						mscPl.getMscPline());
				blitSpecs = craftDao.getBiltSpec(con, designKey,
						mscPl.getMscPline());
				biltCSpecs = craftDao.getBiltCSpec(con, designKey,
						mscPl.getMscPline());
			} else {
				grades = bomQueryDaoImp.getGrade(con, mscPl.getDesginKey(),
						mscPl.getMscPline());
				rollSpecs = bomQueryDaoImp.getSpec(con, mscPl.getDesginKey(),
						mscPl.getMscPline());
				blitSpecs = bomQueryDaoImp.getBlank(con, designKey,
						mscPl.getMscPline());
				biltCSpecs = bomQueryDaoImp.getbilt(con, designKey,
						mscPl.getMscPline());
			}

			// 产品规格代码
			String specCode = bomEntity.getSpecCode();
			// 产品规格描述
			String specName = bomEntity.getSpecName();
			// 全程产线号
			String wholeBacklog = mscPl.getWholeBacklog();
			/**
			 * 1、基础物料编码
			 */
			DesignBomEntity bom = BomFromKey.generationBom(con, designKey,
					bomEntity, mscPl, flag);
			/**
			 * 2、墩拔扩
			 */
			DesignBomEntity bomE = BomFromKey.generationBomE(bom,
					mscPl.getWholeBacklog(), bomEntity.getSpecCode(),
					bomEntity.getSpecName());
			/**
			 * 3、轧管(钢种)
			 */
			List<DesignBomEntity> bomDs = BomFromKey.generationBomD(bomE,
					wholeBacklog, specCode, specName, grades, rollSpecs);
			/**
			 * 4、锻造
			 */
			List<DesignBomEntity> bomCs = BomFromKey.generationBomC(bomDs,
					wholeBacklog, blitSpecs);
			/**
			 * 5、 铸造
			 */
			List<DesignBomEntity> bomBs = BomFromKey.generationBomB(bomCs,
					wholeBacklog, biltCSpecs, bomEntity.getOutsStlPipeFl());
			for (DesignBomEntity bomB : bomBs) {
				boms.add(bomB);
			}
		}
		return boms;
	}

	/**
	 * 形成基础物料编码(只有一套数据)
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @param bomEntity
	 *            合同行
	 * @param mscPl
	 *            制程
	 * @return DesignBomEntity
	 * @throws SQLException
	 */
	public static DesignBomEntity generationBom(Connection con,
			String designKey, BomEntity bomEntity, DesignMscPlEntity mscPl,
			String flag) throws SQLException {
		// 二级工序代码组成字符串
		String[] processCodees = mscPl.getWholeBacklog().split("-");
		String processCodeCs = "";
		for (String processCode : processCodees) {
			SlmOrdDesignMscPlPrcEntity procesInfo = null;
			if (!flag.equals("1")) {
				procesInfo = ibomQueryDao.getOperation(con, designKey,
						mscPl.getMscPline(), processCode);
			} else {
				CraftDao craftDao = new CraftDao();
				procesInfo = craftDao.getOperation(con, designKey,
						mscPl.getMscPline(), processCode);
			}
			processCodeCs += procesInfo.getProcessCodeC();
		}
		// 形成物料编码
		String[] materials = BomMaterial.formEndMaterial(bomEntity, con);
		DesignBomEntity bom = new DesignBomEntity();
		// 物料编码
		bom.setMaterialNo(materials[0]);
		// 物料描述
		bom.setMaterialDesc(materials[1]);
		// 冶金规范码
		bom.setMsc(bomEntity.getMsc());
		// 冶金规范码描述
		bom.setMscDesc(bomEntity.getMscDesc());
		// 全程产线号
		bom.setMscPline(mscPl.getMscPline());
		// 二级工序代码
		bom.setProcessCodeCs(processCodeCs);
		// 全程途径码
		bom.setWholeBacklog(mscPl.getWholeBacklog());
		// 全程途径描述
		bom.setWholeBacklogDesc(mscPl.getWholeBacklogDesc());
		// 冶金规范码类型
		bom.setBomTyp(bomEntity.getMscStyle());
		return bom;
	}

	/**
	 * 墩拔扩(只有一套)
	 * 
	 * @param bom
	 *            BOM
	 * @param wholeBacklog
	 *            全程产线号
	 * @param specCode
	 *            成品规格代码
	 * @param specName
	 *            成品规格描述
	 * @return DesignBomEntity
	 */
	public static DesignBomEntity generationBomE(DesignBomEntity bom,
			String wholeBacklog, String specCode, String specName) {
		if (wholeBacklog.contains("E")) {
			bom.setSpecCodeC(specCode);
			bom.setSpecNameC(specName);
		} else {
			bom.setSpecCodeC("");
			bom.setSpecNameC("");
		}
		return bom;
	}

	/**
	 * 轧管工序
	 * 
	 * @param bom
	 *            物料BOM
	 * @param wholeBacklog
	 *            全程产线号
	 * @param specCode
	 *            成品规格代码
	 * @param specName
	 *            成品规格描述
	 * @param grades
	 *            钢种
	 * @param rollSpecs
	 *            轧管规格
	 * @return List<DesignBomEntity>
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static List<DesignBomEntity> generationBomD(DesignBomEntity bom,
			String wholeBacklog, String specCode, String specName,
			List<DesignMscPlGzEntity> grades,
			List<DesignMscGzGgEntity> rollSpecs) throws IllegalAccessException,
			InvocationTargetException {
		List<DesignBomEntity> bomDs = new ArrayList<DesignBomEntity>();
		if (wholeBacklog.contains("D")) {

			if (wholeBacklog.contains("E")) {
				// 制程有轧管、墩拔扩工序
				for (DesignMscGzGgEntity rollSpec : rollSpecs) {
					DesignBomEntity bomD = new DesignBomEntity();
					BeanUtils.copyProperties(bomD, bom);
					bomD.setGradecode(rollSpec.getGradecode());
					bomD.setGradename(rollSpec.getGradename());
					bomD.setSpecCodeR(rollSpec.getSpecCode());
					bomD.setSpecNameR(rollSpec.getSpecName());
					bomDs.add(bomD);
				}
			} else {
				// 制程有轧管、无墩拔扩工序
				for (DesignMscPlGzEntity grade : grades) {
					DesignBomEntity bomD = new DesignBomEntity();
					BeanUtils.copyProperties(bomD, bom);
					bomD.setGradecode(grade.getGradecode());
					bomD.setGradename(grade.getGradename());
					bomD.setSpecCodeR(specCode);
					bomD.setSpecNameR(specName);
					bomDs.add(bomD);
				}
			}
		} else {
			if (wholeBacklog.contains("E")) {
				// 制程无轧管、有墩拔扩工序
				for (DesignMscGzGgEntity rollSpec : rollSpecs) {
					DesignBomEntity bomD = new DesignBomEntity();
					BeanUtils.copyProperties(bomD, bom);
					bomD.setGradecode(rollSpec.getGradecode());
					bomD.setGradename(rollSpec.getGradename());
					bomD.setSpecCodeR(rollSpec.getSpecCode());
					bomD.setSpecNameR(rollSpec.getSpecName());
					bomDs.add(bomD);
				}
			} else {
				// 制程无轧管、无墩拔扩工序
				for (DesignMscPlGzEntity grade : grades) {
					DesignBomEntity bomD = new DesignBomEntity();
					BeanUtils.copyProperties(bomD, bom);
					bomD.setGradecode(grade.getGradecode());
					bomD.setGradename(grade.getGradename());
					bomD.setSpecCodeR("");
					bomD.setSpecNameR("");
					bomDs.add(bomD);
				}

			}
		}
		return bomDs;
	}

	/**
	 * 锻造
	 * 
	 * @param boms
	 *            物料BOM
	 * @param wholeBacklog
	 *            全程产线号
	 * @param blitSpecs
	 *            断面规格
	 * @return List<DesignBomEntity>
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static List<DesignBomEntity> generationBomC(
			List<DesignBomEntity> boms, String wholeBacklog,
			List<DesignMscBiltEntity> blitSpecs) throws IllegalAccessException,
			InvocationTargetException {
		List<DesignBomEntity> bomCs = new ArrayList<DesignBomEntity>();
		if (wholeBacklog.contains("C")) {
			if (wholeBacklog.contains("D")) {
				// 制程有锻造、轧管
				for (DesignBomEntity bom : boms) {
					List<DesignMscBiltEntity> fitBlitSpecs = BomFromKey
							.fiterBlanks(blitSpecs, bom.getSpecCodeR());
					for (DesignMscBiltEntity fitBlitSpec : fitBlitSpecs) {
						DesignBomEntity bomC = new DesignBomEntity();
						BeanUtils.copyProperties(bomC, bom);
						bomC.setSpecCodeD(fitBlitSpec.getbSpecCode());
						bomC.setSpecNameD(fitBlitSpec.getbSpecName());
						bomCs.add(bomC);
					}
				}
			} else {
				// 制程有锻造、无轧管
				for (DesignBomEntity bom : boms) {
					for (DesignMscBiltEntity blitSpec : blitSpecs) {
						DesignBomEntity bomC = new DesignBomEntity();
						BeanUtils.copyProperties(bomC, bom);
						bomC.setSpecCodeD(blitSpec.getbSpecCode());
						bomC.setSpecNameD(blitSpec.getbSpecName());
						bomCs.add(bomC);
					}
				}
			}
		} else {
			if (wholeBacklog.contains("D")) {
				// 制程无锻造、有轧管
				for (DesignBomEntity bom : boms) {
					bom.setSpecCodeD("");
					bom.setSpecNameD("");
					bomCs.add(bom);
				}
			} else {
				// 制程无锻造、无轧管
				for (DesignBomEntity bom : boms) {
					bom.setSpecCodeD("");
					bom.setSpecNameD("");
					bomCs.add(bom);
				}
			}
		}
		return bomCs;
	}

	/**
	 * 过滤断面
	 * 
	 * @param blanks
	 * @param specCode
	 * @return
	 */
	public static List<DesignMscBiltEntity> fiterBlanks(
			List<DesignMscBiltEntity> blanks, String specCode) {
		List<DesignMscBiltEntity> biltSpecs = new ArrayList<DesignMscBiltEntity>();
		for (DesignMscBiltEntity blank : blanks) {
			if (blank.getSpecCode().equals(specCode)) {
				biltSpecs.add(blank);
			}
		}
		return biltSpecs;
	}

	/**
	 * 铸造
	 * 
	 * @param boms
	 * @param wholeBacklog
	 * @param biltCSpecs
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static List<DesignBomEntity> generationBomB(
			List<DesignBomEntity> boms, String wholeBacklog,
			List<DesignMscBiltCEntity> biltCSpecs, String outsStlPipeFl)
			throws IllegalAccessException, InvocationTargetException {
		List<DesignBomEntity> bomBs = new ArrayList<DesignBomEntity>();
		if (wholeBacklog.contains("B")) {
			if (wholeBacklog.contains("D")) {
				if (wholeBacklog.contains("C")) {
					// 制程有轧管、锻造,有铸造
					for (DesignBomEntity bom : boms) {
						List<DesignMscBiltCEntity> fitBiltCSpecs = BomFromKey
								.fiterBiltsB(biltCSpecs, bom.getSpecCodeD());
						for (DesignMscBiltCEntity fitBiltCSpec : fitBiltCSpecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(fitBiltCSpec.getCSpecCode());
							bomB.setSpecNameZ(fitBiltCSpec.getCSpecName());
							bomBs.add(bomB);
						}
					}
				} else {
					for (DesignBomEntity bom : boms) {
						// 制程有轧管,无锻造,有铸造
						List<DesignMscBiltCEntity> fitBiltCspecs = BomFromKey
								.fiterBiltsA(biltCSpecs, bom.getSpecCodeR());
						for (DesignMscBiltCEntity fitBiltCspec : fitBiltCspecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(fitBiltCspec.getCSpecCode());
							bomB.setSpecNameZ(fitBiltCspec.getCSpecName());
							bomBs.add(bomB);
						}
					}
				}
			} else {
				if (wholeBacklog.contains("C")) {
					// 制程无轧管,有锻造,有铸造
					for (DesignBomEntity bom : boms) {
						List<DesignMscBiltCEntity> fitBiltCSpecs = BomFromKey
								.fiterBiltsB(biltCSpecs, bom.getSpecCodeD());
						for (DesignMscBiltCEntity fitBiltCSpec : fitBiltCSpecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(fitBiltCSpec.getCSpecCode());
							bomB.setSpecNameZ(fitBiltCSpec.getCSpecName());
							bomBs.add(bomB);
						}
					}
				} else {
					// 制程无轧管、无锻造,有铸造
					for (DesignBomEntity bom : boms) {
						for (DesignMscBiltCEntity biltCSpec : biltCSpecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(biltCSpec.getCSpecCode());
							bomB.setSpecNameZ(biltCSpec.getCSpecName());
							bomBs.add(bomB);
						}
					}
				}
			}
		} else {
			if (wholeBacklog.contains("D")) {
				if (wholeBacklog.contains("C")) {
					// 制程有轧管、锻造,无铸造
					for (DesignBomEntity bom : boms) {
						List<DesignMscBiltCEntity> fitBiltCSpecs = BomFromKey
								.fiterBiltsB(biltCSpecs, bom.getSpecCodeD());
						for (DesignMscBiltCEntity fitBiltCSpec : fitBiltCSpecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(fitBiltCSpec.getCSpecCode());
							bomB.setSpecNameZ(fitBiltCSpec.getCSpecName());
							bomBs.add(bomB);
						}
					}
				} else {
					// 制程有轧管,无锻造,无铸造(如果是外购管,则肯定有铸造工序的规格)

					for (DesignBomEntity bom : boms) {
						List<DesignMscBiltCEntity> fitBiltCspecs = BomFromKey
								.fiterBiltsA(biltCSpecs, bom.getSpecCodeR());
						for (DesignMscBiltCEntity fitBiltCspec : fitBiltCspecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(fitBiltCspec.getCSpecCode());
							bomB.setSpecNameZ(fitBiltCspec.getCSpecName());
							bomBs.add(bomB);
						}
					}

				}
			} else {
				if (wholeBacklog.contains("C")) {
					// 制程无轧管,有锻造,无铸造
					for (DesignBomEntity bom : boms) {
						List<DesignMscBiltCEntity> fitBiltCSpecs = BomFromKey
								.fiterBiltsB(biltCSpecs, bom.getSpecCodeD());
						for (DesignMscBiltCEntity fitBiltCSpec : fitBiltCSpecs) {
							DesignBomEntity bomB = new DesignBomEntity();
							BeanUtils.copyProperties(bomB, bom);
							bomB.setSpecCodeZ(fitBiltCSpec.getCSpecCode());
							bomB.setSpecNameZ(fitBiltCSpec.getCSpecName());
							bomBs.add(bomB);
						}
					}
				} else {
					// 制程无轧管、无锻造,无铸造
					for (DesignBomEntity bom : boms) {
						bom.setSpecCodeZ("");
						bom.setSpecNameZ("");
						bomBs.add(bom);
					}
				}
			}
		}
		return bomBs;
	}

	/**
	 * 过滤坯料规格
	 * 
	 * @param bilts
	 * @param specCode
	 *            轧管规格
	 * @return
	 */
	public static List<DesignMscBiltCEntity> fiterBiltsA(
			List<DesignMscBiltCEntity> bilts, String specCode) {
		List<DesignMscBiltCEntity> fitBiltSpecs = new ArrayList<DesignMscBiltCEntity>();
		for (DesignMscBiltCEntity bilt : bilts) {
			if (bilt.getSpecCode().equals(specCode)) {
				fitBiltSpecs.add(bilt);
			}
		}
		return fitBiltSpecs;
	}

	/**
	 * 过滤坯料规格
	 * 
	 * @param bilts
	 * @param specCode
	 *            断面规格
	 * @return
	 */
	public static List<DesignMscBiltCEntity> fiterBiltsB(
			List<DesignMscBiltCEntity> bilts, String specCode) {
		List<DesignMscBiltCEntity> biltBs = new ArrayList<DesignMscBiltCEntity>();
		for (DesignMscBiltCEntity bilt : bilts) {
			if (bilt.getBSpecCode().equals(specCode)) {
				biltBs.add(bilt);
			}
		}

		return biltBs;
	}
}
