package com.steering.pss.plnsaleord.lenthCal;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 求取控制外径壁厚
 * @author tjg 2017-04-23
 */
public class PlnControlOutWallTick {

    /**
     * 求取控制外径壁厚
     * @param param
     * @return
     */
    public static LenCalculateOutObject outWallTick(LenCalculateInObject param) {
        LenCalculateOutObject paramOut = new LenCalculateOutObject();

        double outdiameter = param.getOutdiameter(); // 名义外径
        double wallthick = param.getWallthick(); // 名义壁厚
        double wallLower = param.getWallLower(); // 壁厚公差下限(mm)
        double wallUpper = param.getWallUpper(); // 壁厚公差上限(mm)
        double aimwallthick = 0; // 控制壁厚
        boolean isOutput = true; // 是否能够生产
        String reason = ""; // 不可生产的原因
        double aimOutdiam = 0; // 控制外径
        double outdiamMin = param.getAimLower(); // 外径公差下限(mm)
        double outdiamMax = param.getAimUpper(); // 外径公差上限(mm)

        // #start 判断数据是否正常
        // 外径、壁厚必须大于零
        if (outdiameter <= 0 || wallthick <= 0) {
            isOutput = false;
            reason = "外径、壁厚必须大于零！";
            paramOut.setisOutput(isOutput);
            paramOut.setReason(reason);
            return paramOut;
        }

        // 控制壁厚
        int minTole = 0;
        double aimWall = 0;// 壁厚上限
        if (param.getOrderUnit().equals("米")
                || param.getOrderUnit().equals("英尺")
                || param.getWeighType() == "0"
        // || param.getWeighType() = null
        // && param.getWeighType().contains("理")
        ) {
            minTole = 1;
        }
        else {
            minTole = 0;
        }
        if (param.getOrderUnit().equals("吨")) {
            // 负公差标识为T
            if (minTole == 0) {
                // ([控制壁厚上限]-ABS([控制壁厚下限]))/2
                // aimWall = (wallUpper - Math.abs(wallLower)) / 2;
                aimwallthick = intThe(((wallthick - Math.abs(wallLower)) + (wallthick + wallUpper)) / 2);
            }
            else {
                // [控制壁厚上限]+ABS([控制壁厚下限])>=20
                if (wallUpper + Math.abs(wallLower) > param.getToleranceZone()
                        / 100 * wallthick) {
                    // (20-2*ABS([控制壁厚下限]))/2
                    aimWall = param.getToleranceZone() / 100 * wallthick
                            - Math.abs(wallLower);
                    aimwallthick = intThe(wallthick
                            + (aimWall - Math.abs(wallLower)) / 2);
                }
                else {
                    // aimWall = (wallUpper - Math.abs(wallLower)) / 2;
                    aimwallthick = intThe(((wallthick - Math.abs(wallLower)) + (wallthick + wallUpper)) / 2);
                }
                // aimwallthick = intThe((wallthick * (1 + aimWall / 100) * 100
                // + 0.8) / 100);

            }
        }
        else {
            if (wallUpper + Math.abs(wallLower) > param.getToleranceZone()
                    / 100 * wallthick) {
                // (20-2*ABS([控制壁厚下限]))/2
                aimWall = param.getToleranceZone() / 100 * wallthick
                        - Math.abs(wallLower);
                aimwallthick = intThe((wallthick + (aimWall - Math
                        .abs(wallLower)) / 2));
            }
            else {
                // aimWall = (wallUpper - Math.abs(wallLower)) / 2;
                aimwallthick = intThe(((wallthick - Math.abs(wallLower)) + (wallthick + wallUpper)) / 2);
            }
        }

        // 控制外径
        if (outdiamMax + outdiamMin == 0) {
            aimOutdiam = outdiameter;
        }
        else {
            // 轧管控制外径=轧管名义外径*（1+（内控公差标准上限+下限）/2）
            aimOutdiam = new BigDecimal(outdiameter
                    + (outdiamMax+outdiamMin) / 2).setScale(2,
                    RoundingMode.HALF_UP).doubleValue();
        }

        if (aimwallthick <= 0) {
            isOutput = false;
            reason = "控制壁厚必须大于零！";
            paramOut.setisOutput(isOutput);
            paramOut.setReason(reason);
            return paramOut;
        }
        if (aimOutdiam <= 0) {
            isOutput = false;
            reason = "控制外径必须大于零！";
            paramOut.setisOutput(isOutput);
            paramOut.setReason(reason);
            return paramOut;
        }
        // 管单重=0.02466*控制壁厚*（控制外径-控制壁厚）
        double weight_one = (0.02466 * aimwallthick
                * (aimOutdiam - aimwallthick))/1000;
        paramOut.setAimoutdiameter(aimOutdiam);
        paramOut.setAimwallthick(aimwallthick);
        paramOut.setMiGtWt(weight_one);
        paramOut.setisOutput(isOutput);
        paramOut.setReason(reason);
        return paramOut;
    }

    /**
     * 实现3舍4进
     * @param d
     * @return
     */
    public static double intThe(double d) {
        double i = 0;
        i = d + 0.001;
        BigDecimal bg = new BigDecimal(i).setScale(2, RoundingMode.HALF_UP);
        return bg.doubleValue();
    }
}