package com.steering.pss.sale.order.qualityDesign;

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.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCPlEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscBiltPlEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGxPrlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGzEntity;
import com.steering.pss.sale.order.model.DesignStatusEntity;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.CreateSpecLineDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.GradeDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.ProDucDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.QualityPrePDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.StandardDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.ProLineEntity;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.SpecEntity;
import com.steering.pss.sale.order.qualityDesign.server.factory.FactoryServer;
import com.steering.pss.sale.order.qualityDesign.server.imp.GradeImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.ProLineManageImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.SpecManageImp;
import com.steering.pss.sale.order.qualityDesign.server.iserver.ServerDesignTool;

/**
 * 新增轧管规格
 * 
 * @author Administrator
 * 
 */
public class QualityDesignStandard extends DBCommon {

	/**
	 * 新增规格生成物料
	 * 
	 * @param designKey
	 *            质量主键
	 * @param msc
	 *            冶金规范码
	 * @param mscPlines
	 *            制程号集合
	 * @param wholeBacklogs
	 *            全程途径码集合
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param specCode
	 *            合同产品规格代码
	 * @param specName
	 *            合同产品规格描述
	 * @return
	 */
	public CoreReturnObject checkNewSpec(String designKey, String msc,
			ArrayList<String> mscPlines, ArrayList<String> wholeBacklogs,
			String dimater, String height, String specCode, String specName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		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<DesignMscPlEntity> mscPlEntitys = new ArrayList<DesignMscPlEntity>();
			for (int i = 0; i < mscPlines.size(); i++) {
				DesignMscPlEntity proDuc = new DesignMscPlEntity();
				proDuc.setDesginKey(designKey);
				proDuc.setMscPline(mscPlines.get(i));
				proDuc.setWholeBacklog(wholeBacklogs.get(i));
				mscPlEntitys.add(proDuc);
			}
			// 生成质量设计类
			QualityPrePDaoImp qualityPreDao = FactoryDao.getQualityPrePDaoImp();
			DesignEntity design = new DesignEntity();
			design.setDimater(dimater);
			design.setHeight(height);
			design.setOrderLineMsc(msc);
			design.setDesignKey(designKey);
			String specStyle = qualityPreDao.getSpecStyle(con, specCode);
			design.setSpecStyle(specStyle);
			design.setSpecCode(specCode);
			design.setSpecName(specName);
			QualityDesignTool.nullConverNullString(design);

			GradeDaoImp gradeDaoImp = FactoryDao.getGradeDaoImp();
			GradeImp gradeImp = FactoryServer.getGradeImp();
			ProDucDaoImp processDaoImp = FactoryDao.getProDucDaoImp();
			SpecManageImp specManageImp = FactoryServer.getSpecManageImp();
			CreateSpecLineDaoImp specLineDaoImp = FactoryDao
					.getCreateSpecLineDaoImp();
			ProLineManageImp proLineManageImp = FactoryServer
					.getProLineManageImp();
			StandardDaoImp standardDaoImp = FactoryDao.getStandardDaoImp();
			DesignStatusEntity status = null;
			for (DesignMscPlEntity proDuc : mscPlEntitys) {
				/**
				 * 1、验证钢种
				 */
				Object[] gradeInfo = gradeImp.checkGrade(con,
						proDuc.getMscPline(), proDuc.getWholeBacklog(), msc,
						dimater, height, design.getSpecStyle(),
						design.getSpecName(), "", design.getOrderLineMscStyle(), designKey);
				String errMessageGrade = (String) gradeInfo[0];
				@SuppressWarnings("unchecked")
				List<DesignMscPlGzEntity> grades = (List<DesignMscPlGzEntity>) gradeInfo[1];
				// 如果得到的钢种条数为零，则验证下一个制程
				if (grades == null || grades.size() == 0) {
					core.setV_errMsg(errMessageGrade);
					return core;
				}
				/**
				 * 2、 验证规格
				 */
				SpecEntity specInfo = specManageImp.analysisSpec(con, proDuc,
						grades, design);
				if (specInfo.getStatus() != null) {
					status = specInfo.getStatus();
					core.setV_errMsg(status.getDesignStsDesc());
					return core;
				}

				/**
				 * 3、验证产线
				 */
				ProLineEntity proLineInfo = proLineManageImp.analysisProLine(
						con, design, proDuc, specInfo.getRollSpecs(),
						specInfo.getBlitSpecs(), specInfo.getBlitCSpecs());
				if (proLineInfo.getStatus() != null) {
					status = proLineInfo.getStatus();
					core.setV_errMsg(status.getDesignStsDesc());
					return core;
				}

				// 保存钢种
				for (DesignMscPlGzEntity grade : specInfo.getGrade()) {
					grade.setDesginKey(designKey);
					if (standardDaoImp.checkDesignGrade(con, grade) == 0) {
						gradeDaoImp.saveGrade(con, grade, designKey);
					}
				}

				// 保存轧管规格
				List<DesignMscGzGgEntity> rollSpecs = specInfo.getRollSpecs();
				if (rollSpecs != null && rollSpecs.size() != 0) {
					rollSpecs = ServerDesignTool.fiterRollSpec(rollSpecs);
					for (DesignMscGzGgEntity rollSpec : rollSpecs) {

						if (standardDaoImp.checkRollSpec(con, rollSpec) == 0) {
							specLineDaoImp.savePipeRoll(con, rollSpec);
						}
					}
				}
				List<DesignMscBiltEntity> biltSpecs = specInfo.getBlitSpecs();

				// 保存断面规格
				if (biltSpecs != null && biltSpecs.size() != 0) {
					biltSpecs = ServerDesignTool.fiterBiltSpec(biltSpecs);
					if (biltSpecs != null && biltSpecs.size() != 0) {
						for (DesignMscBiltEntity biltSpec : biltSpecs) {

							if (standardDaoImp.checkBillSpec(con, biltSpec) == 0) {
								specLineDaoImp.saveBlankSection(con, biltSpec);
							}
						}
					}
				}
				List<DesignMscBiltCEntity> biltsSpecs = specInfo
						.getBlitCSpecs();

				// 保存坯料规格
				if (biltsSpecs != null && biltsSpecs.size() != 0) {
					biltsSpecs = ServerDesignTool.fiterBiltCSpec(biltsSpecs);
					for (DesignMscBiltCEntity biltsSpec : biltsSpecs) {

						if (standardDaoImp.checkBillCSpec(con, biltsSpec) == 0) {
							specLineDaoImp.saveStockSpecific(con, biltsSpec);
						}

					}
				}

				// 保存制程适用产线
				List<DesignMscPlGxPrlEntity> proLines = proLineInfo
						.getProLines();
				for (DesignMscPlGxPrlEntity proLine : proLines) {

					if (standardDaoImp.checkZCline(con, proLine) == 0) {
						specLineDaoImp.saveProLine(con, proLine);
					}

				}

				// 保存铸造产线（含铸造规格）
				List<DesignMscBiltCPlEntity> biltCSpecLines = proLineInfo
						.getBiltCSpecLines();
				if (biltCSpecLines != null && biltCSpecLines.size() != 0) {
					for (DesignMscBiltCPlEntity blitCInLine : biltCSpecLines) {

						if (standardDaoImp.checkBillCLine(con, blitCInLine) == 0) {
							specLineDaoImp.saveBlits(con, blitCInLine);
						}

					}
				}
				// 保存轧管产线（含轧管规格）
				List<DesignMscGzGgPlEntity> rollSpecLines = proLineInfo
						.getRollSpecLines();
				if (rollSpecLines != null && rollSpecLines.size() != 0) {
					for (DesignMscGzGgPlEntity specInline : rollSpecLines) {

						if (standardDaoImp.checkRollLine(con, specInline) == 0) {
							specLineDaoImp.saveSpec(con, specInline);
						}

					}
				}

				// 保存锻造产线（含锻造规格）
				List<DesignMscBiltPlEntity> biltSpecLines = proLineInfo
						.getBiltSpecLines();
				if (biltSpecLines != null && biltSpecLines.size() != 0) {
					for (DesignMscBiltPlEntity biltInLine : biltSpecLines) {

						if (standardDaoImp.checkBillLine(con, biltInLine) == 0) {
							specLineDaoImp.saveBlank(con, biltInLine);
						}

					}
				}
				// 保存制程
				if (standardDaoImp.checkProduc(con, proDuc) == 0) {
					processDaoImp.saveProcess(con, proDuc, designKey);
				}
			}
			BOMGeneration.generationBomMain(con, designKey, "0", "0");
			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;
	}
}
