package com.zm.order.tool.server.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.Random;

/**
 * @author 陈标
 * @Description
 * @Date 2020/7/20 0020 11:32
 */
public class CommonUtils {

    public static int getDb(Long userId) {
        int db = (int) (userId % 32);

        return db + 1;
    }

    public static int getTb(Long userId) {

        int tb = (int) ((userId / 32) % 8);

        return tb;
    }

    public static String uniqueOrderNo(String prefix) {
        StringBuffer buffer = new StringBuffer();
        // 用户ID后两位
//        int length = 2;
//        String first = Long.toString(userId).length() >= length ? Long.toString(userId).substring(Long.toString(userId).length() - length) : String.format("%02d", userId);

        // 时间位 13 位
        Long second = System.currentTimeMillis();

        // 随机数位 7 位
        String third = "";
        int thirdLength = 7;
        char[] str = "0123456789".toCharArray();
        for(int i = 0; i < thirdLength; i++) {
            int indexNumber = (int) (Math.random() * str.length);
            third += str[indexNumber];
        }
        // 共20位单号
        return buffer.append(prefix).append(second).append(third).toString();
    }

    /**
     * 获取当前小时的数值
     * @return
     */
    public static int getHour () {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
        return hour;
    }

    public static int stopHour = 9;
    public static int size = 500;

    public static String random(int thirdLength) {
        Random random = new Random();
        random.nextLong();
        StringBuffer buffer = new StringBuffer();

        String third = "";
        char[] str = "0123456789".toCharArray();
        for(int i = 0; i < thirdLength; i++) {
            int indexNumber = (int) (Math.random() * str.length);
            if (indexNumber == 0 && i == 0) {
                indexNumber = 1;
            }
            third += str[indexNumber];
        }
        // 共17位
        return buffer.append(third).toString();
    }

    public static Integer yuanToFen(BigDecimal yuan) {
        if (Objects.isNull(yuan)) {
            return 0;
        }
        BigDecimal multiply = yuan.multiply(new BigDecimal(100));
        return multiply.intValue();
    }

    public static final BigDecimal FEN_TO_YUAN = BigDecimal.valueOf(100);

    public static BigDecimal fenToYuan(Integer cent) {
        return Objects.isNull(cent) ? BigDecimal.ZERO
                : new BigDecimal(String.valueOf(cent)).divide(CommonUtils.FEN_TO_YUAN).setScale(2, RoundingMode.HALF_UP);
    }

    public static Date getEndAt() {
        Calendar calendar = Calendar.getInstance();
        if (LocalTime.now().isAfter(LocalTime.parse("21:00"))) {
            Date d = DateUtils.getStartDate(DateUtils.next(new Date(), Calendar.DAY_OF_YEAR, 1));
            calendar.setTime(d);
            calendar.add(Calendar.HOUR_OF_DAY, 9);
            return calendar.getTime();
        }
        if (LocalTime.now().isBefore(LocalTime.parse("09:00"))) {
            Date d = DateUtils.getStartDate(new Date());
            calendar.setTime(d);
            calendar.add(Calendar.HOUR_OF_DAY, 9);
            return calendar.getTime();
        }
        Date d = DateUtils.getStartDate(DateUtils.next(new Date(), Calendar.DAY_OF_YEAR, 1));
        calendar.setTime(d);
        calendar.add(Calendar.HOUR_OF_DAY, 9);
        return calendar.getTime();
//        return null;
    }
}
