package com.zcxk.common;

import com.zcxk.exception.ServiceException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @description
 * @auto wangli
 * @data 2020-11-19 8:35
 */
public class CalcUtil {

    private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

    private static ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");

    /**
     * 调用js方法执行表达式
     * @param expression
     */
    public static String executeExpression(String expression) throws ScriptException {
        return String.valueOf(scriptEngine.eval(expression));
    }

    /**
     * 根据比较符号返回结果 （ >、≥、<、≤、=、≠ ）
     * 比较规则  param1  （sign）  param2
     * 参数为 null 则默认为 0，符号不存在或为空则返回 false
     * @param param1
     * @param param2
     * @return
     */
    public static Boolean compareBySign(Double param1 , Double param2 , String sign){
           int result = new BigDecimal( param1 != null?param1 :0 ).compareTo(new BigDecimal(param2 != null?param2 :0 ));
        switch (sign){
            case ">": return result == 1;
            case "≥": return result != -1;
            case "<": return result == -1;
            case "≤": return result != 1;
            case "=": return result == 0;
            case "≠": return result != 0;
        }
        return false;
    }

    public static Boolean compareBySign(BigDecimal param1 , BigDecimal param2 , String sign){
        int result = param1.compareTo(param2);
        switch (sign){
            case ">": return result == 1;
            case "≥": return result != -1;
            case "<": return result == -1;
            case "≤": return result != 1;
            case "=": return result == 0;
            case "≠": return result != 0;
        }
        return false;
    }

    public static Boolean compareBySign(BigDecimal param1 ,String operation, BigDecimal param2 , String sign, BigDecimal param3) {
        BigDecimal operationResult = operation(param1, operation, param2);
        int result = operationResult.compareTo(param3);
        switch (sign){
            case ">": return result == 1;
            case "≥": return result != -1;
            case "<": return result == -1;
            case "≤": return result != 1;
            case "=": return result == 0;
            case "≠": return result != 0;
            default: return false;
        }
    }


    public static BigDecimal operation(BigDecimal param1, String operation, BigDecimal param2){
        switch (operation) {
            case "+": return param1.add(param2);
            case "-": return param1.subtract(param2);
            case "*": return param1.multiply(param2);
            case "/": return param1.divide(param2, 2, RoundingMode.HALF_UP);
            default :
                throw new ServiceException(500, "运算错误,没找到相关运算符号+、-、*、/");
        }
    }


    public static void main(String[] args) throws ScriptException{
       System.out.println( CalcUtil.operation(new BigDecimal("17339.98") ,"+", new BigDecimal("17389.73")));
    }


}
