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

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

import com.steering.pss.sale.order.craftDesign.dao.MscDeteDao;
import com.steering.pss.sale.order.craftDesign.model.ComBasePhyRStationEntity;
import com.steering.pss.sale.order.craftDesign.model.ComBaseSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdDicEntity;
import com.steering.pss.sale.order.craftDesign.model.StdEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

public class MscDete {
	/**
	 * 查询探伤索引号
	 * 
	 * @param con
	 * @param msc
	 * @param modelCode
	 * @param mscValig
	 * @return
	 * @throws SQLException
	 */
	public List<String> getDicByMscAndModel(Connection con, String msc,
			String modelCode, String mscValig) throws SQLException {
		MscDeteDao deteDaoImp = new MscDeteDao();
		List<String> dics = null;
		if (mscValig.equals("C")) {
			dics = deteDaoImp.getExamineDic(con, msc, modelCode);
		} else {
			dics = deteDaoImp.getValidDic(con, msc, modelCode);
		}
		return dics;
	}

	/**
	 * 验证探伤标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @return boolean
	 * @throws SQLException
	 */
	public StdEntity checkDete(Connection con, CraftEntity design, String flag,
			List<String> dics, ComBaseSpecEntity spec) throws SQLException {
		MscDeteDao deteDaoImp = new MscDeteDao();
		String message = "";
		String designKey = design.getDesignKey();
		String dimater = design.getDimater();
		String height = design.getHeight();
		String useCode = design.getUseCode();
		String useDesc = design.getUseDesc();
		String specStyle = design.getSpecStyle();
		StdEntity stdEntity = new StdEntity();
		for (String dic : dics) {
			// 查询探伤索引号的探伤标准
			List<CraftOrdDesignStdDicEntity> detes = deteDaoImp.getDeteByDic(
					con, dic);
			if (detes == null) {
				message = "探伤索引号(" + dic + ")不存在探伤标准";
				continue;
			}
			if (specStyle.equals("A")) {
				// 钢管
				detes = MscDete.fiterDete(detes, spec.getDimater(), spec.getHeight());
				if (detes == null || detes.size() == 0) {
					message = "探伤索引号(" + dic + ")不存在符合外径("
							+ design.getDimater() + "),壁厚("
							+ design.getHeight() + ")的探伤标准";
					continue;
				}
			} else if (specStyle.equals("B")) {
				// 管坯
				detes = MscDete.fiterDete(detes, spec.getDimater());
				if (detes == null || detes.size() == 0) {
					message = "探伤索引号(" + dic + ")不存在符合外径("
							+ design.getDimater() + ")的探伤标准";
					continue;
				}
			} else {
				// 钢锭
			}
			// 验证重复
			String returnInfo = MscDete.checkCommon(detes, spec.getDimater(), spec.getHeight());
			if (!returnInfo.equals("")) {
				message = returnInfo;
				continue;
			}
			boolean phyFlag = true;
			// 验证无损检验项
			for (CraftOrdDesignStdDicEntity dete : detes) {
				//处理覆盖率
				if(dete.getPhyCode().equals("P0107") || dete.getPhyCode().equals("P0040")){
					if(dete.getCoverage().equals("") || dete.getCoverage().equals("0")){
						dete.setCoverage("-");
					}
				}
				
				// 查询检验项类型
				String phyStyle = deteDaoImp
						.getPhyStyle(con, dete.getPhyCode());
				// 如果是无损检验项,必须维护检验项对应的工序点
				if (phyStyle.equals("R0006")) {
					// 查询检验项的工序点
					List<ComBasePhyRStationEntity> stations = deteDaoImp
							.getStationByPhyCode(con, dete.getPhyCode());
					if (stations == null) {
						message = "探伤索引号(" + dic + ")的检验项(" + dete.getPhyName()
								+ ")属于无损检验项,需要维护检验项的工序点!";
						phyFlag = false;
						break;
					}
				}
			}
			if (!phyFlag) {
				continue;
			}
			// 赋值
			for (CraftOrdDesignStdDicEntity dete : detes) {
				dete.setMscPline("Y");
				dete.setProcessCode("Y");
				// flag 0 交互标准　1 客户要求　2　内控标准
				dete.setSpeclFl(flag);
				dete.setDesginKey(designKey);
				if (flag.equals("1")) {
					dete.setUseCode(useCode);
					dete.setUseDesc(useDesc);
				}
			}
			// 验证通过
			stdEntity.setFlag(true);
			stdEntity.setMessage("公差标准通过验证");
			stdEntity.setObjM(detes);
			stdEntity.setObjKey(dic);
			return stdEntity;
		}
		stdEntity.setFlag(false);
		stdEntity.setMessage(message);
		return stdEntity;
	}

	public static String checkCommon(List<CraftOrdDesignStdDicEntity> deteList,
			String dimater, String height) {
		for (CraftOrdDesignStdDicEntity deteEntityA : deteList) {
			for (CraftOrdDesignStdDicEntity deteEntityB : deteList) {
				if (!deteEntityA.getXh().equals(deteEntityB.getXh())
						&& deteEntityA.getPhyCode().equals(
								deteEntityB.getPhyCode())
						&& deteEntityA.getGroovePos().equals(
								deteEntityB.getGroovePos())) {
					return "探伤标准维护了多条满足外径(" + dimater + ") 壁厚(" + height
							+ ")的公差项(" + deteEntityA.getPhyName() + ")/刻槽位置("
							+ deteEntityA.getGroovePos() + "),序号分别为"
							+ deteEntityA.getXh() + "," + deteEntityB.getXh();
				}
			}
		}
		return "";
	}

	/**
	 * 过滤探伤外径、壁厚(钢管)
	 * 
	 * @param detes
	 * @param dimater
	 * @param height
	 * @return
	 */
	public static List<CraftOrdDesignStdDicEntity> fiterDete(
			List<CraftOrdDesignStdDicEntity> detes, String dimater,
			String height) {
		if (detes == null || detes.size() == 0) {
			return null;
		} else {
			List<CraftOrdDesignStdDicEntity> deteAs = new ArrayList<CraftOrdDesignStdDicEntity>();
			for (CraftOrdDesignStdDicEntity dete : detes) {
				if (CraftTool.checkStr(dete.getMinD(), "<=", dimater)
						&& CraftTool.checkStr(dete.getMaxD(), ">=", dimater)
						&& CraftTool.checkStr(dete.getMinH(), "<=", height)
						&& CraftTool.checkStr(dete.getMaxH(), ">=", height)) {
					deteAs.add(dete);
				}
			}
			return deteAs;
		}
	}

	/**
	 * 过滤壁厚（管坯）
	 * 
	 * @param detes
	 * @param dimater
	 * @return
	 */
	public static List<CraftOrdDesignStdDicEntity> fiterDete(
			List<CraftOrdDesignStdDicEntity> detes, String dimater) {
		if (detes == null || detes.size() == 0) {
			return null;
		} else {
			List<CraftOrdDesignStdDicEntity> deteAs = new ArrayList<CraftOrdDesignStdDicEntity>();
			for (CraftOrdDesignStdDicEntity dete : detes) {
				if (CraftTool.checkStr(dete.getMinD(), "<=", dimater)
						&& CraftTool.checkStr(dete.getMaxD(), ">=", dimater)
						&& dete.getMinH().equals("")
						&& dete.getMaxH().equals("")) {
					deteAs.add(dete);
				}
			}
			return deteAs;
		}
	}

}
