package com.hai.order.utils;

import com.hai.common.utils.DateUtil;
import com.hai.common.utils.IDGenerator;
import com.hai.entity.HighChildOrder;

import java.math.BigDecimal;
import java.util.*;

/**
 * 订单工具类
 * @className: OrderUtil
 * @author: HuRui
 * @date: 2022/8/26
 **/
public class OrderUtil {

    /**
     * 计算订单金额
     * @param childOrderList  子订单
     * @return
     * @throws Exception
     */
    public static Map<String, Object> calculateOrderPrice(List<HighChildOrder> childOrderList) throws Exception {
        BigDecimal totalPrice = new BigDecimal("0");
        BigDecimal payablePrice = new BigDecimal("0");
        for (HighChildOrder childOrder : childOrderList) {
            // 不是赠品才会计算金额
            if (childOrder.getGiveawayType() == false) {
                totalPrice = totalPrice.add(childOrder.getTotalPrice());
                payablePrice = payablePrice.add(childOrder.getGoodsActualPrice());
            }
        }
        Map<String,Object> param = new HashMap<>();
        param.put("totalPrice", totalPrice);
        param.put("payablePrice", payablePrice);
        return param;
    }

    /**
     * 生成交易订单号
     * @return
     * @throws Exception
     */
    public static String generateOrderNo() throws Exception {
        // 5位随机数 + 1位线程生成数
        String randomNum = (new Random().nextInt(899999) + 10000) + IDGenerator.nextId(1);
        // 生成订单号
        return DateUtil.date2String(new Date(),"yyyyMMddHHmmss") + randomNum;
    }

}
