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.DesignMscPlGzEntity;
import com.steering.pss.sale.order.model.MscGradeRSpecEntity;
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.GradeDaoImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.GradeImp;

/**
 * 钢种业务类
 * 
 * @author Administrator PDY
 * 
 */
public class IGrade implements GradeImp {
	// 验证钢种流程:
	// 1、制程下是否存在钢种
	// 2、是否存在钢种与规格关系,不存在则表示这个钢种适用任何规格。存在则要判断外径、壁厚
	// 3、只要一个钢种通过验证,则钢种通过验证
	//
	/**
	 * 验证钢种
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            产线号
	 * @param wholeBacklog
	 *            全程途径号
	 * @return List<DesignMscPlGzEntity>
	 * @throws SQLException
	 */
	public Object[] checkGrade(Connection con, String mscPline,
			String wholeBacklog, String msc, String dimater, String height,
			String specStyle, String specName, String outsBilietFl, String mscStyle,
			String designKey)
			throws SQLException {
		Object[] reInfo = new Object[2];
		String errMessage = "";
		GradeDaoImp gradeDaoImp = FactoryDao.getGradeDaoImp();
		
		//如果是接箍类型，那么不需要单独维护钢种，只需要获取接箍坯料的钢种。
		if(mscStyle.equals("D")){
			//获取接箍料的钢种
			List<DesignMscPlGzEntity> grades = gradeDaoImp.getBlSlmGrade(con,
					designKey, msc, mscPline);
			errMessage = "制程(" + mscPline + ")钢种验证通过";
			reInfo[0] = errMessage;
			reInfo[1] = grades;
			return reInfo;
		}
		
		// 查询钢种
		List<DesignMscPlGzEntity> grades = gradeDaoImp.getGrade(con, msc,
				mscPline);
		// 如果找不到钢种
		if (grades == null || grades.size() == 0) {
			errMessage = "制程(" + mscPline + ")缺少钢种!";
			reInfo[0] = errMessage;
			reInfo[1] = null;
			return reInfo;
		}

		// 如果钢种与规格关系记录条数为零，则表示这个钢种适用任何规格
		// (用个两次查询:第一次用钢种代码查询,如果查不到、则说明没有维护对应关系,则表明这个钢种适用任何规格。)
		// 第二次
		
		List<DesignMscPlGzEntity> gradeAs = new ArrayList<DesignMscPlGzEntity>();
		for (DesignMscPlGzEntity grade : grades) {
			List<MscGradeRSpecEntity> gradeRSpecs = gradeDaoImp
					.getGradeAndSpec(con, msc, mscPline, grade.getGradecode());
			if (gradeRSpecs == null || gradeRSpecs.size() == 0) {
				gradeAs.add(grade);
				continue;
			} else {
				if (specStyle.equals("C")) {
					errMessage = "制程(" + mscPline + ")/钢种("
							+ grade.getGradename() + ")不能维护规格范围![合同产品规格("
							+ specName + ")为英制规格,不能维护钢种规格范围]";
					continue;
				}
			}

			for (MscGradeRSpecEntity gradeRSpec : gradeRSpecs) {
				if (!IGrade.fiterGradeSpec(gradeRSpec, dimater, height)) {
					errMessage = "制程(" + mscPline + ")/钢种("
							+ grade.getGradename() + ")的适用规格不满足合同产品外径("
							+ dimater + ") 壁厚(" + height + ")要求!";
				} else {
					if (!gradeAs.contains(grade)) {
						gradeAs.add(grade);
					}
				}
			}
		}
		
		if (gradeAs.size() > 0) {
			errMessage = "制程(" + mscPline + ")钢种验证通过!";
		} else {
			reInfo[0] = errMessage;
			reInfo[1] = null;
			return reInfo;
		}
		// 验证是否需要外购钢种（如果合同有外购坯，则钢种必须要有外购钢种）

		if (outsBilietFl.equals("121801") && gradeAs.size() > 0) {
			boolean flag = false;
			for (DesignMscPlGzEntity grade : gradeAs) {
				if (grade.getAllowBuy().equals("1")) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				errMessage = "合同允许外购坯," + "制程:" + mscPline + "必须存在一个钢种允许外购!";
				gradeAs = null;
			}
		}
		reInfo[0] = errMessage;
		reInfo[1] = gradeAs;
		return reInfo;
	}

	/**
	 * 过滤钢种与规格对应关系
	 * 
	 * @param gradeSpecs
	 *            钢种
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<MscGradeRSpecEntity>
	 */
	public static boolean fiterGradeSpec(MscGradeRSpecEntity gradeSpec,
			String dimater, String height) {
		if (!QualityDesignTool.checkStr(gradeSpec.getDimaterMin(), "<=",
				dimater)
				|| !QualityDesignTool.checkStr(gradeSpec.getDimaterMax(), ">=",
						dimater)
				|| !QualityDesignTool.checkStr(gradeSpec.getHeightMin(), "<=",
						height)
				|| !QualityDesignTool.checkStr(gradeSpec.getHeightMiax(), ">=",
						height)) {
			return false;
		}
		return true;
	}
}
