package com.hzs.report.order.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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(41, 42, 43);
        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);
        for (SaOrder saOrder : groupedOrders.get(41)) {
            summary.setRetailRegisterAmount(summary.getRetailDiscountAmount().add(saOrder.getOrderAmount()));
            summary.setRetailRegisterPv(summary.getRetailDiscountPv().add(saOrder.getOrderAchieve()));

            summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
            summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
        }
        for (SaOrder saOrder : groupedOrders.get(42)) {
            summary.setRetailUpgradeAmount(summary.getRetailUpgradeAmount().add(saOrder.getOrderAmount()));
            summary.setRetailUpgradePv(summary.getRetailUpgradePv().add(saOrder.getOrderAchieve()));

            summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
            summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
        }
        for (SaOrder saOrder : groupedOrders.get(43)) {
            summary.setRetailRepurchaseAmount(summary.getRetailRepurchaseAmount().add(saOrder.getOrderAmount()));
            summary.setRetailRepurchasePv(summary.getRetailRepurchasePv().add(saOrder.getOrderAchieve()));

            summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
            summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
        }
        for (SaOrder saOrder : groupedOrders.get(44)) {
            summary.setRetailDiscountAmount(summary.getRetailDiscountAmount().add(saOrder.getOrderAmount()));
            summary.setDividendPv(summary.getDividendPv().add(saOrder.getOrderAchieve()));

            summary.setTotalAmount(summary.getTotalAmount().add(saOrder.getOrderAmount()));
            summary.setTotalPv(summary.getTotalPv().add(saOrder.getOrderAmount()));
        }
        summary.setDividendPv(summary.getTotalPv().multiply(new BigDecimal("0.015")));
        baseMapper.insert(summary);
    }
}
