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

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.pss.judge.comm.JudgeHelper;
import com.steering.pss.judge.comm.PhyJudgeHelper;
import com.steering.pss.qcm.RoundGBT81702008;
import com.steering.pss.qcm.model.ComBaseImpactCEntity;
import com.steering.pss.qcm.model.ComBaseImpactEntity;
import com.steering.pss.sale.order.craftDesign.dao.MscPhyDao;
import com.steering.pss.sale.order.craftDesign.model.*;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.DesignMscEntity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MscPhy {
	/**
	 * 查询理化索引号
	 * 
	 * @param con
	 * @param msc
	 * @param modelCode
	 * @param mscValig
	 * @return
	 * @throws SQLException
	 */
	public List<String> getPicByMscAndModelCode(Connection con, String msc, String modelCode, String mscValig)
			throws SQLException {
		MscPhyDao phyDaoImp = new MscPhyDao();
		List<String> pics = null;
		if (mscValig.equals("C")) {
			pics = phyDaoImp.getExaminePic(con, msc, modelCode);
		} else {
			pics = phyDaoImp.getEValidPic(con, msc, modelCode);
		}
		return pics;
	}

	// 验证理化标准流程：
	// 1、通过基础冶金规范码查询理化索引号
	// 2、通过理化索引号查询理化标准
	// 3、过滤外径、壁厚、径壁比等限制条件
	// 4、查询理化的取样要求
	// 5、只取一个理化索引号通过的数据
	/**
	 * 验证理化标准
	 * 
	 * @param con
	 *            连接字符串
	 *            质量设计状态类
	 * @return boolean
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public StdEntity checkPhy(Connection con, CraftEntity design, String flag, List<String> pics,
			ComBaseSpecEntity spec, String processCode, String processDesc, String mscPline) throws Exception {
		MscPhyDao phyDaoImp = new MscPhyDao();
		String message = "";
		String designKey = design.getDesignKey();
		String specStyle = spec.getMscStyle();
		String specCode = spec.getSpecCode();
		String specName = spec.getSpecName();
		String useCode = design.getUseCode();
		String useDesc = design.getUseDesc();
		String dimater = spec.getDimater();
		String height = spec.getHeight();
		StdEntity stdEntity = new StdEntity();
		if (pics == null || pics.size() == 0) {
			stdEntity.setFlag(true);
			stdEntity.setMessage("");
			return stdEntity;
		}
		for (String pic : pics) {
			// 通过索引号查询理化标准
			List<CraftOrdDesignStdPicEntity> phys = phyDaoImp.getPhyByPic(con, pic);
			if (phys == null) {
				message = "理化索引号:" + pic + "找不到理化标准";
				continue;
			}
			// 过滤理化标准，如果是钢管，过滤外径/壁厚/内径等，如果是管坯，只过滤外径，如果是钢锭，或者其它规格，则过滤规格
			List<CraftOrdDesignStdPicEntity> phyAs = null;
			if (specStyle.equals("A")) {
				// 钢管
				// 径壁比
				String dh = String.valueOf(Double.parseDouble(dimater) / Double.parseDouble(height));
				// 壁径比
				String hd = String.valueOf(Double.parseDouble(height) / Double.parseDouble(dimater));
				// 内径
				String in = String.valueOf(Double.parseDouble(dimater) - 2 * Double.parseDouble(height));
				phyAs = MscPhy.fiterPhyA(phys, dimater, height, dh, hd, in);
			} else if (specStyle.equals("B")) {
				phyAs = MscPhy.fiterPhyB(phys, dimater);
			} else {
				phyAs = MscPhy.fiterPhyByCAndD(phys, specCode);
			}
			// 验证理化标准
			Object[] info = MscPhy.fiterPhy(phyAs, phys, specStyle, dimater, height, specName);
			message = (String) info[1];
			phyAs = (ArrayList<CraftOrdDesignStdPicEntity>) info[0];
			if (message.length() > 0) {
				message = "理化索引号:" + pic + "," + message;
				continue;
			}
			
			List<CraftOrdDesignStdPicSmpEntity> smps = null;
			// 验证理化标准的检验项目
			//验证理化公式
			boolean existsRm = false;
			boolean existsP0088 = false;
			boolean existsJzw = false;//夹杂物
			boolean existsJzw4 = false;//夹杂物(四象限)
			boolean existsCcc = false;//超尺寸
			boolean existsCcc4 = false;//超尺寸(四象限)
			for (CraftOrdDesignStdPicEntity phy : phyAs) {
				if(phy.getStdmin().contains("Rm") || phy.getStdmax().contains("Rm")){
					existsRm = true;
				}
				if(phy.getPhyCode().equals("P0088")){
					existsP0088 = true;
				}
				if(phy.getPhyCodeMin().equals("C0011")){
					existsJzw = true;
					if(phy.getPhyCode().equals("P0618")){
						existsCcc = true;
					}
				}
				if(phy.getPhyCodeMin().equals("C0115")){
					existsJzw4 = true;
					if(phy.getPhyCode().equals("P0619")){
						existsCcc4 = true;
					}
				}
			}
			if(existsRm && !existsP0088){
				message = "理化索引号:" + pic + "," + "公式中存在Rm, 请维护抗拉强度（高温）Rm";
				continue;
			}
			if(flag.equals("2") && existsJzw && !existsCcc){
				message = "理化索引号:" + pic + "," + "存在【夹杂物】试样组, 请维护【是否存在超尺寸非金属夹杂物】检验项";
				continue;
			}
			if(flag.equals("2") && existsJzw4 && !existsCcc4){
				message = "理化索引号:" + pic + "," + "存在【夹杂物(四象限)】试样组, 请维护【是否存在超尺寸非金属夹杂物(四象限)】检验项";
				continue;
			}
			
			smps = new ArrayList<CraftOrdDesignStdPicSmpEntity>();
			
			if(flag.equals("2")){
				//取样频率对应的分批单位、分批数量必须一致
				HashMap<String, String[]> rateMap = new HashMap<String, String[]>();
				HashMap<String, String> phyMinMap = new HashMap<String, String>();
				//处理试样组序号
				//HashMap<String, ArrayList<String>> phyMap = new HashMap<String, ArrayList<String>>();
				HashMap<String, HashMap<String, ArrayList<String>>> phyMinSeqMap = new HashMap<String, HashMap<String, ArrayList<String>>>();
				HashMap<String, String> phyMinInfo = new HashMap<String, String>();
				for (CraftOrdDesignStdPicEntity phy : phyAs) {
					if(phy.getPhyMinSeq().equals("")){
						message = "理化索引号:" + pic + " 序号：" + phy.getIndexSeq() + "不存在试样组序号，请重新保存以生成试样组序号！";
						break;
					}
					if(phy.getPhyCodeMax().equals("R0001")){
						if(phy.getItemCodeF().equals("")){
							message = "理化索引号:" + pic + " 序号：" + phy.getIndexSeq() + " " + phy.getPhyNameMax() + "必须维护试验方向！";
							break;
						}
						if(phy.getItemCodeW().equals("")){
							message = "理化索引号:" + pic + " 序号：" + phy.getIndexSeq() + " " + phy.getPhyNameMax() + "必须维护试验温度！";
							break;
						}
					}
					if(phy.getPhyCodeMax().equals("R0002")){
						if(phy.getItemCodeF().equals("")){
							message = "理化索引号:" + pic + " 序号：" + phy.getIndexSeq() + " " + phy.getPhyNameMax() + "必须维护试验方向！";
							break;
						}
						if(phy.getItemCodeS().equals("")){
							message = "理化索引号:" + pic + " 序号：" + phy.getIndexSeq() + " " + phy.getPhyNameMax() + "必须维护试验尺寸！";
							break;
						}
						if(phy.getItemCodeW().equals("")){
							message = "理化索引号:" + pic + " 序号：" + phy.getIndexSeq() + " " + phy.getPhyNameMax() + "必须维护试验温度！";
							break;
						}
					}
					
					if(phyMinSeqMap.containsKey(phy.getPhyCodeMin())){
						HashMap<String, ArrayList<String>> tmpPhyMap = phyMinSeqMap.get(phy.getPhyCodeMin());
						if(tmpPhyMap.containsKey(phy.getPhyMinSeq())){
							ArrayList<String> tmpPhys = tmpPhyMap.get(phy.getPhyMinSeq());
							tmpPhys.add(phy.getPhyCode());
						}else{
							ArrayList<String> tmpPhys = new ArrayList<String>();
							tmpPhys.add(phy.getPhyCode());
							tmpPhyMap.put(phy.getPhyMinSeq(), tmpPhys);
						}
					}else{
						ArrayList<String> tmpPhys = new ArrayList<String>();
						tmpPhys.add(phy.getPhyCode());
						HashMap<String, ArrayList<String>> tmpPhyMap = new HashMap<String, ArrayList<String>>();
						tmpPhyMap.put(phy.getPhyMinSeq(), tmpPhys);
						phyMinSeqMap.put(phy.getPhyCodeMin(), tmpPhyMap);
					}
					
					phyMinInfo.put(phy.getPhyCodeMin(), phy.getPhyNameMin());
				}
				
				for(String key : phyMinSeqMap.keySet()){
					HashMap<String, ArrayList<String>> tmpPhyMap = phyMinSeqMap.get(key);
					ArrayList<String> maxLengthList = null;
					int maxLength = 0;
					for(String seq : tmpPhyMap.keySet()){
						if(tmpPhyMap.get(seq).size() > maxLength){
							maxLength = tmpPhyMap.get(seq).size();
							maxLengthList = tmpPhyMap.get(seq);
						}
					}
					if(maxLengthList == null) continue;
					
					boolean exists = true;
					
					for(String seq : tmpPhyMap.keySet()){
						for(String phy : maxLengthList){
							ArrayList<String> tmpPhys = tmpPhyMap.get(seq);
							if(!tmpPhys.contains(phy)){
								exists = false;
							}
						}
					}
					if(!exists){
						
						message = "理化索引号:" + pic + " 试样组：" + phyMinInfo.get(key) + "中的各试样组序号所维护的检验项数量、名称必须一致！";
						break;
					}
				}
				
				for (CraftOrdDesignStdPicEntity phy : phyAs) {
					// 验证取样要求
					if(!phy.getIscheck().equals("1") && !phy.getIscheck().equals("1") && !phy.getIscheck().equals("1")&&phy.getPhyCodeMax().equals("C0156"))
					{
						continue;
					}
					List<CraftOrdDesignStdPicSmpEntity> picSmpEntity = phyDaoImp.getPicSample(con, pic, phy.getPhyCodeMin(),
							phy.getItemCodeF(), phy.getItemCodeS(), phy.getItemCodeW(), design.getStdCode(), design.getSteelCode(),
							design.getProducCode());
					CraftOrdDesignStdPicSmpEntity smp = MscPhy.checkSmp(picSmpEntity, design.getSteelCode(), dimater,
							height);

					if (smp == null) {
						message = "理化索引号:" + pic + "序号:" + phy.getIndexSeq() + " 理化标准没有取样要求(钢级:" + design.getSteelName()
								+ ",外径:" + dimater + ",壁厚:" + height + ")";
						break;
					} else {
						if (smp.getSic().equals("")) {
							message = "理化索引号:" + pic + "序号:" + phy.getIndexSeq() + " 理化标准没有取样要求(钢级:" + design.getSteelName()
									+ ",外径:" + dimater + ",壁厚:" + height + ")";
							break;
						} else if (smp.getSmpCount() >= 2) {
							message = "理化索引号：" + pic + "序号:" + phy.getIndexSeq() + " 试样组:" + phy.getPhyNameMin()
									+ " 找到多条取样要求！";
							break;
						}else if(phyMinMap.containsKey(smp.getPhyCodeMin())){
							if(!phyMinMap.get(smp.getPhyCodeMin()).equals(smp.getRate())){
								message = "理化索引号：" + pic + "序号:" + phy.getIndexSeq() + " 相同试样组的取样频率必须一致！";
								break;
							}
						}else if(rateMap.containsKey(smp.getRate())){
							String[] batchs = rateMap.get(smp.getRate());
							if(!batchs[0].equals(smp.getBatchUnit()) || !batchs[1].equals(smp.getBatchCnt())){
								message = "理化索引号：" + pic + "序号:" + phy.getIndexSeq() + " 取样频率相同的试样组，所对应的分批单位、分批数量必须一致！";
								break;
							}
						}
						phyMinMap.put(smp.getPhyCodeMin(), smp.getRate());
						rateMap.put(smp.getRate(), new String[]{ smp.getBatchUnit(), smp.getBatchCnt() });
					}

					if(smp.getRate().equals("408601")){
						if(smp.getTopStove().equals("")){
							message = "理化索引号：" + pic + "序号:" + phy.getIndexSeq() + " 取样频率为"+ smp.getDescRate() +"，请维护头几炉";
							break;
						}
						if(smp.getLoopValue().equals("")){
							message = "理化索引号：" + pic + "序号:" + phy.getIndexSeq() + " 取样频率为"+ smp.getDescRate() +"，请维护循环值";
							break;
						}
					}

					smp.setDesginKey(designKey);
					smp.setProcessCode(processCode);
					smp.setProcessDesc(processDesc);
					smp.setMscPline(mscPline);
					smps.add(smp);
				}
			}

			if (!message.equals("")) {
				continue;
			}

			// 录入理化标准检验项
			// for (CraftOrdDesignStdPicSmpEntity smp : smps) {
			// phyDaoImp.savePicSample(con, smp);
			// }

			for (CraftOrdDesignStdPicEntity phy : phyAs) {
				if(!mscPline.equals("/")){
					phy.setMscPline(mscPline);
					phy.setProcessCode(processCode);
				}else{
					phy.setMscPline("Y");
					phy.setProcessCode("Y");
				}
				
				// 是否特殊要求(0否;1是 2内控)
				phy.setSpeclFl(flag);
				if (flag.equals("1")) {
					phy.setUseCode(useCode);
					phy.setUseDesc(useDesc);
				}
				phy.setDesginKey(designKey);
				// phyDaoImp.savePhy(con, phy);
			}
			
			if(flag.equals("2")){
				info = CheckImpact(phyAs, smps, pic, dimater, height, con);
				if(info[0].toString() != ""){
					message = "理化索引号:" + pic + "," + info[0].toString();
					continue;
				}
				
				info = CheckStretch(phyAs, smps, pic, dimater, height, con);
				if(info[0].toString() != ""){
					message = "理化索引号:" + pic + "," + info[0].toString();
					continue;
				}
			}
			
//			phyAs = CraftTool.fiterCommon(phyAs, new String[] {
//					"desginKey", "pic", "speclFl", "indexSeq",
//					"mscPline", "processCode" });
//			MscPhyDao mscPhyDao = new MscPhyDao();
//			for (CraftOrdDesignStdPicEntity phy : phyAs) {
//				mscPhyDao.savePhy(con, phy);
//			}
			// if (flag.equals("0")) {
			// phyDaoImp.updateDesignStd(con, pic, designKey);
			// }
			stdEntity.setFlag(true);
			stdEntity.setMessage("");
			stdEntity.setObjM(phyAs);
			stdEntity.setObjC(smps);
			stdEntity.setObjKey(pic);
			return stdEntity;
		}
		
		stdEntity.setFlag(false);
		stdEntity.setMessage(message);
		return stdEntity;
	}
	
	private Object[] CheckImpact(List<CraftOrdDesignStdPicEntity> phys,
			List<CraftOrdDesignStdPicSmpEntity> smps,
			String pic, String strDimater, String strHeight, Connection con) throws SQLException{
		Object[] results = new Object[3];
		boolean existR0002 = false;
		boolean existI009 = false;
		
		ComMscStdPicEntity mscStdPic = MscPhyDao.getMscStdPicByPk(pic, con);
		if(mscStdPic == null || mscStdPic.getImpactId().equals("")){
			results[0] = "";
			results[1] = phys;
			results[2] = smps;
			return results;
		}
		HashMap<String, String> mapSmps = new HashMap<String, String>();
		for(CraftOrdDesignStdPicSmpEntity smp : smps){
			String key = smp.getPhyCodeMin() + smp.getItemCodeF()
					+ smp.getItemCodeC() + smp.getItemCodeF();
			if(!mapSmps.containsKey(key)){
				mapSmps.put(key, smp.getStdCodeTest());
			}
		}
 		ComBaseImpactEntity impact = MscPhyDao.getBaseImpact(mscStdPic.getImpactId(), con);
		HashMap<String, String[]> phyGroupMap = new HashMap<String, String[]>();
		
//		boolean existsOther = false; //是否存在其他的冲击尺寸。
//		boolean existsI0009 = false;
//		for(CraftOrdDesignStdPicEntity phy : phys){
//			if(phy.getIscheck().equals("0")) continue;
//			if(phy.getPhyCodeMax().equals("R0002") && phy.getPhyName().contains("冲击功") &&
//					!phy.getItemCodeS().equals("I0009") && !phy.getItemCodeS().equals("")){
//				existsOther = true;
//			}
//			if(phy.getPhyCodeMax().equals("R0002") && phy.getPhyName().contains("冲击功") &&
//					(phy.getItemCodeS().equals("I0009") || phy.getItemCodeS().equals(""))){
//				existsI0009 = true;
//			}
//		}
//		if(existsOther){//存在其他的冲击尺寸就不套用模型。
//			results[0] = "使用了冲击模型，只能维护10mx10m的冲击尺寸！";
//			results[1] = phys;
//			results[2] = smps;
//			return results;
//		}
//		if(!existsI0009){
//			results[0] = "使用了冲击模型，但找不到10mx10m的冲击尺寸！";
//			results[1] = phys;
//			results[2] = smps;
//			return results;
//		}
		
		for(int i = phys.size() -1; i >= 0; i--){
			CraftOrdDesignStdPicEntity phy = phys.get(i);
			if(phy.getIscheck().equals("0")) continue;
			if(phy.getPhyCodeMax().equals("R0002") && !phy.getPhyCodeMin().equals("C0156")&&
					(phy.getItemCodeS().equals("I0009")))
			{
					if(strHeight.equals("")){
						results[0] = "该工序没有壁厚，无法进行冲击计算！";
						results[1] = phys;
						results[2] = smps;
						return results;
					}
					if((!phy.getStdmin().equals("") && !StringTool.isNumeric(phy.getStdmin()))
							|| (!phy.getStdmax().equals("") && !StringTool.isNumeric(phy.getStdmax()))
							|| (!phy.getStdmin2().equals("") && !StringTool.isNumeric(phy.getStdmin2()))
							|| (!phy.getStdmax2().equals("") && !StringTool.isNumeric(phy.getStdmax2()))){
						results[0] = "理化项目序号:" + phy.getIndexSeq() + " 系统不支持冲击功的标准值录入公式！";
						results[1] = phys;
						results[2] = smps;
						return results;
					}
					if(mscStdPic == null || mscStdPic.getImpactId().equals("")){
						results[0] = "向下选择的冲击试样模型数据维护不完整，请重新选择！";
						results[1] = phys;
						results[2] = smps;
						return results;
					}else{
						//获取试验标准
						if(phy.getIndexSeq().equals("65")){
							String s = "";
						}
						String splineStdCode = "";
						String key = phy.getPhyCodeMin() + phy.getItemCodeF()
								+ phy.getItemCodeS() + phy.getItemCodeF();
						if(mapSmps.containsKey(key)){
							splineStdCode = mapSmps.get(key);
						}
//						for(CraftOrdDesignStdPicSmpEntity smp : smps){
//							if(smp.getPhyCodeMin().equals(phy.getPhyCodeMin()) && smp.getItemCodeC().equals(phy.getItemCodeS())
//									&& smp.getItemCodeF().equals(phy.getItemCodeF()) && smp.getItemCodeW().equals(phy.getItemCodeW())){
//								splineStdCode = smp.getStdCodeTest();
//								break;
//							}
//						}
						boolean existsItemCode = false;
						double height = Double.parseDouble(strHeight);
						double dimater = Double.parseDouble(strDimater);
						double calculateValue1 = 0;
						if(phy.getItemCodeF().equals("I0004")){
							calculateValue1 = Math.sqrt(dimater * dimater/4 - 55 * 55/4) - (dimater - 2*height)/2 - 1;
						}else{
							calculateValue1 = Math.sqrt(dimater * dimater/4 - 10 * 10/4) - (dimater - 2*height)/2 - 1;
						}
						calculateValue1 = RoundGBT81702008.round3_2(calculateValue1, 2);
						
						SqlBuilder b = new SqlBuilder("MscPhyDao.queryMscStdName");
						b.Fill( new String[]{ phy.getDesginKey() });
						DesignMscEntity designMscEntity = EntityTool.getEntityBySqlNoClsCon(
								b.toString(), con, DesignMscEntity.class);
						String stdName=designMscEntity.getStdName();
						if(designMscEntity.getStdName().contains(","))
							stdName=designMscEntity.getStdName().split(",")[0];
						else if(designMscEntity.getStdName().contains("，"))
							stdName=designMscEntity.getStdName().split("，")[0];
						if(stdName.contains("-20"))
							stdName=stdName.substring(0, stdName.lastIndexOf("-20"));
						String count=MscPhyDao.checkStdIsExist(con, phy.getItemCodeF(), stdName);
						ComBaseSplineMinSizeEntity splineMinSize=new ComBaseSplineMinSizeEntity();
						if(!"0".equals(count)){
							splineMinSize = MscPhyDao.getSplineMinSizeByName(
									con, phy.getItemCodeF(), stdName, String.valueOf(calculateValue1),
									String.valueOf(dimater), String.valueOf(height));
						}
						else{
							splineMinSize = MscPhyDao.getSplineMinSize(
									con, phy.getItemCodeF(), splineStdCode, String.valueOf(calculateValue1),
									String.valueOf(dimater), String.valueOf(height));
						}
						if(splineMinSize != null){
							double stdMin = -1;
							double stdMax = -1;
							double stdmin2 = -1;
							double stdmax2 = -1;
							double addstdMin = -1;
							double addstdMax = -1;
							
							//只有检验项包含冲击功的才需要用递减系数计算。
							if(phy.getPhyName().contains("冲击功")){
								ComBaseImpactCEntity baseImpactC = MscPhyDao.getBaseImpactC(
										splineMinSize.getItemCodeS(), mscStdPic.getImpactId(), con);
								if(baseImpactC == null){
									results[0] = "找不到冲击尺寸"+ splineMinSize.getItemNameS() +"对应的递减系数，请重新选择冲击方案！";
									results[1] = phys;
									results[2] = smps;
									return results;
								}
								if(!StringTool.isNumeric(baseImpactC.getDeclineRatio())){
									baseImpactC.setDeclineRatio("1");
								}
								double rate = Double.parseDouble(baseImpactC.getDeclineRatio());
								if(!phy.getStdmin().equals("")){
									stdMin = Double.parseDouble(phy.getStdmin()) * rate;
									stdMin = Math.round(stdMin);
								}
								if(!phy.getStdmax().equals("")){
									stdMax = Double.parseDouble(phy.getStdmax()) * rate;
									stdMax = Math.round(stdMax);
								}
								if(!phy.getStdmin2().equals("")){
									stdmin2 = Double.parseDouble(phy.getStdmin2()) * rate;
									stdmin2 = Math.round(stdmin2);
								}
								if(!phy.getStdmax2().equals("")){
									stdmax2 = Double.parseDouble(phy.getStdmax2()) * rate;
									stdmax2 = Math.round(stdmax2);
								}
								if(!phy.getAddstdMin().equals("")){
									if(StringTool.isNumeric(phy.getAddstdMin())){
										addstdMin = Double.parseDouble(phy.getAddstdMin()) * rate;
										addstdMin = Math.ceil(addstdMin);
									}
								}
								if(!phy.getAddstdMax().equals("")){
									if(StringTool.isNumeric(phy.getAddstdMax())){
										addstdMax = Double.parseDouble(phy.getAddstdMax()) * rate;
										addstdMax = Math.ceil(addstdMax);
									}
								}
							}
							
							String minImpact = impact.getMinImpact();
							if(minImpact.equals("")){
								minImpact = "0";
							}
							
							ComBaseImpactTempCEntity impactTemp = MscPhyDao.getComBaseImpactTempC(
									con, mscStdPic.getImpactId(), splineMinSize.getItemCodeS(), String.valueOf(height));
							String itemCodeW = phy.getItemCodeW();
							String itemNameW = phy.getItemNameW();
							if(impactTemp != null){
								String[] wResults = CalculateTemp(con, itemCodeW, itemNameW, impactTemp);
								if(!wResults[0].equals("")){
									results[0] = wResults[0];
									results[1] = phys;
									results[2] = smps;
									return results;
								}else{
									itemCodeW = wResults[1];
									itemNameW = wResults[2];
								}
							}
							//if(!phy.getStdmin().equals("")){
							if(((stdMin == -1 || stdMin >= Double.parseDouble(minImpact))
								&& (stdMax == -1 || stdMax >= Double.parseDouble(minImpact))
								&& (stdmin2 == -1 || stdmin2 >= Double.parseDouble(minImpact))
								&& (stdmax2 == -1 || stdmax2 >= Double.parseDouble(minImpact))
								&& phy.getItemCodeF().equals("I0004")
								) || phy.getItemCodeF().equals("I0001")){
								existsItemCode = true;
								
								for(CraftOrdDesignStdPicSmpEntity smp : smps){
									if(smp.getPhyCodeMin().equals(phy.getPhyCodeMin())
											&& smp.getItemCodeF().equals(phy.getItemCodeF())
											&& smp.getItemCodeC().equals(phy.getItemCodeS())){
										//smp.setItemCodeF(phy.getItemCodeF());
										smp.setItemCodeC(splineMinSize.getItemCodeS());
										smp.setItemCodeW(itemCodeW);
//										smp.setSplineItemCodeF(phy.getItemCodeF());
//										smp.setSplineItemNameF(phy.getItemNameF());
										smp.setSplineItemCodeS(splineMinSize.getItemCodeS());
										smp.setSplineItemNameS(splineMinSize.getItemNameS());
										String sampleKey = GetSampleKey(phy.getPhyCodeMin(), phy.getItemCodeF(),
												phy.getItemCodeW(), splineMinSize.getItemCodeS());
										smp.setSampleKey(sampleKey);
										String sampleKeyDesc = GetSampleKey(phy.getPhyNameMin(), phy.getItemNameF(),
												phy.getItemNameW(), splineMinSize.getItemNameS());
										smp.setSampleKeyDesc(sampleKeyDesc);
										break;
									}
								}
								
								if(stdMin != -1){
									phy.setStdmin(String.valueOf(stdMin));
								}
								if(stdMax != -1){
									phy.setStdmax(String.valueOf(stdMax));
								}
								if(stdmin2 != -1){
									phy.setStdmin2(String.valueOf(stdmin2));
								}
								if(stdmax2 != -1){
									phy.setStdmax2(String.valueOf(stdmax2));
								}
								if(addstdMin != -1){
									phy.setAddstdMin(String.valueOf(addstdMin));
								}
								if(addstdMax != -1){
									phy.setAddstdMax(String.valueOf(addstdMax));
								}
//								phy.setItemCodeF(phy.getItemCodeF());
//								phy.setItemNameF(phy.getItemNameF());
								phy.setItemCodeW(itemCodeW);
								phy.setItemNameW(itemNameW);
								phy.setItemCodeS(splineMinSize.getItemCodeS());
								phy.setItemNameS(splineMinSize.getItemNameS());
								if(!phy.getGroupSeq().equals("")){
									phyGroupMap.put(phy.getGroupSeq(), new String[]{"true", phy.getIndexSeq()});
								}
							}
						}						
							
						if(!existsItemCode){
							if(!phy.getGroupSeq().equals("")){
								if(!phyGroupMap.containsKey(phy.getGroupSeq()) || phyGroupMap.get(phy.getGroupSeq())[0].equals("false")){
									phyGroupMap.put(phy.getGroupSeq(), new String[]{"false", phy.getIndexSeq()});
								}
								phys.remove(i);
								for(int j = smps.size() - 1; j >= 0; j--){
									CraftOrdDesignStdPicSmpEntity smp = smps.get(j);
									if(smp.getPhyCodeMin().equals(phy.getPhyCodeMin())
											&& smp.getItemCodeF().equals(phy.getItemCodeF())
											&& smp.getItemCodeC().equals(phy.getItemCodeS())){
										smps.remove(j);
										break;
									}
								}
								continue;
							}
							results[0] = "序号为"+ phy.getIndexSeq() +"的夏比冲击试验的方向与尺寸转换失败！";
							results[1] = phys;
							results[2] = smps;
							return results;
						}
					}
			}
		}
		
		for(String key : phyGroupMap.keySet()){
			if(phyGroupMap.get(key)[0].equals("false")){
				results[0] = "序号为"+ phyGroupMap.get(key)[1] +"的夏比冲击试验的方向与尺寸转换失败！";
				results[1] = phys;
				results[2] = smps;
				return results;
			}
		}
		
		//处理分组情况
		HashMap<String, CraftOrdDesignStdPicEntity> phyMap = new HashMap<String, CraftOrdDesignStdPicEntity>();
		for(int i = phys.size() - 1; i >= 0; i--){
			if(phys.get(i).getGroupSeq().equals("")) continue;
			if(phys.get(i).getIscheck().equals("0")) continue;
			if(!phys.get(i).getPhyCodeMax().equals("R0002")) continue;
			if(phyMap.containsKey(phys.get(i).getGroupSeq())){
				//优先取横向
				CraftOrdDesignStdPicEntity phy = null;
				if(phyMap.get(phys.get(i).getGroupSeq()).getItemCodeF().equals("I0001")
						&& phys.get(i).getItemCodeF().equals("I0004")){
					phy = phyMap.get(phys.get(i).getGroupSeq());
					
				}else if(phyMap.get(phys.get(i).getGroupSeq()).getItemCodeF().equals("I0004")
						&& phys.get(i).getItemCodeF().equals("I0001")){
					phy = phys.get(i);
				}
				else{
					continue;
				}
				
				//去掉取样要求
				for(int j = smps.size() - 1; j >= 0; j--){
					CraftOrdDesignStdPicSmpEntity smp = smps.get(j);
					if(smp.getPhyCodeMin().equals(phy.getPhyCodeMin())
							&& smp.getItemCodeF().equals(phy.getItemCodeF())
							&& smp.getItemCodeC().equals(phy.getItemCodeS())){
						smps.remove(j);
						break;
					}
				}
				
				phys.remove(phy);
				phyMap.remove(phys.get(i).getGroupSeq());
				phyMap.put(phys.get(i).getGroupSeq(), phys.get(i));
				
			}else{
				phyMap.put(phys.get(i).getGroupSeq(), phys.get(i));
			}
		}
		
		results[0] = "";
		results[1] = phys;
		results[2] = smps;
		return results;
	}
	
	/**
	 * 计算递减温度
	 * @return
	 * @throws SQLException 
	 */
	private String[] CalculateTemp(Connection con, String itemCodeW, String itemNameW,
			ComBaseImpactTempCEntity impactTemp) throws SQLException{
		String[] results = new String[]{ "", itemCodeW, itemNameW };
		if(impactTemp.getTempReduce().equals("") || impactTemp.getTempReduce().equals("0")){
			return results;
		}else{
			String tmpW = itemNameW.replaceAll("℃", "").replaceFirst("°", "");
			if(StringTool.isNumeric(tmpW)){
				int dTmpW = Integer.parseInt(tmpW);
				int dReduce = Integer.parseInt(impactTemp.getTempReduce());
				int dW = dTmpW - Math.abs(dReduce);
				String strW = String.valueOf(dW);
				ComBasePhyItemEntity phyItem = MscPhyDao.getPhyItemW(con, strW + "℃");
				if(phyItem == null){
					ComBasePhyItemEntity phyItem2 = MscPhyDao.getPhyItemW(con, strW + "°");
					if(phyItem2 == null){
						results[0] = "在温度基础表中找不到递减后的温度[" + strW + "℃]";
					}else{
						results[1] = phyItem2.getItemCode();
						results[2] = phyItem2.getItemName();
					}
				}else{
					results[1] = phyItem.getItemCode();
					results[2] = phyItem.getItemName();
				}
			}
		}
		
		return results;
	}
	
	private String GetSampleKey(String phyMin, String itemF, String itemW, String itemS){
		String sampleKey = phyMin;
        if (sampleKey.equals(""))
        {
        	sampleKey = itemF;
        }
        else
        {
            if (!itemF.equals(""))
            	sampleKey = sampleKey + "_" + itemF;
        }
        if (sampleKey.equals(""))
        {
        	sampleKey = itemW;
        }
        else
        {
            if (!itemW.equals(""))
            	sampleKey = sampleKey + "_" + itemW;
        }
        if (sampleKey.equals(""))
        {
        	sampleKey = itemS;
        }
        else
        {
            if (!itemS.equals(""))
            	sampleKey = sampleKey + "_" + itemS;
        }
        
        return sampleKey;
	}
	
	private Object[] CheckStretch(List<CraftOrdDesignStdPicEntity> phys,
			List<CraftOrdDesignStdPicSmpEntity> smps,
			String pic, String strDimater, String strHeight, Connection con){
		Object[] results = new Object[3];
		ComMscStdPicEntity mscStdPic = MscPhyDao.getMscStdPicByPk(pic, con);
		
		HashMap<String, List<CraftOrdDesignStdPicEntity>> groups = new HashMap<String, List<CraftOrdDesignStdPicEntity>>();
		boolean isC001 = false;
		boolean isC016 = false;
		boolean isI0004 = false;
		for(CraftOrdDesignStdPicEntity phy : phys){
			if(phy.getPhyCodeMin().equals("C0001")){
				isC001 = true;
				if(phy.getItemCodeF().equals("I0004")){
					isI0004 = true;
				}
			}else if(phy.getPhyCodeMin().equals("C0016")){
				isC016 = true;
				if(phy.getItemCodeF().equals("I0004")){
					isI0004 = true;
				}
			}
			if(phy.getGroupSeq().equals("")){
				results = CheckStretch(phy, strDimater, strHeight, mscStdPic.getStretchPlan());
				if(!results[0].equals("")){
					results[1] = phys;
					results[2] = smps;
					return results;
				}
			}else{
				if(groups.containsKey(phy.getGroupSeq())){
					List<CraftOrdDesignStdPicEntity> groupPhys = groups.get(phy.getGroupSeq());
					groupPhys.add(phy);
				}else{
					List<CraftOrdDesignStdPicEntity> groupPhys = new ArrayList<CraftOrdDesignStdPicEntity>();
					groupPhys.add(phy);
					groups.put(phy.getGroupSeq(), groupPhys);
				}
			}
		}
		
		if(!isC001 && !isC016){
			if(mscStdPic.getStretchPlan().equals("1")){
				results[0] = "拉伸方案为：横拉展平,但没有找到试样组为" + "室温拉伸或者高温拉伸的记录！";
				return results;
			}else if(mscStdPic.getStretchPlan().equals("2")){
				results[0] = "拉伸方案为：公式,但没有找到试样组为" + "室温拉伸或者高温拉伸的记录！";
				return results;
			}
		}
		if((mscStdPic.getStretchPlan().equals("1") || mscStdPic.getStretchPlan().equals("2")) && !isI0004){
			if(mscStdPic.getStretchPlan().equals("1")){
				results[0] = "拉伸方案为：横拉展平，系统必须存在一条横向拉伸记录";
				return results;
			}else if(mscStdPic.getStretchPlan().equals("2")){
				results[0] = "拉伸方案为：公式，系统必须存在一条横向拉伸记录";
				return results;
			}
		}
		
		for(String key : groups.keySet()){
			List<CraftOrdDesignStdPicEntity> groupPhys = groups.get(key);
			CraftOrdDesignStdPicEntity phyTmp = null;
			for(CraftOrdDesignStdPicEntity phy : groupPhys){
					results = CheckStretch(phy, strDimater, strHeight, mscStdPic.getStretchPlan());
					phys.remove(phy);
				if(!results[0].equals("")){
					//去掉取样要求
					RemoveSmp(smps, phy);
					continue;
				}
				RemoveSmp(smps, phyTmp);
				phyTmp = phy;
				if(phy.getPhyCodeMin().equals("C0001") || phy.getPhyCodeMin().equals("C0016")){
					if(phy.getItemCodeF().equals("I0004")){
						break;
					}
				}
			}
			if(phyTmp == null){
				results[1] = phys;
				results[2] = smps;
				return results;
			}else{
				phys.add(phyTmp);
			}
		}
		
		results[0] = "";
		results[1] = phys;
		results[2] = smps;
		return results;
	}
	
	private void RemoveSmp(List<CraftOrdDesignStdPicSmpEntity> smps, CraftOrdDesignStdPicEntity phy){
		//去掉取样要求
		if(phy == null) return;
		for(int j = smps.size() - 1; j >= 0; j--){
			CraftOrdDesignStdPicSmpEntity smp = smps.get(j);
			if(smp.getPhyCodeMin().equals(phy.getPhyCodeMin())
					&& smp.getItemCodeF().equals(phy.getItemCodeF())
					&& smp.getItemCodeC().equals(phy.getItemCodeS())){
				smps.remove(j);
				break;
			}
		}
	}
	
	private Object[] CheckStretch(CraftOrdDesignStdPicEntity phy, String strDimater, String strHeight,
			String stretchPlan){
		Object[] results = new Object[3];
		if(stretchPlan.equals("2")){
			if(phy.getPhyCodeMin().equals("C0001") || phy.getPhyCodeMin().equals("C0016")){
				if(phy.getItemCodeF().equals("I0004")){
					double dimater = Double.parseDouble(strDimater);
					//strHeight = "100";
					double height = Double.parseDouble(strHeight);
					double value = height - dimater/2 + Math.sqrt(dimater*dimater/4 - 100*100/4) - 1;
					if(value < 10){
						results[0] = "拉伸方案为：公式，序号" + phy.getIndexSeq() + "没有通过公式验证！";
						return results;
					}
				}
			}
		}
		
		results[0] = "";
		return results;
	}

	/**
	 * 过滤理化标准(钢管)
	 * 
	 * @param phys
	 *            理化标准类
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<DesignStdPicEntity>
	 */
	public static List<CraftOrdDesignStdPicEntity> fiterPhyA(List<CraftOrdDesignStdPicEntity> phys, String dimater,
			String height, String DHProportion, String HDProportion, String innerProportion) {
		if (phys == null || phys.size() == 0) {
			return null;
		}
		// 有限制规格的理化标准
		List<CraftOrdDesignStdPicEntity> phyAs = new ArrayList<CraftOrdDesignStdPicEntity>();
		for (int i = 0; i < phys.size(); i++) {
			CraftOrdDesignStdPicEntity phy = phys.get(i);
			if (phy.getSizeCode().equals("") && phy.getSizeCode2().equals("")
					&& phy.getFormula().equals("")) {
				phyAs.add(phys.get(i));
				continue;
			}
			// 外径 :400301 壁厚:400302 径壁比:400303 壁径比 400304 内径 400305 长度 400306
			if (phy.getSizeCode2().equals("400301")) {
				if ((!CraftTool.checkStr(dimater, phy.getSizeMinSign2(), phy.getSizeMin2()))
						|| (!CraftTool.checkStr(dimater, phy.getSizeMaxSign2(), phy.getSizeMax2()))) {
					continue;
				}
			}
			if (phy.getSizeCode2().equals("400302")) {
				if ((!CraftTool.checkStr(height, phy.getSizeMinSign2(), phy.getSizeMin2()))
						|| (!CraftTool.checkStr(height, phy.getSizeMaxSign2(), phy.getSizeMax2()))) {
					continue;
				}
			}
			if (phy.getSizeCode2().equals("400303")) {
				if ((!CraftTool.checkStr(DHProportion, phy.getSizeMinSign2(), phy.getSizeMin2()))
						|| (!CraftTool.checkStr(DHProportion, phy.getSizeMaxSign2(), phy.getSizeMax2()))) {
					continue;
				}
			}
			if (phy.getSizeCode2().equals("400304")) {
				if ((!CraftTool.checkStr(HDProportion, phy.getSizeMinSign2(), phy.getSizeMin2()))
						|| (!CraftTool.checkStr(HDProportion, phy.getSizeMaxSign2(), phy.getSizeMax2()))) {
					continue;
				}
			}
			if (phy.getSizeCode2().equals("400305")) {
				if ((!CraftTool.checkStr(innerProportion, phy.getSizeMinSign2(), phy.getSizeMin2()))
						|| (!CraftTool.checkStr(innerProportion, phy.getSizeMaxSign2(), phy.getSizeMax2()))) {
					continue;
				}
			}

			if (phy.getSizeCode().equals("400301")) {
				if ((!CraftTool.checkStr(dimater, phy.getSizeMinSign(), phy.getSizeMin()))
						|| (!CraftTool.checkStr(dimater, phy.getSizeMaxSign(), phy.getSizeMax()))) {
					continue;
				}
			}
			if (phy.getSizeCode().equals("400302")) {
				if ((!CraftTool.checkStr(height, phy.getSizeMinSign(), phy.getSizeMin()))
						|| (!CraftTool.checkStr(height, phy.getSizeMaxSign(), phy.getSizeMax()))) {
					continue;
				}
			}
			if (phy.getSizeCode().equals("400303")) {
				if ((!CraftTool.checkStr(DHProportion, phy.getSizeMinSign(), phy.getSizeMin()))
						|| (!CraftTool.checkStr(DHProportion, phy.getSizeMaxSign(), phy.getSizeMax()))) {
					continue;
				}
			}
			if (phy.getSizeCode().equals("400304")) {
				if ((!CraftTool.checkStr(HDProportion, phy.getSizeMinSign(), phy.getSizeMin()))
						|| (!CraftTool.checkStr(HDProportion, phy.getSizeMaxSign(), phy.getSizeMax()))) {
					continue;
				}
			}
			if (phy.getSizeCode().equals("400305")) {
				if ((!CraftTool.checkStr(innerProportion, phy.getSizeMinSign(), phy.getSizeMin()))
						|| (!CraftTool.checkStr(innerProportion, phy.getSizeMaxSign2(), phy.getSizeMax()))) {
					continue;
				}
			}
			
			//公式验证。
			String formula = phy.getFormula();
			if(!formula.equals("")){
				formula = formula.replaceAll("t", height).replaceAll("D", dimater);
				if(StringTool.isDouble(JudgeHelper.eval(formula))){
					double dFormula = Double.parseDouble(JudgeHelper.eval(formula));
					BigDecimal d = new BigDecimal(dFormula);
					dFormula = d.setScale(2, RoundingMode.HALF_UP).doubleValue();//4舍5入
					formula = String.valueOf(dFormula);
				}else{
					formula = "-1";
				}
			}
			
			if(!phy.getMinFSign().equals("")){
				if(!CraftTool.checkStr(formula, phy.getMinFSign(), phy.getMinF())){
					continue;
				}
			}
			if(!phy.getMaxFSign().equals("")){
				if(!CraftTool.checkStr(formula, phy.getMaxFSign(), phy.getMaxF())){
					continue;
				}
			}
			
			phyAs.add(phy);
		}

		return phyAs;
	}

	/**
	 * 过滤钢锭或者其它规格
	 * @param specCode
	 * @return
	 */
	public static List<CraftOrdDesignStdPicEntity> fiterPhyByCAndD(List<CraftOrdDesignStdPicEntity> phys,
			String specCode) {
		List<CraftOrdDesignStdPicEntity> list = new ArrayList<CraftOrdDesignStdPicEntity>();
		for (CraftOrdDesignStdPicEntity phy : phys) {
			if (phy.getSpecJg().equals("") || phy.getSpecJg().equals(specCode)) {
				list.add(phy);
			}
		}
		return list;
	}

	/**
	 * 过滤理化标准（管坯）
	 * 
	 * @param phys
	 *            理化标准类
	 * @param dimater
	 *            外径
	 *            壁厚
	 * @return List<DesignStdPicEntity>
	 */
	public static List<CraftOrdDesignStdPicEntity> fiterPhyB(List<CraftOrdDesignStdPicEntity> phys, String dimater) {
		if (phys == null || phys.size() == 0) {
			return null;
		}
		List<CraftOrdDesignStdPicEntity> phyAs = new ArrayList<CraftOrdDesignStdPicEntity>();

		for (int i = 0; i < phys.size(); i++) {
			CraftOrdDesignStdPicEntity phy = phys.get(i);
			if (phy.getSizeCode().equals("") && phy.getSizeCode2().equals("")) {
				phyAs.add(phys.get(i));
				continue;
			}
			// 如果限制规格不为空且不为外径
			if (!phy.getSizeCode().equals("") && !phy.getSizeCode().equals("400301")) {
				continue;
			}
			if (!phy.getSizeCode2().equals("") && !phy.getSizeCode2().equals("400301")) {
				continue;
			}
			// 外径 :400301 壁厚:400302 径壁比:400303 壁径比 400304 内径 400305 长度 400306
			if (phy.getSizeCode2().equals("400301")) {
				if ((!CraftTool.checkStr(dimater, phy.getSizeMinSign2(), phy.getSizeMin2()))
						|| (!CraftTool.checkStr(dimater, phy.getSizeMaxSign2(), phy.getSizeMax2()))) {
					continue;
				}
			}

			if (phy.getSizeCode().equals("400301")) {
				if ((!CraftTool.checkStr(dimater, phy.getSizeMinSign(), phy.getSizeMin()))
						|| (!CraftTool.checkStr(dimater, phy.getSizeMaxSign(), phy.getSizeMax()))) {
					continue;
				}
			}
			phyAs.add(phys.get(i));
		}

		return phyAs;
	}

	/**
	 * 验证理化标准
	 * 
	 * @param phys
	 * @param phyAlls
	 * @param specStyle
	 * @param dimater
	 * @param height
	 * @param specName
	 * @return
	 */
	public static Object[] fiterPhy(List<CraftOrdDesignStdPicEntity> phys, List<CraftOrdDesignStdPicEntity> phyAlls,
			String specStyle, String dimater, String height, String specName) {
		Object[] arr = new Object[2];
		if (phys == null || phys.size() == 0 || phyAlls == null || phyAlls.size() == 0) {
			arr[0] = null;
			if (specStyle.equals("A") || specStyle.equals("B")) {
				arr[1] = "满足外径(" + dimater + ")、壁厚(" + height + ")等约束理化标准不存在";
			} else {
				arr[1] = "满足规格(" + specName + ")约束理化标准不存在";
			}
			return arr;
		}
		// 有限制规格
		List<CraftOrdDesignStdPicEntity> phyYs = new ArrayList<CraftOrdDesignStdPicEntity>();
		List<String> phyYKeys = new ArrayList<String>();
		// 没有限制规格
		List<CraftOrdDesignStdPicEntity> phyNs = new ArrayList<CraftOrdDesignStdPicEntity>();
		List<String> phyNKeys = new ArrayList<String>();
		for (CraftOrdDesignStdPicEntity phy : phys) {
			// 理化主键代码组合
			String keyCode = phy.getPhyCodeMax() + "," + phy.getPhyCodeMin() + "," + phy.getPhyCode() + ","
					+ phy.getItemCodeF() + "," + phy.getItemCodeS() + "," + phy.getItemCodeW();
			// 理化主键名称组合
			String keyName = phy.getPhyNameMax() + "," + phy.getPhyNameMin() + "," + phy.getPhyName() + ","
					+ phy.getItemNameF() + "," + phy.getItemNameS() + "," + phy.getItemNameW();

			/**
			 * 钢管或者管坯
			 */
			if (specStyle.equals("A") || specStyle.equals("B")) {
				if (phy.getSizeCode().equals("") && phy.getSizeCode2().equals("")) {
					if (phyNKeys.contains(keyCode)) {
						arr[0] = null;
						arr[1] = "理化检验项目(" + keyName + ")存在多条理化标准,且没有任何限制规格!";
						return arr;
					}
					phyNKeys.add(keyCode);
					phyNs.add(phy);
				} else {
					if (phyYKeys.contains(keyCode)) {
						arr[0] = null;
						arr[1] = "理化检验项目(" + keyName + ")存在多条满足外径(" + dimater + ")、壁厚(" + height + ")的理化标准!";
						return arr;
					}
					phyYKeys.add(keyCode);
					phyYs.add(phy);
				}
			} else {
				/**
				 * 钢锭或者接箍规格
				 */
				if (phy.getSpecJg().equals("")) {
					if (phyNKeys.contains(keyCode)) {
						arr[0] = null;
						arr[1] = "理化检验项目(" + keyName + ")存在多条理化标准,且没有任何限制规格!";
						return arr;
					}
					phyNKeys.add(keyCode);
					phyNs.add(phy);
				} else {
					if (phyYKeys.contains(keyCode)) {
						arr[0] = null;
						arr[1] = "理化检验项目(" + keyName + ")存在多条满足规格(" + specName + ")的理化标准!";
						return arr;
					}
					phyYKeys.add(keyCode);
					phyYs.add(phy);
				}
			}
		}
		for (int i = 0; i < phyNs.size(); i++) {
			CraftOrdDesignStdPicEntity phy = phyNs.get(i);
			// 理化主键代码组合
			String keyCode = phy.getPhyCodeMax() + "," + phy.getPhyCodeMin() + "," + phy.getPhyCode() + ","
					+ phy.getItemCodeF() + "," + phy.getItemCodeS() + "," + phy.getItemCodeW();
			// 新增 限制规格理化集合中没有的理化标准
			if (!phyYKeys.contains(keyCode)) {
				phyYs.add(phy);
			}
		}
		
//		// 验证理化标准的检验项目
//		Object[] objPhyCodes = MscPhy.getPhySizes(phys);
//		List<String> phyCodeKeys = (List<String>)objPhyCodes[0];
//		ArrayList<String> phyCodeGroupSeqs = (ArrayList<String>)objPhyCodes[1];
//		Object[] objPhyAllCodes = MscPhy.getPhySizes(phyAlls);
//		List<String> phyAllCodeKeys = (List<String>)objPhyAllCodes[0];
//		ArrayList<String> phyAllGroupSeqs = (ArrayList<String>)objPhyAllCodes[1];
//		
//		// 如果检验项目数不相同
//		if (phyCodeKeys.size() != phyAllCodeKeys.size()) {
//			for (int i = 0; i < phyAllCodeKeys.size(); i ++) {
//				String key = phyAllCodeKeys.get(i);
//				if (!phyCodeKeys.contains(key) &&
//						(phyAllGroupSeqs.get(i).equals("") || !phyCodeGroupSeqs.contains(phyAllGroupSeqs.get(i)))) {
//					arr[0] = null;
//					if (specStyle.equals("A") || specStyle.equals("B")) {
//						arr[1] = "理化标准维护理化项目:" + phyAllCodeKeys.size() + "项," + "但满足约束:" + phyCodeKeys.size()
//								+ "项,理化检验项(" + key + ")不满足外径(" + dimater + ")、壁厚(" + height + ")等约束";
//					} else {
//						arr[1] = "理化标准维护理化项目:" + phyAllCodeKeys.size() + "项," + "但满足约束:" + phyCodeKeys.size()
//								+ "项,理化检验项(" + key + ")不满足规格(" + specName + ")约束";
//					}
//					return arr;
//				}
//			}
//		} else {
//			for (int i = 0; i < phyCodeKeys.size(); i++) {
//				if (!phyCodeKeys.get(i).equals(phyAllCodeKeys.get(i)) &&
//						(phyAllGroupSeqs.get(i).equals("") || !phyCodeGroupSeqs.contains(phyAllGroupSeqs.get(i)))) {
//					arr[0] = null;
//					if (specStyle.equals("A") || specStyle.equals("B")) {
//						arr[1] = "理化标准维护理化项目:" + phyAllCodeKeys.size() + "项," + "但满足约束:" + phyCodeKeys.size() + "项,"
//								+ phyCodeKeys.get(i) + "不满足外径(" + dimater + ")、壁厚(" + height + ")等约束";
//					} else {
//						arr[1] = "理化标准维护理化项目:" + phyAllCodeKeys.size() + "项," + "但满足约束:" + phyCodeKeys.size() + "项,"
//								+ phyCodeKeys.get(i) + "不满足规格(" + specName + ")约束";
//					}
//					return arr;
//				}
//			}
//		}
		arr[0] = phyYs;
		arr[1] = "";
		return arr;
	}

	/**
	 * 查询理化标准维护项目
	 * 
	 * @param phys
	 * @return
	 */
	public static Object[] getPhySizes(List<CraftOrdDesignStdPicEntity> phys) {
		if (phys == null || phys.size() == 0) {
			return null;
		}
		List<String> phyKeys = new ArrayList<String>();
		ArrayList<String> groupSeqs = new ArrayList<String>();
		for (CraftOrdDesignStdPicEntity phy : phys) {
			// 理化主键名称组合
			String keyName = phy.getPhyNameMax() + "," + phy.getPhyNameMin() + "," + phy.getPhyName() + ","
					+ phy.getItemNameF() + "," + phy.getItemNameS() + "," + phy.getItemNameW();
			if (!phyKeys.contains(keyName) && (phy.getGroupSeq().equals("") 
					|| !groupSeqs.contains(phy.getGroupSeq()))) {
				phyKeys.add(keyName);
				groupSeqs.add(phy.getGroupSeq());
			}
		}
		// 升序排序
		//Collections.sort(phyKeys);
		return new Object[]{ phyKeys, groupSeqs};
	}

	/**
	 * 过滤取样要求
	 * 
	 * @param smpEntitys
	 * @param dimater
	 * @param height
	 * @return
	 */
	public static CraftOrdDesignStdPicSmpEntity checkSmp(List<CraftOrdDesignStdPicSmpEntity> smpEntitys,
			String steelCode, String dimater, String height) {

		CraftOrdDesignStdPicSmpEntity returnData = null;
		int smpCnt = 0;
		if (smpEntitys != null) {
			for (CraftOrdDesignStdPicSmpEntity smp : smpEntitys) {
				if (CraftTool.checkStr(dimater, smp.getMinDSign(), smp.getMinD())
						&& CraftTool.checkStr(dimater, smp.getMaxDSign(), smp.getMaxD())
						&& CraftTool.checkStr(height, smp.getMinHSign(), smp.getMinH())
						&& CraftTool.checkStr(height, smp.getMaxHSign(), smp.getMaxH())) {
					returnData = smp;
					smpCnt++;
				} else if (smp.getMinD().equals("") && smp.getMaxD().equals("")
						&& smp.getMinH().equals("") && smp.getMaxH().equals("")) {
					returnData = smp;
					smpCnt++;
				}
			}
		}

		if (returnData != null) {
			returnData.setSmpCount(smpCnt);
			return returnData;
		} else {
			return null;
		}
	}

	/**
	 * 过滤试样要求
	 *
	 * @param dimater
	 * @param height
	 * @return
	 */
	public static CraftOrdDesignStdPicSmpEntity checkSpline(List<CraftOrdDesignStdPicSmpEntity> splineEntitys,
			String steelCode, String dimater, String height) {

		CraftOrdDesignStdPicSmpEntity returnData = null;

		ArrayList<String> eics = new ArrayList<String>();
		int splineCnt = 0;
		if (splineEntitys != null) {
			for (CraftOrdDesignStdPicSmpEntity splineEntity : splineEntitys) {
				String value = "";
				if(!splineEntity.getFormula().equals("")){
					value = splineEntity.getFormula().replaceAll("t", height).replaceAll("D", dimater);
					value = JudgeHelper.eval(value).toString();
				}
				if (CraftTool.checkStr(dimater, splineEntity.getMinDSign(), splineEntity.getMinD())
						&& CraftTool.checkStr(dimater, splineEntity.getMaxDSign(), splineEntity.getMaxD())
						&& CraftTool.checkStr(height, splineEntity.getMinHSign(), splineEntity.getMinH())
						&& CraftTool.checkStr(height, splineEntity.getMaxHSign(), splineEntity.getMaxH())
						&& CraftTool.checkStr(value, splineEntity.getMinFSign(), splineEntity.getMinF())
						&& CraftTool.checkStr(value, splineEntity.getMaxFSign(), splineEntity.getMaxF())) {

					if (returnData != null) {
						returnData.setLength(splineEntity.getLength());
						returnData.setWidth(splineEntity.getWidth());
						returnData.setSampleStyle(splineEntity.getSampleStyle());
						returnData.setEic(splineEntity.getEic());
						returnData.setPhyDir(splineEntity.getPhyDir());
						returnData.setSizeDesc(splineEntity.getSizeDesc());
					} else {
						returnData = splineEntity;
					}
					if (!eics.contains(splineEntity.getEic())) {
						splineCnt++;
						eics.add(splineEntity.getEic());
					}
				} else if (splineEntity.getMinD().equals("") && splineEntity.getMaxD().equals("")
						&& splineEntity.getMinH().equals("") && splineEntity.getMaxH().equals("")
						&& splineEntity.getMinF().equals("") && splineEntity.getMaxF().equals("")) {
					returnData = splineEntity;
					if (!eics.contains(splineEntity.getEic())) {
						splineCnt++;
						eics.add(splineEntity.getEic());
					}
				}
			}
		}

		if (returnData != null) {
			returnData.setSplineCount(splineCnt);
			return returnData;
		} else {
			return null;
		}
	}


	public static void calculateLaShen(Connection con, String designKey, String height){
		Statement st = null;
		try{
			st =  con.createStatement();
			String getCraftPhys = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getLashenCraftPhys");
			getCraftPhys = getCraftPhys.replaceAll("#DESGIN_KEY#", designKey);
			ArrayList<CraftOrdDesignStdPicEntity> craftPhys = EntityTool.getEntitysBySqlNoClsCon(
					getCraftPhys, con, CraftOrdDesignStdPicEntity.class);
			String getCraftPhySize = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getLashenSize");
			getCraftPhySize = getCraftPhySize.replaceAll("#DESGIN_KEY#", designKey);
			ArrayList<CraftOrdDesignStdPicSmpEntity> craftPhySizes = EntityTool.getEntitysBySqlNoClsCon(
					getCraftPhySize, con, CraftOrdDesignStdPicSmpEntity.class);
			for(CraftOrdDesignStdPicEntity craftPhy : craftPhys){
				for(CraftOrdDesignStdPicSmpEntity craftPhySize : craftPhySizes){
					if(craftPhy.getPhyCodeMin().equals(craftPhySize.getPhyCodeMin())
					 && craftPhy.getItemCodeF().equals(craftPhySize.getItemCodeF())){
						//计算
						String axc = PhyJudgeHelper.CalculateAxc(craftPhySize.getSizeDesc(), height);
						//替换公式符号
						String stdMin = craftPhy.getStdmin();
						if(!stdMin.equals("") || !StringUtils.isNumeric(stdMin)){
							stdMin = stdMin.replaceAll("t", height).replaceAll("Axc", axc);
							craftPhy.setStdmin(JudgeHelper.getRoundValue(stdMin, 0));
						}
						String stdMax = craftPhy.getStdmax();
						if(!stdMax.equals("") || !StringUtils.isNumeric(stdMax)){
							stdMax = stdMax.replaceAll("t", height).replaceAll("Axc", axc);
							craftPhy.setStdmax(JudgeHelper.getRoundValue(stdMax, 0));
						}
						String stdMin2 = craftPhy.getStdmin2();
						if(!stdMin2.equals("") || !StringUtils.isNumeric(stdMin2)){
							stdMin2 = stdMin2.replaceAll("t", height).replaceAll("Axc", axc);
							craftPhy.setStdmin2(JudgeHelper.getRoundValue(stdMin2, 0));
						}
						String stdMax2 = craftPhy.getStdmax2();
						if(!stdMax2.equals("") || !StringUtils.isNumeric(stdMax2)){
							stdMax2 = stdMax2.replaceAll("t", height).replaceAll("Axc", axc);
							craftPhy.setStdmax2(JudgeHelper.getRoundValue(stdMax2, 0));
						}

						String setLaShenStdValue = CoreXmlSqlParsersFactory.getSql("MscPhyDao.setLashenStdValue");
						setLaShenStdValue = EntityTool.fillSqlByEntity(setLaShenStdValue, craftPhy);
						st.executeUpdate(setLaShenStdValue);
					}
				}
			}


		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
}
