package com.hzs.retail.member.utils;

import cn.hutool.core.util.ObjectUtil;
import com.hzs.retail.member.vo.CuMemberRetailDataStatisticsDetailVO;
import com.hzs.retail.member.vo.CuMemberRetailDataStatisticsResultVO;

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

/**
 * 用户统计数据工具
 * 主要用户统计用户的业绩/盒数/明细等
 * 主要处理的类为
 * @link com.hzs.retail.member.vo.CuMemberRetailDataStatisticsDetailVO
 */
public class DataStatisticsUtil {
    private static final Long SELF_VIRTUAL_ID = -1000L;
    private static final Long BIG_RANGE_VIRTUAL_ID = -2000L;
    private static final Long SMALL_RANGE_VIRTUAL_ID = -3000L;
    private static final String SELF_VIRTUAL_NAME = "个人消费虚拟分支";
    private static final String BIG_RANGE_VIRTUAL_NAME = "注水大区虚拟分支";
    private static final String SMALL_RANGE_VIRTUAL_NAME = "注水小区虚拟分支";

    public static CuMemberRetailDataStatisticsResultVO getCalcMap(List<CuMemberRetailDataStatisticsDetailVO> detailVOList, Long pkId){
        calcDetailVOTotalField(detailVOList);
        addVirtualBranch(detailVOList, pkId);
        CuMemberRetailDataStatisticsDetailVO total = getDetailVOByPKID(detailVOList, pkId);
        CuMemberRetailDataStatisticsDetailVO bigRange = getBigRangeDetailVO(detailVOList, pkId);
        return CuMemberRetailDataStatisticsResultVO.builder()
                .totalBoxNum(total.getTotalBoxNum())
                .totalPv(total.getTotalPv())
                .bigRangeTotalBoxNum(bigRange.getTotalBoxNum())
                .bigRangeTotalPv(bigRange.getTotalPv())
                .smallRangeTotalBoxNum(total.getTotalBoxNum() - bigRange.getTotalBoxNum())
                .smallRangeTotalPv(total.getTotalPv().subtract(bigRange.getTotalPv()))
                .build();
    }
    /**
     * 计算集合内数据总数
     * @param detailVOList
     */
    public static void calcDetailVOTotalField(List<CuMemberRetailDataStatisticsDetailVO> detailVOList){
        detailVOList.parallelStream().forEach(detailVO -> {
            detailVO.setTotalBoxNum(
                    detailVO.getConsumeBoxNum()         // 个人累计消费盒数(日结)
                     + detailVO.getNewBoxNum()          // 个人新增盒数(日结)
                     + detailVO.getTeamBoxNum()         // 团队累计消费盒数(日结)
                     + detailVO.getTeamNewBoxNum()      // 团队新增盒数(日结)
                     + detailVO.getSNewBoxNum()         // 个人新增消费盒数(秒结)
                     + detailVO.getSTeamNewBoxNum()     // 团队新增盒数(秒结)
//                     - detailVO.getBigBoxNum()          // 个人注水大区盒数(日结)
//                     - detailVO.getSmallBoxNum()        // 个人注水小区盒数(日结)
            );
            detailVO.setTotalPv(detailVO.getConsumePv()
                    .add(detailVO.getNewConsumePv())
                    .add(detailVO.getTeamConsumePv())
                    .add(detailVO.getTeamNewPv())
                    .add(detailVO.getSNewConsumePv())
                    .add(detailVO.getSTeamNewPv())
            );
        });
    }

    /**
     * 增加虚拟分支 主要为
     * 1.自己的消费总数
     * 2.注水的大小区
     *
     * @param detailVOList
     */
    public static void addVirtualBranch(List<CuMemberRetailDataStatisticsDetailVO> detailVOList, Long pkId){
        CuMemberRetailDataStatisticsDetailVO detailVO = getDetailVOByPKID(detailVOList, pkId);
        if(ObjectUtil.isNotEmpty(detailVO)){
            CuMemberRetailDataStatisticsDetailVO selfVirtualDetailVO = getCuMemberRetailDataStatisticsDetailVO(detailVO);
            detailVOList.add(selfVirtualDetailVO);
            if(detailVO.getBigBoxNum() >0 || detailVO.getSmallBoxNum() > 0){
                CuMemberRetailDataStatisticsDetailVO bigRangeVirtualDetailVO = CuMemberRetailDataStatisticsDetailVO.builder()
                        .pkId(BIG_RANGE_VIRTUAL_ID)
                        .memberCode(BIG_RANGE_VIRTUAL_NAME)
                        .totalBoxNum(detailVO.getBigBoxNum())
                        .totalPv(BigDecimal.ZERO)
                        .build();
                CuMemberRetailDataStatisticsDetailVO smallRangeVirtualDetailVO = CuMemberRetailDataStatisticsDetailVO.builder()
                        .pkId(SMALL_RANGE_VIRTUAL_ID)
                        .memberCode(SMALL_RANGE_VIRTUAL_NAME)
                        .totalBoxNum(detailVO.getBigBoxNum())
                        .totalPv(BigDecimal.ZERO)
                        .build();
                detailVOList.add(bigRangeVirtualDetailVO);
                detailVOList.add(smallRangeVirtualDetailVO);
            }
        }

    }

    private static CuMemberRetailDataStatisticsDetailVO getCuMemberRetailDataStatisticsDetailVO(CuMemberRetailDataStatisticsDetailVO detailVO) {
        return CuMemberRetailDataStatisticsDetailVO.builder()
                .pkId(SELF_VIRTUAL_ID)
                .memberCode(SELF_VIRTUAL_NAME)
                .totalBoxNum(
                        detailVO.getConsumeBoxNum()
                        + detailVO.getNewBoxNum()
                        + detailVO.getSNewBoxNum()
                )
                .totalPv(
                        detailVO.getConsumePv()
                                .add(detailVO.getNewConsumePv())
                                .add(detailVO.getSNewConsumePv())
                )
                .build();
    }

    /**
     * 移除总数Top1
     * @param detailVOList
     */
    public static List<CuMemberRetailDataStatisticsDetailVO> getSmallRangeList(List<CuMemberRetailDataStatisticsDetailVO> detailVOList){
        List<CuMemberRetailDataStatisticsDetailVO> result = new ArrayList<>();
        Integer maxTotalBoxNum = getMaxTotalBoxNum(detailVOList);
        if (maxTotalBoxNum == null) {
            return new ArrayList<>(detailVOList);
        }
        boolean removed = false;
        for (CuMemberRetailDataStatisticsDetailVO vo : detailVOList) {
            if (!removed && Objects.equals(vo.getTotalBoxNum(), maxTotalBoxNum)) {
                removed = true;
            } else {
                result.add(vo);
            }
        }
        return result;
    }

    /**
     * 根据ID获取对应数据
     * @param detailVOList
     * @return
     */
    public static CuMemberRetailDataStatisticsDetailVO getDetailVOByPKID(List<CuMemberRetailDataStatisticsDetailVO> detailVOList, Long pkId){
        return detailVOList.stream().filter(o -> o.getPkId().equals(pkId)).findFirst().orElse(null);
    }

    private static Integer getMaxTotalBoxNum(List<CuMemberRetailDataStatisticsDetailVO> detailVOList) {
        return detailVOList.stream()
                .map(CuMemberRetailDataStatisticsDetailVO::getTotalBoxNum)
                .filter(Objects::nonNull)
                .max(Integer::compare)
                .orElse(null);
    }
    private static CuMemberRetailDataStatisticsDetailVO getBigRangeDetailVO(List<CuMemberRetailDataStatisticsDetailVO> detailVOList, Long pkId){
        return detailVOList.stream()
                .filter(vo -> vo.getTotalBoxNum() != null && !vo.getPkId().equals(pkId))
                .max(Comparator.comparingInt(CuMemberRetailDataStatisticsDetailVO::getTotalBoxNum))
                .orElse(null);
    }
}
