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

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

import com.steering.pss.sale.order.model.DesignMscBiltCPlEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscBiltPlEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGxPrlEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;

public class ServerDesignTool {
	/**
	 * 过滤轧管适用产线
	 * 
	 * @param proLines
	 * @return
	 */
	public static List<DesignMscGzGgPlEntity> checkComSpecPline(
			List<DesignMscGzGgPlEntity> proLines) {
		if (proLines == null || proLines.size() == 0) {
			return null;
		}
		List<DesignMscGzGgPlEntity> list = new ArrayList<DesignMscGzGgPlEntity>();
		List<String> strs = new ArrayList<String>();
		for (DesignMscGzGgPlEntity proLine : proLines) {
			String str = proLine.getDesginKey() + proLine.getMscPline()
					+ proLine.getPlineCode() + proLine.getSpecCode();
			if (!strs.contains(str)) {
				strs.add(str);
				list.add(proLine);
			}
		}
		return list;
	}

	/**
	 * 过滤适用产线
	 * 
	 * @param proLines
	 * @return
	 */
	public static List<DesignMscPlGxPrlEntity> fiterProLine(
			List<DesignMscPlGxPrlEntity> proLines) {
		if (proLines == null || proLines.size() == 0) {
			return null;
		}
		List<DesignMscPlGxPrlEntity> list = new ArrayList<DesignMscPlGxPrlEntity>();
		List<String> strs = new ArrayList<String>();
		for (DesignMscPlGxPrlEntity proLine : proLines) {
			String str = proLine.getDesginKey() + proLine.getBacklogSeq()
					+ proLine.getMscPline() + proLine.getPlineCode()
					+ proLine.getProcessCode();
			if (!strs.contains(str)) {
				strs.add(str);
				list.add(proLine);
			}
		}
		return list;
	}

	/**
	 * 过滤坯料产线
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<DesignMscBiltCPlEntity> fiterBiltCSpecLine(
			List<DesignMscBiltCPlEntity> biltsCSpecLines) {
		if (biltsCSpecLines == null || biltsCSpecLines.size() == 0) {
			return null;
		}
		List<DesignMscBiltCPlEntity> list = new ArrayList<DesignMscBiltCPlEntity>();
		List<String> keys = new ArrayList<String>();
		for (DesignMscBiltCPlEntity biltsCSpecLine : biltsCSpecLines) {
			String key = biltsCSpecLine.getDesginKey()
					+ biltsCSpecLine.getMscPline()
					+ biltsCSpecLine.getCSpecCode()
					+ biltsCSpecLine.getPlineCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltsCSpecLine);
			}
		}
		return list;
	}

	/**
	 * 过滤轧管规格
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<DesignMscGzGgEntity> fiterRollSpec(
			List<DesignMscGzGgEntity> rollSpecs) {
		if (rollSpecs == null || rollSpecs.size() == 0) {
			return null;
		}
		List<DesignMscGzGgEntity> list = new ArrayList<DesignMscGzGgEntity>();
		List<String> keys = new ArrayList<String>();
		for (DesignMscGzGgEntity rollSpec : rollSpecs) {
			String key = rollSpec.getDesginKey() + rollSpec.getMscPline()
					+ rollSpec.getSpecCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(rollSpec);
			}
		}
		return list;
	}

	/**
	 * 过滤锻造规格
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<DesignMscBiltEntity> fiterBiltSpec(
			List<DesignMscBiltEntity> biltSpecs) {
		if (biltSpecs == null || biltSpecs.size() == 0) {
			return null;
		}
		List<DesignMscBiltEntity> list = new ArrayList<DesignMscBiltEntity>();
		List<String> keys = new ArrayList<String>();
		for (DesignMscBiltEntity biltSpec : biltSpecs) {
			String key = biltSpec.getDesginKey() + biltSpec.getMscPline()
					+ biltSpec.getSpecCode() + biltSpec.getbSpecCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltSpec);
			}
		}
		return list;
	}

	/**
	 * 过滤断面产线
	 * 
	 * @param biltSpecLines
	 *            断面产线集合
	 * @return List<DesignMscBiltPlEntity>
	 */
	public static List<DesignMscBiltPlEntity> fiterBiltSpecLine(
			List<DesignMscBiltPlEntity> biltSpecLines) {
		if (biltSpecLines == null || biltSpecLines.size() == 0) {
			return null;
		}
		List<DesignMscBiltPlEntity> list = new ArrayList<DesignMscBiltPlEntity>();
		List<String> keys = new ArrayList<String>();
		for (DesignMscBiltPlEntity biltSpecLine : biltSpecLines) {
			String key = biltSpecLine.getDesginKey()
					+ biltSpecLine.getMscPline() + biltSpecLine.getBSpecCode()
					+ biltSpecLine.getPlineCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltSpecLine);
			}
		}
		return list;
	}

	/**
	 * 过滤坯料规格
	 * 
	 * @param biltsCSpecLines
	 * @return List<DesignMscBiltCPlEntity>
	 */
	public static List<DesignMscBiltCEntity> fiterBiltCSpec(
			List<DesignMscBiltCEntity> biltSpecs) {
		if (biltSpecs == null || biltSpecs.size() == 0) {
			return null;
		}
		List<DesignMscBiltCEntity> list = new ArrayList<DesignMscBiltCEntity>();
		List<String> keys = new ArrayList<String>();
		for (DesignMscBiltCEntity biltSpec : biltSpecs) {
			String key = biltSpec.getDesginKey() + biltSpec.getMscPline()
					+ biltSpec.getSpecCode() + biltSpec.getBSpecCode()
					+ biltSpec.getCSpecCode();
			if (!keys.contains(key)) {
				keys.add(key);
				list.add(biltSpec);
			}
		}
		return list;
	}

	/**
	 * 压缩比计算方法
	 * 
	 * @param rollSpecs
	 * @param biltsSpecs
	 * @return
	 */
	public static boolean compressioRatio(List<DesignMscGzGgEntity> rollSpecs,
			List<DesignMscBiltCEntity> biltsSpecs) {
		if (rollSpecs == null || rollSpecs.size() == 0) {
			return true;
		}
		if (biltsSpecs == null || biltsSpecs.size() == 0) {
			return true;
		}
		for (DesignMscGzGgEntity rollSpec : rollSpecs) {
			double dimater = Double.parseDouble(rollSpec.getDimater());
			double height = Double.parseDouble(rollSpec.getHeight());
			for (DesignMscBiltCEntity biltsSpec : biltsSpecs) {
				double specName = Double.parseDouble(biltsSpec.getCSpecName());
				// 坯料断面=πD²/4
				double blank = Math.PI * specName * specName / 4;
				// 轧管断面=πD1²/4-πD2²/4 “D1”为轧管外径；“D2”=轧管外径-轧管壁厚×2
				double roll = Math.PI * dimater * dimater / 4 - Math.PI
						* (dimater - height * 2) * (dimater - height * 2) / 4;
				if (blank / roll <= 3) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 查询断面规格集
	 * 
	 * @param biltCSpecs
	 *            断面规格集合
	 * @return List<String>
	 * @throws SQLException
	 */
	public static List<Double> getBiltSpecNum(
			List<DesignMscBiltEntity> biltSpecs,
			List<DesignMscBiltCEntity> biltCSpecs, String specCode)
			throws SQLException {
		List<Double> list = new ArrayList<Double>();
		if (biltSpecs != null && biltSpecs.size() > 0) {
			biltSpecs = ServerDesignTool.fiterBiltByRollSpec(biltSpecs,
					specCode);
			for (DesignMscBiltEntity biltSpec : biltSpecs) {
				list.add(Double.parseDouble(biltSpec.getbSpecName()));
			}
		} else {
			biltCSpecs = ServerDesignTool.fiterBiltCByRollSpec(biltCSpecs,
					specCode);
			for (DesignMscBiltCEntity biltCSpec : biltCSpecs) {
				list.add(Double.parseDouble(biltCSpec.getCSpecName()));
			}
		}

		return list;
	}

	/**
	 * 通过轧管规格过滤坯料规格
	 * 
	 * @param bilts
	 * @param specCode
	 *            轧管规格
	 * @return
	 */
	public static List<DesignMscBiltCEntity> fiterBiltCByRollSpec(
			List<DesignMscBiltCEntity> bilts, String specCode) {
		List<DesignMscBiltCEntity> fitBiltSpecs = new ArrayList<DesignMscBiltCEntity>();
		for (DesignMscBiltCEntity bilt : bilts) {
			if (bilt.getSpecCode().equals(specCode)) {
				fitBiltSpecs.add(bilt);
			}
		}
		return fitBiltSpecs;
	}

	/**
	 * 通过轧管规格过滤断面规格
	 * 
	 * @param blanks
	 * @param specCode
	 *            轧管规格
	 * @return
	 */
	public static List<DesignMscBiltEntity> fiterBiltByRollSpec(
			List<DesignMscBiltEntity> blanks, String specCode) {
		List<DesignMscBiltEntity> biltSpecs = new ArrayList<DesignMscBiltEntity>();
		for (DesignMscBiltEntity blank : blanks) {
			if (blank.getSpecCode().equals(specCode)) {
				biltSpecs.add(blank);
			}
		}
		return biltSpecs;
	}

}
