package com.hzs.report.order.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.RetailConstants;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.report.CuMemberPerformanceSummary;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.report.order.mapper.CuMemberPerformanceSummaryMapper;
import com.hzs.report.order.service.ICuMemberPerformanceSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售业绩统计表 服务实现类
 * </p>
 *
 * @author sangelxiu1
 * @since 2025-09-22
 */
@Service
@Slf4j
public class CuMemberPerformanceSummaryServiceImpl extends ServiceImpl<CuMemberPerformanceSummaryMapper, CuMemberPerformanceSummary> implements ICuMemberPerformanceSummaryService {

    @Override
    public void createDataByDay(String day) {
        Date startDate = DateUtils.getStartOfDay(day);
        Date endDate = DateUtils.getEndOfDay(day);
        LambdaQueryWrapper<CuMemberPerformanceSummary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuMemberPerformanceSummary::getSummaryDate, startDate);
        baseMapper.delete(queryWrapper);
        List<Integer> orderTypeList = Arrays.asList(EOrderType.RETAIL_REGISTER.getValue(), EOrderType.RETAIL_UPGRADE.getValue(), EOrderType.RETAIL_REPURCHASE.getValue());
        List<SaOrder> orderList = baseMapper.getOrderByDay(startDate, endDate, orderTypeList);
        Map<Integer, List<SaOrder>> groupedOrders = orderList.stream().collect(Collectors.groupingBy(SaOrder::getOrderType));
        CuMemberPerformanceSummary summary = CuMemberPerformanceSummary.getInitCuMemberPerformanceSummary(startDate);

        BigDecimal dvSource;

        if(ObjectUtil.isNotEmpty(groupedOrders.get(EOrderType.RETAIL_REGISTER.getValue()))) {
            for (SaOrder saOrder : groupedOrders.get(EOrderType.RETAIL_REGISTER.getValue())) {
                summary.setRetailRegisterAmount(summary.getRetailRegisterAmount().add(saOrder.getOrderAmount()));
                summary.setRetailRegisterPv(summary.getRetailRegisterPv().add(saOrder.getOrderAchieve()));

                dvSource = getDvSource(summary.getRetailRegisterPv());
                BigDecimal dvUL = dvSource;
                if(saOrder.getOrderAmount().compareTo(RetailConstants.SPECIAL_AREA_41_MAX_ORDER_DIVIDEND_PV_AMOUNT) > 0){
                    dvUL = RetailConstants.SPECIAL_AREA_41_MAX_ORDER_DIVIDEND_PV_VALUE;
                }
                summary.setRetailRegisterDvSource(dvSource);
                summary.setRetailRegisterDvUL(dvUL);

                summary.setTotalDvSource(summary.getTotalDvSource().add(dvSource));
                summary.setTotalDvUL(summary.getTotalDvUL().add(dvUL));

                summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
                summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
            }
        }
        if(ObjectUtil.isNotEmpty(groupedOrders.get(EOrderType.RETAIL_UPGRADE.getValue()))) {
            for (SaOrder saOrder : groupedOrders.get(EOrderType.RETAIL_UPGRADE.getValue())) {
                summary.setRetailUpgradeAmount(summary.getRetailUpgradeAmount().add(saOrder.getOrderAmount()));
                summary.setRetailUpgradePv(summary.getRetailUpgradePv().add(saOrder.getOrderAchieve()));

                dvSource = getDvSource(summary.getRetailUpgradePv());
                BigDecimal dvUL = dvSource;
                if(saOrder.getOrderAmount().compareTo(RetailConstants.SPECIAL_AREA_41_MAX_ORDER_DIVIDEND_PV_AMOUNT) > 0){
                    dvUL = RetailConstants.SPECIAL_AREA_41_MAX_ORDER_DIVIDEND_PV_VALUE;
                }
                summary.setRetailUpgradeDvSource(dvSource);
                summary.setRetailUpgradeDvUL(dvUL);

                summary.setTotalDvSource(summary.getTotalDvSource().add(dvSource));
                summary.setTotalDvUL(summary.getTotalDvUL().add(dvUL));

                summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
                summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
            }
        }
        if(ObjectUtil.isNotEmpty(groupedOrders.get(EOrderType.RETAIL_REPURCHASE.getValue()))) {
            for (SaOrder saOrder : groupedOrders.get(EOrderType.RETAIL_REPURCHASE.getValue())) {
                summary.setRetailRepurchaseAmount(summary.getRetailRepurchaseAmount().add(saOrder.getOrderAmount()));
                summary.setRetailRepurchasePv(summary.getRetailRepurchasePv().add(saOrder.getOrderAchieve()));

                dvSource = getDvSource(summary.getRetailRepurchasePv());
                summary.setRetailRepurchaseDvSource(dvSource);
                summary.setRetailRepurchaseDvUL(dvSource);

                summary.setTotalDvSource(summary.getTotalDvSource().add(dvSource));
                summary.setTotalDvUL(summary.getTotalDvUL().add(dvSource));

                summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
                summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
            }
        }
        if(ObjectUtil.isNotEmpty(groupedOrders.get(EOrderType.DISCOUNT_ORDER.getValue()))){
            for (SaOrder saOrder : groupedOrders.get(EOrderType.DISCOUNT_ORDER.getValue())) {
                summary.setRetailDiscountAmount(summary.getRetailDiscountAmount().add(saOrder.getOrderAmount()));
                summary.setRetailDiscountPv(summary.getRetailDiscountPv().add(saOrder.getOrderAchieve()));

                dvSource = getDvSource(summary.getRetailDiscountPv());
                summary.setRetailDiscountDvSource(dvSource);
                summary.setRetailDiscountDvUL(dvSource);

                summary.setTotalDvSource(summary.getTotalDvSource().add(dvSource));
                summary.setTotalDvUL(summary.getTotalDvUL().add(dvSource));

                summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
                summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
            }
        }
        summary.setPkCreator(MagicNumberConstants.PK_ADMIN);
        baseMapper.insert(summary);
    }

    private BigDecimal getDvSource(BigDecimal source){
        return source.multiply(RetailConstants.SPECIAL_AREA_DIVIDEND_PV_PROPORTION);
    }
}
