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.GradeDao;
import com.steering.pss.sale.order.craftDesign.model.ComMscGradeRSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlGzEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

public class Grade {
	// 验证钢种流程:
	// 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[3];
		String errMessage = "";
		GradeDao gradeDaoImp = new GradeDao();
		
		//如果是接箍类型，那么不需要单独维护钢种，只需要获取接箍坯料的钢种。
		if(mscStyle.equals("D")){
			//获取接箍料的钢种
			List<CraftOrdDesignMscPlGzEntity> grades = gradeDaoImp.getBlCraftGrade(con,
					designKey, msc, mscPline);
			errMessage = "制程(" + mscPline + ")钢种验证通过";
			reInfo[0] = true;
			reInfo[1] = errMessage;
			reInfo[2] = grades;
			return reInfo;
		}
		
		// 查询钢种
		List<CraftOrdDesignMscPlGzEntity> grades = gradeDaoImp.getGrade(con,
				msc, mscPline);
		// 如果找不到钢种
		if (grades == null || grades.size() == 0) {
			errMessage = "制程(" + mscPline + " )缺少钢种";
			reInfo[0] = false;
			reInfo[1] = errMessage;
			reInfo[2] = null;
			return reInfo;
		}

		// 如果钢种与规格关系记录条数为零，则表示这个钢种适用任何规格
		// (用个两次查询:第一次用钢种代码查询,如果查不到、则说明没有维护对应关系,则表明这个钢种适用任何规格。)
		// 第二次
		List<CraftOrdDesignMscPlGzEntity> gradeAs = new ArrayList<CraftOrdDesignMscPlGzEntity>();
		
		for (CraftOrdDesignMscPlGzEntity grade : grades) {
			List<ComMscGradeRSpecEntity> 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 (ComMscGradeRSpecEntity gradeRSpec : gradeRSpecs) {
				if (!Grade.fiterGradeSpec(gradeRSpec, dimater, height)) {
//					errMessage = "制程(" + mscPline + ")/钢种("
//							+ grade.getGradename() + ")的适用规格不满足合同行产品外径("
//							+ dimater + ")/壁厚(" + height + ")";
				} else {
					if (!gradeAs.contains(grade)) {
						gradeAs.add(grade);
					}
				}
			}
		}
		// 如果过滤后的钢种为空
		if (gradeAs == null || gradeAs.size() == 0) {
			errMessage = "制程(" + mscPline + ")中找不到符合合同规格（外径:"+ dimater +"  壁厚:"+ height 
					+"）的钢种！";
			reInfo[0] = false;
			reInfo[1] = errMessage;
			reInfo[2] = null;
			return reInfo;
		}
		// 钢种验证通过标识
		boolean flag = false;
		// 外购坯对钢种的制约：合同允许外购坯，至少有一个钢种允许外购
		if (gradeAs.size() > 0 && outsBilietFl.equals("121801")) {
			for (CraftOrdDesignMscPlGzEntity grade : gradeAs) {
				if (grade.getAllowBuy().equals("1")) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				errMessage = "制程(" + mscPline
						+ ")钢种必须有一个钢种允许外购![合同允许外购坯,必须有外购钢种]";
			}
		} else {
			flag = true;
		}
		
		ArrayList<String> prioritys = new ArrayList<String>();
		for (CraftOrdDesignMscPlGzEntity grade : gradeAs) {
			if(grade.getPriority().equals("")){
				errMessage = "制程"+ mscPline +" - 钢种" + grade.getGradename() + "必须维护钢种优先级";
				reInfo[0] = false;
				reInfo[1] = errMessage;
				reInfo[2] = null;
				return reInfo;
			}
			if(prioritys.contains(grade.getPriority())){
				errMessage = "制程"+ mscPline +"存在多个钢种，请维护钢种优先级！（钢种优先级不能重复）";
				reInfo[0] = false;
				reInfo[1] = errMessage;
				reInfo[2] = null;
				return reInfo;
			}
			prioritys.add(grade.getPriority());
		}
		
		if (flag) {
			errMessage = "制程(" + mscPline + ")钢种验证通过";
		}
		reInfo[0] = flag;
		reInfo[1] = errMessage;
		reInfo[2] = gradeAs;
		return reInfo;
	}

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