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

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

import com.steering.pss.sale.order.craftDesign.dao.Msc0Dao;
import com.steering.pss.sale.order.craftDesign.dao.MscPhyDao;
import com.steering.pss.sale.order.craftDesign.dao.MscSplineDao;
import com.steering.pss.sale.order.craftDesign.model.ComBaseSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdCicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdDicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdGicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicSmpEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdSicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdWicEntity;
import com.steering.pss.sale.order.craftDesign.model.MscNEntity;
import com.steering.pss.sale.order.craftDesign.model.StdEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

public class MscN {
	/**
	 * 验证特殊要求的MSC
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @return boolean
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public MscNEntity checkMscN(Connection con, CraftEntity design, String msc)
			throws Exception {
		Msc0Dao mscDaoImp = new Msc0Dao();
		MscNEntity mscNEntity = new MscNEntity();
		// 特殊冶金规范码
		ComMscEntity comMscEntity = mscDaoImp.getMscInfo(con, msc);
		// 如果为NULL，没有此MSC的记录
		if (comMscEntity == null) {
			mscNEntity.setFlag(false);
			mscNEntity.setMessage("有特殊要求,但找不到特殊要求MSC(n),请重新生成MSC(n)!");
			return mscNEntity;
		}
		// 无效的冶金规范码
		if (comMscEntity.getValidflag().equals("D")) {
			mscNEntity.setFlag(false);
			mscNEntity.setMessage("冶金规范码:" + msc + "无效!");
			return mscNEntity;
		}
		// 冶金规范码使用次数为零
		if (comMscEntity.getUserCount().equals("0")) {
			mscNEntity.setFlag(false);
			mscNEntity.setMessage("冶金规范码:" + msc + "使用次数为零!");
			return mscNEntity;
		}

		// 将用途码放入工艺设计中
		String modelCode = design.getModelCode();
		// 通过冶金规范码验证交付标准
		// 成分标准
		CraftOrdDesignStdEntity ordstdEntity = new CraftOrdDesignStdEntity();
		MscChem chemImp = new MscChem();
		List<String> cics = chemImp.getCicByMscAndModel(con, msc, modelCode,
				design.getMscNValig());
		StdEntity stdEntity = null;
		if (cics != null && cics.size() > 0) {
			ComBaseSpecEntity spec = new MscSplineDao().getSpecInfoBySpecCode(design.getSpecCode(), con);
			stdEntity = chemImp.checkChem(con, design, spec, "1", cics);
			if (!stdEntity.isFlag()) {
				mscNEntity.setFlag(false);
				mscNEntity.setMessage("冶金规范码:" + msc + stdEntity.getMessage());
				return mscNEntity;
			} else {
				mscNEntity
						.setChems((List<CraftOrdDesignStdCicEntity>) stdEntity
								.getObjM());
				ordstdEntity.setCicN(stdEntity.getObjKey());
				ordstdEntity.setSmeltingCntN(stdEntity.getSmeltingCnt());
				ordstdEntity.setProductCntN(stdEntity.getProducCnt());
				ordstdEntity.setRateN(stdEntity.getRate());
			}
		}
		// 理化标准
		MscPhy mscPhyImp = new MscPhy();
		List<String> pics = mscPhyImp.getPicByMscAndModelCode(con, msc,
				modelCode, design.getMscNValig());
		ComBaseSpecEntity spec = new MscSplineDao().getSpecInfoBySpecCode(design.getSpecCode(), con);
		if (pics != null && pics.size() > 0) {
			stdEntity = mscPhyImp.checkPhy(con, design, "1", pics, spec, "/", "/", "/");
			if (!stdEntity.isFlag()) {
				mscNEntity.setFlag(false);
				mscNEntity.setMessage("冶金规范码:" + msc + stdEntity.getMessage());
				return mscNEntity;
			} else {
				List<CraftOrdDesignStdPicEntity> phys = (List<CraftOrdDesignStdPicEntity>) stdEntity
						.getObjM();
				phys = CraftTool.fiterCommon(phys, new String[] {
						"desginKey", "pic", "speclFl", "indexSeq",
						"mscPline", "processCode" });
				MscPhyDao mscPhyDao = new MscPhyDao();
				for (CraftOrdDesignStdPicEntity phy : phys) {
					mscPhyDao.savePhy(con, phy);
				}
				
				mscNEntity.setPhys((List<CraftOrdDesignStdPicEntity>) stdEntity
						.getObjM());
				mscNEntity
						.setSmps((List<CraftOrdDesignStdPicSmpEntity>) stdEntity
								.getObjC());
				ordstdEntity.setPicN(stdEntity.getObjKey());
			}
		}

		// 公差标准
		MscSpec mscSpecImp = new MscSpec();
		List<String> sics = mscSpecImp.getSicByMscAndModel(con, msc, modelCode,
				design.getMscNValig());
		if (sics != null && sics.size() > 0) {
			stdEntity = mscSpecImp.checkSpec(con, design, "1", sics, "", spec);
			if (!stdEntity.isFlag()) {
				mscNEntity.setFlag(false);
				mscNEntity.setMessage("冶金规范码:" + msc + stdEntity.getMessage());
				return mscNEntity;
			} else {
				mscNEntity
						.setSpecs((List<CraftOrdDesignStdSicEntity>) stdEntity
								.getObjM());
				ordstdEntity.setSicN(stdEntity.getObjKey());
			}
		}

		// 探伤标准
		MscDete mscDeteImp = new MscDete();
		List<String> dics = mscDeteImp.getDicByMscAndModel(con, msc, modelCode,
				design.getMscNValig());
		if (dics != null && dics.size() > 0) {
			stdEntity = mscDeteImp.checkDete(con, design, "1", dics, spec);
			if (!stdEntity.isFlag()) {
				mscNEntity.setFlag(false);
				mscNEntity.setMessage("冶金规范码:" + msc + stdEntity.getMessage());
				return mscNEntity;
			} else {
				mscNEntity
						.setDetes((List<CraftOrdDesignStdDicEntity>) stdEntity
								.getObjM());
				ordstdEntity.setDicN(stdEntity.getObjKey());
			}
		}

		// 水压标准
		MscWater mscWaterImp = new MscWater();
		List<String> wics = mscWaterImp.getWicByMscAndModel(con, msc,
				modelCode, design.getMscNValig(), design.getDimater(),
				design.getHeight());
		if (wics != null && wics.size() > 0) {
			stdEntity = mscWaterImp.checkWater(con, design, "1", wics, null);
			if (!stdEntity.isFlag()) {
				mscNEntity.setFlag(false);
				mscNEntity.setMessage("冶金规范码:" + msc + stdEntity.getMessage());
				return mscNEntity;
			} else {
				mscNEntity
						.setWaters((List<CraftOrdDesignStdWicEntity>) stdEntity
								.getObjM());
				ordstdEntity.setWicN(stdEntity.getObjKey());
			}
		}

		// 加工标准
		MscGprocess mscProcessingImp = new MscGprocess();
		List<String> gics = mscProcessingImp.getGicByMscAndModel(con, msc,
				modelCode, design.getMscNValig());
		if (gics != null && gics.size() > 0) {
			stdEntity = mscProcessingImp.checkProcess(con, design, "1", gics);
			if (!stdEntity.isFlag()) {
				mscNEntity.setFlag(false);
				mscNEntity.setMessage("冶金规范码:" + msc + stdEntity.getMessage());
				return mscNEntity;
			} else {
				mscNEntity
						.setProesses((List<CraftOrdDesignStdGicEntity>) stdEntity
								.getObjM());
				ordstdEntity.setGicN(stdEntity.getObjKey());
			}
		}
		mscNEntity.setStdEntity(ordstdEntity);
		mscNEntity.setFlag(true);
		mscNEntity.setMessage("Msc(n)通过验证!");
		return mscNEntity;
	}
}
