package market.Qms.slmord.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseStationMapper;
import market.Com.mapper.ComBaseStationRCraftMapper;
import market.Com.vo.ComBaseStation;
import market.Com.vo.ComBaseStationRCraft;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.service.impl.ComBaseConfigServiceImpl;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscMapper;
import market.Qms.metallurgy.mapper.ComMscProcessMapper;
import market.Qms.metallurgy.mapper.ComMscStationMapper;
import market.Qms.metallurgy.mapper.ComMscStationRPlineMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscBacklog;
import market.Qms.metallurgy.vo.ComMscProcess;
import market.Qms.metallurgy.vo.ComMscStation;
import market.Qms.metallurgy.vo.ComMscStationRPline;
import market.Qms.qcm.mapper.ComBaseStdCraftMapper;
import market.Qms.qcm.mapper.ComBaseStdCraftTicItemMapper;
import market.Qms.qcm.mapper.ComBaseStdCraftTicMapper;
import market.Qms.qcm.mapper.ComMscProcessRCraftMapper;
import market.Qms.qcm.mapper.ComMscStationRCraftMapper;
import market.Qms.qcm.vo.ComBaseStdCraft;
import market.Qms.qcm.vo.ComBaseStdCraftTic;
import market.Qms.qcm.vo.ComBaseStdCraftTicItem;
import market.Qms.qcm.vo.ComMscProcessRCraft;
import market.Qms.qcm.vo.ComMscStationRCraft;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlGxPrlMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPrcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlStiMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlStiMcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscTicItemMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscTicMapper;
import market.Qms.slmord.service.SlmOrdDesignMscTicService;
import market.Qms.slmord.vo.SlmOrdDesignMscPl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlGxPrl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlPrc;
import market.Qms.slmord.vo.SlmOrdDesignMscPlSti;
import market.Qms.slmord.vo.SlmOrdDesignMscPlStiMc;
import market.Qms.slmord.vo.SlmOrdDesignMscTic;
import market.Qms.slmord.vo.SlmOrdDesignMscTicItem;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path = "SlmOrdDesignMscTicService")

public class SlmOrdDesignMscTicServiceImpl extends BaseServiceImpl<SlmOrdDesignMscTic, Object>
		implements SlmOrdDesignMscTicService {

	public boolean ValidationCraftStd(ReturnMsg msg, List<SlmOrdDesignMscPl> listOrdMscPl,
			SlmContractProcessPlan orderLine, String designKey) {

		ComMsc msc = null;
		if (orderLine.getMsc_n() != null) {
			msc = this.getMapper(ComMscMapper.class).findById(orderLine.getMsc_n());

		} else {
			msc = this.getMapper(ComMscMapper.class).findById(orderLine.getMsc());

		}
		if ("0".equals(msc.getIs_craft())) {// 冶金规范码不需要控制工艺要求
			msg.setMessage("工艺标准无要求");
			return true;
		}
		// 遍历制程
		try {
			/*
			 * List<ComBaseStdCraft> TarGetTic = new
			 * ArrayList<ComBaseStdCraft>(); List<ComBaseStdCraftTicItem>
			 * TarGetTicItem = new ArrayList<ComBaseStdCraftTicItem>();
			 */
			List<SlmOrdDesignMscTicItem> ITEM = new ArrayList<SlmOrdDesignMscTicItem>();
			List<SlmOrdDesignMscTic> TIC = new ArrayList<SlmOrdDesignMscTic>();
			for (SlmOrdDesignMscPl msc_pline : listOrdMscPl) {
				// 查询制程向下的工序
				List<SlmOrdDesignMscPlPrc> process = this.getMapper(SlmOrdDesignMscPlPrcMapper.class).findWithCondition(
						null, "DESIGN_KEY='" + designKey + "' and MSC_PLINE='" + msc_pline.getMsc_pline() + "'",
						"BACKLOG_SEQ", 0, 0);
				for (SlmOrdDesignMscPlPrc pro : process) {
					// 查询工序向下的工序点
					List<SlmOrdDesignMscPlSti> stition = this.getMapper(SlmOrdDesignMscPlStiMapper.class)
							.findWithCondition(null,
									"DESIGN_KEY='" + designKey + "' and MSC_PLINE='" + pro.getMsc_pline()
											+ "' and BACKLOG_SEQ='" + pro.getBacklog_seq() + "' and PROCESS_CODE='"
											+ pro.getProcess_code() + "'",
									"PROCESS_POINT_SEQ", 0, 0);
					if (stition == null || stition.size() < 1) {
						continue;
					}
					// 获取工序的适用产线
					List<SlmOrdDesignMscPlGxPrl> plines = this.getMapper(SlmOrdDesignMscPlGxPrlMapper.class)
							.findWithCondition(null,
									"DESIGN_KEY='" + pro.getDesign_key() + "' and MSC_PLINE='" + pro.getMsc_pline()
											+ "' and BACKLOG_SEQ='" + pro.getBacklog_seq() + "' and PROCESS_CODE='"
											+ pro.getProcess_code() + "'",
									null, 0, 0);

					for (SlmOrdDesignMscPlGxPrl pline : plines) {
						// 查询产线对应的工序点
						List<ComMscStationRPline> plineRcodes = this.getMapper(ComMscStationRPlineMapper.class)
								.findWithCondition(null, "PLINE_CODE='" + pline.getPline_code() + "' and VALIDFLAG='1'",
										null, 0, 0);

						if (plineRcodes != null && plineRcodes.size() > 0) {

							for (int i = 0; i < stition.size(); i++) {

								for (int j = 0; j < plineRcodes.size(); j++) {

									if (plineRcodes.get(j).getProcessPointCode()
											.equals(stition.get(i).getProcess_point_code())) {
										// 查询是否需要工艺控制
										ComBaseStation baseStation = this.getMapper(ComBaseStationMapper.class)
												.findById(stition.get(i).getProcess_point_code());
										if (baseStation != null) {
											if ("0".equals(baseStation.getIs_craft())) {
												stition.remove(i);
												i--;
												break;
											}
											break;
										}
										break;

									}

									if (j == plineRcodes.size() - 1) {
										stition.remove(i);
										i--;
										break;

									}

								}

							}

						}

						if (stition == null || stition.size() < 1) {
							continue;

						}
						// 需要工艺控制的工序点
						// 读取配置，判断采用哪套信息
						String mark = this.getBean(ComBaseConfigServiceImpl.class).getConfig("CAF0001");

						List<ComBaseStdCraft> craft = new ArrayList<ComBaseStdCraft>();
						if ("1".equals(mark)) {
							// 查询基础表，获取适用订单要求的工艺标准码
							craft = this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null,
									"VALIDFLAG='1' and PROCESS_CODE='" + pro.getProcess_code() + "'", "CRAFT_STD_CODE",
									0, 0);

						} else if ("0".equals(mark)) {

							// 通过质量设计工序结果表，获取guid 对象
							String processGuid = pro.getGuid();

							List<ComMscProcessRCraft> pCraft = this.getMapper(ComMscProcessRCraftMapper.class)
									.findWithCondition(null, "GUID='" + processGuid + "'", "CRAFT_STD_CODE", 0, 0);

							if (pCraft == null || pCraft.size() < 1) {
								msg.setMessage("全程产线号[" + msc_pline.getMsc_pline() + "]向下的工序[" + pro.getProcess_desc()
										+ "]的工艺标准未指定");
								return false;
							}
							for (ComMscProcessRCraft pc : pCraft) {
								craft.addAll(this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null,
										"CRAFT_STD_CODE='" + pc.getCraft_std_code() + "' and VALIDFLAG='1'", null, 0,
										0));
							}

						}

						if (craft == null || craft.size() < 1) {
							msg.setMessage("未找到有效的适用[" + pro.getProcess_desc() + "]工序的工艺标准码");
							return false;
						}
						// 过滤交货状态
						for (int i = 0; i < craft.size(); i++) {
							if (craft.get(i).getDelivery_state_code() != null
									&& !"".equals(craft.get(i).getDelivery_state_code())) {
								if (!craft.get(i).getDelivery_state_code()
										.contains(orderLine.getDelivery_state_code())) {
									craft.remove(i);
									i--;
									continue;
								}
							}

						}
						if (craft == null || craft.size() < 1) {
							msg.setMessage("未找到适用[" + pro.getProcess_desc() + "]工序并符合订单交货状态["
									+ orderLine.getDelivery_state_desc() + "]的工艺标准码");
							return false;
						}

						// 过滤适用产品名称
						for (int i = 0; i < craft.size(); i++) {
							if (craft.get(i).getProd_code() != null && !"".equals(craft.get(i).getProd_code())) {
								if (!craft.get(i).getProd_code().contains(orderLine.getProd_code())) {
									craft.remove(i);
									i--;
									continue;
								}
							}

						}
						if (craft == null || craft.size() < 1) {
							msg.setMessage("未找到适用[" + pro.getProcess_desc() + "]工序并符合订单产品名称[" + orderLine.getProd_name()
									+ "]的工艺标准码");
							return false;
						}

						// 过滤订货标准
						for (int i = 0; i < craft.size(); i++) {
							if (craft.get(i).getStd_code() != null && !"".equals(craft.get(i).getStd_code())) {
								if (!craft.get(i).getStd_code().contains(orderLine.getStd_code())) {
									craft.remove(i);
									i--;
									continue;
								}
							}

						}
						if (craft == null || craft.size() < 1) {
							msg.setMessage("未找到适用[" + pro.getProcess_desc() + "]工序并符合订单订货标准[" + orderLine.getStd_name()
									+ "]的工艺标准码");
							return false;
						}
						// 过滤牌号
						for (int i = 0; i < craft.size(); i++) {
							if (craft.get(i).getSteel_code() != null && !"".equals(craft.get(i).getSteel_code())) {
								if (!craft.get(i).getSteel_code().contains(orderLine.getSteel_code())) {
									craft.remove(i);
									i--;
									continue;
								}
							}

						}
						if (craft == null || craft.size() < 1) {
							msg.setMessage("未找到适用[" + pro.getProcess_desc() + "]工序并符合订单产品牌号["
									+ orderLine.getSteel_name() + "]的工艺标准码");
							return false;
						}

						// 过滤限制规格
						List<ComBaseStdCraft> craftTarget = FilterSize(craft, orderLine);
						if (craftTarget == null || craftTarget.size() < 1) {
							msg.setMessage("未找到适用[" + pro.getProcess_desc() + "]工序并符合订单规格的工艺标准码");
							return false;
						}
						if (craftTarget != null && craftTarget.size() > 1) {
							msg.setMessage("找到多条适用[" + pro.getProcess_desc() + "]工序并符合订单规格的工艺标准码");
							return false;
						}

						// 目标工艺标准码
						ComBaseStdCraft oneCraft = craftTarget.get(0);
						// 根据订单需要工艺的工序点
						String craft_std_code = craftTarget.get(0).getCraft_std_code();

						for (SlmOrdDesignMscPlSti sti : stition) {
							List<ComBaseStdCraftTic> tics = new ArrayList<ComBaseStdCraftTic>();
							if ("1".equals(mark)) {
								tics = this.getMapper(ComBaseStdCraftTicMapper.class)
										.findWithCondition(null,
												"CRAF_STD_CODE='" + craft_std_code + "' and PROCESS_POINT_CODE='"
														+ sti.getProcess_point_code() + "' and VALIDFLAG='1'",
												null, 0, 0);

							} else {
								List<ComMscStationRCraft> sCraft = this.getMapper(ComMscStationRCraftMapper.class)
										.findWithCondition(null, "GUID='" + sti.getGuid() + "' and CRAFT_STD_CODE='"
												+ craft_std_code + "'", "TIC", 0, 0);
								if (sCraft == null || sCraft.size() < 1) {
									msg.setMessage("全程产线号[" + msc_pline + "]向下的工序[" + pro.getProcess_desc() + "]的工序点["
											+ sti.getProcess_point_desc() + "]的工艺标准索引号未指定");
									return false;
								}
								for (ComMscStationRCraft cr : sCraft) {
									tics.addAll(this.getMapper(ComBaseStdCraftTicMapper.class).findWithCondition(null,
											"TIC='" + cr.getTic() + "' and VALIDFLAG='1'", "TIC", 0, 0));
								}

							}
							if (tics == null || tics.size() < 1) {
								msg.setMessage("未找到工序[" + pro.getProcess_desc() + "]向下的工序点["
										+ sti.getProcess_point_desc() + "]的有效的工艺标准索引号");
								return false;
							}
							// 找到质量设计对应的机台信息
							List<SlmOrdDesignMscPlStiMc> mations = this.getMapper(SlmOrdDesignMscPlStiMcMapper.class)
									.findWithCondition(null,
											"DESIGN_KEY='" + sti.getDesign_key() + "' and MSC_PLINE='"
													+ sti.getMsc_pline() + "' and BACKLOG_SEQ='" + sti.getBacklog_seq()
													+ "' and PROCESS_CODE='" + sti.getProcess_code()
													+ "' and PROCESS_POINT_CODE='" + sti.getProcess_point_code() + "' and PLINE_CODE='"+pline.getPline_code()+"'",
											null, 0, 0);

							if (mations == null || mations.size() < 1) {
								continue;
							}
							// 根据质量设计的机台信息，过滤工艺索引号
							for (SlmOrdDesignMscPlStiMc mat : mations) {
								// 目标tic
								List<ComBaseStdCraftTic> tarTic = new ArrayList<ComBaseStdCraftTic>();
								for (int i = 0; i < tics.size(); i++) {
									if (tics.get(i).getMachine_code() != null
											&& !"".equals(tics.get(i).getMachine_code())) {

										if (!tics.get(i).getMachine_code().contains(mat.getMachine_code())) {
											continue;
										}

										// 过滤适用规格
										boolean pass = FilterStiSize(tics.get(i), orderLine);

										if (!pass) {
											continue;
										}
										tarTic.add(tics.get(i));

									} else {
										// 工艺索引号适用机台为空，适用所有， 过滤适用规格
										boolean pass = FilterStiSize(tics.get(i), orderLine);

										if (!pass) {
											continue;
										}
										tarTic.add(tics.get(i));

									}

								}

								if (tarTic == null || tarTic.size() < 1) {
									msg.setMessage(
											"未找到工序[" + pro.getProcess_desc() + "]向下的工序点[" + sti.getProcess_point_desc()
													+ "]适应机台[" + mat.getMachine_name() + "]的工艺标准索引号");
									return false;
								}

								if (tarTic != null && tarTic.size() > 1) {
									msg.setMessage(
											"找到多条工序[" + pro.getProcess_desc() + "]向下的工序点[" + sti.getProcess_point_desc()
													+ "]适应机台[" + mat.getMachine_name() + "]的工艺标准索引号");
									return false;
								}

								ComBaseStdCraftTic oneTic = tarTic.get(0);
								// 查询工序点关联的工艺标准项目
								List<ComBaseStationRCraft> stationCraft = new ArrayList<ComBaseStationRCraft>();
								stationCraft = this.getMapper(ComBaseStationRCraftMapper.class)
										.findWithCondition(null,
												"PROCESS_CODE='" + sti.getProcess_code() + "' and PROCESS_POINT_CODE='"
														+ sti.getProcess_point_code() + "' and VALIDFLAG='1'",
												"SEQ", 0, 0);
								// 查询标准索引号对应的项目
								List<ComBaseStdCraftTicItem> items = this.getMapper(ComBaseStdCraftTicItemMapper.class)
										.findWithCondition(null, "TIC='" + oneTic.getTic() + "' and VALIDFLAG='1'",
												"INDEX_SEQ", 0, 0);
								// 判断工序点关联的工艺标准是否在工艺标准索引号向下存在
								for (ComBaseStationRCraft si : stationCraft) {
									List<ComBaseStdCraftTicItem> ranItem = new ArrayList<ComBaseStdCraftTicItem>();
									for (int j = 0; j < items.size(); j++) {

										if (si.getCraft_param_code().equals(items.get(j).getCraft_param_code())) {

											if ((items.get(j).getMin_value() == null
													|| "".equals(items.get(j).getMin_value()))
													&& (items.get(j).getMax_value() == null
															|| "".equals(items.get(j).getMax_value()))) {
												msg.setMessage("工序[" + pro.getProcess_desc() + "]向下的工序点["
														+ sti.getProcess_point_desc() + "]的工艺标准["
														+ items.get(j).getCraft_param_desc() + "]的标准上下限值同时为空");
												return false;
											}

											ranItem.add(items.get(j));

										}

									}
									if (ranItem == null || ranItem.size() < 1) {
										msg.setMessage(
												"工序[" + pro.getProcess_desc() + "]向下的工序点[" + sti.getProcess_point_desc()
														+ "]的工艺标准[" + si.getCraft_param_desc() + "]的未找到标准信息");
										return false;
									}

									for (ComBaseStdCraftTicItem ran : ranItem) {
										// 查询项目是否已经添加是否存在

										if (ITEM != null && ITEM.size() > 0) {
											for (int i = 0; i < ITEM.size(); i++) {
												if ((ran.getTic() + ran.getCraft_param_code() + ran.getIndex_seq())
														.equals(ITEM.get(i).getTic() + ITEM.get(i).getCraft_param_code()
																+ ITEM.get(i).getIndex_seq())) {
													break;
												}

												if (i == ITEM.size() - 1l) {
													SlmOrdDesignMscTicItem item = new SlmOrdDesignMscTicItem();
													DataHelper.CopyEntity(ran, item);
													item.setDesign_key(designKey);
													ITEM.add(item);
													break;
												}

											}

										} else {
											
											SlmOrdDesignMscTicItem item = new SlmOrdDesignMscTicItem();
											DataHelper.CopyEntity(ran, item);
											item.setDesign_key(designKey);
											ITEM.add(item);
										}

									}

								}

								// 工艺标准索引号
								SlmOrdDesignMscTic resultTic = new SlmOrdDesignMscTic();
								DataHelper.CopyEntity(oneTic,resultTic);
								resultTic.setDesign_key(designKey);
								resultTic.setMsc(msc.getMsc());
								resultTic.setGuid(pro.getGuid());
								resultTic.setBacklog_seq(pro.getBacklog_seq());
								resultTic.setMachine_code(mat.getMachine_code());
								resultTic.setMachine_name(mat.getMachine_name());
								resultTic.setMsc_pline(pro.getMsc_pline());
								resultTic.setProcess_code(pro.getProcess_code());
								resultTic.setProcess_desc(pro.getProcess_desc());
								resultTic.setProcess_code_c(pro.getProcess_code_c());
								resultTic.setProcess_desc_c(pro.getProcess_desc_c());
								resultTic.setProcess_point_code(sti.getProcess_point_code());
								resultTic.setProcess_point_desc(sti.getProcess_point_desc());
								resultTic.setPline_code(pline.getPline_code());
								resultTic.setPline_name(pline.getPline_name());
								resultTic.setTic(tarTic.get(0).getTic());
								TIC.add(resultTic);
							}

						}

					}

				}

			}
			// 插入质量设计结果表
			for (SlmOrdDesignMscTic tic : TIC) {
				this.getMapper(SlmOrdDesignMscTicMapper.class).insert(tic);
			}

			Integer index_seq = 1;
			for (SlmOrdDesignMscTicItem item : ITEM) {
				item.setIndex_seq(index_seq++);
				this.getMapper(SlmOrdDesignMscTicItemMapper.class).insert(item);
			}

		} catch (Exception e) {
			e.printStackTrace();
			msg.setMessage("工艺验证异常" + e.getMessage());
			return false;
		}

		msg.setMessage("工艺标准验证通过");
		return true;
	}

	/**
	 * 过滤工艺标准索引号规格
	 * 
	 * @param comBaseStdCraftTic
	 * @param orderLine
	 * @return
	 */

	private boolean FilterStiSize(ComBaseStdCraftTic tic, SlmContractProcessPlan orderLine) {

		boolean pass;
		// 厚度
		if (tic.getThick_min() != null) {

			pass = DataHelper.checkSignAndValue(tic.getThick_min_sign(), tic.getThick_min(),
					DataHelper.GetString(orderLine.getThick()));

		} else {
			pass = true;
		}
		if (!pass) {
			return false;
		}

		if (tic.getThick_max() != null) {

			pass = DataHelper.checkSignAndValue(tic.getThick_max_sign(), tic.getThick_max(),
					DataHelper.GetString(orderLine.getThick()));
		} else {
			pass = true;
		}
		if (!pass) {
			return false;
		}

		// 宽度
		if (tic.getWidth_min() != null) {
			pass = DataHelper.checkSignAndValue(tic.getWidth_min_sign(), tic.getWidth_min(),
					DataHelper.GetString(orderLine.getWidth()));
		} else {
			pass = true;
		}
		if (!pass) {
			return false;
		}

		if (tic.getWidth_max() != null) {

			pass = DataHelper.checkSignAndValue(tic.getWidth_max_sign(), tic.getWidth_max(),
					DataHelper.GetString(orderLine.getWidth()));
		} else {
			pass = true;
		}
		if (!pass) {
			return false;
		}

		// 长度

		if (tic.getLength_min() != null) {

			pass = DataHelper.checkSignAndValue(tic.getLength_min_sign(), tic.getLength_min(),
					DataHelper.GetString(orderLine.getLength()));
		} else {
			pass = true;
		}
		if (!pass) {
			return false;
		}

		if (tic.getLength_max() != null) {
			pass = DataHelper.checkSignAndValue(tic.getLength_max_sign(), tic.getLength_max(),
					DataHelper.GetString(orderLine.getLength()));
		} else {
			pass = true;
		}
		if (!pass) {
			return false;
		}

		return true;
	}

	private List<ComBaseStdCraft> FilterSize(List<ComBaseStdCraft> craft, SlmContractProcessPlan orderLine) {

		List<ComBaseStdCraft> tar = new ArrayList<ComBaseStdCraft>();

		for (ComBaseStdCraft car : craft) {
			boolean pass;
			// 厚度
			if (car.getRough_thick_min() != null) {
				pass = DataHelper.checkSignAndValue(car.getRough_thick_min_sign(), car.getRough_thick_min(),
						DataHelper.GetString(orderLine.getThick()));

			} else {
				pass = true;
			}

			if (!pass) {
				continue;
			}

			if (car.getRough_thick_max() != null) {
				pass = DataHelper.checkSignAndValue(car.getRough_thick_max_sign(), car.getRough_thick_max(),
						DataHelper.GetString(orderLine.getThick()));

			} else {
				pass = true;
			}

			if (!pass) {
				continue;
			}

			// 宽度
			if (car.getRough_width_min() != null) {
				pass = DataHelper.checkSignAndValue(car.getRough_width_min_sign(), car.getRough_width_min(),
						DataHelper.GetString(orderLine.getWidth()));

			} else {
				pass = true;
			}

			if (!pass) {
				continue;
			}

			if (car.getRough_width_max() != null) {
				pass = DataHelper.checkSignAndValue(car.getRough_width_max_sign(), car.getRough_width_max(),
						DataHelper.GetString(orderLine.getWidth()));

			} else {
				pass = true;
			}

			if (!pass) {
				continue;
			}

			// 长度
			if (car.getRough_length_min() != null) {
				pass = DataHelper.checkSignAndValue(car.getRough_length_min_sign(), car.getRough_length_min(),
						DataHelper.GetString(orderLine.getLength()));

			} else {
				pass = true;
			}

			if (!pass) {
				continue;
			}

			if (car.getRough_length_max() != null) {
				pass = DataHelper.checkSignAndValue(car.getRough_length_max_sign(), car.getRough_length_max(),
						DataHelper.GetString(orderLine.getWidth()));

			} else {
				pass = true;
			}

			if (!pass) {
				continue;
			}

			tar.add(car);

		}

		return tar;
	}

	/**
	 * 制造规范界面过滤工艺标准号
	 */
	public ResultModel FitlerCraft(CommonPage<ComBaseStdCraft> com) {

		ResultModel rm = new ResultModel();

		try {

			List<ComBaseStdCraft> Target = new ArrayList<ComBaseStdCraft>();
			ComBaseStdCraft craft = com.getObject();
			String process_code = craft.getProcess_code();
			String std_code = craft.getStd_code();
			String steel_code = craft.getSteel_code();
			String prod_code = craft.getProd_code();
			String delivery_state_code = craft.getDelivery_state_code();
			String[] delivery = null;

			List<ComMscProcess> process = this.getMapper(ComMscProcessMapper.class).findWithCondition(null,
					"MSC='" + craft.getMsc() + "' and MSC_PLINE='" + craft.getMsc_pline() + "' and BACKLOG_SEQ='"
							+ craft.getBacklog_seq() + "' and PROCESS_CODE='" + craft.getProcess_code()
							+ "' and PROCESS_CODE_C='" + craft.getProcess_code_c() + "' and VALIDFLAG='1'",
					null, 0, 0);
			if (process == null || process.size() < 1) {
				rm.setMsgInfo("该工序信息未查询到");
				rm.setState("500");
				return rm;

			}
			String process_guid = process.get(0).getGuid();

			List<ComMscStation> stitions = new ArrayList<ComMscStation>();
			stitions = this.getMapper(ComMscStationMapper.class).findWithCondition(null,
					"MSC='" + process.get(0).getMsc() + "' and MSC_PLINE='" + process.get(0).getMsc_pline()
							+ "' and BACKLOG_SEQ='" + process.get(0).getBacklog_seq() + "' and PROCESS_CODE='"
							+ process.get(0).getProcess_code() + "' and PROCESS_CODE_C='"
							+ process.get(0).getProcess_code_c() + "' and VALIDFLAG='1'",
					null, 0, 0);

			List<ComBaseStdCraft> tarCraft = new ArrayList<ComBaseStdCraft>();
			tarCraft = this.getMapper(ComBaseStdCraftMapper.class).findWithCondition(null,
					"VALIDFLAG='1' and PROCESS_CODE='" + process_code + "'", "CRAFT_STD_CODE", 0, 0);
			// 过滤
			// 过滤适用产品名称
			for (int i = 0; i < tarCraft.size(); i++) {
				if (tarCraft.get(i).getProd_code() != null && !"".equals(tarCraft.get(i).getProd_code())) {
					if (!tarCraft.get(i).getProd_code().contains(prod_code)) {
						tarCraft.remove(i);
						i--;
						continue;
					}
				}

			}
			// 过滤订货标准
			for (int i = 0; i < tarCraft.size(); i++) {
				if (tarCraft.get(i).getStd_code() != null && !"".equals(tarCraft.get(i).getStd_code())) {
					if (!tarCraft.get(i).getStd_code().contains(std_code)) {
						tarCraft.remove(i);
						i--;
						continue;
					}
				}

			}

			// 过滤牌号
			for (int i = 0; i < tarCraft.size(); i++) {
				if (tarCraft.get(i).getSteel_code() != null && !"".equals(tarCraft.get(i).getSteel_code())) {
					if (!tarCraft.get(i).getSteel_code().contains(steel_code)) {
						tarCraft.remove(i);
						i--;
						continue;
					}
				}

			}

			// 过滤交货状态
			if (delivery_state_code != null && !"".equals(delivery_state_code)) {
				delivery = delivery_state_code.split(",");
				for (int i = 0; i < tarCraft.size(); i++) {

					for (int j = 0; j < delivery.length; j++) {
						if (tarCraft.get(i).getDelivery_state_code() != null
								&& !"".equals(tarCraft.get(i).getDelivery_state_code())) {
							if (tarCraft.get(i).getDelivery_state_code().contains(delivery[j])) {
								break;
							}

						} else {
							break;
						}

						if (j == delivery.length - 1) {
							tarCraft.remove(i);
							i--;
						}
					}

				}
			}

			

			// 过滤规格

			Target = FilterSize(tarCraft, craft);

			for (ComBaseStdCraft tar : Target) {
				tar.setGuid(process_guid);
				ComMscProcessRCraft oldProcess = this.getMapper(ComMscProcessRCraftMapper.class).findById(tar.getGuid(),
						tar.getCraft_std_code());
				if (oldProcess != null) {
					tar.setIs_sure("1");
				} else {
					tar.setIs_sure("0");
				}

				// 根据工艺标准码查询工艺标准索引号
				List<ComBaseStdCraftTic> tics = this.getMapper(ComBaseStdCraftTicMapper.class).findWithCondition(null,
						"CRAF_STD_CODE='" + tar.getCraft_std_code() + "' and VALIDFLAG='1'", "TIC", 0, 0);

				// 过滤不需要进行工艺控制的工序点
				if (stitions != null && stitions.size() > 0) {

					for (int i = 0; i < stitions.size(); i++) {
						ComBaseStation baseStation = this.getMapper(ComBaseStationMapper.class)
								.findById(stitions.get(i).getProcess_point_code());
						if (baseStation != null) {
							if ("0".equals(baseStation.getIs_craft())) {
								stitions.remove(i);
								i--;
								continue;
							}
						}

					}

					if (stitions == null || stitions.size() < 1) {
						continue;
					}

					for (int i = 0; i < tics.size(); i++) {

						for (int j = 0; j < stitions.size(); j++) {

							if (tics.get(i).getProcess_point_code().equals(stitions.get(j).getProcess_point_code())) {
								tics.get(i).setGuid(stitions.get(j).getGuid());
								break;
							}

							if (j == stitions.size() - 1) {
								tics.remove(i);
								i--;
								break;
							}

						}

					}

					// 过滤成品厚度宽度长度
					ComMscBacklog backlog = craft.getBacklog();
					List<ComBaseStdCraftTic> finallyTic = new ArrayList<ComBaseStdCraftTic>();
					if (backlog != null) {
						finallyTic = FilterSignStation(tics, backlog);
					}
					tar.setTics(finallyTic);

				}

			}

			rm.setData(Target);
			rm.setState("200");

		} catch (Exception e) {
			e.printStackTrace();
			rm.setMsgInfo("查询失败");
			return rm;

		}

		return rm;

	}

	private List<ComBaseStdCraftTic> FilterSignStation(List<ComBaseStdCraftTic> tics, ComMscBacklog backlog) {

		List<ComBaseStdCraftTic> tarTic = new ArrayList<ComBaseStdCraftTic>();
		for (ComBaseStdCraftTic tic : tics) {

			boolean pass;
			// 厚度
			if (tic.getThick_min() != null) {

				pass = DataHelper.checkSignAndValue(tic.getThick_min_sign(), backlog.getThick_min_sign(),
						tic.getThick_min(), DataHelper.GetString(backlog.getThick_min()));

			} else {
				pass = true;
			}
			if (!pass) {
				continue;
			}

			if (tic.getThick_max() != null) {

				pass = DataHelper.checkSignAndValue(tic.getThick_max_sign(), backlog.getThick_max_sign(),
						tic.getThick_max(), DataHelper.GetString(backlog.getThick_max()));

			} else {
				pass = true;
			}
			if (!pass) {
				continue;
			}

			// 宽度
			if (tic.getWidth_min() != null) {
				pass = DataHelper.checkSignAndValue(tic.getWidth_min_sign(), backlog.getWidth_min_sign(),
						tic.getWidth_min(), DataHelper.GetString(backlog.getWidth_min()));

			} else {
				pass = true;
			}
			if (!pass) {
				continue;
			}

			if (tic.getWidth_max() != null) {
				pass = DataHelper.checkSignAndValue(tic.getWidth_max_sign(), backlog.getWidth_max_sign(),
						tic.getWidth_max(), DataHelper.GetString(backlog.getWidth_max()));

			} else {
				pass = true;
			}
			if (!pass) {
				continue;
			}

			// 长度

			if (tic.getLength_min() != null) {
				pass = DataHelper.checkSignAndValue(tic.getLength_min_sign(), backlog.getLenth_min_sign(),
						tic.getLength_min(), DataHelper.GetString(backlog.getLenth_min()));

			} else {
				pass = true;
			}
			if (!pass) {
				continue;
			}

			if (tic.getLength_max() != null) {
				pass = DataHelper.checkSignAndValue(tic.getLength_max_sign(), backlog.getLenth_max_sign(),
						tic.getLength_max(), DataHelper.GetString(backlog.getLenth_max()));

			} else {
				pass = true;
			}
			if (!pass) {
				continue;
			}

			ComMscStationRCraft tarStation = this.getMapper(ComMscStationRCraftMapper.class).findById(tic.getGuid(),
					tic.getTic(), tic.getCraf_std_code());
			if (tarStation != null) {
				tic.setIs_sure("1");
			} else {
				tic.setIs_sure("0");
			}
			tarTic.add(tic);

		}

		return tarTic;
	}

	/**
	 * 过滤双符号标准
	 * 
	 * @param tarCraft
	 * @param craft
	 * @return
	 */
	private List<ComBaseStdCraft> FilterSize(List<ComBaseStdCraft> tarCraft, ComBaseStdCraft craft) {

		List<ComBaseStdCraft> finallyCraft = new ArrayList<ComBaseStdCraft>();

		if (tarCraft != null && tarCraft.size() > 0) {
			for (ComBaseStdCraft car : tarCraft) {
				boolean pass;
				// 厚度
				if (car.getRough_thick_min() != null) {
					pass = DataHelper.checkSignAndValue(car.getRough_thick_min_sign(), craft.getRough_thick_min_sign(),
							car.getRough_thick_min(), DataHelper.GetString(craft.getRough_thick_min()));

				} else {
					pass = true;
				}

				if (!pass) {
					continue;
				}

				if (car.getRough_thick_max() != null) {
					pass = DataHelper.checkSignAndValue(car.getRough_thick_max_sign(), craft.getRough_thick_max_sign(),
							car.getRough_thick_max(), DataHelper.GetString(craft.getRough_thick_max()));

				} else {
					pass = true;
				}

				if (!pass) {
					continue;
				}

				// 宽度
				if (car.getRough_width_min() != null) {

					pass = DataHelper.checkSignAndValue(car.getRough_width_min_sign(), craft.getRough_width_min_sign(),
							car.getRough_width_min(), DataHelper.GetString(craft.getRough_width_min()));

				} else {
					pass = true;
				}

				if (!pass) {
					continue;
				}

				if (car.getRough_width_max() != null) {
					pass = DataHelper.checkSignAndValue(car.getRough_width_max_sign(), craft.getRough_width_max_sign(),
							car.getRough_width_max(), DataHelper.GetString(craft.getRough_width_max()));

				} else {
					pass = true;
				}

				if (!pass) {
					continue;
				}

				// 长度
				if (car.getRough_length_min() != null) {

					pass = DataHelper.checkSignAndValue(car.getRough_length_min_sign(),
							craft.getRough_length_min_sign(), car.getRough_length_min(),
							DataHelper.GetString(craft.getRough_length_min()));

				} else {
					pass = true;
				}

				if (!pass) {
					continue;
				}

				if (car.getRough_length_max() != null) {
					pass = DataHelper.checkSignAndValue(car.getRough_length_max_sign(),
							craft.getRough_length_max_sign(), car.getRough_length_max(),
							DataHelper.GetString(craft.getRough_length_max()));

				} else {
					pass = true;
				}

				if (!pass) {
					continue;
				}

				finallyCraft.add(car);

			}

		}

		return finallyCraft;
	}

	/**
	 * 制造规范界面确认工艺标准
	 * 
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel sureCraft(CommonPage<ComMscProcessRCraft> com) throws Exception {
		ResultModel rm = new ResultModel();

		try {
			List<ComMscProcessRCraft> tics = com.getObjectList();
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			// 删除项目
			if (tics != null && tics.size() > 0) {

				for (ComMscProcessRCraft craft : tics) {
					this.getMapper(ComMscProcessRCraftMapper.class).delete(craft.getGuid(), craft.getCraft_std_code());
					List<ComMscStationRCraft> stition = craft.getTics();
					for (ComMscStationRCraft sti : stition) {
						this.getMapper(ComMscStationRCraftMapper.class).delete(sti.getGuid(), sti.getTic(),
								sti.getCraft_std_code());
					}

				}

				for (ComMscProcessRCraft craft : tics) {

					if ("1".equals(craft.getIs_sure())) {
						craft.setCreate_name(userName);
						craft.setCreate_time(new Date());
						this.getMapper(ComMscProcessRCraftMapper.class).insert(craft);
					}

					List<ComMscStationRCraft> stition = craft.getTics();
					for (ComMscStationRCraft sti : stition) {
						if ("1".equals(sti.getIs_sure())) {
							sti.setCreate_name(userName);
							sti.setCreate_time(new Date());
							this.getMapper(ComMscStationRCraftMapper.class).insert(sti);

						}

					}

				}

			}

			rm.setData(tics);
			rm.setMsgInfo("保存成功");

		} catch (Exception e) {

			e.printStackTrace();
			throw new Exception("保存失败");

		}

		return rm;
	}

	/**
	 * 查询已经关联的工艺要求
	 */

	/*
	 * public ResultModel getSureCraft(CommonPage<ComMscProcessRCraft> com) {
	 * ResultModel rm = new ResultModel(); try {
	 * 
	 * List<ComMscProcessRCraft> craft = com.getObjectList();
	 * 
	 * if (craft!=null && craft.size()>0) { for (int i = 0; i < craft.size();
	 * i++) { //查询中间表是否有该数据，没有的话删除 ComMscProcessRCraft
	 * only=this.getMapper(ComMscProcessRCraftMapper.class).findById(craft.get(i
	 * ).getGuid(),craft.get(i).getCraft_std_code()); if (condition) {
	 * 
	 * } }
	 * 
	 * }
	 * 
	 * 
	 * 
	 * 
	 * 
	 * for (ComMscProcessRCraft cr : craft) { List<ComMscStationRCraft> sta =
	 * this.getMapper(ComMscStationRCraftMapper.class).findWithCondition(null,
	 * "CRAFT_STD_CODE='" + cr.getCraft_std_code() + "", null, 0, 0);
	 * cr.setTics(sta); }
	 * 
	 * rm.setState("200"); rm.setData(craft);
	 * 
	 * } catch (Exception e) { rm.setMsgInfo("查询失败"); rm.setState("500"); return
	 * rm; }
	 * 
	 * return rm; }
	 */

	public ResultModel getStation(CommonPage<SlmOrdDesignMscPlStiMc> com) throws Exception {
		ResultModel rm = new ResultModel();
		try {

			SlmOrdDesignMscPlStiMc mation = com.getObject();
			String condition = "1=1";
			if (mation == null) {
				return rm;
			}
			if (mation.getMsc_pline() != null && !"".equals(mation.getMsc_pline())) {
				condition += " and MSC_PLINE='" + mation.getMsc_pline() + "'";
			}
			if (mation.getDesign_key() != null && !"".equals(mation.getDesign_key())) {
				condition += " and DESIGN_KEY='" + mation.getDesign_key() + "'";
			}
			if (!"".equals(mation.getBacklog_seq())) {
				condition += " and BACKLOG_SEQ='" + mation.getBacklog_seq() + "'";
			}
			if (mation.getProcess_code() != null && !"".equals(mation.getProcess_code())) {
				condition += " and PROCESS_CODE='" + mation.getProcess_code() + "'";
			}
			if (mation.getPline_code() != null && !"".equals(mation.getPline_code())) {
				condition += " and PLINE_CODE='" + mation.getPline_code() + "'";
			}

			List<SlmOrdDesignMscPlStiMc> stations = this.getMapper(SlmOrdDesignMscPlStiMcMapper.class)
					.findWithCondition(null, condition, null, 0, 0);
			rm.setState("200");
			rm.setData(stations);

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			return rm;
		}

		return rm;
	}

	public ResultModel getCraftResult(CommonPage<SlmOrdDesignMscPlStiMc> com) throws Exception {
		ResultModel rm = new ResultModel();
		try {

			SlmOrdDesignMscPlStiMc mation = com.getObject();
			String condition = "1=1";
			if (mation == null) {
				return rm;
			}
			if (mation.getMsc_pline() != null && !"".equals(mation.getMsc_pline())) {
				condition += " and MSC_PLINE='" + mation.getMsc_pline() + "'";
			}
			if (mation.getDesign_key() != null && !"".equals(mation.getDesign_key())) {
				condition += " and DESIGN_KEY='" + mation.getDesign_key() + "'";
			}
			if (!"".equals(mation.getBacklog_seq())) {
				condition += " and BACKLOG_SEQ='" + mation.getBacklog_seq() + "'";
			}
			if (mation.getProcess_code() != null && !"".equals(mation.getProcess_code())) {
				condition += " and PROCESS_CODE='" + mation.getProcess_code() + "'";
			}
			if (mation.getPline_code() != null && !"".equals(mation.getPline_code())) {
				condition += " and PLINE_CODE='" + mation.getPline_code() + "'";
			}
			if (mation.getProcess_point_code() != null && !"".equals(mation.getProcess_point_code())) {
				condition += " and PROCESS_POINT_CODE='" + mation.getProcess_point_code() + "'";
			}

			List<SlmOrdDesignMscTic> tics = this.getMapper(SlmOrdDesignMscTicMapper.class).findWithCondition(null,
					condition, null, 0, 0);

			for (SlmOrdDesignMscTic tic : tics) {
				List<SlmOrdDesignMscTicItem> items = this.getMapper(SlmOrdDesignMscTicItemMapper.class)
						.findWithCondition(null,
								"DESIGN_KEY='" + tic.getDesign_key() + "' and TIC='" + tic.getTic() + "'", null, 0, 0);
				tic.setItems(items);
			}

			rm.setData(tics);
			rm.setState("200");

		} catch (Exception e) {

			e.printStackTrace();
			rm.setState("500");
			return rm;
		}

		return rm;
	}

}