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

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

import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdGicEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.ComMscEntity;
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.MscProcessingDaoImp;
import com.steering.pss.sale.order.qualityDesign.ReturnsModel.DesignEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdGicEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscProcessingImp;

public class IMscProcessing implements MscProcessingImp {
	/**
	 * 验证加工标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param design
	 *            质量设计类
	 * @return boolean
	 * @throws SQLException
	 */
	public String checkProcess(Connection con, String msc, DesignEntity design,
			String flag) throws SQLException {
		String message = "";
		String modelCode = design.getModelCode();
		String designKey = design.getDesignKey();
		String useCode = design.getUserCode();
		String useDesc = design.getUserDesc();
		String specStyle = design.getSpecStyle();
		String dimater = design.getDimater();
		String height = design.getHeight();
		MscProcessingDaoImp mscProcessDaoImp = FactoryDao
				.getMscProcessingDaoImp();
		MscDaoImp mscDaoImp = FactoryDao.getMscDaoImp();
		ComMscEntity comMscEntity = mscDaoImp.getMsc(con, msc);
		// 如果冶金规范码已经生效，则查生效的加工标准，否则查有效的加工标准
		List<String> gics = null;
		if (comMscEntity.getValidflag().equals("C")) {
			gics = mscProcessDaoImp.getExamineGic(con, msc, modelCode);
		} else {
			gics = mscProcessDaoImp.getValidGic(con, msc, modelCode);
		}
		if (gics == null || gics.size() == 0) {
			return message;
		}
		//根据扣型验证加工选择的工序点
		String[] modelStyles = mscProcessDaoImp.getModelStyle(con, design.getModelCode());
		String modelStyle = modelStyles[0];
		String modelStyleDesc = modelStyles[1];
				
		for (String gic : gics) {
			List<SlmOrdDesignStdGicEntity> processes = mscProcessDaoImp
					.getProcessByGic(con, gic);
			if (processes == null) {
				message = "加工索引号:" + gic + "找不到加工标准";
				continue;
			}
			
			//过滤规格
			for(int i = processes.size() - 1; i >= 0; i--){
				SlmOrdDesignStdGicEntity 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--) {
				SlmOrdDesignStdGicEntity 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;
				}
				
				// 是否特殊要求(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("")){
				return message;
			}
			
			if (flag.equals("0")) {
				mscProcessDaoImp.updateDesignStd(con, gic, designKey);
			}
			message = "";
			break;
		}

		return message;
	}
}
