package com.slodon.b2b2c.vo.statistics;

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

import java.math.BigDecimal;

import static com.slodon.b2b2c.vo.statistics.FlowOverviewVO.*;
import static com.slodon.b2b2c.vo.statistics.TradeOverviewVO.dealOrderPayAtv;

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

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

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

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

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

    @ApiModelProperty("人均浏览量")
    private BigDecimal perViewNum = BigDecimal.ZERO;

    @ApiModelProperty("较上期人均浏览量")
    private String prePerViewNum;

    @ApiModelProperty("商品访客数")
    private Integer goodsVisitorNum = 0;

    @ApiModelProperty("较上期商品访客数")
    private String preGoodsVisitorNum;

    @ApiModelProperty("商品浏览量")
    private Integer goodsViewNum = 0;

    @ApiModelProperty("较上期商品浏览量")
    private String preGoodsViewNum;

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

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

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

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

    @ApiModelProperty("支付金额")
    private BigDecimal orderPayAmount = BigDecimal.ZERO;

    @ApiModelProperty("较上期支付金额")
    private String preOrderPayAmount;

    @ApiModelProperty("支付客单价，支付金额/支付订单人数")
    private BigDecimal orderPayAtv = BigDecimal.ZERO;

    @ApiModelProperty("较上期支付客单价")
    private String preOrderPayAtv;

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

    @ApiModelProperty("较上期访问-支付转化率")
    private String prePvPayRate;

    public SellerFlowOverviewVO(StoreBase storeBase, StoreBase previousData) {
        this.visitorNum = storeBase.getVisitorNum();
        this.viewNum = storeBase.getViewNum();
        if (storeBase.getVisitorNum() > 0) {
            this.perViewNum = new BigDecimal(storeBase.getViewNum())
                    .divide(new BigDecimal(storeBase.getVisitorNum()), 2, BigDecimal.ROUND_HALF_UP);
        } else {
            this.perViewNum = BigDecimal.ZERO;
        }
        this.goodsVisitorNum = storeBase.getGoodsVisitorNum();
        this.goodsViewNum = storeBase.getGoodsViewNum();
        this.orderPayMemberNum = storeBase.getOrderPayMemberNum();
        this.orderPayNum = storeBase.getOrderPayNum();
        this.orderPayAmount = storeBase.getOrderPayAmount();
        BigDecimal payAtv = dealOrderPayAtv(storeBase.getOrderPayAmount(), storeBase.getOrderPayMemberNum());
        this.orderPayAtv = payAtv;
        this.pvPayRate = dealPvPayRate(storeBase.getOrderPayMemberNum(), storeBase.getVisitorNum());
        if (previousData != null) {
            this.preVisitorNum = dealPreviousVisitorNum(storeBase.getVisitorNum(), previousData.getVisitorNum());
            this.preViewNum = dealPreviousViewNum(storeBase.getViewNum(), previousData.getViewNum());
            this.prePerViewNum = dealPerViewNum(storeBase.getViewNum(), storeBase.getVisitorNum(), previousData.getViewNum(), previousData.getVisitorNum());
            this.preGoodsVisitorNum = dealPreviousOrderSubmitNum(storeBase.getGoodsVisitorNum(), previousData.getGoodsVisitorNum());
            this.preGoodsViewNum = dealPreviousOrderSubmitMemberNum(storeBase.getGoodsViewNum(), previousData.getGoodsViewNum());
            this.preOrderPayMemberNum = dealPreviousOrderPayMemberNum(storeBase.getOrderPayMemberNum(), previousData.getOrderPayMemberNum());
            this.preOrderPayNum = dealPreviousOrderPayNum(storeBase.getOrderPayNum(), previousData.getOrderPayNum());
            this.preOrderPayAmount = dealPreOrderPayAmount(storeBase.getOrderPayAmount(), previousData.getOrderPayAmount());
            BigDecimal prePayAtv = dealOrderPayAtv(previousData.getOrderPayAmount(), previousData.getOrderPayMemberNum());
            this.preOrderPayAtv = dealPreOrderPayAtv(payAtv, prePayAtv);
            this.prePvPayRate = dealPvPayRate(previousData.getOrderPayMemberNum(), previousData.getVisitorNum());
        }
    }

    public static String dealPerViewNum(Integer viewNum, Integer visitorNum, Integer preViewNum, Integer preVisitorNum) {
        if (visitorNum == 0 || preVisitorNum == 0) return "0.00%";
        BigDecimal perViewNum = new BigDecimal(viewNum).divide(new BigDecimal(visitorNum), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal prePerViewNum = new BigDecimal(preViewNum).divide(new BigDecimal(preVisitorNum), 2, BigDecimal.ROUND_HALF_UP);
        return (perViewNum.subtract(prePerViewNum)).multiply(new BigDecimal(100))
                .divide(prePerViewNum, 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreOrderPayAmount(BigDecimal orderPayAmount, BigDecimal preOrderPayAmount) {
        if (preOrderPayAmount.compareTo(BigDecimal.ZERO) == 0) return "0.00%";
        return (orderPayAmount.subtract(preOrderPayAmount)).multiply(new BigDecimal(100))
                .divide(preOrderPayAmount, 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

    public static String dealPreOrderPayAtv(BigDecimal orderPayAtv, BigDecimal preOrderPayAtv) {
        if (preOrderPayAtv.compareTo(BigDecimal.ZERO) == 0) return "0.00%";
        return (orderPayAtv.subtract(preOrderPayAtv)).multiply(new BigDecimal(100))
                .divide(preOrderPayAtv, 2, BigDecimal.ROUND_HALF_UP) + "%";
    }

}
