package com.steering.pss.plan.order;

/*
 * 长度计算方法类
 * @author 杨天海
 * @version v1.1
 * @date 2015.05.22
 */
public class CoreLengthCompute{
	/*
	 * 圆周率
	 */
	private static double Pi = 3.1415926;		//圆周率
	
	/*
	 * 计算过程
	 */
	public static LenComputeOutParam comput(LenComputeParam param){
		LenComputeOutParam paramOut = new LenComputeOutParam();
		
		double outdiameter = param.getOutdiameter();		//外径
		double wallthick = param.getWallthick();		//壁厚
		double wallLower = param.getWallLower();		//壁厚公差下限(mm)
		double wallUpper = param.getWallUpper();		//壁厚公差上限(mm)
		boolean shrt_stove = param.getShrtstove();		//短尺随炉
		double sigl_len = param.getSigllen();		//单倍尺长
		double burning = param.getBurning();			//烧损
		double Diameter = param.getDiameter();		//断面(管坯直径)
		double lengthmax = param.getLengthmax();		//最大长度
		double lengthmin = param.getLengthmin();		//最小长度
		double Cuthead_tail = param.getCutheadtail();	//切头尾
		double Billetmax = param.getBilletmax();		//坯料max(mm)
		double Sizing = param.getSizing();		//定径后max(m)
		double Footmin = param.getFootmin();
		
		double aimwallthick = 0; 	//控制壁厚
		double aimlength = 0;		//目标长度
		int Cutnum = 0;
		double lengthZz = 0;
		double lossprcent = 0;
		double lengthLimit = 0.;		//管极限长度(m)
		double Billet = 0;
		
		
		//壁厚公差标准（内控）
		aimwallthick = ((wallthick - wallLower) + (wallthick + wallUpper)) / 2;

		//管坯到扎管，冷床过程
		double weight_one = 0.02466 * aimwallthick * (outdiameter - aimwallthick);	//管单重
		double weight_m = (7.8*Pi/4*Diameter*Diameter)/1000/1000;//坯料米单重
		double Billet_weight = weight_m*Billetmax/1000;			//坯料重量
		double lengthZzmax = Billet_weight*(1-burning)*1000/weight_one;		//轧制后管长max
		if(Sizing > lengthZzmax){
			lengthLimit = lengthZzmax;
		}
		else{					//轧制长度max大于冷床最长，反推管坯极限长度
			lengthLimit = Sizing;
			lengthZzmax = Sizing;
			Billet_weight = lengthZzmax*weight_one/1000/(1-burning);
			Billetmax = Billet_weight*1000/weight_m;
		}

		///分切过程
		//1.无短尺随炉and无使用单倍长度
		if(!shrt_stove && sigl_len == 0){
			int x = (int)((lengthLimit-Cuthead_tail)/lengthmax);
			int y = (int)((lengthLimit-Cuthead_tail)/lengthmin);
			int z = y - x;
			if(z>=1){
				Cutnum = x + 1;
				lengthZz = lengthLimit;
				aimlength = (lengthZz-Cuthead_tail)/Cutnum;		//成品长度
			}
			else{
				Cutnum = x;
				lengthZz = x*lengthmax + Cuthead_tail;
				aimlength = lengthmax;		//成品长度
			}
		}
		//2.短尺随炉
		if(shrt_stove && sigl_len == 0){
			int x = (int)((lengthLimit-Cuthead_tail)/lengthmax);
			int y = (int)((lengthLimit-Cuthead_tail)/lengthmin);
			int z = y - x;
			if(z>1){
				Cutnum = x + 1;
				lengthZz = lengthLimit;
				aimlength = (lengthZz-Cuthead_tail)/Cutnum;		//成品长度
			}
			else{
				if(((((lengthLimit-Cuthead_tail)/lengthmin)-y)*lengthmin) >= 6.5){
					Cutnum = y + 1;
					lengthZz = lengthLimit;
					aimlength = lengthmin;
				}
				else{
					Cutnum = x;
					lengthZz = x * lengthmax + Cuthead_tail;
					aimlength = lengthmax;		//成品长度
				}
			}
		}
		//3.单倍尺长
		if(sigl_len != 0){
			int a = 0;						//最小倍尺数
			int b = 0;						//最大倍尺数
			int w = 0;						//有多少种分切方案
			double min = lengthmin;			//短尺最短与长度下限比较取小者
			if(Footmin != 0 && Footmin < lengthmin){
				min = Footmin;
			}
			a = (int)(min/sigl_len);			//最小倍尺数
			b = (int)(lengthmax/sigl_len);		//最大倍尺数
			if((a*sigl_len)<min){
				a = a + 1;
			}
			w = b - a + 1;						//有多少种分切方案
			for(int i = 0; i < w; i++){
				double result = (int)(lengthLimit/((b-i)*sigl_len))
				*((b-i)*sigl_len);
				if(lengthZz < result){
					lengthZz = result;
					Cutnum = (int)(lengthLimit/((b-i)*sigl_len));
					aimlength = (b-i)*sigl_len;		//成品长度
				}
			}
			lengthZz = lengthZz + Cuthead_tail;
		}
		
		///损失率
		lossprcent = (lengthZz-(Cutnum*aimlength))/lengthZz*100;
		//铸坯
		Billet_weight = (weight_one*((Cutnum*aimlength)
				+Cuthead_tail))/(1-burning)/1000;
		Billet = Billet_weight*1000/weight_m;
		
		paramOut.setAimwallthick(aimwallthick);
		paramOut.setAimlength(aimlength);
		paramOut.setCutnum(Cutnum);
		paramOut.setLengthZz(lengthZz);
		paramOut.setLossprcent(lossprcent);
		paramOut.setLengthLimit(lengthLimit);
		paramOut.setBillet(Billet);
		return paramOut;
	}

}