package com.slodon.b2b2c.bo;

import com.slodon.b2b2c.business.pojo.Order;
import com.slodon.b2b2c.business.pojo.OrderProduct;
import com.slodon.b2b2c.business.pojo.OrderReturn;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.goods.pojo.GoodsBrand;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.statistics.pojo.MemberBehavior;
import com.slodon.b2b2c.statistics.pojo.base.StoreBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

import static com.slodon.b2b2c.bo.BrandOrderBO.isOrderPay;
import static com.slodon.b2b2c.enums.StatsConstant.*;

/**
 * 店铺统计业务关联对象
 */
@Data
public class StoreStatsBO {

    @ApiModelProperty("店铺id")
    private Long storeId;
    @ApiModelProperty("店铺名称")
    private String storeName;
    @ApiModelProperty("订单列表")
    private List<Order> orderList = new ArrayList<>();
    @ApiModelProperty("订单货品列表")
    private List<OrderProduct> orderProductList = new ArrayList<>();
    @ApiModelProperty("订单退货列表")
    private List<OrderReturn> orderReturnList = new ArrayList<>();
    @ApiModelProperty("店铺相关会员行为列表")
    private List<MemberBehavior> memberBehaviorList = new ArrayList<>();
    @ApiModelProperty("店铺新增商品列表")
    private List<Goods> storeNewGoodsList = new ArrayList<>();
    @ApiModelProperty("店铺新增品牌列表")
    private List<GoodsBrand> storeNewBrandList = new ArrayList<>();
    @ApiModelProperty("统计阶段内的新会员列表，用于统计店铺新增用户数，规则为：统计阶段内的新会员，访问了店铺，即定义为店铺的新增用户")
    private List<Member> newMemberList = new ArrayList<>();

    /**
     * 生成统计数据
     * 参数构造完毕后才能调用此方法
     *
     * @param startTime 统计开始时间
     */
    public StoreBase initStats(Date startTime) {
        StoreBase storeBase = new StoreBase();
        storeBase.setStatsTime(startTime);
        storeBase.setStoreId(storeId);
        storeBase.setStoreName(storeName);
        storeBase.setVisitorNum(0);
        storeBase.setViewNum(0);
        storeBase.setOrderSubmitNum(this.orderList.size());
        storeBase.setOrderSubmitAmount(new BigDecimal("0.00"));
        storeBase.setOrderSubmitMemberNum(0);
        storeBase.setOrderSubmitAtv(new BigDecimal("0.00"));
        storeBase.setOrderPayNum(0);
        storeBase.setOrderPayAmount(new BigDecimal("0.00"));
        storeBase.setOrderPayMemberNum(0);
        storeBase.setOrderPayAtv(new BigDecimal("0.00"));
        storeBase.setPvSubmitRate("");
        storeBase.setPvPayRate("");
        storeBase.setSubmitPayRate("");
        storeBase.setGoodsVisitorNum(0);
        storeBase.setGoodsViewNum(0);
        storeBase.setViewGoodsNum(0);
        storeBase.setNewGoodsNum(storeNewGoodsList.size());
        storeBase.setNewBrandNum(storeNewBrandList.size());
        storeBase.setCollectionGoodsNum(0);
        storeBase.setCollectionStoreNum(0);
        storeBase.setSalingGoodsNum(0);
        storeBase.setReturnNum(this.orderReturnList.size());
        storeBase.setReturnAmount(new BigDecimal("0.00"));
        storeBase.setNewMemberNum(0);
        storeBase.setPerViewNum(new BigDecimal("0.00"));

        //统计访问信息
        //店铺新增用户统计，暂时按照登录会员去重统计
        Set<Integer> visitorStoreNewMemberSet = new HashSet<>();
        //店铺访客统计，暂时按照登录会员去重统计 key为会员id+uuid
        Set<String> visitorStoreMemberSet = new HashSet<>();
        //商品访客统计，按照登录会员去重
        Set<String> visitorGoodsMemberSet = new HashSet<>();
        //被访问商品数统计，按照商品id去重
        Set<Long> viewGoodsSet = new HashSet<>();
        //被收藏商品数统计，按照商品id去重
        Set<Long> favoriteGoodsSet = new HashSet<>();
        //关注店铺的会员数统计，按照会员id去重
        Set<Integer> followStoreMemberSet = new HashSet<>();
        this.memberBehaviorList.forEach(memberBehavior -> {
            String key;
            if (!StringUtil.isNullOrZero(memberBehavior.getMemberId())) {
                key = memberBehavior.getMemberId().toString();
            } else {
                key = memberBehavior.getUuid();
            }
            switch (memberBehavior.getBehaviorType()) {
                case GOODS_PAGE_VIEW://商品浏览
                    //增加商品浏览量
                    storeBase.setGoodsViewNum(storeBase.getGoodsViewNum() + 1);
                    //商品访客去重
                    visitorGoodsMemberSet.add(key);
                    //被访问商品去重
                    viewGoodsSet.add(memberBehavior.getGoodsId());
                    break;
                case GOODS_FAVORITE://商品收藏
                    //被收藏商品数去重
                    favoriteGoodsSet.add(memberBehavior.getGoodsId());
                    break;
                case STORE_PAGE_VIEW://店铺浏览
                    //增加店铺浏览量
                    storeBase.setViewNum(storeBase.getViewNum() + 1);
                    //店铺访客去重
                    visitorStoreMemberSet.add(key);
                    //店铺新增用户去重
                    visitorStoreNewMemberSet.add(memberBehavior.getMemberId());
                    break;
                case STORE_FOLLOW://店铺关注
                    //增加收藏数
                    followStoreMemberSet.add(memberBehavior.getMemberId());
                    break;
                default:
                    //do nothing
            }
        });
        //计算店铺访客数
        storeBase.setVisitorNum(visitorStoreMemberSet.size());
        //计算商品访客数
        storeBase.setGoodsVisitorNum(visitorGoodsMemberSet.size());
        //计算被访问商品数
        storeBase.setViewGoodsNum(viewGoodsSet.size());
        //计算被收藏商品数
        storeBase.setCollectionGoodsNum(favoriteGoodsSet.size());
        //计算店铺关注数
        storeBase.setCollectionStoreNum(followStoreMemberSet.size());

        //统计店铺新增用户
        this.newMemberList.forEach(member -> {
            if (visitorStoreNewMemberSet.contains(member.getMemberId())) {
                //该会员在本阶段的访客中，定义为新访客
                storeBase.setNewMemberNum(storeBase.getNewMemberNum() + 1);
            }
        });
        //下单的会员set
        Set<Integer> orderSubmitMemberSet = new HashSet<>();
        //支付订单的会员set
        Set<Integer> orderPayMemberSet = new HashSet<>();
        this.orderList.forEach(order -> {
            //计算下单金额
            storeBase.setOrderSubmitAmount(storeBase.getOrderSubmitAmount().add(order.getOrderAmount()));
            //下单人数去重
            orderSubmitMemberSet.add(order.getMemberId());
            if (isOrderPay(order)) {
                // 订单已支付
                // 计算支付订单数
                storeBase.setOrderPayNum(storeBase.getOrderPayNum() + 1);
                // 计算支付订单金额
                storeBase.setOrderPayAmount(storeBase.getOrderPayAmount().add(order.getOrderAmount()));
                // 支付人数去重
                orderPayMemberSet.add(order.getMemberId());
            }
        });
        //计算支付订单人数
        storeBase.setOrderPayMemberNum(orderPayMemberSet.size());
        //计算下单人数
        storeBase.setOrderSubmitMemberNum(orderSubmitMemberSet.size());
        if (storeBase.getOrderSubmitMemberNum() > 0) {
            //计算下单客单价
            storeBase.setOrderSubmitAtv(storeBase.getOrderSubmitAmount()
                    .divide(new BigDecimal(storeBase.getOrderSubmitMemberNum()), 2, BigDecimal.ROUND_HALF_UP));
        }
        if (storeBase.getOrderPayMemberNum() > 0) {
            //计算支付客单价
            storeBase.setOrderPayAtv(storeBase.getOrderPayAmount()
                    .divide(new BigDecimal(storeBase.getOrderPayMemberNum()), 2, BigDecimal.ROUND_HALF_UP));
        }
        if (storeBase.getVisitorNum() > 0) {
            //计算访问-下单转化率
            storeBase.setPvSubmitRate(new BigDecimal(storeBase.getOrderSubmitMemberNum() * 100)
                    .divide(new BigDecimal(storeBase.getVisitorNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
            //计算访问-支付转化率
            storeBase.setPvPayRate(new BigDecimal(storeBase.getOrderPayMemberNum() * 100)
                    .divide(new BigDecimal(storeBase.getVisitorNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        if (storeBase.getOrderSubmitMemberNum() > 0) {
            //计算下单-支付转化率
            storeBase.setSubmitPayRate(new BigDecimal(storeBase.getOrderPayMemberNum() * 100)
                    .divide(new BigDecimal(storeBase.getOrderSubmitMemberNum()), 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        if (storeBase.getVisitorNum() > 0) {
            //计算人均浏览量
            storeBase.setPerViewNum(new BigDecimal(storeBase.getViewNum())
                    .divide(new BigDecimal(storeBase.getVisitorNum()), 2, BigDecimal.ROUND_HALF_UP));
        }

        //动销商品set，按照商品id去重
        Set<Long> salingGoodsSet = new HashSet<>();
        this.orderProductList.forEach(orderProduct -> {
            //动销商品去重
            salingGoodsSet.add(orderProduct.getGoodsId());
        });
        //计算动销商品数
        storeBase.setSalingGoodsNum(salingGoodsSet.size());

        //计算退货退款信息
        this.orderReturnList.forEach(orderReturn -> {
            //计算退单金额
            storeBase.setReturnAmount(storeBase.getReturnAmount().add(orderReturn.getReturnMoneyAmount()));
        });

        return storeBase;
    }

}
