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.ProvinceBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.*;

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

/**
 * 省份统计业务关联对象
 */
@Data
public class ProvinceStatsBO {

    @ApiModelProperty("省份编码")
    private String provinceCode;
    @ApiModelProperty("省份名称")
    private String provinceName;
    @ApiModelProperty("订单列表")
    private List<Order> orderList = new ArrayList<>();
    @ApiModelProperty("订单退货列表")
    private List<OrderReturn> orderReturnList = new ArrayList<>();
    @ApiModelProperty("新增会员数")
    private Integer newMemberNum;
    @ApiModelProperty("新增店铺数")
    private Integer newStoreNum;

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     */
    public ProvinceBase initStats(Date startTime) {
        ProvinceBase provinceBase = new ProvinceBase();
        provinceBase.setStatsTime(startTime);
        provinceBase.setProvinceName(provinceName);
        provinceBase.setProvinceCode(provinceCode);
        provinceBase.setOrderSubmitNum(orderList.size());
        provinceBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        provinceBase.setOrderSubmitMemberNum(0);
        provinceBase.setOrderPayNum(0);
        provinceBase.setOrderPayAmount(new BigDecimal("0.00"));
        provinceBase.setOrderPayMemberNum(0);
        provinceBase.setNewMemberNum(newMemberNum);
        provinceBase.setNewStoreNum(newStoreNum);
        provinceBase.setActiveMemberNum(0);
        provinceBase.setReturnNum(orderReturnList.size());
        provinceBase.setReturnAmount(new BigDecimal("0.00"));
        //下单人数去重
        Set<Integer/*memberId*/> orderSubmitMemberSet = new HashSet<>();
        //支付订单人数去重
        Set<Integer/*memberId*/> orderPayMemberSet = new HashSet<>();
        this.orderList.forEach(order -> {
            //计算下单金额
            provinceBase.setOrderSubmitAmount(provinceBase.getOrderSubmitAmount().add(order.getOrderAmount()));
            orderSubmitMemberSet.add(order.getMemberId());

            if (isOrderPay(order)) {
                //已支付订单
                //计算支付订单数
                provinceBase.setOrderPayNum(provinceBase.getOrderPayNum() + 1);
                //计算支付订单金额
                provinceBase.setOrderPayAmount(provinceBase.getOrderPayAmount().add(order.getOrderAmount()));
                orderPayMemberSet.add(order.getMemberId());
            }
        });
        //计算下单人数
        provinceBase.setOrderSubmitMemberNum(orderSubmitMemberSet.size());
        //计算支付订单人数
        provinceBase.setOrderPayMemberNum(orderPayMemberSet.size());

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

        return provinceBase;
    }

}
