package com.xiaoniu.system.datastatistics.service.impl;

import com.xiaoniu.common.core.constant.Constants;
import com.xiaoniu.common.core.utils.DateUtils;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.vo.LabelAndValueNameVO;
import com.xiaoniu.common.core.vo.LabelAndValueVO;
import com.xiaoniu.system.datastatistics.dto.DataStatisticsQueryDTO;
import com.xiaoniu.system.datastatistics.service.IDataStatisticsService;
import com.xiaoniu.system.datastatistics.vo.*;
import com.xiaoniu.system.order.aftersales.mapper.TbAfterSalesMapper;
import com.xiaoniu.system.order.storeorder.mapper.TbOrderInfoMapper;
import com.xiaoniu.system.platformusers.user.mapper.TbClientUserMapper;
import com.xiaoniu.system.platformusers.user.mapper.TbUserInfoMapper;
import com.xiaoniu.system.store.store.mapper.TbStoreInfoMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author haoxuyang
 * @Date 2024/10/19 16:34
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DataStatisticsServiceImpl implements IDataStatisticsService {
    private final TbOrderInfoMapper tbOrderInfoMapper;
    private final TbStoreInfoMapper tbStoreInfoMapper;
    private final TbClientUserMapper tbClientUserMapper;
    private final TbAfterSalesMapper tbAfterSalesMapper;
    private final TbUserInfoMapper tbUserInfoMapper;

    @Override
    public PlatformOrderVO platformOrder(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        return tbOrderInfoMapper.platformOrder(dto);
    }

    @Override
    public StoreOrderVO storeOrder(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        return tbOrderInfoMapper.storeOrder(dto);
    }

    @Override
    public RealTimeDataVO realTimeData(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        RealTimeDataVO dataVO = new RealTimeDataVO();
        RealTimeDataVO date = tbOrderInfoMapper.realTimeData(dto);
        RealTimeDataVO date1 = tbStoreInfoMapper.realTimeData(dto);
        RealTimeDataVO date2 = tbClientUserMapper.realTimeData(dto);
        dataVO.setTurnover(date.getTurnover());
        dataVO.setOrderPaymentNum(date.getOrderPaymentNum());
        dataVO.setRegisterNum(date2.getRegisterNum());
        dataVO.setVisitsNum(date1.getVisitsNum());
        return dataVO;
    }

    @Override
    public Integer storeNum(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        return tbStoreInfoMapper.storeNum(dto);
    }

    @Override
    public CUusServiceInformationVO cusServiceInformation(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        return tbAfterSalesMapper.cusServiceInformation(dto);
    }

    @Override
    public BusinessDataSummaryVO businessDataSummary(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        List<String> months = new ArrayList<>();
        List<LabelAndValueVO> labelAndValueList = new ArrayList<>();
        if (StringUtils.isNotEmpty(dto.getDateParam()) || Constants.THREE.equals(dto.getType())) {
            months = DateUtils.getBeforeDays();
            dto.setDataType("day");
            labelAndValueList = tbOrderInfoMapper.businessDataSummary(dto);
        } else if (Constants.ONE.equals(dto.getType())) {
            months = DateUtils.getCurrentMonths();
            dto.setDataType("month");
            labelAndValueList = tbOrderInfoMapper.businessDataSummary(dto);
        } else if (Constants.TWO.equals(dto.getType())) {
            months = DateUtils.quarterMonths();
            dto.setDataType("month");
            labelAndValueList = tbOrderInfoMapper.businessDataSummary(dto);
        } else if (Constants.FOUR.equals(dto.getType())) {
            months = DateUtils.getWeekDaysList();
            dto.setDataType("day");
            labelAndValueList = tbOrderInfoMapper.businessDataSummary(dto);
        } else if (Constants.FIVE.equals(dto.getType())) {
            months = DateUtils.getDayHourTime();
            dto.setDataType("hour");
            labelAndValueList = tbOrderInfoMapper.businessDataSummary(dto);
        } else {
            dto.setBeginTime(DateUtils.getDate("YYYY") + "-01-01 00:00:00");
            dto.setEndTime(DateUtils.getYesTodayDate(9) + " 23:59:59");
            months = DateUtils.getYesTodayDateList(9);
            dto.setDataType("day");
            labelAndValueList = tbOrderInfoMapper.businessDataSummary(dto);
        }
        BusinessDataSummaryVO businessDataSummaryVO = new BusinessDataSummaryVO();
        businessDataSummaryVO.setDataList(new ArrayList<>());
        businessDataSummaryVO.setNameList(new ArrayList<>());
        if (!CollectionUtils.isEmpty(labelAndValueList)) {
            Map<String, String> map = labelAndValueList.stream().collect(Collectors.toMap(LabelAndValueVO::getValue, LabelAndValueVO::getLabel));
            for (String month : months) {
                businessDataSummaryVO.getNameList().add(month);
                businessDataSummaryVO.getDataList().add(StringUtils.isNotNull(map.get(month)) ? map.get(month) : "0");
            }
        } else {
            for (String month : months) {
                businessDataSummaryVO.getNameList().add(month);
                businessDataSummaryVO.getDataList().add("0");
            }
        }
        return businessDataSummaryVO;
    }

    @Override
    public OrderDataStatisticsVO orderStatistics(DataStatisticsQueryDTO dto) {
        OrderDataStatisticsVO orderDataStatisticsVO = new OrderDataStatisticsVO();
        orderDataStatisticsVO.setNameList(new ArrayList<>());
        orderDataStatisticsVO.setOrderAmount(new ArrayList<>());
        orderDataStatisticsVO.setDataNum(new ArrayList<>());

        //处理时间查询参数
        setDateParam(dto);
        if (StringUtils.isEmpty(dto.getBeginTime()) && StringUtils.isEmpty(dto.getEndTime())) {
            dto.setBeginTime(DateUtils.getDate("YYYY") + "-01-01 00:00:00");
            dto.setEndTime(DateUtils.getYesTodayDate(9) + " 23:59:59");
        }
        List<OrderStatisticsVO> list = tbOrderInfoMapper.orderStatistics(dto);
        if (!CollectionUtils.isEmpty(list)) {
            for (OrderStatisticsVO orderStatisticsVO : list) {
                orderDataStatisticsVO.getNameList().add(StringUtils.isNotEmpty(orderStatisticsVO.getStoreName()) ? orderStatisticsVO.getStoreName() : "0");
                orderDataStatisticsVO.getDataNum().add(StringUtils.isNotNull(orderStatisticsVO.getOrderNum()) ? String.valueOf(orderStatisticsVO.getOrderNum()) : "0");
                orderDataStatisticsVO.getOrderAmount().add(StringUtils.isNotNull(orderStatisticsVO.getOrderAmount()) ? String.valueOf(orderStatisticsVO.getOrderAmount()) : "0");
            }
        }
        if (orderDataStatisticsVO.getNameList().size() <= 6) {
            for (int i = orderDataStatisticsVO.getNameList().size(); i <= 6; i++) {
                orderDataStatisticsVO.getNameList().add("");
            }
        }
        if (orderDataStatisticsVO.getDataNum().size() <= 6) {
            for (int i = orderDataStatisticsVO.getDataNum().size(); i <= 6; i++) {
                orderDataStatisticsVO.getDataNum().add("0");
            }
        }
        if (orderDataStatisticsVO.getOrderAmount().size() <= 6) {
            for (int i = orderDataStatisticsVO.getOrderAmount().size(); i <= 6; i++) {
                orderDataStatisticsVO.getOrderAmount().add("0");
            }
        }
        return orderDataStatisticsVO;
    }

    @Override
    public List<LabelAndValueNameVO> userStatisticalAnalysis(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        return tbUserInfoMapper.userStatisticalAnalysis(dto);
    }

    @Override
    public List<LabelAndValueNameVO> userBehaviorStatistics(DataStatisticsQueryDTO dto) {
        //处理时间查询参数
        setDateParam(dto);
        List<LabelAndValueNameVO> list = tbClientUserMapper.userBehaviorStatistics(dto);
        list = list.stream().filter(o -> StringUtils.isNotNull(o.getName())).toList();
        return list;
    }

    /**
     * 处理时间
     *
     * @param dto 查询参数
     */
    public void setDateParam(DataStatisticsQueryDTO dto) {
        if (StringUtils.isNotEmpty(dto.getDateParam())) {
            dto.setBeginTime(dto.getDateParam() + "-01 00:00:00");
            int daysOfMonth = DateUtils.getDaysOfMonth(DateUtils.parseDate(dto.getDateParam()));
            dto.setEndTime(dto.getDateParam() + "-" + daysOfMonth + " 23:59:59");
        } else if (StringUtils.isNotNull(dto.getType())) {
            switch (dto.getType()) {
                case 1:
                    dto.setBeginTime(DateUtils.getDate("YYYY") + "-01-01 00:00:00");
                    dto.setEndTime(DateUtils.getDate("YYYY") + "-12-31" + " 23:59:59");
                    break;
                case 2:
                    dto.setBeginTime(DateUtils.getDate("YYYY") + "-01-01 00:00:00");
                    dto.setEndTime(DateUtils.getDate("YYYY") + "-12-31" + " 23:59:59");
                    break;
                case 3:
                    String date = DateUtils.getDate("YYYY");
                    int quarter = DateUtils.getQuarterOfYear();
                    dto.setBeginTime(DateUtils.getStartDayOfQuarter(Integer.parseInt(date), quarter) + " 00:00:00");
                    dto.setEndTime(DateUtils.getLastDayOfQuarter(Integer.parseInt(date), quarter) + " 23:59:59");
                    break;
                case 4:
                    dto.setBeginTime(DateUtils.getMonday() + " 00:00:00");
                    dto.setEndTime(DateUtils.getSunday() + " 23:59:59");
                    break;
                case 5:
                    dto.setBeginTime(DateUtils.getDate("YYYY_MM_DD") + " 00:00:00");
                    dto.setEndTime(DateUtils.getDate("YYYY_MM_DD") + " 23:59:59");
                    break;
            }
        }
    }
}
