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

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;

import com.steering.pss.sale.order.model.ComMscEntity;
import com.steering.pss.sale.order.model.DesignMscEntity;
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.QualityPrePDaoImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.QualityPreImp;

/**
 * 质量设计预处理类
 * 
 * @author Administrator
 */
public class IQulityPre implements QualityPreImp {

	/**
	 * 质量设计预处理
	 * 
	 * @param conn
	 *            连接字符串
	 * @param qualityPre
	 *            合同行
	 * @return designKey
	 * @throws SQLException
	 */
	public String DesignQualityPre(Connection con, DesignMscEntity orderLine,
			String userName) throws SQLException, ParseException {
		if (orderLine == null) {
			return "";
		}
		QualityPrePDaoImp qualityPreDao = FactoryDao.getQualityPrePDaoImp();
		qualityPreDao.deleteDesignPlControl(con, orderLine.getOrdLnPk());
		// 查询出合同行号在质量主记录MSC的原有designKey

		String designKey = qualityPreDao.getDesignKeyByOrdLnPk(con,
				orderLine.getOrdLnPk());
		// 如果designKey存在,则清除质量设计关系这个designKey所有的记录信息
		if (!designKey.equals("")) {
			qualityPreDao.updateTklgLstFalse(con, designKey, userName);
			qualityPreDao.deleteDesignMsc(con, orderLine.getOrdLnPk());
			qualityPreDao.deleteDesignMscBL(con, designKey);
			qualityPreDao.deleteDesignMscJG(con, designKey);
			qualityPreDao.deleteDesignMscJGBL(con, designKey);
			qualityPreDao.deleteDesignMscPL(con, designKey);
			qualityPreDao.deleteDesignMscPlGxPrl(con, designKey);
			qualityPreDao.deleteDesignMscPLGZ(con, designKey);
			qualityPreDao.deleteDesignMscJGPL(con, designKey);
			// qualityPreDao.deleteDesignMscPlPC(con, designKey);
			// qualityPreDao.deleteDesignMscPlPCLst(con, designKey);
			qualityPreDao.deleteDesignMscPLPRC(con, designKey);
			qualityPreDao.deleteDesignMscPlSTI(con, designKey);
			qualityPreDao.deleteDesignStd(con, designKey);
			qualityPreDao.deleteDesignStdCic(con, designKey);
			qualityPreDao.deleteDesignStdDic(con, designKey);
			qualityPreDao.deleteDesignStdSic(con, designKey);
			qualityPreDao.deleteDesignStdPic(con, designKey);
			qualityPreDao.deleteDesignStdPicSmp(con, designKey);
			qualityPreDao.deleteDesignStdWic(con, designKey);
			qualityPreDao.deleteDesignStsTklg(con, designKey);
			qualityPreDao.deleteDesignStsTklgLst(con, designKey);
			qualityPreDao.deleteBilt(con, designKey);
			qualityPreDao.deleteBiltLine(con, designKey);
			qualityPreDao.deleteBlank(con, designKey);
			qualityPreDao.deleteBlankLine(con, designKey);
			qualityPreDao.deleteSpec(con, designKey);
			qualityPreDao.deleteSpecLine(con, designKey);
			qualityPreDao.deleteLineBom(con, designKey);
			qualityPreDao.deleteLineBomBL(con, designKey);
			qualityPreDao.deleteLineBomJG(con, designKey);
			qualityPreDao.deleteSlmOrdDesignCicDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignPicDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignWicDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignGicDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignSicDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignDicDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignAddAskDiff(con, designKey);
			qualityPreDao.deleteSlmOrdDesignZbsTemplate(con, designKey);
			qualityPreDao.deleteSlmOrdDesignBiasC(con, designKey);
		}
		// 新增一条质量设计主记录，designKey自动生成 32位，再查询新生成designKey
		orderLine.setCreateName(userName);
		qualityPreDao.addDesignMsc(con, orderLine);
		String designKeyNew = qualityPreDao.getDesignKeyByOrdLnPk(con,
				orderLine.getOrdLnPk());
		// 新加质量设计交付标准索引号记录
		qualityPreDao.addDesignStd(con, designKeyNew);
		// 由于有可能设定接箍存在,要修改设定接箍的key
		qualityPreDao.updateDesignMscJG(con, designKey, designKeyNew);
		// 修改设定制程主键
		// qualityPreDao.updateDesignMscPL(con, designKey, designKeyNew);
		return designKeyNew;
	}

	/**
	 * 验证传入的合同行数据是否符合标准
	 * 
	 * @param orderLine
	 *            合同行
	 * @return true 合格
	 * @throws SQLException
	 */
	public String checkOrderLine(Connection con, DesignMscEntity orderLine)
			throws SQLException {
		QualityPrePDaoImp qualityPreDao = FactoryDao.getQualityPrePDaoImp();
		String errMessage = "";
		if (orderLine.getOrdLnPk().equals("")
				|| orderLine.getOrdPk().equals("")) {
			return "合同行号不能为空";
		}
		if (orderLine.getProduccode().equals("")
				|| orderLine.getProducname().equals("")) {
			return "合同行品名代码、描述不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getMsc().equals("")) {
			return "合同行冶金规范码不能为空,请确认合同行信息的完整性";
		}
		if (orderLine.getStdCode().equals("")
				|| orderLine.getStdName().equals("")) {
			return "合同行标准代码、名称不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getSteelcode().equals("")
				|| orderLine.getSteelname().equals("")) {
			return "合同行钢级(牌号)代码、名称不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getOrderSpecCode().equals("")
				|| orderLine.getOrderSpecDesc().equals("")) {
			return "合同行产品规格代码、名称不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getSpecCode().equals("")
				|| orderLine.getSpecName().equals("")) {
			return "合同行订货规格代码、描述不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getDimater().equals("")
				|| orderLine.getHeight().equals("")) {
			return "合同行外径、壁厚不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getFinalUser().equals("")
				|| orderLine.getFinalUserDesc().equals("")) {
			return "合同行最终客户不能为空,请确认合同信息维护的完整性";
		}
		if (orderLine.getAsselFl().length() != 1) {
			return "合同行ASSEL只能为0、1,请确认合同信息维护的完整性";
		}
		String specStyle = qualityPreDao.getSpecStyle(con,
				orderLine.getSpecCode());
		if ((specStyle.equals("B") || specStyle.equals("D"))
				&& (orderLine.getMscStyle().equals("B")
						|| orderLine.getMscStyle().equals("C") || orderLine
						.getMscStyle().equals("D"))) {
			return "合同产品规格:  " + orderLine.getSpecName()
					+ "  属于钢锭/管坯产品规格,不需要接箍,而冶金规范码:  " + orderLine.getMscDesc()
					+ "  对接箍码有要求,请确定合同维护的合理性";
		}
		if ((specStyle.equals("B") || specStyle.equals("D"))
				&& orderLine.getAsselFl().equals("1")) {
			return "合同产品规格:  " + orderLine.getSpecName()
					+ "  属于钢锭/管坯产品规格,不能对Assel有要求,但合同选择了Assel,请确定合同维护的合理性";
		}

		return errMessage;
	}

	/**
	 * 客户ALPHA处理
	 * 
	 * @param con
	 * @param ordLnPk
	 * @param alphaNo
	 * @throws SQLException
	 */
	public void checkAlpha(Connection con, String ordLnPk, String psc,
			DesignMscEntity designMscEntity) throws SQLException {
		String alphaNo = designMscEntity.getCustAlphaNo();
		QualityPrePDaoImp qualityPreDao = FactoryDao.getQualityPrePDaoImp();
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		if (alphaNo.equals("")) {
			return;
		}
		String mscN = qualityPreDao.queryAlphaMsc(con, psc, alphaNo);
		if (mscN.equals("")) {
			return;
		}
		ComMscEntity mscInfo = mscDaoImp.getMsc(con, mscN);
		if (mscInfo == null) {
			return;
		}
		qualityPreDao.updateLine(con, ordLnPk, mscInfo.getMsc(),
				mscInfo.getMscDesc(), mscInfo.getUseCode(),
				mscInfo.getUseDesc());
		designMscEntity.setMsc(mscInfo.getMsc());
		designMscEntity.setMscDesc(mscInfo.getMscDesc());
		designMscEntity.setUseCode(mscInfo.getUseCode());
		designMscEntity.setUseDesc(mscInfo.getUseDesc());
		designMscEntity.setCustAlphaNo("");
	}
}
