package com.slodon.b2b2c.bo;

import com.slodon.b2b2c.business.pojo.Order;
import com.slodon.b2b2c.business.pojo.OrderReturn;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.member.pojo.MemberBalanceRecharge;
import com.slodon.b2b2c.statistics.pojo.base.MemberBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.slodon.b2b2c.bo.BrandOrderBO.isOrderPay;

/**
 * 会员统计业务关联对象
 */
@Data
public class MemberStatsBO {

    @ApiModelProperty("会员信息")
    private Member member;
    @ApiModelProperty("订单列表")
    private List<Order> orderList = new ArrayList<>();
    @ApiModelProperty("订单退货列表")
    private List<OrderReturn> orderReturnList = new ArrayList<>();
    @ApiModelProperty("商品相关会员行为列表")
    private List<MemberBalanceRecharge> memberBalanceRechargeList = new ArrayList<>();

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     */
    public MemberBase initStats(Date startTime) {
        MemberBase memberBase = new MemberBase();
        memberBase.setStatsTime(startTime);
        memberBase.setMemberId(member.getMemberId());
        memberBase.setMemberName(member.getMemberName());
        memberBase.setRegisterTime(member.getRegisterTime());
        memberBase.setRechargeAmount(new BigDecimal("0.00"));
        memberBase.setOrderSubmitNum(orderList.size());
        memberBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        memberBase.setOrderPayNum(0);
        memberBase.setOrderPayAmount(new BigDecimal("0.00"));
        memberBase.setReturnNum(orderReturnList.size());
        memberBase.setReturnAmount(new BigDecimal("0.00"));

        this.memberBalanceRechargeList.forEach(memberBalanceRecharge -> {
            //计算充值金额
            memberBase.setRechargeAmount(memberBase.getRechargeAmount().add(memberBalanceRecharge.getPayAmount()));
        });

        this.orderList.forEach(order -> {
            //计算下单金额
            memberBase.setOrderSubmitAmount(memberBase.getOrderSubmitAmount().add(order.getOrderAmount()));

            if (isOrderPay(order)) {
                //已支付订单
                //计算支付订单数
                memberBase.setOrderPayNum(memberBase.getOrderPayNum() + 1);
                //计算支付订单金额
                memberBase.setOrderPayAmount(memberBase.getOrderPayAmount().add(order.getOrderAmount()));
            }
        });

        this.orderReturnList.forEach(orderReturn -> {
            //计算退单金额
            memberBase.setReturnAmount(memberBase.getReturnAmount().add(orderReturn.getReturnMoneyAmount()));
        });

        return memberBase;
    }

}
