package com.slodon.b2b2c.vo.statistics;

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

import java.math.BigDecimal;

import static com.slodon.b2b2c.vo.statistics.FlowOverviewVO.dealPreviousVisitorNum;

/**
 *
 * 封装会员分析-会员总览Vo对象
 * @author wuxy
 */
@Data
@NoArgsConstructor
public class MemberOverviewVO {

    @ApiModelProperty("新增会员数")
    private Integer newMemberNum;

    @ApiModelProperty("上期新增会员数")
    private Integer preNewMemberNum;

    @ApiModelProperty("较上期新增会员")
    private String preNewMember;

    @ApiModelProperty("储值会员数")
    private Integer rechargeMemberNum;

    @ApiModelProperty("上期储值会员数")
    private Integer preRechargeMemberNum;

    @ApiModelProperty("较上期储值会员")
    private String preRechargeMember;

    @ApiModelProperty("支付人数")
    private Integer payMemberNum;

    @ApiModelProperty("上期支付人数")
    private Integer prePayMemberNum;

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

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

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

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

    public MemberOverviewVO(PlatformBase platformBase, PlatformBase previousData) {
        this.newMemberNum = platformBase.getNewMemberNum();
        this.rechargeMemberNum = platformBase.getRechargeMemberNum();
        this.payMemberNum = platformBase.getOrderPayMemberNum();
        if (previousData != null) {
            this.preNewMemberNum = previousData.getNewMemberNum();
            this.preNewMember = dealPreNewMemberNum(platformBase.getNewMemberNum(), previousData.getNewMemberNum());
            this.preRechargeMemberNum = previousData.getRechargeMemberNum();
            this.preRechargeMember = dealPreRechargeMember(platformBase.getRechargeMemberNum(), previousData.getRechargeMemberNum());
            this.prePayMemberNum = previousData.getOrderPayMemberNum();
            this.prePayMember = dealPrePayMember(platformBase.getOrderPayMemberNum(), previousData.getOrderPayMemberNum());
        }
    }

    public MemberOverviewVO(TerminalBase terminalBase, TerminalBase previousData) {
        this.newMemberNum = terminalBase.getNewMemberNum();
        this.rechargeMemberNum = terminalBase.getRechargeMemberNum();
        this.payMemberNum = terminalBase.getOrderPayMemberNum();
        if (previousData != null) {
            this.preNewMemberNum = previousData.getNewMemberNum();
            this.preNewMember = dealPreNewMemberNum(terminalBase.getNewMemberNum(), previousData.getNewMemberNum());
            this.preRechargeMemberNum = previousData.getRechargeMemberNum();
            this.preRechargeMember = dealPreRechargeMember(terminalBase.getRechargeMemberNum(), previousData.getRechargeMemberNum());
            this.prePayMemberNum = previousData.getOrderPayMemberNum();
            this.prePayMember = dealPrePayMember(terminalBase.getOrderPayMemberNum(), previousData.getOrderPayMemberNum());
        }
    }

    public MemberOverviewVO(StoreBase storeBase, StoreBase previousData) {
        this.newMemberNum = storeBase.getNewMemberNum();
        this.visitorNum = storeBase.getVisitorNum();
        this.payMemberNum = storeBase.getOrderPayMemberNum();
        if (previousData != null) {
            this.preNewMemberNum = previousData.getNewMemberNum();
            this.preNewMember = dealPreNewMemberNum(storeBase.getNewMemberNum(), previousData.getNewMemberNum());
            this.preVisitorNum = previousData.getVisitorNum();
            this.preVisitor = dealPreviousVisitorNum(storeBase.getVisitorNum(), previousData.getVisitorNum());
            this.prePayMemberNum = previousData.getOrderPayMemberNum();
            this.prePayMember = dealPrePayMember(storeBase.getOrderPayMemberNum(), previousData.getOrderPayMemberNum());
        }
    }

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

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

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