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.statistics.pojo.base.CategoryBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

import static com.slodon.b2b2c.bo.BrandOrderBO.isOrderPay;

/**
 * 商品分类（一级）、订单业务关联对象
 * 商品分类->商品->订单货品->订单
 */
@Data
public class CategoryOrderBO {

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

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     * @param orderMap  从库中查询的原始订单数据
     */
    public CategoryBase initStats(Date startTime, Map<String/*orderSn*/, Order> orderMap) {
        CategoryBase categoryBase = new CategoryBase();
        categoryBase.setStatsTime(startTime);
        categoryBase.setCategoryId(categoryId);
        categoryBase.setCategoryName(categoryName);
        categoryBase.setOrderSubmitNum(0);
        categoryBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        categoryBase.setOrderPayNum(0);
        categoryBase.setSaleAmount(new BigDecimal("0.00"));
        categoryBase.setSaleNum(0);
        categoryBase.setReturnNum(orderReturnList.size());
        categoryBase.setReturnProductNum(0);
        categoryBase.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);
            }

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

        //订单分组数，即为该分类下单数
        categoryBase.setOrderSubmitNum(orderProductListGroupMap.size());

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

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

        return categoryBase;
    }

}
