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.goods.pojo.Goods;
import com.slodon.b2b2c.statistics.pojo.MemberBehavior;
import com.slodon.b2b2c.statistics.pojo.base.GoodsBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

import static com.slodon.b2b2c.bo.BrandOrderBO.isOrderPay;
import static com.slodon.b2b2c.enums.StatsConstant.*;

/**
 * 商品统计业务关联对象
 */
@Data
public class GoodsStatsBO {

    @ApiModelProperty("商品信息")
    private Goods goods;
    @ApiModelProperty("订单货品列表")
    private List<OrderProduct> orderProductList = new ArrayList<>();
    @ApiModelProperty("订单退货列表")
    private List<OrderReturn> orderReturnList = new ArrayList<>();
    @ApiModelProperty("商品相关会员行为列表")
    private List<MemberBehavior> memberBehaviorList = new ArrayList<>();

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     * @param orderMap  从库中查询的原始订单数据
     */
    public GoodsBase initStats(Date startTime, Map<String/*orderSn*/, Order> orderMap) {
        GoodsBase goodsBase = new GoodsBase();
        goodsBase.setStatsTime(startTime);
        goodsBase.setGoodsId(goods.getGoodsId());
        goodsBase.setGoodsName(goods.getGoodsName());
        goodsBase.setGoodsImage(goods.getMainImage());
        goodsBase.setBrandId(goods.getBrandId());
        goodsBase.setBrandName(goods.getBrandName());
        goodsBase.setStoreId(goods.getStoreId());
        goodsBase.setStoreName(goods.getStoreName());
        goodsBase.setGoodsCategoryId1(goods.getCategoryId1());
        goodsBase.setGoodsCategoryName1(goods.getCategoryPath().substring(0, goods.getCategoryPath().indexOf("->")));
        goodsBase.setGoodsPrice(goods.getGoodsPrice());
        goodsBase.setVisitorNum(0);
        goodsBase.setViewNum(0);
        goodsBase.setOrderSubmitNum(0);
        goodsBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        goodsBase.setOrderPayNum(0);
        goodsBase.setSaleAmount(new BigDecimal("0.00"));
        goodsBase.setSaleNum(0);
        goodsBase.setCollectionNum(0);
        goodsBase.setAddCartMemberNum(0);
        goodsBase.setReturnNum(orderReturnList.size());
        goodsBase.setReturnProductNum(0);
        goodsBase.setReturnAmount(new BigDecimal("0.00"));
        goodsBase.setOrderSubmitMemberNum(0);

        //统计商品访问信息
        //访客统计，暂时按照登录会员去重统计
        Set<Integer> visitorMemberSet = new HashSet<>();
        //加购人数，按照登录会员去重
        Set<Integer> addCartMemberSet = new HashSet<>();
        this.memberBehaviorList.forEach(memberBehavior -> {
            switch (memberBehavior.getBehaviorType()) {
                case GOODS_PAGE_VIEW://商品浏览
                    //增加浏览量
                    goodsBase.setViewNum(goodsBase.getViewNum() + 1);
                    //访问量去重
                    visitorMemberSet.add(memberBehavior.getMemberId());
                    break;
                case GOODS_TO_CART://加入购物车
                    //加购人数去重
                    addCartMemberSet.add(memberBehavior.getMemberId());
                    break;
                case GOODS_FAVORITE://商品收藏
                    //增加收藏数
                    goodsBase.setCollectionNum(goodsBase.getCollectionNum() + 1);
                    break;
                default:
                    //do nothing
            }
        });
        //访问量
        goodsBase.setVisitorNum(visitorMemberSet.size());
        //加购人数
        goodsBase.setAddCartMemberNum(addCartMemberSet.size());

        //将本商品对应的订单货品按照订单号分组
        Map<String/*orderSn*/, List<OrderProduct>> orderProductListGroupMap = new HashMap<>();
        //按照会员id去重统计下单人数
        Set<Integer/*memberId*/> memberIdListSet = new HashSet<>();
        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);
            }

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

            memberIdListSet.add(orderProduct.getMemberId());
        });

        //订单分组数，即为该商品下单数
        goodsBase.setOrderSubmitNum(orderProductListGroupMap.size());
        //下单人数
        goodsBase.setOrderSubmitMemberNum(memberIdListSet.size());

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

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

        return goodsBase;
    }

}
