package com.slodon.b2b2c.bo;

import com.slodon.b2b2c.business.pojo.Order;
import com.slodon.b2b2c.business.pojo.OrderProduct;
import com.slodon.b2b2c.business.pojo.OrderReturn;
import com.slodon.b2b2c.core.constant.OrderConst;
import com.slodon.b2b2c.statistics.pojo.base.BrandBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

/**
 * 品牌、订单业务关联对象
 * 品牌->商品->订单货品->订单
 */
@Data
public class BrandOrderBO {

    @ApiModelProperty("品牌id")
    private Integer brandId;
    @ApiModelProperty("品牌名称")
    private String brandName;
    @ApiModelProperty("订单货品列表")
    private List<OrderProduct> orderProductList = new ArrayList<>();
    @ApiModelProperty("订单退货列表")
    private List<OrderReturn> orderReturnList = new ArrayList<>();

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     * @param orderMap  从库中查询的原始订单数据
     */
    public BrandBase initStats(Date startTime, Map<String/*orderSn*/, Order> orderMap) {
        BrandBase brandBase = new BrandBase();
        brandBase.setStatsTime(startTime);
        brandBase.setBrandId(brandId);
        brandBase.setBrandName(brandName);
        brandBase.setOrderSubmitNum(0);
        brandBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        brandBase.setOrderPayNum(0);
        brandBase.setSaleAmount(new BigDecimal("0.00"));
        brandBase.setSaleNum(0);
        brandBase.setReturnNum(orderReturnList.size());
        brandBase.setReturnProductNum(0);
        brandBase.setReturnAmount(new BigDecimal("0.00"));

        //将本品牌对应的订单货品按照订单号分组
        Map<String/*orderSn*/, List<OrderProduct>> orderProductListGroupMap = new HashMap<>();
        this.orderProductList.forEach(orderProduct -> {
            if (orderProductListGroupMap.containsKey(orderProduct.getOrderSn())) {
                orderProductListGroupMap.get(orderProduct.getOrderSn()).add(orderProduct);
            } else {
                List<OrderProduct> list = new ArrayList<>();
                list.add(orderProduct);
                orderProductListGroupMap.put(orderProduct.getOrderSn(), list);
            }

            //计算下单金额
            brandBase.setOrderSubmitAmount(brandBase.getOrderSubmitAmount().add(orderProduct.getMoneyAmount()));
            //计算销量
            brandBase.setSaleNum(brandBase.getSaleNum() + orderProduct.getProductNum());
        });

        //订单分组数，即为该品牌下单数
        brandBase.setOrderSubmitNum(orderProductListGroupMap.size());

        //遍历订单分组，计算订单信息
        orderProductListGroupMap.forEach((orderSn, list) -> {
            if (isOrderPay(orderMap.get(orderSn))) {
                // 订单已支付
                // 计算支付订单数
                brandBase.setOrderPayNum(brandBase.getOrderPayNum() + 1);
                // 计算销售额
                list.forEach(orderProduct -> brandBase.setSaleAmount(brandBase.getSaleAmount().add(orderProduct.getMoneyAmount())));
            }
        });

        //计算退货退款信息
        orderReturnList.forEach(orderReturn -> {
            //计算退单sku件数
            brandBase.setReturnProductNum(brandBase.getReturnProductNum() + orderReturn.getReturnNum());
            //计算退单金额
            brandBase.setReturnAmount(brandBase.getReturnAmount().add(orderReturn.getReturnMoneyAmount()));
        });

        return brandBase;
    }

    /**
     * 订单是否支付
     *
     * @param order
     * @return
     */
    public static boolean isOrderPay(Order order) {
        Integer orderState = order.getOrderState();
        return orderState.equals(OrderConst.ORDER_STATE_20)
                || orderState.equals(OrderConst.ORDER_STATE_30)
                || orderState.equals(OrderConst.ORDER_STATE_40);
    }
}
