package com.slodon.b2b2c.bo;

import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.statistics.pojo.base.PlatformBase;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

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

/**
 * 平台统计业务关联对象
 */
@Data
public class PlatformStatsBO {

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     */
    public PlatformBase initStats(Date startTime, StatsBasicDataBO basicData) {
        PlatformBase platformBase = new PlatformBase();
        platformBase.setStatsTime(startTime);
        //访客数
        platformBase.setVisitorNum(0);
        //新访客数
        platformBase.setNewVisitorNum(0);
        //浏览量
        platformBase.setViewNum(0);
        //店铺访客数
        platformBase.setStoreVisitorNum(0);
        //店铺浏览量
        platformBase.setStoreViewNum(0);
        //商品访客数
        platformBase.setGoodsVisitorNum(0);
        //商品浏览量
        platformBase.setGoodsViewNum(0);
        //下单数
        platformBase.setOrderSubmitNum(basicData.getOrderList().size());
        //下单金额
        platformBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        //下单人数
        platformBase.setOrderSubmitMemberNum(basicData.getMemberOrderMap().size());
        //下单客单价
        platformBase.setOrderSubmitAtv(new BigDecimal("0.00"));
        //支付订单数
        platformBase.setOrderPayNum(0);
        //支付订单金额
        platformBase.setOrderPayAmount(new BigDecimal("0.00"));
        //支付订单人数
        platformBase.setOrderPayMemberNum(0);
        //下单客单价
        platformBase.setOrderPayAtv(new BigDecimal("0.00"));
        //访问-下单转化率
        platformBase.setPvSubmitRate("0.00%");
        //访问-支付转化率
        platformBase.setPvPayRate("0.00%");
        //下单-支付转化率
        platformBase.setSubmitPayRate("0.00%");
        //动销商品数
        platformBase.setSalingGoodsNum(basicData.getGoodsOrderProductMap().size());
        //新增商品数
        platformBase.setNewGoodsNum(basicData.getNewGoodsList().size());
        //新增品牌数
        platformBase.setNewBrandNum(basicData.getNewBrandList().size());
        //新增会员数
        platformBase.setNewMemberNum(basicData.getNewMemberList().size());
        //储值会员数
        platformBase.setRechargeMemberNum(basicData.getMemberBalanceRechargeMap().size());
        //新增店铺数
        platformBase.setNewStoreNum(basicData.getNewStoreList().size());

        //遍历阶段时间内用户行为，统计流量
        //新访客数set
        Set<Integer> newVisitorNumSet = new HashSet<>();
        //访客数set key为会员id+uuid
        Set<String> visitorNumSet = new HashSet<>();
        //店铺访客数set
        Set<String> storeVisitorNumSet = new HashSet<>();
        //商品访客数set
        Set<String> goodsVisitorNumSet = new HashSet<>();
        basicData.getMemberBehaviorList().forEach(memberBehavior -> {
            String key;
            if (!StringUtil.isNullOrZero(memberBehavior.getMemberId())) {
                key = memberBehavior.getMemberId().toString();
            } else {
                key = memberBehavior.getUuid();
            }
            switch (memberBehavior.getBehaviorType()) {
                case PAGE_VIEW://访问类型为浏览
                    //新访客数
                    newVisitorNumSet.add(memberBehavior.getMemberId());
                    //访客数
                    visitorNumSet.add(key);
                    //计算浏览量
                    platformBase.setViewNum(platformBase.getViewNum() + 1);
                    break;
                case STORE_PAGE_VIEW://访问类型为店铺浏览
                    //店铺访客数
                    storeVisitorNumSet.add(key);
                    //计算店铺浏览量
                    platformBase.setStoreViewNum(platformBase.getStoreViewNum() + 1);
                    break;
                case GOODS_PAGE_VIEW://访问类型为商品浏览
                    //商品访客数
                    goodsVisitorNumSet.add(key);
                    //计算店铺浏览量
                    platformBase.setGoodsViewNum(platformBase.getGoodsViewNum() + 1);
                    break;
                default:
                    //do nothing
            }
        });
        //计算访客数
        platformBase.setVisitorNum(visitorNumSet.size());
        //计算店铺访客数
        platformBase.setStoreVisitorNum(storeVisitorNumSet.size());
        //计算商品访客数
        platformBase.setGoodsVisitorNum(goodsVisitorNumSet.size());
        //计算新访客数，定义：所有访客数中，新注册的会员数
        basicData.getNewMemberList().forEach(member -> {
            if (newVisitorNumSet.contains(member.getMemberId())) {
                //该会员在本阶段的访客中，定义为新访客
                platformBase.setNewVisitorNum(platformBase.getNewVisitorNum() + 1);
            }
        });
        //支付订单的会员set
        Set<Integer> orderPayMemberSet = new HashSet<>();
        basicData.getOrderList().forEach(order -> {
            //计算下单金额
            platformBase.setOrderSubmitAmount(platformBase.getOrderSubmitAmount().add(order.getOrderAmount()));
            if (isOrderPay(order)) {
                // 订单已支付
                // 计算支付订单数
                platformBase.setOrderPayNum(platformBase.getOrderPayNum() + 1);
                // 计算支付订单金额
                platformBase.setOrderPayAmount(platformBase.getOrderPayAmount().add(order.getOrderAmount()));
                // 支付人数去重
                orderPayMemberSet.add(order.getMemberId());
            }
        });
        //计算支付订单人数
        platformBase.setOrderPayMemberNum(orderPayMemberSet.size());
        if (platformBase.getOrderSubmitMemberNum() > 0) {
            //计算下单客单价
            platformBase.setOrderSubmitAtv(platformBase.getOrderSubmitAmount()
                    .divide(new BigDecimal(platformBase.getOrderSubmitMemberNum()), 2, BigDecimal.ROUND_HALF_UP));
        }
        if (platformBase.getOrderPayMemberNum() > 0) {
            //计算支付客单价
            platformBase.setOrderPayAtv(platformBase.getOrderPayAmount()
                    .divide(new BigDecimal(platformBase.getOrderPayMemberNum()), 2, BigDecimal.ROUND_HALF_UP));
        }
        if (platformBase.getVisitorNum() > 0) {
            //计算访问-下单转化率
            platformBase.setPvSubmitRate(new BigDecimal(platformBase.getOrderSubmitMemberNum() * 100)
                    .divide(new BigDecimal(platformBase.getVisitorNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
            //计算访问-支付转化率
            platformBase.setPvPayRate(new BigDecimal(platformBase.getOrderPayMemberNum() * 100)
                    .divide(new BigDecimal(platformBase.getVisitorNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        if (platformBase.getOrderSubmitMemberNum() > 0) {
            //计算下单-支付转化率
            platformBase.setSubmitPayRate(new BigDecimal(platformBase.getOrderPayMemberNum() * 100)
                    .divide(new BigDecimal(platformBase.getOrderSubmitMemberNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
        }

        return platformBase;
    }

}
