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

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.steering.comm.db.DBCommon;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.craftDesign.dao.CraftInnerDao;
import com.steering.pss.sale.order.craftDesign.dao.CraftStatusDao;
import com.steering.pss.sale.order.craftDesign.dao.InnerStdDao;
import com.steering.pss.sale.order.craftDesign.dao.Msc0Dao;
import com.steering.pss.sale.order.craftDesign.dao.MscChemDao;
import com.steering.pss.sale.order.craftDesign.dao.MscDeteDao;
import com.steering.pss.sale.order.craftDesign.dao.MscGProcessDao;
import com.steering.pss.sale.order.craftDesign.dao.MscPhyDao;
import com.steering.pss.sale.order.craftDesign.dao.MscSpecDao;
import com.steering.pss.sale.order.craftDesign.dao.MscSplineDao;
import com.steering.pss.sale.order.craftDesign.dao.MscWaterDao;
import com.steering.pss.sale.order.craftDesign.dao.PretreatmentDao;
import com.steering.pss.sale.order.craftDesign.dao.ProDucDao;
import com.steering.pss.sale.order.craftDesign.dao.SpecManageDao;
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.CraftOrdDesignMscBiltCEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscCftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscGzGgEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlGzEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlPrcEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdCicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdDicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdGicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicSmpEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdSicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdWicEntity;
import com.steering.pss.sale.order.craftDesign.model.InnerStdEntity;
import com.steering.pss.sale.order.craftDesign.model.SpecEntity;
import com.steering.pss.sale.order.craftDesign.server.InnerStd;
import com.steering.pss.sale.order.craftDesign.server.ProDuc;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

/**
 * 内控评审(只评审内控)
 * 
 * @author Administrator
 * 
 */
public class CraftInnerMain extends DBCommon {
	public String craftInner(Connection con, String designKey, String ordLnPk)
			throws SQLException {
		PretreatmentDao pretreatmentDao = new PretreatmentDao();
		CraftStatusDao statusDaoImp = new CraftStatusDao();
		CraftOrdDesignMscEntity craftDesign = pretreatmentDao.getOrderInfo(con,
				ordLnPk);
		if (craftDesign == null) {
			return "通过" + ordLnPk + "找不到合同信息";
		}
		CraftEntity design = CraftInnerMain.initcraftEntity(con, craftDesign,
				designKey);
		//查找技术评审designKey
		String slmDesignKey = pretreatmentDao.getSlmDesignkey(con, ordLnPk);

		CraftInnerDao craftInnerDao = new CraftInnerDao();
		craftInnerDao.deleteChem(con, designKey);
		craftInnerDao.deleteDete(con, designKey);
		craftInnerDao.deletePhy(con, designKey);
		craftInnerDao.deletePhySmp(con, designKey);
		craftInnerDao.deleteProcess(con, designKey);
		craftInnerDao.deleteSpec(con, designKey);
		craftInnerDao.deleteWater(con, designKey);
		// 查询制程
		List<CraftOrdDesignMscPlEntity> proDucs = craftInnerDao.getProDuc(con,
				slmDesignKey);
		if (proDucs == null) {
			return "通过" + designKey + "找不到制程";
		}

		
		InnerStdEntity innerStdEntity = null;
		ProDucDao proDucDao = new ProDucDao();
		
		
		
		if (design.getAllowNk().equals("1")) {
			for (CraftOrdDesignMscPlEntity proDuc : proDucs) {
				//钢种
				List<CraftOrdDesignMscPlGzEntity> grades = proDucDao.getSlmDesignGrade(
						slmDesignKey, proDuc.getMscPline(), con);
				//获取技术评审规格
				SpecEntity specEntity = SpecManageDao.getSlmDesignSpec(
						slmDesignKey, proDuc.getMscPline(), con);
				
				// 工艺参数
				ProDuc proDucimp = new ProDuc();
				Object[] craftResults =  proDucimp.checkCraft(
						designKey, design.getMscOr(), proDuc.getMscPline(), grades,
						design.getDimater(), design.getHeight(), design.getSpecCode(),
						specEntity.getRollSpecs(), specEntity.getBillSpecs(),
						specEntity.getBillSpecCs(),con);
				if (!craftResults[0].toString().equals("")) {
					statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
							design.getMscOr(), "F", "91", craftResults[0].toString(), "",
							designKey, "内控标准");
					statusDaoImp.updateDesignStsTklg(con, designKey, "", "F",
							"W");
					return "";
				}else{
					@SuppressWarnings("unchecked")
					List<CraftOrdDesignMscCftEntity> cftEntitys = (List<CraftOrdDesignMscCftEntity>)craftResults[1];
					for (CraftOrdDesignMscCftEntity cftEntity : cftEntitys) {
						cftEntity.setDesginKey(designKey);
						proDucDao.saveCft(con, cftEntity);
					}
				}
				
				// 内控标准
				InnerStd innerStd = new InnerStd();
				try {
					innerStdEntity = innerStd.checkInnerStd(con,
							design.getMscOr(), proDuc.getMscPline(),
							proDuc.getWholeBacklog(), design, grades, specEntity);
					if (!innerStdEntity.isFlag()) {
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "F", "91",
								innerStdEntity.getMessage(), "", designKey,
								"内控标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"F", "W");
						return "";
					} else {
						MscChemDao chemDao = new MscChemDao();
						MscPhyDao mscPhyDao = new MscPhyDao();
						MscSpecDao mscSpecDao = new MscSpecDao();
						MscDeteDao mscDeteDao = new MscDeteDao();
						MscWaterDao mscWaterDao = new MscWaterDao();
						MscGProcessDao mscGProcessDao = new MscGProcessDao();
						// 内控标准(对内控标准有要求)
						List<CraftOrdDesignStdCicEntity> chems = innerStdEntity
								.getChems();
						if (chems != null && chems.size() > 0) {
							chems = CraftTool.fiterCommon(chems, new String[] {
									"desginKey", "cic", "speclFl", "indexSeq",
									"mscPline", "processCode" });
							for (CraftOrdDesignStdCicEntity chem : chems) {
								chemDao.addChem(con, chem);
							}
						}
//						List<CraftOrdDesignStdPicEntity> phys = innerStdEntity
//								.getPhys();
//						if (phys != null && phys.size() > 0) {
//							for (CraftOrdDesignStdPicEntity phy : phys) {
//								mscPhyDao.savePhy(con, phy);
//							}
//						}
						List<CraftOrdDesignStdPicSmpEntity> smps = innerStdEntity
								.getSmps();
						if (smps != null && smps.size() > 0) {
							for (CraftOrdDesignStdPicSmpEntity smp : smps) {
								if (!InnerStdDao.checkInnerPicSmp(con, smp)) {
									mscPhyDao.savePicSample(con, smp);
								}
							}
						}
						List<CraftOrdDesignStdSicEntity> specs = innerStdEntity
								.getSpecs();
						if (specs != null && specs.size() > 0) {
							specs = CraftTool.fiterCommon(specs, new String[] {
									"desginKey", "sic", "speclFl", "indexSeq",
									"mscPline", "processCode" });
							for (CraftOrdDesignStdSicEntity spec : specs) {
								mscSpecDao.saveSpec(con, spec);
							}
						}
						List<CraftOrdDesignStdDicEntity> detes = innerStdEntity
								.getDetes();
						if (detes != null && detes.size() > 0) {
							detes = CraftTool.fiterCommon(detes, new String[] {
									"desginKey", "dic", "speclFl", "xh",
									"mscPline", "processCode" });
							for (CraftOrdDesignStdDicEntity dete : detes) {
								mscDeteDao.saveDete(con, dete);
							}
						}
						List<CraftOrdDesignStdWicEntity> waters = innerStdEntity
								.getWaters();
						if (waters != null && waters.size() > 0) {
							waters = CraftTool.fiterCommon(waters, new String[] {
									"desginKey", "wic", "speclFl", "indexSeq",
									"mscPline", "processCode" });
							for (CraftOrdDesignStdWicEntity water : waters) {
								mscWaterDao.saveWater(con, water);
							}
						}
						List<CraftOrdDesignStdGicEntity> processes = innerStdEntity
								.getProesses();
						if (processes != null && processes.size() > 0) {
							processes = CraftTool.fiterCommon(processes,
									new String[] { "desginKey", "gic", "speclFl",
											"xh", "mscPline", "processCode" });
							for (CraftOrdDesignStdGicEntity process : processes) {
								mscGProcessDao.saveProcess(con, process);
							}
						}
						
						//更新工艺评审工序表索引码集合
						craftInnerDao.updateCraftPrc(con, designKey, proDuc.getMscPline());
						
						statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
								design.getMscOr(), "S", "11", "内控标准通过验证", "",
								designKey, "内控标准");
						statusDaoImp.updateDesignStsTklg(con, designKey, "",
								"S", "W");
						
						//试样码评审
						String[] splineResults = MscSplineDao.CheckSpline(con, design, "2", proDuc, specEntity);
						if (splineResults[0].equals("")) {
							statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
									design.getMsc0(), "S", "11", "试样码验证通过", "", designKey,
									"试样码");
							statusDaoImp.updateDesignStsTklg(con, designKey, "", "S", "S");
						} else {
							if(design.getAllowNk().equals("1")){
								 statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
											design.getMsc0(), "F", "91", splineResults[0],
											splineResults[4], designKey, "试样码");
								 statusDaoImp.updateDesignStsTklg(con, designKey, "", "F","W");
								 return "";
							 }else{
								 statusDaoImp.updateCraftOrdDesignStsTklgLst(con,
											design.getMsc0(), "F", "91", splineResults[0],
											splineResults[4], designKey, "试样码");
							 }
						}
						return "";
					}
					
				} catch (Exception e) {
					e.printStackTrace();
					return e.getMessage();
				}

			}
		} else {
			statusDaoImp.updateCraftOrdDesignStsTklgLst(con, design.getMscOr(),
					"S", "00", "内控标准无要求", "", designKey, "内控标准");
			return "";
		}
		return "";
	}

	/**
	 * 初始化工艺评审类
	 * 
	 * @param con
	 * @param craftDesign
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public static CraftEntity initcraftEntity(Connection con,
			CraftOrdDesignMscEntity craftDesign, String designKey)
			throws SQLException {
		Msc0Dao msc0Dao = new Msc0Dao();
		PretreatmentDao pretreatmentDao = new PretreatmentDao();
		ComMscEntity comMscOrEntity = msc0Dao.getMscInfo(con,
				craftDesign.getMsc());
		CraftEntity craftEntity = new CraftEntity();
		/* 合同行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());
		// 规格类型
		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.setShortest(craftDesign.getShortest());
		craftEntity.setShrtStove(craftDesign.getShrtStove());
		craftEntity.setSiglLen(craftDesign.getSiglLen());
		return craftEntity;
	}
}
