package com.slodon.b2b2c.bo;

import com.slodon.b2b2c.business.pojo.Order;
import com.slodon.b2b2c.business.pojo.OrderReturn;
import com.slodon.b2b2c.statistics.pojo.base.MemberStoreBase;
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 MemberStoreStatsBO {

    @ApiModelProperty("会员id")
    private Integer memberId;
    @ApiModelProperty("会员名称")
    private String memberName;
    @ApiModelProperty("店铺id")
    private Long storeId;
    @ApiModelProperty("店铺名称")
    private String storeName;
    @ApiModelProperty("订单列表")
    private List<Order> orderList = new ArrayList<>();
    @ApiModelProperty("订单退货列表")
    private List<OrderReturn> orderReturnList = new ArrayList<>();

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     */
    public MemberStoreBase initStats(Date startTime) {
        MemberStoreBase memberStoreBase = new MemberStoreBase();
        memberStoreBase.setStatsTime(startTime);
        memberStoreBase.setMemberId(memberId);
        memberStoreBase.setMemberName(memberName);
        memberStoreBase.setStoreId(storeId);
        memberStoreBase.setStoreName(storeName);
        memberStoreBase.setOrderSubmitNum(orderList.size());
        memberStoreBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        memberStoreBase.setOrderPayNum(0);
        memberStoreBase.setOrderPayAmount(new BigDecimal("0.00"));
        memberStoreBase.setReturnNum(orderReturnList.size());
        memberStoreBase.setReturnAmount(new BigDecimal("0.00"));

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

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

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

        return memberStoreBase;
    }

}
