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

import java.sql.Connection;

import com.steering.pss.sale.order.model.ComMscEntity;
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.MscDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.StatusDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.server.factory.FactoryServer;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscChemImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscDeteImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscPhyImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscProcessingImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscSpecImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscWaterImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.SpecialMscImp;

/**
 * 冶金规范码（特殊要求的MSC码）业务实现类
 * 
 * @author Administrator 彭德阳 2015/04/22
 */
public class ISpecialMsc implements SpecialMscImp {
	/**
	 * 设计状态类
	 */
	private DesignStatusEntity status = null;
	/**
	 * 设计状态操作数据库接口
	 */
	private static StatusDaoImp statusDaoImp = FactoryDao.getStatusDaoImp();

	/**
	 * 验证特殊要求的MSC
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @return boolean
	 * @throws Exception
	 */
	public boolean checkSpecialMc(Connection con, DesignEntity design)
			throws Exception {
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		// 特殊冶金规范码
		String msc = design.getMscN();
		// 如果冶金规范码为空，表示没有特殊冶金规范码
		if (msc.equals("")) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "S", "00", "没有特殊要求MSC(n)", design.getMsc(),
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return true;
		}
		ComMscEntity comMscEntity = mscDaoImp.getMsc(con, msc);
		// 如果为NULL，没有此MSC的记录
		if (comMscEntity == null) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "90", "有特殊要求,但找不到特殊要求MSC(N),请重新生成MSC(n)",
					msc, design.getMscDunitCode(), design.getMscDunitDesc());
			status.setMsc(msc);
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}
		// 无效的冶金规范码
		if (comMscEntity.getValidflag().equals("D")) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "92", "特殊要求MSC(n)无效", msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			status.setMsc(msc);
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}

		// 将用途码放入质量设计中

		// 通过冶金规范码查询交付标准
		// 成分标准
		MscChemImp chemImp = FactoryServer.getChemImp();
		String message = chemImp.checkChem(con, msc, design, "1");
		if (message.length() > 0) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "91", "冶金规范码:" + msc + " " + message, msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}
		// 理化标准
		MscPhyImp mscPhyImp = FactoryServer.getMscPhyImp();
		message = mscPhyImp.checkPhy(con, msc, design, "1");
		if (message.length() > 0) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "91", "冶金规范码:" + msc + " " + message, msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}
		// 公差标准
		MscSpecImp mscSpecImp = FactoryServer.getMscSpecImp();
		message = mscSpecImp.checkSpec(con, msc, design, "1");
		if (message.length() > 0) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "91", "冶金规范码:" + msc + " " + message, msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}

		// 探伤标准
		MscDeteImp mscDeteImp = FactoryServer.getMscDeteImp();
		message = mscDeteImp.checkDete(con, msc, design, "1");
		if (!message.equals("")) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "91", "冶金规范码:" + msc + " " + message, msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}

		// 加工标准
		MscProcessingImp mscProcessingImp = FactoryServer.getMscProcessingImp();
		message = mscProcessingImp.checkProcess(con, msc, design, "1");
		if (!message.equals("")) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "91", "冶金规范码:" + msc + " " + message, msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}

		// 水压标准
		MscWaterImp mscWaterImp = FactoryServer.getMscWaterImp();
		String[] waterResults = mscWaterImp.checkWater(con, msc, design, "1");
		message = waterResults[0];
		if (!message.equals("")) {
			status = statusDaoImp.setDesignStatus(design, design.getMscN(),
					"MSC(n)", "F", "91", "冶金规范码:" + msc + " " + message, msc,
					design.getMscDunitCode(), design.getMscDunitDesc());
			statusDaoImp.updateDesignStatus(con, status);
			return false;
		}
		status = statusDaoImp.setDesignStatus(design, design.getMscN(),
				"MSC(n)", "S", "11", "MSC(n)通过验证", msc,
				design.getMscDunitCode(), design.getMscDunitDesc());
		statusDaoImp.updateDesignStatus(con, status);
		return true;
	}

}
