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

import java.sql.Connection;
import java.sql.SQLException;

import com.steering.pss.sale.order.model.ComMscBlEntity;
import com.steering.pss.sale.order.model.ComMscEntity;
import com.steering.pss.sale.order.model.ComPscEntity;
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.BLQualityPreDaoImp;
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.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.BLQualityPreImp;

/**
 * 备料码技术评审预处理(2015/09/14)
 * 
 * @author Administrator
 */
public class BLQualityPre implements BLQualityPreImp {

	/**
	 * 查询备料码
	 * 
	 * @param con
	 * @param blCode
	 *            备料码
	 * @return ComMscBlEntity
	 * @throws SQLException
	 */
	public ComMscBlEntity getMscBlEntity(Connection con, String blCode)
			throws SQLException {
		BLQualityPreDaoImp blpreimp = FactoryDao.getBLQualityPreDaoImp();
		ComMscBlEntity blEntity = blpreimp.getMscBlEntity(con, blCode);
		return blEntity;
	}

	/**
	 * 验证备料码
	 * 
	 * @param blEntity
	 * @return String
	 * @throws SQLException
	 */
	public String checkMscBlEntity(Connection con, ComMscBlEntity blEntity)
			throws SQLException {
		BLQualityPreDaoImp blpreimp = FactoryDao.getBLQualityPreDaoImp();
		String message = "";
		if (blEntity == null) {
			message = "通过备料码找不到有效备料码信息,请确认备料码是否有误!";
			return message;
		}
		if (blEntity.getProduccode().length() == 0) {
			message = "备料码" + blEntity.getPscBl() + "品名不能为空!";
			return message;
		}
		if (blEntity.getStdCode().length() == 0) {
			message = "备料码" + blEntity.getPscBl() + "标准不能为空!";
			return message;
		}
		if (blEntity.getMinD().length() == 0) {
			message = "备料码" + blEntity.getPscBl() + "外径不能为空!";
			return message;
		}
		if (blEntity.getMinH().length() == 0) {
			message = "备料码" + blEntity.getPscBl() + "壁厚不能为空!";
			return message;
		}
		String[] spec = blpreimp.getBaseSpec(con, blEntity.getMinD(),
				blEntity.getMinH());
		if (spec == null) {
			message = "备料码" + blEntity.getPscBl() + "外径:" + blEntity.getMinD()
					+ "壁厚:" + blEntity.getMinH() + "在基础规格的钢管类型不存在数据!";
			return message;
		}
		ComPscEntity pscEntity = blpreimp.getPsc(con, blEntity);
		if (pscEntity == null) {
			message = "备料码" + blEntity.getPscBl()
					+ "四项标准(品名,钢级,标准,标准类别)找不到唯一对应的产品码!";
			return message;
		}
		return message;
	}

	/**
	 * 初始化备料码
	 * 
	 * @param con
	 * @param blEntity
	 * @param userName
	 * @return
	 * @throws SQLException
	 */
	public String initBLCode(Connection con, ComMscBlEntity blEntity,
			String userName) throws SQLException {
		BLQualityPreDaoImp blpreimp = FactoryDao.getBLQualityPreDaoImp();
		QualityPrePDaoImp qualityPreDao = FactoryDao.getQualityPrePDaoImp();
		String designKey = blpreimp.getDesignKeyByblCode(con,
				blEntity.getPscBl());
		if (designKey.length() > 0) {
			qualityPreDao.updateTklgLstFalse(con, designKey, userName);
			blpreimp.updateSlmOrdDesignBl(con, designKey);
			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.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);
		}
		blEntity.setCreateName(userName);
		blpreimp.addSlmOrdDesignBl(con, blEntity);
		designKey = blpreimp.getDesignKeyByblCode(con, blEntity.getPscBl());
		// 新加质量设计交付标准索引号记录
		qualityPreDao.addDesignStd(con, designKey);
		return designKey;
	}

	/**
	 * 验证基础冶金规范码
	 * 
	 * @param con
	 * @param designKey
	 * @param blEntity
	 * @return
	 * @throws SQLException
	 */
	public DesignEntity checkMsc(Connection con, String designKey,
			ComMscBlEntity blEntity) throws SQLException {
		BLQualityPreDaoImp blpreimp = FactoryDao.getBLQualityPreDaoImp();
		// 查询产品码
		ComPscEntity pscEntity = blpreimp.getPsc(con, blEntity);
		String psc = pscEntity.getPsc();
		String blCode = blEntity.getPscBl();
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		DesignStatusEntity status = null;
		// 通过产品码找基础冶金规范码信息
		String msc = mscDaoImp.getMscByPsc(con, psc);
		String[] unit = mscDaoImp.getPscUnit(con, psc);
		if (msc.equals("")) {
			status = IMsc.setMscStatus("F", "90", "产品码:" + psc
					+ "没有基础冶金规范码,待人工确认", "", psc, unit[0], unit[1], designKey,
					blCode, "MSC(0)");
			mscDaoImp.updateMscStatus(con, status);
			return null;
		}
		// 通过用途码与产品码查询冶金规范码
		String mscN = blpreimp.getMscByPscAndUseCode(con, psc,
				blEntity.getUseCode());
		// 如果找不到则是数据维护错误
		if (mscN.equals("")) {
			status = IMsc.setMscStatus("F", "90", "产品码:" + psc + "备料码用途码:"
					+ blEntity.getUseDesc() + "找不到冶金规范码,请确认备料码与MSC数据的维护合理性",
					"", psc, unit[0], unit[1], designKey, blCode, "MSC(0)");
			mscDaoImp.updateMscStatus(con, status);
			return null;
		}
		// 基础冶金规范码
		ComMscEntity mscInfo = mscDaoImp.getMsc(con, msc);
		// 验证基础冶金规范码的合理性
		if (Integer.parseInt(mscInfo.getUserCount()) == 0) {
			status = IMsc.setMscStatus("F", "92", "基础冶金规范码:" + mscInfo.getMsc()
					+ "使用次数为零,待人工确认", msc, msc, mscInfo.getUnitCode(),
					mscInfo.getUnitDesc(), designKey, blCode, "MSC(0)");
			mscDaoImp.updateMscStatus(con, status);
			return null;
		}
		if (!msc.equals(mscN)) {
			if (!mscInfo.getValidflag().equals("C")) {
				status = IMsc.setMscStatus("F", "91", "有Mscn特殊要求,基础冶金规范码:"
						+ msc + "未通过审核,待人工确认", msc, msc, mscInfo.getUnitCode(),
						mscInfo.getUnitDesc(), designKey, blCode, "MSC(0)");
				mscDaoImp.updateMscStatus(con, status);
				return null;
			}
		}
		status = IMsc.setMscStatus("S", "11", "基础冶金规范码通过验证", msc, msc,
				mscInfo.getUnitCode(), mscInfo.getUnitDesc(), designKey,
				blCode, "MSC(0)");
		mscDaoImp.updateMscStatus(con, status);
		DesignEntity design = BLQualityPre.setDesignEntity(blEntity);
		// 产品码
		design.setPsc(psc);
		// 产品规格
		String[] spec = blpreimp.getBaseSpec(con, blEntity.getMinD(),
				blEntity.getMinH());
		design.setSpecCode(spec[0]);
		design.setSpecName(spec[1]);
		design.setSpecStyle("A");
		// 质量设计主键
		design.setDesignKey(designKey);
		// 合同行号
		design.setOrdLnPk(blCode);
		// 操作人
		design.setUserName("");
		// 基础冶金规范码
		design.setMsc(mscInfo.getMsc());
		// 基础冶金规范码类型
		design.setMscStyle(mscInfo.getMscStyle());
		// 基础冶金规范码状态
		design.setMscValig(mscInfo.getValidflag());
		// 基础冶金规范码管理科室
		design.setDunitCode(mscInfo.getUnitCode());
		design.setDunitDesc(mscInfo.getUnitDesc());
		design.setMunitCode(mscInfo.getUnitCodeProcess());
		design.setMunitDesc(mscInfo.getUnitDescProcess());
		// 备料码冶金规范码信息
		ComMscEntity mscInfoN = mscDaoImp.getMsc(con, mscN);
		// MSCN
		design.setMscDunitCode(mscInfoN.getUnitCode());
		design.setMscDunitDesc(mscInfoN.getUnitDesc());
		design.setMscUnitCode(mscInfoN.getUnitCodeProcess());
		design.setMscUnitDesc(mscInfoN.getUnitDescProcess());
		if (msc.equals(mscN)) {
			design.setMscN("");
			design.setMscNStyle("");
		} else {
			design.setMscN(mscInfoN.getMsc());
			design.setMscNStyle(mscInfoN.getMscStyle());
		}
		// 备料码冶金规范码
		design.setOrderLineMsc(mscInfoN.getMsc());
		design.setOrderLineMscStyle(mscInfoN.getMscStyle());
		design.setOutsBilietFl(blEntity.getOutsBilletFl());
		design.setOutsStlPileFl(blEntity.getOutsStlPipeFl());
		
		return design;
	}

	/**
	 * 设计质量设计
	 * 
	 * @param designMsc
	 *            合同行
	 * @return DesignEntity
	 */
	public static DesignEntity setDesignEntity(ComMscBlEntity blEntity) {
		DesignEntity design = new DesignEntity();
		// 外径
		design.setDimater(blEntity.getMinD());
		// 壁厚
		design.setHeight(blEntity.getMinH());
		// 扣型
		design.setModelCode(blEntity.getModelCode());
		design.setModelDesc(blEntity.getModelDesc());
		// 规格
		// design.setJgCode(designMsc.getSpecCode());
		// 客户Alpha编号
		// design.setCustAlphyNo(designMsc.getCustAlphaNo());
		// 特殊要求
		// design.setAddAskNo(blEntity.getAddAskNo());
		// 标准名称
		design.setStdCode(blEntity.getStdCode());
		// 钢级
		design.setSteelCode(blEntity.getSteelcode());
		// 最终客户
		design.setFinalUser("");
		// 最终客户描述
		design.setFinalUserDesc("");
		// 产品规范码
		// design.setPsc(designMsc.getPsc());
		// 外径
		double dimater = Double.parseDouble(blEntity.getMinD());
		// 壁厚
		double height = Double.parseDouble(blEntity.getMinH());
		// 径壁比
		double DHProportion = 0;
		// 壁径比
		double HDProportion = 0;
		// 内径
		double innerProportion = dimater - height;
		if (height != 0 && dimater != 0) {
			DHProportion = dimater / height;
			HDProportion = height / dimater;
		}
		design.setDHProportion(String.valueOf(DHProportion));
		design.setHDProportion(String.valueOf(HDProportion));
		design.setInnerProportion(String.valueOf(innerProportion));
		// 长度上限
		design.setLengthMax(blEntity.getMaxL());
		// 长度下限
		design.setLengthMin(blEntity.getMinL());
		// 产品规格代码
		// design.setSpecCode(designMsc.getSpecCode());
		// 产品规格名称
		// design.setSpecName(designMsc.getSpecName());
		// 钢级名称
		design.setSteelName(blEntity.getSteelname());
		design.setUserCode(blEntity.getUseCode());
		design.setUserDesc(blEntity.getUseDesc());
		// mps要求
		// design.setMpsFl(designMsc.getMpsFl());
		// 短尺随炉
		// design.setShrtStove(designMsc.getShrtStove());
		// 单倍尺长
		// design.setSiglLen(designMsc.getSiglLen());
		// 短尺最短
		// design.setShortest(designMsc.getShortest());
		// 标准类别代码
		design.setStdStyle(blEntity.getStdStyle());
		// 标准类别描述
		design.setStdStyleDesc(blEntity.getStdStyleDesc());
		// Assel
		design.setAsselFl("0");
		// 品名
		design.setProducCode(blEntity.getProduccode());
		design.setProducDesc(blEntity.getProducname());
		design.setOutsBilietFl("");
		design.setOutsStlPileFl("");
		// design.setLenUnit(designMsc.getLenUnit());
		return design;
	}
}
