package com.slodon.b2b2c.bo;

import com.slodon.b2b2c.business.pojo.Order;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.statistics.pojo.MemberBehavior;
import com.slodon.b2b2c.statistics.pojo.base.TerminalBase;
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.PAGE_VIEW;

/**
 * 终端统计业务关联对象
 */
@Data
public class TerminalStatsBO {

    @ApiModelProperty("终端编码")
    private String terminalCode;
    @ApiModelProperty("终端名称")
    private String terminalName;
    @ApiModelProperty("订单列表")
    private List<Order> orderList = new ArrayList<>();
    @ApiModelProperty("店铺相关会员行为列表")
    private List<MemberBehavior> memberBehaviorList = new ArrayList<>();
    @ApiModelProperty("新增的会员")
    private List<Member> newMemberList = new ArrayList<>();
    @ApiModelProperty("储值的会员set key为memberId")
    private Set<Integer> rechargeMemberSet = new HashSet<>();

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     */
    public TerminalBase initStats(Date startTime) {
        TerminalBase terminalBase = new TerminalBase();
        terminalBase.setStatsTime(startTime);
        terminalBase.setTerminalName(terminalName);
        terminalBase.setTerminalCode(terminalCode);
        terminalBase.setVisitorNum(0);
        terminalBase.setNewVisitorNum(0);
        terminalBase.setViewNum(0);
        terminalBase.setOrderSubmitNum(orderList.size());
        terminalBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        terminalBase.setOrderSubmitMemberNum(0);
        terminalBase.setOrderSubmitAtv(new BigDecimal("0.00"));
        terminalBase.setOrderPayNum(0);
        terminalBase.setOrderPayAmount(new BigDecimal("0.00"));
        terminalBase.setOrderPayMemberNum(0);
        terminalBase.setOrderPayAtv(new BigDecimal("0.00"));
        terminalBase.setPvSubmitRate("");
        terminalBase.setPvPayRate("");
        terminalBase.setSubmitPayRate("");
        terminalBase.setNewMemberNum(newMemberList.size());
        terminalBase.setRechargeMemberNum(rechargeMemberSet.size());

        //遍历阶段时间内用户行为，统计流量
        //新访客数set
        Set<Integer> newVisitorNumSet = new HashSet<>();
        //访客数set key为会员id+uuid
        Set<String> visitorNumSet = new HashSet<>();
        this.memberBehaviorList.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);
                    //计算浏览量
                    terminalBase.setViewNum(terminalBase.getViewNum() + 1);
                    break;
                default:
                    //do nothing
            }
        });
        //计算访客数
        terminalBase.setVisitorNum(visitorNumSet.size());
        //计算新访客数，定义：所有访客数中，新注册的会员数
        this.newMemberList.forEach(member -> {
            if (newVisitorNumSet.contains(member.getMemberId())) {
                //该会员在本阶段的访客中，定义为新访客
                terminalBase.setNewVisitorNum(terminalBase.getNewVisitorNum() + 1);
            }
        });
        //下单的会员set
        Set<Integer> orderSubmitMemberSet = new HashSet<>();
        //支付订单的会员set
        Set<Integer> orderPayMemberSet = new HashSet<>();
        this.orderList.forEach(order -> {
            //计算下单金额
            terminalBase.setOrderSubmitAmount(terminalBase.getOrderSubmitAmount().add(order.getOrderAmount()));
            //下单人数去重
            orderSubmitMemberSet.add(order.getMemberId());
            if (isOrderPay(order)) {
                // 订单已支付
                // 计算支付订单数
                terminalBase.setOrderPayNum(terminalBase.getOrderPayNum() + 1);
                // 计算支付订单金额
                terminalBase.setOrderPayAmount(terminalBase.getOrderPayAmount().add(order.getOrderAmount()));
                // 支付人数去重
                orderPayMemberSet.add(order.getMemberId());
            }
        });
        //计算支付订单人数
        terminalBase.setOrderPayMemberNum(orderPayMemberSet.size());
        //计算下单人数
        terminalBase.setOrderSubmitMemberNum(orderSubmitMemberSet.size());
        if (terminalBase.getOrderSubmitMemberNum() > 0) {
            //计算下单客单价
            terminalBase.setOrderSubmitAtv(terminalBase.getOrderSubmitAmount()
                    .divide(new BigDecimal(terminalBase.getOrderSubmitMemberNum()), 2, BigDecimal.ROUND_HALF_UP));
        }
        if (terminalBase.getOrderPayMemberNum() > 0) {
            //计算支付客单价
            terminalBase.setOrderPayAtv(terminalBase.getOrderPayAmount()
                    .divide(new BigDecimal(terminalBase.getOrderPayMemberNum()), 2, BigDecimal.ROUND_HALF_UP));
        }
        if (terminalBase.getViewNum() > 0) {
            //计算访问-下单转化率
            terminalBase.setPvSubmitRate(new BigDecimal(terminalBase.getOrderSubmitNum() * 100)
                    .divide(new BigDecimal(terminalBase.getViewNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
            //计算访问-支付转化率
            terminalBase.setPvPayRate(new BigDecimal(terminalBase.getOrderPayNum() * 100)
                    .divide(new BigDecimal(terminalBase.getViewNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        if (terminalBase.getOrderSubmitNum() > 0) {
            //计算下单-支付转化率
            terminalBase.setSubmitPayRate(new BigDecimal(terminalBase.getOrderPayNum() * 100)
                    .divide(new BigDecimal(terminalBase.getOrderSubmitNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
        }

        return terminalBase;
    }

}
