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.DesignMscPlPrcEntity;
import com.steering.pss.sale.order.model.DesignMscPlStiEntity;
import com.steering.pss.sale.order.model.MscProcessEntity;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.OperationDaoImp;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.OperationPointDaoImp;
import com.steering.pss.sale.order.qualityDesign.server.imp.InnerStandardImp;

/**
 * 內控标准业务类
 * 
 * @author Administrator
 * 
 */
public class IInneStandard implements InnerStandardImp {
	/**
	 * 验证内控标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @param wholeBacklog
	 *            全程途径码
	 * @param wholeBacklogDesc
	 *            全程途径描述
	 * @param designKey
	 *            主键
	 * @return Object[]
	 * @throws SQLException
	 */
	public Object[] checkInnerStd(Connection con, String msc, String mscPline,
			String wholeBacklog, String wholeBacklogDesc, String designKey)
			throws SQLException {
		Object[] innerInfo = new Object[3];
		List<DesignMscPlPrcEntity> innerStds = new ArrayList<DesignMscPlPrcEntity>();
		OperationDaoImp operationDaoImp = FactoryDao.getOperationDaoImp();
		OperationPointDaoImp operationPointDaoImp = FactoryDao
				.getOperationPointDaoImp();
		// 提示信息
		String errMessage = "";
		// 工序集合
		String[] processCodes = wholeBacklog.split("-");
		// 工序点类型集合
		List<String> pointTypes = new ArrayList<String>();
		// 内控标准集合
		List<String> innerSeqs = new ArrayList<String>();
		for (int i = 0; i < processCodes.length; i++) {
			// 工序点集合
			List<DesignMscPlStiEntity> points = operationPointDaoImp
					.getDesignPoint(con, msc, mscPline, processCodes[i]);
			for (DesignMscPlStiEntity point : points) {
				if (!pointTypes.contains(point.getStationType())) {
					pointTypes.add(point.getStationType());
				}
			}
			MscProcessEntity processInfo = operationDaoImp.getOperation(con,
					msc, mscPline, processCodes[i]);
			if (!processInfo.getCic().equals("")) {
				if (!innerSeqs.contains("CIC")) {
					innerSeqs.add("CIC");
				}
			}
			if (!processInfo.getPic().equals("")) {
				if (!innerSeqs.contains("PIC")) {
					innerSeqs.add("PIC");
				}
			}
			if (!processInfo.getSic().equals("")) {
				if (!innerSeqs.contains("SIC")) {
					innerSeqs.add("SIC");
				}
			}
			if (!processInfo.getDic().equals("")) {
				if (!innerSeqs.contains("DIC")) {
					innerSeqs.add("DIC");
				}
			}
			if (!processInfo.getWic().equals("")) {
				if (!innerSeqs.contains("WIC")) {
					innerSeqs.add("WIC");
				}
			}
			if (!processInfo.getGic().equals("")) {
				if (!innerSeqs.contains("GIC")) {
					innerSeqs.add("GIC");
				}
			}
			// 生成内控标准数据
			DesignMscPlPrcEntity innerStd = new DesignMscPlPrcEntity();
			innerStd.setDesginKey(designKey);
			innerStd.setMscPline(mscPline);
			innerStd.setWholeBacklog(wholeBacklog);
			innerStd.setWholeBacklogDesc(wholeBacklogDesc);
			innerStd.setBacklogSeq(processInfo.getBacklogSeq());
			innerStd.setProcessCode(processInfo.getProcessCode());
			innerStd.setProcessDesc(processInfo.getProcessDesc());
			innerStd.setProcessSeq(processInfo.getProcessSeq());
			innerStd.setProcessCodeC(processInfo.getProcessCodeC());
			innerStd.setProcessDescC(processInfo.getProcessDescC());
			innerStd.setMinuteProduct(processInfo.getMinuteProduct());
			innerStd.setMinuteWaite(processInfo.getMinuteWaite());
			innerStd.setCic(processInfo.getCic());
			innerStd.setPic(processInfo.getPic());
			innerStd.setSic(processInfo.getSic());
			innerStd.setDic(processInfo.getDic());
			innerStd.setWic(processInfo.getWic());
			innerStd.setGic(processInfo.getGic());
			innerStd.setMemo(processInfo.getMemo());
			innerStds.add(innerStd);

		}
		if (innerSeqs.contains("CIC") && !pointTypes.contains("403201")) {
			errMessage = "制程" + mscPline + "存在成分内控标准,但不存在成分工序点";
		}
		if (innerSeqs.contains("PIC") && !pointTypes.contains("403202")) {
			errMessage = "工序" + mscPline + "存在理化内控标准,但不存在理化工序点";
		}
		if (innerSeqs.contains("SIC") && !pointTypes.contains("403203")) {
			errMessage = "工序" + mscPline + "存在公差内控标准,但不存在公差工序点";
		}
		if (innerSeqs.contains("DIC") && !pointTypes.contains("403204")) {
			errMessage = "工序" + mscPline + "存在探伤内控标准,但不存在探伤工序点";
		}
		if (innerSeqs.contains("WIC") && !pointTypes.contains("403205")) {
			errMessage = "工序" + mscPline + "存在水压内控标准,但不存在水压工序点";
		}
		if (innerSeqs.contains("GIC") && !pointTypes.contains("403208")) {
			errMessage = "工序" + mscPline + "存在加工内控标准,但不存在加工工序点";
		}

		if (!innerSeqs.contains("CIC") && pointTypes.contains("403201")) {
			errMessage = "制程" + mscPline + "存在成分工序点,但不存在成分内控标准";
		}
		if (!innerSeqs.contains("PIC") && pointTypes.contains("403202")) {
			errMessage = "工序" + mscPline + "存在理化工序点,但不存在理化内控标准";
		}
		if (!innerSeqs.contains("SIC") && pointTypes.contains("403203")) {
			errMessage = "工序" + mscPline + "存在公差工序点,但不存在公差内控标准";
		}
		if (!innerSeqs.contains("DIC") && pointTypes.contains("403204")) {
			errMessage = "工序" + mscPline + "存在探伤工序点,但不存在探伤内控标准";
		}
		if (!innerSeqs.contains("WIC") && pointTypes.contains("403205")) {
			errMessage = "工序" + mscPline + "存在水压工序点,但不存在水压内控标准";
		}
		if (!innerSeqs.contains("GIC") && pointTypes.contains("403208")) {
			errMessage = "工序" + mscPline + "存在加工工序点,但不存在加工内控标准";
		}
		if (errMessage.equals("")) {
			errMessage = "内控标准通过验证";
			innerInfo[0] = true;
			innerInfo[1] = errMessage;
			innerInfo[2] = innerStds;
		} else {
			innerInfo[0] = false;
			innerInfo[1] = errMessage;
			innerInfo[2] = innerStds;
		}
		return innerInfo;
	}
}
