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

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

import com.steering.pss.sale.order.craftDesign.dao.MscGProcessDao;
import com.steering.pss.sale.order.craftDesign.model.CraftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdGicEntity;
import com.steering.pss.sale.order.craftDesign.model.StdEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

/**
 * 加工
 * 
 * @author Administrator
 * 
 */
public class MscGprocess {
	/**
	 * 查询加工索引号
	 * 
	 * @param con
	 * @param msc
	 * @param modelCode
	 * @param mscValig
	 * @return
	 * @throws SQLException
	 */
	public List<String> getGicByMscAndModel(Connection con, String msc,
			String modelCode, String mscValig) throws SQLException {
		List<String> gics = null;
		MscGProcessDao mscProcessDaoImp = new MscGProcessDao();
		if (mscValig.equals("C")) {
			gics = mscProcessDaoImp.getExamineGic(con, msc, modelCode);
		} else {
			gics = mscProcessDaoImp.getValidGic(con, msc, modelCode);
		}
		return gics;
	}

	/**
	 * 验证加工标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @return boolean
	 * @throws SQLException
	 */
	public StdEntity checkProcess(Connection con, CraftEntity design,
			String flag, List<String> gics) throws SQLException {
		String message = "";

		String designKey = design.getDesignKey();
		String useCode = design.getUseCode();
		String useDesc = design.getUseDesc();
		String specStyle = design.getSpecStyle();
		String dimater = design.getDimater();
		String height = design.getHeight();
		MscGProcessDao mscProcessDaoImp = new MscGProcessDao();
		StdEntity stdEntity = new StdEntity();
		
		//根据扣型验证加工选择的工序点
		String[] modelStyles = mscProcessDaoImp.getModelStyle(con, design.getModelCode());
		String modelStyle = modelStyles[0];
		String modelStyleDesc = modelStyles[1];
		
//		if(gics.size() == 0 && (modelStyle.equals("405101") || modelStyle.equals("405102")
//				|| modelStyle.equals("405104") || modelStyle.equals("405105")
//				|| modelStyle.equals("405107"))){
//			
//		}
		
		for (String gic : gics) {
			List<CraftOrdDesignStdGicEntity> processes = mscProcessDaoImp
					.getProcessByGic(con, gic);
			if (processes == null) {
				message = "加工索引号(" + gic + ")没有维护加工标准!";
				continue;
			}
			
			for(int i = processes.size() - 1; i >= 0; i--){
				CraftOrdDesignStdGicEntity process = processes.get(i);
				if(specStyle.equals("A")){ // 钢管
					if(!CraftTool.checkStr(dimater, process.getMinDSign(), process.getMinD())
							|| !CraftTool.checkStr(dimater, process.getMaxDSign(), process.getMaxD())
							|| !CraftTool.checkStr(height, process.getMinHSign(), process.getMinH())
							|| !CraftTool.checkStr(height, process.getMaxHSign(), process.getMaxH())){
						processes.remove(i);
					}
				}else if(specStyle.equals("B")){ // 坯子
					if(!CraftTool.checkStr(dimater, process.getMinDSign(), process.getMinD())
							|| !CraftTool.checkStr(dimater, process.getMaxDSign(), process.getMaxD())){
						processes.remove(i);
					}
				}
			}
			if(processes.size() == 0){
				if(specStyle.equals("A")){
					message = "加工索引号("+ gic +")<钢管产品> 没有找到符合外径(" + dimater + ")/壁厚(" + height
							+ ")的加工标准";
					continue;
				}else if(specStyle.equals("B")){ // 坯子
					message = "加工索引号("+ gic +")<管坯产品>没有找到符合外径(" + dimater + ")的加工标准";
					continue;
				}
			}

			for (int i = processes.size() - 1; i >= 0; i--) {
				CraftOrdDesignStdGicEntity process = processes.get(i);
				//根据扣型验证加工选择的工序点
				// 如果扣型分类:倒棱,加工二级工序必为车丝
				if (modelStyle.equals("405101")
						&& !process.getStationCode().equals("70490")) {
					String stationDesc = mscProcessDaoImp.getStationDesc(con, "70490");
					message = "扣型类型为:"+ modelStyleDesc +" 加工索引号(" + gic + ")必须维护"
							+ stationDesc + "工序点";
					
					processes.remove(i);
					continue;
				}
				// 如果扣型分类:车丝,加工二级工序必为车丝
				if ((modelStyle.equals("405102")
						|| modelStyle.equals("405104") || modelStyle
							.equals("405105"))
						&& !process.getStationCode().equals("70210")) {
					String stationDesc = mscProcessDaoImp.getStationDesc(con, "70210");
					message = "扣型类型为:"+ modelStyleDesc +" 加工索引号(" + gic + ")必须维护"
							+ stationDesc + "工序点";
					processes.remove(i);
					continue;
				}
				// 如果扣型分类：平端面，加工工序点必须为平端面
				if (modelStyle.equals("405107")
						&& !process.getStationCode().equals("70220")) {
					String stationDesc = mscProcessDaoImp.getStationDesc(con, "70220");
					message = "扣型类型为:"+ modelStyleDesc +" 加工索引号(" + gic + ")必须维护"
							+ stationDesc + "工序点";
					processes.remove(i);
					continue;
				}
				
				process.setMscPline("Y");
				process.setProcessCode("Y");
				// 是否特殊要求(0否;1是)
				process.setSpeclFl(flag);
				process.setDesginKey(designKey);
				if (flag.equals("1")) {
					process.setUseCode(useCode);
					process.setUseDesc(useDesc);
				}
				// mscProcessDaoImp.saveProcess(con, process);
			}
			
//			if(processes.size() > 1){
//				if(specStyle.equals("A")){
//					message = "加工索引号("+ gic +")<钢管产品> 找到多条符合外径(" + dimater + ")/壁厚(" + height
//							+ ")的加工标准";
//					continue;
//				}else if(specStyle.equals("B")){ // 坯子
//					message = "加工索引号("+ gic +")<管坯产品> 找到多条符合外径(" + dimater + ")的加工标准";
//					continue;
//				}
//			}
			
			if(processes.size() == 0 && !message.equals("")){
				stdEntity.setFlag(false);
				stdEntity.setMessage(message);
				return stdEntity;
			}
			
			// if (flag.equals("0")) {
			// mscProcessDaoImp.updateDesignStd(con, gic, designKey);
			// }
			stdEntity.setFlag(true);
			stdEntity.setMessage("加工标准通过验证!");
			stdEntity.setObjKey(gic);
			stdEntity.setObjM(processes);
			return stdEntity;
		}
		stdEntity.setFlag(false);
		stdEntity.setMessage(message);
		return stdEntity;
	}
}
