package com.ycwl.basic.service.pc.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.clickhouse.service.StatsQueryService;
import com.ycwl.basic.mapper.BrokerRecordMapper;
import com.ycwl.basic.model.pc.broker.entity.BrokerRecord;
import com.ycwl.basic.model.pc.broker.req.BrokerRecordReqQuery;
import com.ycwl.basic.model.pc.broker.resp.BrokerRecordRespVO;
import com.ycwl.basic.model.pc.broker.resp.DailySummaryRespVO;
import com.ycwl.basic.service.pc.BrokerRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author：longbinbin
 * @Date：2024/12/12 10:00
 */
@Service
public class BrokerRecordServiceImpl implements BrokerRecordService {
    @Autowired
    private BrokerRecordMapper brokerRecordMapper;
    @Autowired
    private StatsQueryService statsQueryService;

    @Override
    public PageInfo<BrokerRecordRespVO> pageQuery(BrokerRecordReqQuery brokerRecordReqQuery) {
        PageHelper.startPage(brokerRecordReqQuery.getPageNum(), brokerRecordReqQuery.getPageSize());
        List<BrokerRecordRespVO> list = brokerRecordMapper.list(brokerRecordReqQuery);
        PageInfo<BrokerRecordRespVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<BrokerRecordRespVO> list(BrokerRecordReqQuery brokerRecordReqQuery) {
        return brokerRecordMapper.list(brokerRecordReqQuery);
    }

    @Override
    public BrokerRecordRespVO getById(Long id) {
        return brokerRecordMapper.getById(id);
    }

    @Override
    public int add(BrokerRecord brokerRecord) {
        return brokerRecordMapper.add(brokerRecord);
    }

    @Override
    public int delete(Long id) {
        return brokerRecordMapper.deleteById(id);
    }

    @Override
    public int update(BrokerRecord brokerRecord) {
        return brokerRecordMapper.update(brokerRecord);
    }

    @Override
    public List<DailySummaryRespVO> getDailySummaryByBrokerId(Long brokerId, Date startTime, Date endTime) {
        // 从 MySQL 获取订单数据
        List<HashMap<String, Object>> orderStats = brokerRecordMapper.getDailyOrderStats(brokerId, startTime, endTime);

        // 从 ClickHouse/MySQL 获取扫码数据
        List<HashMap<String, Object>> scanStats = statsQueryService.getDailyScanStats(brokerId, startTime, endTime);

        // 将扫码数据转换为 Map 便于查找
        Map<String, Long> scanCountByDate = new HashMap<>();
        if (scanStats != null) {
            for (HashMap<String, Object> stat : scanStats) {
                Object dateObj = stat.get("date");
                String dateKey = dateObj != null ? DateUtil.formatDate((Date) dateObj) : null;
                Object scanCountObj = stat.get("scanCount");
                Long scanCount = scanCountObj != null ? ((Number) scanCountObj).longValue() : 0L;
                if (dateKey != null) {
                    scanCountByDate.put(dateKey, scanCount);
                }
            }
        }

        // 合并数据
        List<DailySummaryRespVO> result = new ArrayList<>();
        for (HashMap<String, Object> orderStat : orderStats) {
            DailySummaryRespVO vo = new DailySummaryRespVO();

            Object dateObj = orderStat.get("date");
            if (dateObj instanceof Date) {
                vo.setDate((Date) dateObj);
            }

            String dateKey = dateObj != null ? DateUtil.formatDate((Date) dateObj) : null;
            vo.setScanCount(scanCountByDate.getOrDefault(dateKey, 0L));

            Object orderCountObj = orderStat.get("orderCount");
            vo.setOrderCount(orderCountObj != null ? ((Number) orderCountObj).longValue() : 0L);

            Object totalOrderPriceObj = orderStat.get("totalOrderPrice");
            vo.setTotalOrderPrice(totalOrderPriceObj != null ? new BigDecimal(totalOrderPriceObj.toString()) : BigDecimal.ZERO);

            Object totalBrokerPriceObj = orderStat.get("totalBrokerPrice");
            vo.setTotalBrokerPrice(totalBrokerPriceObj != null ? new BigDecimal(totalBrokerPriceObj.toString()) : BigDecimal.ZERO);

            result.add(vo);
        }

        return result;
    }

}