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

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

import com.steering.pss.sale.order.model.ComMscEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
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.MscDeteDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.model.ComBasePhyRStationEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdDicEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscDeteImp;

public class IMscDete implements MscDeteImp {

	/**
	 * 验证探伤标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @return boolean
	 * @throws SQLException
	 */
	public String checkDete(Connection con, String msc, DesignEntity design,
			String flag) throws SQLException {
		MscDeteDaoImp deteDaoImp = FactoryDao.getDeteDaoImp();
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		String message = "";
		String designKey = design.getDesignKey();
		String modelCode = design.getModelCode();
		String dimater = design.getDimater();
		String height = design.getHeight();
		String useCode = design.getUserCode();
		String useDesc = design.getUserDesc();
		String specStyle = design.getSpecStyle();
		// 基础冶金规范码
		ComMscEntity comMscEntity = mscDaoImp.getMsc(con, msc);
		// 如果冶金规范码已经通过审批
		List<String> dics = null;
		if (comMscEntity.getValidflag().equals("C")) {
			dics = deteDaoImp.getExamineDic(con, msc, modelCode);
		} else {
			dics = deteDaoImp.getValidDic(con, msc, modelCode);
		}
		if (dics == null || dics.size() == 0) {
			return message;
		}
		for (String dic : dics) {
			List<SlmOrdDesignStdDicEntity> detes = deteDaoImp.getDeteByDic(con,
					dic);
			if (specStyle.equals("A")) {
				detes = IMscDete.fiterDete(detes, dimater, height);
				if (detes == null || detes.size() == 0) {
					message = "探伤标准满足外径(" + design.getDimater() + "),壁厚("
							+ design.getHeight() + ")的探伤标准不存在";
					continue;
				}
			} else if (specStyle.equals("B")) {
				detes = IMscDete.fiterDete(detes, dimater);
				if (detes == null || detes.size() == 0) {
					message = "探伤标准满足外径(" + design.getDimater() + ")的探伤标准不存在";
					continue;
				}
			} else {
				// 钢锭
			}
			// 验证是否重复
			String returnInfo = IMscDete.checkCommon(detes, dimater, height);
			if (!returnInfo.equals("")) {
				message = returnInfo;
				continue;
			}
			boolean phyFlag = true;
			// 验证无损检验项
			for (SlmOrdDesignStdDicEntity dete : detes) {
				String phyStyle = deteDaoImp
						.getPhyStyle(con, dete.getPhyCode());
				// 如果是无损检验项,必须维护检验项对应的工序点
				if (phyStyle.equals("R0006")) {
					List<ComBasePhyRStationEntity> stations = deteDaoImp
							.getStationByPhyCode(con, dete.getPhyCode());
					if (stations == null) {
						message = "检验项:" + dete.getPhyName()
								+ "是 无损检验,需要维护检验项对应的工序点!";
						phyFlag = false;
						break;
					}
				}
			}
			if (!phyFlag) {
				continue;
			}

			// 保存探伤标准
			for (SlmOrdDesignStdDicEntity dete : detes) {
				// 是否特殊要求(0否;1是)
				dete.setSpeclFl(flag);
				dete.setDesginKey(designKey);
				if (flag.equals("1")) {
					dete.setUseCode(useCode);
					dete.setUseDesc(useDesc);
				}
				deteDaoImp.saveDete(con, dete);
			}
			// 如果是交付标准,则修改质量设计主表
			if (flag.equals("0")) {
				deteDaoImp.updateDesignStd(con, dic, designKey);
			}
			message = "";
			break;
		}
		return message;
	}

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

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

	public static String checkCommon(List<SlmOrdDesignStdDicEntity> deteList,
			String dimater, String height) {
		for (SlmOrdDesignStdDicEntity deteEntityA : deteList) {
			for (SlmOrdDesignStdDicEntity 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 "";
	}

}
