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

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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.pss.sale.order.bomGeneration.BOMGeneration;
import com.steering.pss.sale.order.craftDesign.dao.GradeDao;
import com.steering.pss.sale.order.craftDesign.dao.PretreatmentDao;
import com.steering.pss.sale.order.craftDesign.dao.ProDucDao;
import com.steering.pss.sale.order.craftDesign.dao.ProDucSaveDao;
import com.steering.pss.sale.order.craftDesign.dao.StandardDao;
import com.steering.pss.sale.order.craftDesign.model.CraftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltCEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltCPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscGzGgEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscGzGgPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlGxPrlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlGzEntity;
import com.steering.pss.sale.order.craftDesign.model.InnerStdEntity;
import com.steering.pss.sale.order.craftDesign.model.LineEntity;
import com.steering.pss.sale.order.craftDesign.model.SpecEntity;
import com.steering.pss.sale.order.craftDesign.server.Grade;
import com.steering.pss.sale.order.craftDesign.server.LineManage;
import com.steering.pss.sale.order.craftDesign.server.SpecManage;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

/**
 * 镦拔扩新增规格生成物料
 * 
 * @author Administrator
 * 
 */
public class CraftDesignStandard extends DBCommon {
	/**
	 * 新增规格生成物料
	 * 
	 * @param designKey
	 *            质量主键
	 * @param msc
	 *            冶金规范码
	 * @param mscPlines
	 *            制程号集合
	 * @param wholeBacklogs
	 *            全程途径码集合
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param specCode
	 *            合同产品规格代码
	 * @param specName
	 *            合同产品规格描述
	 * @return
	 */
	public CoreReturnObject checkNewSpec(ArrayList<String> params,
			ArrayList<String> mscPlines, ArrayList<String> wholeBacklogs) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		// (desginKey);0
		// listParam.Add(msc);1
		// listParam.Add(mscType);//冶金规范描述 2
		// listParam.Add(psc);//产品码 3
		// listParam.Add(outdiameter);4
		// listParam.Add(wallthick);5
		// listParam.Add(specCode);//规格6
		// listParam.Add(specName);7
		// listParam.Add(modelCode);//扣型8
		// listParam.Add(modelDesc);9
		// listParam.Add(producCode);//品名 10
		// listParam.Add(producName);11
		// listParam.Add(outsBilletFl);//外购坯（COM_BASE_INFO:1218) 12
		// listParam.Add(outsStlPipeFl);//13
		String designKey = params.get(0);
		String msc = params.get(1);
		String mscType = params.get(2);
		String psc = params.get(3);
		String dimater = params.get(4);
		String height = params.get(5);
		String specCode = params.get(6);
		String specName = params.get(7);
		String modelCode = params.get(8);
		String modeDesc = params.get(9);
		String producCode = params.get(10);
		String producDesc = params.get(11);
		String outsBilietFl = params.get(12);
		String outsStlPipeFl = params.get(13);
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			if (mscPlines.size() != wholeBacklogs.size()
					|| mscPlines.size() == 0) {
				core.setV_errMsg("请确认传入制程与全程途径码的合理性");
				return core;
			}

			if (designKey.length() == 0 || msc.length() == 0
					|| dimater.length() == 0 || height.length() == 0) {
				core.setV_errMsg("请确认传入质量主键/MSC等参数的合理性");
				return core;
			}

			List<CraftOrdDesignMscPlEntity> mscPlEntitys = new ArrayList<CraftOrdDesignMscPlEntity>();
			for (int i = 0; i < mscPlines.size(); i++) {
				CraftOrdDesignMscPlEntity proDuc = new CraftOrdDesignMscPlEntity();
				proDuc.setDesginKey(designKey);
				proDuc.setMscPline(mscPlines.get(i));
				proDuc.setWholeBacklog(wholeBacklogs.get(i));
				mscPlEntitys.add(proDuc);
			}
			// 生成质量设计类
			PretreatmentDao pretreatmentDao = new PretreatmentDao();
			CraftEntity design = new CraftEntity();
			design.setPsc(psc);
			design.setDimater(dimater);
			design.setHeight(height);
			design.setMscOr(msc);
			design.setDesignKey(designKey);
			String specStyle = pretreatmentDao.getSpecCodeStyle(con, specCode);
			design.setSpecStyle(specStyle);
			design.setSpecCode(specCode);
			design.setSpecName(specName);
			design.setPsc(psc);
			design.setMscOrStyle(mscType);
			design.setModelCode(modelCode);
			design.setModelDesc(modeDesc);
			design.setProducCode(producCode);
			design.setProducDesc(producDesc);
			design.setOutsBilietFl(outsBilietFl);
			design.setOutsStlPipeFl(outsStlPipeFl);
			CraftTool.nullConverNullString(design);
			GradeDao gradeDao = new GradeDao();
			Grade grade = new Grade();
			SpecManage specManage = new SpecManage();
			LineManage lineManage = new LineManage();
			ProDucSaveDao proDucSaveDao = new ProDucSaveDao();
			StandardDao standardDao = new StandardDao();
			ProDucDao proDucDao = new ProDucDao();
			for (CraftOrdDesignMscPlEntity proDuc : mscPlEntitys) {
				/**
				 * 1、验证钢种
				 */
				Object[] gradeInfo = grade.checkGrade(con,
						proDuc.getMscPline(), proDuc.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) {
					core.setV_errMsg(gradeMessage);
					return core;
				}
				// 钢种集合
				@SuppressWarnings("unchecked")
				List<CraftOrdDesignMscPlGzEntity> grades = (List<CraftOrdDesignMscPlGzEntity>) gradeInfo[2];

				/**
				 * 2、 验证规格
				 */
				SpecEntity specEntity = specManage.checkSpec(con, proDuc,
						grades, design);
				if (!specEntity.isFlag()) {
					core.setV_errMsg(specEntity.getErrMessage());
					return core;
				}

				/**
				 * 10.3产线
				 */
				LineEntity lineEntity = lineManage.analysisProLine(con, design,
						proDuc, specEntity.getRollSpecs(),
						specEntity.getBillSpecs(), specEntity.getBillSpecCs(), null);
				if (!lineEntity.isFlag()) {
					core.setV_errMsg(lineEntity.getErrMessage());
					return core;
				}

				// 钢种
				for (CraftOrdDesignMscPlGzEntity gradeEntity : grades) {
					gradeEntity.setDesginKey(designKey);
					if (standardDao.checkDesignGrade(con, gradeEntity) == 0) {
						gradeDao.saveGrade(con, gradeEntity);
					}
				}
				// 规格(轧管规格)
				List<CraftOrdDesignMscGzGgEntity> rollSpecs = specEntity
						.getRollSpecs();
				if (rollSpecs != null && rollSpecs.size() > 0) {
					for (CraftOrdDesignMscGzGgEntity rollSpec : rollSpecs) {
						if (standardDao.checkRollSpec(con, rollSpec) == 0) {
							proDucSaveDao.saveRollSpec(con, rollSpec);
						}
					}
				}
				// 锻造规格
				List<CraftOrdDesignMscBiltEntity> billSpecs = specEntity
						.getBillSpecs();
				if (billSpecs != null && billSpecs.size() > 0) {
					for (CraftOrdDesignMscBiltEntity billSpec : billSpecs) {
						if (standardDao.checkBillSpec(con, billSpec) == 0)
							proDucSaveDao.savebiltSpec(con, billSpec);
					}
				}
				// 铸造规格
				List<CraftOrdDesignMscBiltCEntity> billSpecCs = specEntity
						.getBillSpecCs();
				if (billSpecCs != null && billSpecCs.size() > 0) {
					for (CraftOrdDesignMscBiltCEntity billSpecC : billSpecCs) {
						if (standardDao.checkBillCSpec(con, billSpecC) == 0)
							proDucSaveDao.saveBiltcSpec(con, billSpecC);
					}
				}
				// 适用产线
				List<CraftOrdDesignMscPlGxPrlEntity> lines = lineEntity
						.getLines();
				if (lines != null && lines.size() > 0) {
					for (CraftOrdDesignMscPlGxPrlEntity line : lines) {
						if (standardDao.checkZCline(con, line) == 0)
							proDucSaveDao.saveProLine(con, line);
					}
				}
				// 轧管产线
				List<CraftOrdDesignMscGzGgPlEntity> rollLines = lineEntity
						.getRollLines();
				if (rollLines != null && rollLines.size() > 0) {
					for (CraftOrdDesignMscGzGgPlEntity rollLine : rollLines) {
						if (standardDao.checkRollLine(con, rollLine) == 0)
							proDucSaveDao.saveRollLine(con, rollLine);
					}
				}
				// 锻造产线
				List<CraftOrdDesignMscBiltPlEntity> biltLines = lineEntity
						.getBiltLines();
				if (biltLines != null && biltLines.size() > 0) {
					for (CraftOrdDesignMscBiltPlEntity biltLine : biltLines) {
						if (standardDao.checkBillLine(con, biltLine) == 0)
							proDucSaveDao.saveBiltLine(con, biltLine);
					}
				}
				// 铸造产线
				List<CraftOrdDesignMscBiltCPlEntity> biltCLines = lineEntity
						.getBiltCLines();
				if (biltCLines != null && biltCLines.size() > 0) {
					for (CraftOrdDesignMscBiltCPlEntity biltCLine : biltCLines) {
						if (standardDao.checkBillCLine(con, biltCLine) == 0)
							proDucSaveDao.saveBlitcLine(con, biltCLine);
					}
				}
				proDuc.setDesginKey(designKey);
				if (standardDao.checkProduc(con, proDuc) == 0) {
					proDucDao.saveProDuc(con, proDuc);
				}
			}
			BOMGeneration.generationBomMain(con, designKey, "2", "1");
			core.setV_errMsg("生成物料成功!");
			con.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.toString());
			e.printStackTrace();
		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}

		return core;
	}
}
