package com.slodon.b2b2c.vo.statistics;

import com.slodon.b2b2c.statistics.pojo.base.PlatformBase;
import com.slodon.b2b2c.statistics.pojo.base.TerminalBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 *
 * 封装流量分析-流量总览Vo对象
 * @author wuxy
 */
@NoArgsConstructor
@Data
public class FlowOverviewVO {

    @ApiModelProperty("访客数")
    private Integer visitorNum = 0;

    @ApiModelProperty("较上期访客数")
    private String previousVisitorNum;

    @ApiModelProperty("新访客数")
    private Integer newVisitorNum = 0;

    @ApiModelProperty("较上期新访客数")
    private String previousNewVisitorNum;

    @ApiModelProperty("浏览量")
    private Integer viewNum = 0;

    @ApiModelProperty("较上期浏览量")
    private String previousViewNum;

    @ApiModelProperty("下单数")
    private Integer orderSubmitNum = 0;

    @ApiModelProperty("较上期下单数")
    private String previousOrderSubmitNum;

    @ApiModelProperty("下单人数")
    private Integer orderSubmitMemberNum = 0;

    @ApiModelProperty("较上期下单人数")
    private String previousOrderSubmitMemberNum;

    @ApiModelProperty("支付订单数")
    private Integer orderPayNum = 0;

    @ApiModelProperty("较上期支付订单数")
    private String previousOrderPayNum;

    @ApiModelProperty("支付人数")
    private Integer orderPayMemberNum = 0;

    @ApiModelProperty("较上期支付人数")
    private String previousOrderPayMemberNum;

    @ApiModelProperty("访问-下单转化率，下单人数/访客数*100%")
    private String pvSubmitRate;

    @ApiModelProperty("访问-支付转化率，支付订单人数/访客数*100%")
    private String pvPayRate;

    @ApiModelProperty("下单-支付转化率，支付订单人数/下单人数*100%")
    private String submitPayRate;

    public FlowOverviewVO(PlatformBase platformBase, PlatformBase previousData) {
        this.visitorNum = platformBase.getVisitorNum();
        this.newVisitorNum = platformBase.getNewVisitorNum();
        this.viewNum = platformBase.getViewNum();
        this.orderSubmitNum = platformBase.getOrderSubmitNum();
        this.orderSubmitMemberNum = platformBase.getOrderSubmitMemberNum();
        this.orderPayNum = platformBase.getOrderPayNum();
        this.orderPayMemberNum = platformBase.getOrderPayMemberNum();
        this.pvSubmitRate = dealPvSubmitRate(platformBase.getOrderSubmitMemberNum(), platformBase.getVisitorNum());
        this.pvPayRate = dealPvPayRate(platformBase.getOrderPayMemberNum(), platformBase.getVisitorNum());
        this.submitPayRate = dealSubmitPayRate(platformBase.getOrderPayMemberNum(), platformBase.getOrderSubmitMemberNum());
        if (previousData != null) {
            this.previousVisitorNum = dealPreviousVisitorNum(platformBase.getVisitorNum(), previousData.getVisitorNum());
            this.previousNewVisitorNum = dealPreviousNewVisitorNum(platformBase.getNewVisitorNum(), previousData.getNewVisitorNum());
            this.previousViewNum = dealPreviousViewNum(platformBase.getViewNum(), previousData.getViewNum());
            this.previousOrderSubmitNum = dealPreviousOrderSubmitNum(platformBase.getOrderSubmitNum(), previousData.getOrderSubmitNum());
            this.previousOrderSubmitMemberNum = dealPreviousOrderSubmitMemberNum(platformBase.getOrderSubmitMemberNum(), previousData.getOrderSubmitMemberNum());
            this.previousOrderPayNum = dealPreviousOrderPayNum(platformBase.getOrderPayNum(), previousData.getOrderPayNum());
            this.previousOrderPayMemberNum = dealPreviousOrderPayMemberNum(platformBase.getOrderPayMemberNum(), previousData.getOrderPayMemberNum());
        }
    }

    public FlowOverviewVO(TerminalBase terminalBase, TerminalBase previousData) {
        this.visitorNum = terminalBase.getVisitorNum();
        this.newVisitorNum = terminalBase.getNewVisitorNum();
        this.viewNum = terminalBase.getViewNum();
        this.orderSubmitNum = terminalBase.getOrderSubmitNum();
        this.orderSubmitMemberNum = terminalBase.getOrderSubmitMemberNum();
        this.orderPayNum = terminalBase.getOrderPayNum();
        this.orderPayMemberNum = terminalBase.getOrderPayMemberNum();
        this.pvSubmitRate = dealPvSubmitRate(terminalBase.getOrderSubmitMemberNum(), terminalBase.getVisitorNum());
        this.pvPayRate = dealPvPayRate(terminalBase.getOrderPayMemberNum(), terminalBase.getVisitorNum());
        this.submitPayRate = dealSubmitPayRate(terminalBase.getOrderPayMemberNum(), terminalBase.getOrderSubmitMemberNum());
        if (previousData != null) {
            this.previousVisitorNum = dealPreviousVisitorNum(terminalBase.getVisitorNum(), previousData.getVisitorNum());
            this.previousNewVisitorNum = dealPreviousNewVisitorNum(terminalBase.getNewVisitorNum(), previousData.getNewVisitorNum());
            this.previousViewNum = dealPreviousViewNum(terminalBase.getViewNum(), previousData.getViewNum());
            this.previousOrderSubmitNum = dealPreviousOrderSubmitNum(terminalBase.getOrderSubmitNum(), previousData.getOrderSubmitNum());
            this.previousOrderSubmitMemberNum = dealPreviousOrderSubmitMemberNum(terminalBase.getOrderSubmitMemberNum(), previousData.getOrderSubmitMemberNum());
            this.previousOrderPayNum = dealPreviousOrderPayNum(terminalBase.getOrderPayNum(), previousData.getOrderPayNum());
            this.previousOrderPayMemberNum = dealPreviousOrderPayMemberNum(terminalBase.getOrderPayMemberNum(), previousData.getOrderPayMemberNum());
        }
    }

    public static String dealPvSubmitRate(Integer orderSubmitMemberNum, Integer visitorNum) {
        if (visitorNum == 0) return "0.00%";
        return new BigDecimal(orderSubmitMemberNum).multiply(new BigDecimal(100))
                .divide(new BigDecimal(visitorNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPvPayRate(Integer orderPayMemberNum, Integer visitorNum) {
        if (visitorNum == 0) return "0.00%";
        return new BigDecimal(orderPayMemberNum).multiply(new BigDecimal(100))
                .divide(new BigDecimal(visitorNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealSubmitPayRate(Integer orderPayMemberNum, Integer orderSubmitMemberNum) {
        if (orderSubmitMemberNum == 0) return "0.00%";
        return new BigDecimal(orderPayMemberNum).multiply(new BigDecimal(100))
                .divide(new BigDecimal(orderSubmitMemberNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousVisitorNum(Integer visitorNum, Integer previousVisitorNum) {
        if (previousVisitorNum == 0) return "0.00%";
        return (new BigDecimal(visitorNum).subtract(new BigDecimal(previousVisitorNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousVisitorNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousNewVisitorNum(Integer newVisitorNum, Integer previousNewVisitorNum) {
        if (previousNewVisitorNum == 0) return "0.00%";
        return (new BigDecimal(newVisitorNum).subtract(new BigDecimal(previousNewVisitorNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousNewVisitorNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousViewNum(Integer viewNum, Integer previousViewNum) {
        if (previousViewNum == 0) return "0.00%";
        return (new BigDecimal(viewNum).subtract(new BigDecimal(previousViewNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousViewNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousOrderSubmitNum(Integer orderSubmitNum, Integer previousOrderSubmitNum) {
        if (previousOrderSubmitNum == 0) return "0.00%";
        return (new BigDecimal(orderSubmitNum).subtract(new BigDecimal(previousOrderSubmitNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousOrderSubmitNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousOrderSubmitMemberNum(Integer orderSubmitMemberNum, Integer previousOrderSubmitMemberNum) {
        if (previousOrderSubmitMemberNum == 0) return "0.00%";
        return (new BigDecimal(orderSubmitMemberNum).subtract(new BigDecimal(previousOrderSubmitMemberNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousOrderSubmitMemberNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousOrderPayNum(Integer orderPayNum, Integer previousOrderPayNum) {
        if (previousOrderPayNum == 0) return "0.00%";
        return (new BigDecimal(orderPayNum).subtract(new BigDecimal(previousOrderPayNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousOrderPayNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreviousOrderPayMemberNum(Integer orderPayMemberNum, Integer previousOrderPayMemberNum) {
        if (previousOrderPayMemberNum == 0) return "0.00%";
        return (new BigDecimal(orderPayMemberNum).subtract(new BigDecimal(previousOrderPayMemberNum))).multiply(new BigDecimal(100))
                .divide(new BigDecimal(previousOrderPayMemberNum), 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

}
