package org.springblade.common.cache.support;

import java.math.BigDecimal;
import java.util.Random;
import org.springframework.util.StringUtils;

//import org.apache.commons.lang.StringUtils;

public class NumberUtil {

        /**
         * 与记算操作
         * 
         * @param value1
         * @param value2
         * @return
         */
        public static boolean andOperate(int value1, int value2) {
                if ((value1 & value2) == value2) {
                        return true;
                }
                return false;
        }

        public static int getRandomInt(int min, int max) {
                return (int) Math.round((Math.random() * (max - min)) + min);
        }

        public static int parseInt(String in) {
                return parseInt(in, 0);
        }

        public static int parseInt(String in, int defaultValue) {
                int re = defaultValue;
                if (!StringUtils.isEmpty(in)) {
                        try {
                                re = Integer.parseInt(in);
                        } catch (Exception e) {
                                // e.printStackTrace();
                                re = defaultValue;
                        }
                }
                return re;
        }

        public static long parseLong(String in) {
                return parseLong(in, 0);
        }

        public static long parseLong(String in, long defaultValue) {
                long re = defaultValue;
                if (!StringUtils.isEmpty(in)) {
                        try {
                                re = Long.parseLong(in);
                        } catch (Exception e) {
                                // e.printStackTrace();
                                re = defaultValue;
                        }
                }
                return re;
        }

        public static double parseDouble(String in) {
                return parseDouble(in, 0);
        }

        public static double parseDouble(String in, double defaultValue) {
                double re = defaultValue;
                if (!StringUtils.isEmpty(in)) {
                        try {
                                re = Double.parseDouble(in);
                        } catch (Exception e) {
                                // e.printStackTrace();
                                re = defaultValue;
                        }
                }
                return re;
        }

        public static int[] parseInt(String[] in) {
                int[] arr = new int[in.length];
                int i = 0;
                for (String s : in) {
                        arr[i] = parseInt(s);
                        i++;
                }
                return arr;
        }
        
        /**
         * 生成固定长度的随机数
         * 
         * @param digCount
         * @return
         */
        public static String getRandomNumberWithFixLength(int digCount) {
                Random rnd = new Random();
                StringBuilder sb = new StringBuilder(digCount);
                for (int i = 0; i < digCount; i++)
                        sb.append((char) ('0' + rnd.nextInt(10)));
                return sb.toString();
        }

        /**
         * 除法，四舍五入
         * 
         * @param scale
         *            保留几位小数
         * @param dividend
         *            除数
         * @param divisor
         *            被除数
         * @return
         */
        public static double divide(int scale, Long dividend, Long divisor) {
                Long zero = new Long(0);
                if (dividend == null || zero.equals(dividend) || divisor == null || zero.equals(divisor)) {
                        return 0.00d;
                }

                BigDecimal ret = null;
                try {
                        ret = new BigDecimal(dividend).divide(new BigDecimal(divisor), scale, BigDecimal.ROUND_HALF_UP);
                } catch (Exception e) {
                        // e.printStackTrace();
                        return 0.00d;
                }
                return ret.doubleValue();
        }

        public static double divide(int scale, Double dividend, Long divisor) {
                Long zero = new Long(0);
                if (dividend == null || zero.equals(dividend) || divisor == null || zero.equals(divisor)) {
                        return 0.00d;
                }

                BigDecimal ret = null;
                try {
                        ret = new BigDecimal(dividend).divide(new BigDecimal(divisor), scale, BigDecimal.ROUND_HALF_UP);
                } catch (Exception e) {
                        // e.printStackTrace();
                        return 0.00d;
                }
                return ret.doubleValue();
        }

        public static double divide(int scale, Double dividend, Double divisor) {
                Long zero = new Long(0);
                if (dividend == null || zero.equals(dividend) || divisor == null || zero.equals(divisor)) {
                        return 0.00d;
                }

                BigDecimal ret = null;
                try {
                        ret = new BigDecimal(dividend).divide(new BigDecimal(divisor), scale, BigDecimal.ROUND_HALF_UP);
                } catch (Exception e) {
                        // e.printStackTrace();
                        return 0.00d;
                }
                return ret.doubleValue();
        }

        public static double divide(int scale, String dividend, String divisor) {
                Long zero = new Long(0);
                if (dividend == null || zero.equals(dividend) || divisor == null || zero.equals(divisor)) {
                        return 0.00d;
                }
                
                BigDecimal ret = null;
                try {
                        ret = new BigDecimal(dividend).divide(new BigDecimal(divisor), scale, BigDecimal.ROUND_HALF_UP);
                } catch (Exception e) {
                        // e.printStackTrace();
                        return 0.00d;
                }
                return ret.doubleValue();
        }
        
        /**
         * 将里转换为分 by yeshun
         * @param liValue
         * @return
         */
        public static Long liConvertFeng(Long liValue){
                if(liValue == null || liValue == 0l) return 0l;
                
                BigDecimal ret = null;
                try {
                        ret = new BigDecimal(liValue).divide(new BigDecimal(10), 0, BigDecimal.ROUND_HALF_UP);
                } catch (Exception e) {
                        return 0l;
                }
                return ret.longValue();
        }
}