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

import com.steering.pss.sale.order.craftDesign.dao.CraftStatusDao;
import com.steering.pss.sale.order.craftDesign.dao.Msc0Dao;
import com.steering.pss.sale.order.craftDesign.dao.PretreatmentDao;
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.CraftOrdDesignMscEntity;

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

/**
 * MSC0
 * 
 * @author Administrator
 * 
 */
public class Msc0 {
	public CraftEntity checkMsc0(Connection con, String designKey, String userName, CraftOrdDesignMscEntity craftDesign, String[] saleInfo)
			throws SQLException {
		CraftStatusDao statusDao = new CraftStatusDao();
		Msc0Dao msc0Dao = new Msc0Dao();
		String psc = craftDesign.getPsc();
		String msc0 = msc0Dao.getMsc0(con, psc);
		// 合同行冶金规范码信息
		ComMscEntity comMsc0Entity = msc0Dao.getMscInfo(con, msc0);
		if(comMsc0Entity == null){
			return null;
		}
		// 修改工艺设计记录的科室
		msc0Dao.updateCraftDepement(con, comMsc0Entity.getUnitCodeProcess(), comMsc0Entity.getUnitDescProcess(),
				designKey);
		// 通过产品码找基础冶金规范码
		if (msc0.equals("")) {
			statusDao.updateCraftOrdDesignStsTklgLst(con, msc0, "F", "90", "产品码(" + psc + ")找不到基础冶金规范码!", "", designKey,
					"MSC(0)");
			return null;
		}
		ComMscEntity comMscEntity = msc0Dao.getMscInfo(con, msc0);
		// 合同行MSC与MSC0
		if (!msc0.equals(craftDesign.getMsc()) && !comMscEntity.getValidflag().equals("C")) {
			statusDao.updateCraftOrdDesignStsTklgLst(con, msc0, "F", "90", "合同行有技术要求,MSC0(" + msc0 + ")需要审核!", "",
					designKey, "MSC(0)");
			return null;
		}
		// MSC0使用次数
		if (Integer.parseInt(comMscEntity.getUserCount()) == 0) {
			statusDao.updateCraftOrdDesignStsTklgLst(con, msc0, "F", "90", "MSC0(" + msc0 + ")使用次数为零!", "", designKey,
					"MSC(0)");
			return null;
		}
		// msc0通过验证
		statusDao.updateCraftOrdDesignStsTklgLst(con, msc0, "S", "11", "MSC(0)(" + msc0 + ")通过验证!", "", designKey,
				"MSC(0)");
		/* 新增标准主记录 */
		msc0Dao.addCraftOrdDesignStd(con, designKey);
		/****** 将一些常用的合同变量放入工艺评审类中 ************/
		// 合同行冶金规范码信息
		ComMscEntity comMscOrEntity = msc0Dao.getMscInfo(con, craftDesign.getMsc());
		// 天淮合同，天管msc ，使用天淮工艺科
		if (saleInfo != null && saleInfo.length > 0 && "100105".equals(saleInfo[0])
				&& comMscOrEntity !=null && !comMscOrEntity.getUnitCodeProcess().contains("002023005")) {
			comMscOrEntity.setUnitCodeProcess("002023005003");
			comMscOrEntity.setUnitDescProcess("工艺科");
		}
		// 修改工艺设计记录的科室
		msc0Dao.updateCraftDepement(con, comMscOrEntity.getUnitCodeProcess(), comMscOrEntity.getUnitDescProcess(),
				designKey);
		CraftEntity craftEntity = new CraftEntity();
		/* MSC0 */
		craftEntity.setMsc0(msc0);
		craftEntity.setMsc0Style(comMscEntity.getMscStyle());
		craftEntity.setMsc0Valig(comMscEntity.getValidflag());
		/* MSCN */
		if (msc0.equals(craftDesign.getMsc())) {
			craftEntity.setMscN("");
			craftEntity.setMscNStyle("");
			craftEntity.setMscNValig("");
		} else {
			craftEntity.setMscN(comMscOrEntity.getMsc());
			craftEntity.setMscNStyle(comMscOrEntity.getMscStyle());
			craftEntity.setMscNValig(comMscOrEntity.getValidflag());
		}
		/* 合同行MSC */
		craftEntity.setMscOr(comMscOrEntity.getMsc());
		craftEntity.setMscOrStyle(comMscOrEntity.getMscStyle());
		craftEntity.setMscOrValig(comMscOrEntity.getValidflag());
		craftEntity.setAllowNk(comMscOrEntity.getAllowNk());
		// 工艺评审主键
		craftEntity.setDesignKey(designKey);
		craftEntity.setOrdLnPk(craftDesign.getOrdLnPk());
		// 规格
		craftEntity.setSpecCode(craftDesign.getSpecCode());
		craftEntity.setSpecName(craftDesign.getSpecName());
		// 规格类型
		PretreatmentDao pretreatmentDao = new PretreatmentDao();
		String specStyle = pretreatmentDao.getSpecCodeStyle(con, craftDesign.getSpecCode());
		craftEntity.setSpecStyle(specStyle);
		// 用途码
		craftEntity.setUseCode(craftDesign.getUseCode());
		craftEntity.setUseDesc(craftDesign.getUseDesc());
		// 外径
		craftEntity.setDimater(craftDesign.getDimater());
		// 壁厚
		craftEntity.setHeight(craftDesign.getHeight());
		// 扣型
		craftEntity.setModelCode(craftDesign.getModelCode());
		craftEntity.setModelDesc(craftDesign.getModelDesc());
		// 品名
		craftEntity.setProducCode(craftDesign.getProduccode());
		craftEntity.setProducDesc(craftDesign.getProducname());
		// 钢级
		craftEntity.setSteelCode(craftDesign.getSteelcode());
		craftEntity.setSteelName(craftDesign.getSteelname());
		// 标准
		craftEntity.setStdCode(craftDesign.getStdCode());
		craftEntity.setStdName(craftDesign.getStdName());
		// 标准类别
		craftEntity.setStdStyle(craftDesign.getStdStyle());
		craftEntity.setStdStyleDesc(craftDesign.getStdStyleDesc());
		// 长度上下限
		if (craftDesign.getLenUnit().equals("英尺")) {
			craftEntity.setLengthMax(String.valueOf(Double.parseDouble(craftDesign.getLenMax()) * 0.3048));
			craftEntity.setLengthMin(String.valueOf(Double.parseDouble(craftDesign.getLenMin()) * 0.3048));
		} else {
			craftEntity.setLengthMax(craftDesign.getLenMax());
			craftEntity.setLengthMin(craftDesign.getLenMin());
		}
		// MPS
		craftEntity.setMps(craftDesign.getMpsFl());
		// AsselFl
		craftEntity.setAsselFl(craftDesign.getAsselFl());
		// 附加要求
		craftEntity.setAddAskNo(craftDesign.getAddAskNo());
		// 最终客户编码
		craftEntity.setCustAlphyNo(craftDesign.getCustAlphaNo());
		craftEntity.setFinalUser(craftDesign.getFinalUser());
		craftEntity.setShortest(craftDesign.getShortest());
		craftEntity.setShrtStove(craftDesign.getShrtStove());
		craftEntity.setSiglLen(craftDesign.getSiglLen());
		craftEntity.setManInspection(craftDesign.getManInspection());
		craftEntity.setInspectionValue(craftDesign.getInspectionValue());
		// 外购坯
		craftEntity.setOutsBilietFl(craftDesign.getOutsBilletFl());
		// 外购管
		craftEntity.setOutsStlPipeFl(craftDesign.getOutsStlPipeFl());
		// 客户ALPHANO
		craftEntity.setCustAlphaNo2(craftDesign.getCustAlphaNo2());
		// 产品码
		craftEntity.setPsc(craftDesign.getPsc());
		return craftEntity;
	}
}
