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

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

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

import org.apache.commons.lang.ObjectUtils;

/**
 * 冶金规范码 业务验证 类
 * 
 * @author Administrator
 */
public class IMsc implements MscImp {
	// 冶金规范码验证流程:
	// 1、通过产品码查询基础冶金规范码
	// 2、有客户特殊要求,基础冶金规范码必需通过审批
	// 3、基础冶金规范码使用次数大于或者等于零
	// 4、生成质量设计对象(常用值类)
	private static QualityPrePDaoImp qualityPreDao = FactoryDao
			.getQualityPrePDaoImp();

	public DesignEntity checkMsc(Connection con, String designKey,
			String userName, DesignMscEntity designMsc,String[] saleInfo) throws Exception {
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		DesignStatusEntity status = null;
		// 通过产品码找基础冶金规范码信息
		String msc = mscDaoImp.getMscByPsc(con, designMsc.getPsc());
		if (msc.equals("")) {
			String[] unit = mscDaoImp.getPscUnit(con, designMsc.getPsc());
			status = IMsc.setMscStatus("F", "90",
					"产品码:" + designMsc.getPscDesc() + "没有基础冶金规范码,待人工确认", "",
					designMsc.getPsc(), unit[0], unit[1], designKey,
					designMsc.getOrdLnPk(), "MSC(0)");
			mscDaoImp.updateMscStatus(con, status);
			return null;
		}
		ComMscEntity mscInfo = mscDaoImp.getMsc(con, msc);

		// 合同的销售组织 天淮
		if (saleInfo != null && saleInfo.length > 0 && "100105".equals(saleInfo[0])) {
			mscInfo.setUnitCode("002023005003");
			mscInfo.setUnitDesc("工艺科");
			mscInfo.setUnitCodeProcess("002023005003");
			mscInfo.setUnitDescProcess("工艺科");
		}

		// 如果含有客户特殊要求，则要求冶金规范码状态为审核状态
		if (!designMsc.getCustAlphaNo().equals("")
				&& !mscInfo.getValidflag().equals("C")) {
			status = IMsc.setMscStatus("F", "91", "有客户技术特殊要求,基础冶金规范码:"
					+ mscInfo.getMsc() + "未通过审核,待人工确认", mscInfo.getMsc(),
					designMsc.getMsc(), mscInfo.getUnitCode(),
					mscInfo.getUnitDesc(), designKey, designMsc.getOrdLnPk(),
					"MSC(0)");
			mscDaoImp.updateMscStatus(con, status);
			return null;
		} else {
			status = IMsc.setMscStatus("S", "11", "基础冶金规范码通过验证",
					mscInfo.getMsc(), designMsc.getMsc(),
					mscInfo.getUnitCode(), mscInfo.getUnitDesc(), designKey,
					designMsc.getOrdLnPk(), "MSC(0)");
			mscDaoImp.updateMscStatus(con, status);

			// 修改质量设计主表

			if (designMsc.getMsc().equals("")) {
				mscDaoImp.updateDesignMsc(con, designKey, mscInfo.getMsc(),
						mscInfo.getMscDesc(), mscInfo.getMscStyle());
			}
			/**
			 * 二、 将常用值重新组成一个设计类中（这些值 过程使用,不会发生改变）(依次：designKey，合同行号，用户名等)
			 */
			DesignEntity design = IMsc.setDesignEntity(designMsc);
			// 质量设计主键
			design.setDesignKey(designKey);
			// 合同行号
			design.setOrdLnPk(designMsc.getOrdLnPk());
			// 操作人
			design.setUserName("");
			// 基础冶金规范码
			design.setMsc(mscInfo.getMsc());
			// 基础冶金规范码类型
			design.setMscStyle(mscInfo.getMscStyle());
			// 基础冶金规范码状态
			design.setMscValig(mscInfo.getValidflag());
			design.setMsc0UseCount(mscInfo.getUserCount());
			// 基础冶金规范码管理科室
			design.setDunitCode(mscInfo.getUnitCode());
			design.setDunitDesc(mscInfo.getUnitDesc());
			design.setMunitCode(mscInfo.getUnitCodeProcess());
			design.setMunitDesc(mscInfo.getUnitDescProcess());
			// 如果合同行没有冶金规范码，则生成的基础冶金规范码就是合同行的冶金码(但如果有特殊要求，则在生成特殊要求之后回填到合同行的冶金规范码中)
			if (designMsc.getMsc().equals("")) {
				design.setOrderLineMsc(mscInfo.getMsc());
				design.setOrderLineMscStyle(mscInfo.getMscStyle());
				design.setMscN("");
				design.setMscNStyle("");
				design.setMscDunitCode(mscInfo.getUnitCode());
				design.setMscDunitDesc(mscInfo.getUnitDesc());
				design.setMscUnitCode(mscInfo.getUnitCodeProcess());
				design.setMscUnitDesc(mscInfo.getUnitDescProcess());
			} else {
				// 如果合同行有冶金规范码，则要判断合同行的冶金规范码等于基础冶金码,如果不是则特殊冶金规范码为合同行的冶金规范码
				design.setOrderLineMsc(designMsc.getMsc());
				design.setOrderLineMscStyle(designMsc.getMscStyle());
				if (!designMsc.getMsc().equals(mscInfo.getMsc())) {
					ComMscEntity mscN = mscDaoImp.getMsc(con,
							designMsc.getMsc());
					if(mscN == null){
						throw new Exception("MSC码不存在："+designMsc.getMsc());
					}
					// 设置合同行冶金规范码管理科室
					design.setMscDunitCode(mscN.getUnitCode());
					design.setMscDunitDesc(mscN.getUnitDesc());
					design.setMscUnitCode(mscN.getUnitCodeProcess());
					design.setMscUnitDesc(mscN.getUnitDescProcess());
					design.setMscN(designMsc.getMsc());
					design.setMscNStyle(designMsc.getMscStyle());
					design.setMscNUseCount(mscN.getUserCount());

				} else {
					// 如果合同行是基础冶金规范码，则没有特殊冶金规范码
					design.setMscDunitCode(mscInfo.getUnitCode());
					design.setMscDunitDesc(mscInfo.getUnitDesc());
					design.setMscUnitCode(mscInfo.getUnitCodeProcess());
					design.setMscUnitDesc(mscInfo.getUnitDescProcess());
					design.setMscN(designMsc.getMsc());
					design.setMscN("");
					design.setMscNStyle("");
				}
			}
			// MSCN使用次数
			String specStyle = qualityPreDao.getSpecStyle(con,
					designMsc.getSpecCode());
			// 产品规格类型
			design.setSpecStyle(specStyle);
			return design;

		}
	}

	/**
	 * MSC 设计明细行
	 * 
	 * @param designSts
	 *            明细项目
	 * @param designStsRs
	 *            设计状态
	 * @param designStsDesc
	 *            原因
	 * @param msc
	 *            冶金规范码
	 * @param designObjKey
	 *            KEY
	 * @param unitCode
	 *            科室代码
	 * @param unitDesc
	 *            科室名称
	 * @param designKey
	 *            质量主键
	 * @param ordLnPk
	 *            合同行号
	 * @param listItem
	 *            明细编码
	 * @return DesignStatusEntity
	 */
	public static DesignStatusEntity setMscStatus(String designSts,
			String designStsRs, String designStsDesc, String msc,
			String designObjKey, String unitCode, String unitDesc,
			String designKey, String ordLnPk, String listItem) {
		DesignStatusEntity status = new DesignStatusEntity();
		status.setDesginKey(designKey);
		status.setOrdLnPk(ordLnPk);
		status.setLstItem(listItem);
		status.setMsc(msc);
		status.setDesignSts(designSts);
		status.setDesignStsRs(designStsRs);
		status.setDesignStsDesc(designStsDesc);
		status.setDesignObjKey(designObjKey);
		status.setUnitCode(unitCode);
		status.setUnitDesc(unitDesc);
		return status;
	}

	/**
	 * 设计质量设计
	 * 
	 * @param designMsc
	 *            合同行
	 * @return DesignEntity
	 */
	public static DesignEntity setDesignEntity(DesignMscEntity designMsc) {
		DesignEntity design = new DesignEntity();
		// 外径
		design.setDimater(designMsc.getDimater());
		// 壁厚
		design.setHeight(designMsc.getHeight());
		// 扣型
		design.setModelCode(designMsc.getModelCode());
		design.setModelDesc(designMsc.getModelDesc());
		// 规格
		design.setJgCode(designMsc.getSpecCode());
		// 客户Alpha编号
		design.setCustAlphyNo(designMsc.getCustAlphaNo());
		// 特殊要求
		design.setAddAskNo(designMsc.getAddAskNo());
		// 标准名称
		design.setStdCode(designMsc.getStdCode());
		// 钢级
		design.setSteelCode(designMsc.getSteelcode());
		// 最终客户
		design.setFinalUser(designMsc.getFinalUser());
		// 最终客户描述
		design.setFinalUserDesc(designMsc.getFinalUserDesc());
		// 产品规范码
		design.setPsc(designMsc.getPsc());
		// 外径
		double dimater = Double.parseDouble(designMsc.getDimater());
		// 壁厚
		double height = Double.parseDouble(designMsc.getHeight());
		// 径壁比
		double DHProportion = 0;
		// 壁径比
		double HDProportion = 0;
		// 内径
		double innerProportion = dimater - 2 * 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(designMsc.getLenMax());
		// 长度下限
		design.setLengthMin(designMsc.getLenMin());
		// 产品规格代码
		design.setSpecCode(designMsc.getSpecCode());
		// 产品规格名称
		design.setSpecName(designMsc.getSpecName());
		// 钢级名称
		design.setSteelName(designMsc.getSteelname());
		design.setUserCode(designMsc.getUseCode());
		design.setUserDesc(designMsc.getUseDesc());
		// mps要求
		design.setMpsFl(designMsc.getMpsFl());
		// 短尺随炉
		design.setShrtStove(designMsc.getShrtStove());
		// 单倍尺长
		design.setSiglLen(designMsc.getSiglLen());
		// 短尺最短
		design.setShortest(designMsc.getShortest());
		// 标准类别代码
		design.setStdStyle(designMsc.getStdStyle());
		// 标准类别描述
		design.setStdStyleDesc(designMsc.getStdStyleDesc());
		// Assel
		design.setAsselFl(designMsc.getAsselFl());
		// 品名
		design.setProducCode(designMsc.getProduccode());
		design.setProducDesc(designMsc.getProducname());
		design.setLenUnit(designMsc.getLenUnit());
		// 在线机探盲区 1可手探；0不可手探
		design.setManInspection(designMsc.getManInspection());
		design.setInspectionValue(designMsc.getInspectionValue());
		design.setOutsBilietFl(designMsc.getOutsBilletFl());
		design.setOutsStlPileFl(designMsc.getOutsStlPipeFl());
		// 技术评审后的custAlphaNo
		design.setOldCustAlphaNo(designMsc.getCustAlphaNo2());
		return design;
	}
}
