package com.sundot.airport.ledger.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.ledger.domain.OperationStationHourlyThroughput;
import com.sundot.airport.ledger.domain.vo.AreaFlowVO;
import com.sundot.airport.ledger.domain.vo.StationHourlyThroughputGroupResVO;
import com.sundot.airport.ledger.domain.vo.StationHourlyThroughputVO;
import com.sundot.airport.ledger.mapper.OperationStationHourlyThroughputMapper;
import com.sundot.airport.ledger.service.IOperationStationHourlyThroughputService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 站级时间段别总过检Service业务层处理
 */
@Service
public class OperationStationHourlyThroughputServiceImpl extends ServiceImpl<OperationStationHourlyThroughputMapper, OperationStationHourlyThroughput>
        implements IOperationStationHourlyThroughputService {

    @Autowired
    private OperationStationHourlyThroughputMapper operationStationHourlyThroughputMapper;

    /**
     * 查询站级时间段别总过检列表
     *
     * @param operationStationHourlyThroughput 站级时间段别总过检
     * @return 站级时间段别总过检
     */
    @Override
    public List<OperationStationHourlyThroughput> selectOperationStationHourlyThroughputList(OperationStationHourlyThroughput operationStationHourlyThroughput) {
        return operationStationHourlyThroughputMapper.selectOperationStationHourlyThroughputList(operationStationHourlyThroughput);
    }

    /**
     * 新增站级时间段别总过检
     *
     * @param operationStationHourlyThroughput 站级时间段别总过检
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOperationStationHourlyThroughput(OperationStationHourlyThroughput operationStationHourlyThroughput) {
        operationStationHourlyThroughput.setCreateTime(DateUtils.getNowDate());
        return operationStationHourlyThroughputMapper.insert(operationStationHourlyThroughput);
    }

    /**
     * 修改站级时间段别总过检
     *
     * @param operationStationHourlyThroughput 站级时间段别总过检
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOperationStationHourlyThroughput(OperationStationHourlyThroughput operationStationHourlyThroughput) {
        operationStationHourlyThroughput.setUpdateTime(DateUtils.getNowDate());
        return operationStationHourlyThroughputMapper.updateById(operationStationHourlyThroughput);
    }

    /**
     * 批量删除站级时间段别总过检
     *
     * @param ids 需要删除的站级时间段别总过检主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOperationStationHourlyThroughputByIds(Long[] ids) {
        return operationStationHourlyThroughputMapper.deleteBatchIds(java.util.Arrays.asList(ids));
    }

    /**
     * 删除站级时间段别总过检信息
     *
     * @param id 站级时间段别总过检主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOperationStationHourlyThroughputById(Long id) {
        return operationStationHourlyThroughputMapper.deleteById(id);
    }

    /**
     * 功能描述：统计站级时间段人流量
     *
     * @return List<StationHourlyThroughputGroupResVO> 站级时间段人流量
     * @method countStationHourlyThroughput
     * @author PanHu Sun
     * @date 2026/5/19 16:46
     */
    @Override
    public List<StationHourlyThroughputGroupResVO> countStationHourlyThroughput() {
        List<StationHourlyThroughputVO> stationHourlyThroughputList = this.baseMapper.countStationHourlyThroughput();
        if (CollUtil.isEmpty(stationHourlyThroughputList)) {
            return Collections.emptyList();
        }

        // 1. 预处理：获取区域名称
        Set<String> areaIds = stationHourlyThroughputList.stream().map(StationHourlyThroughputVO::getAreaId).collect(Collectors.toSet());

        // TODO PanHu Sun 2026/5/19 17:16 待办事项：查询base_position表获取区域名称

        // 2. 核心：按小时分组 → 组装成前端需要的层级结构
        Map<String, List<StationHourlyThroughputVO>> hourGroupMap = stationHourlyThroughputList.stream().collect(Collectors.groupingBy(StationHourlyThroughputVO::getHourOfDay));

        return hourGroupMap.entrySet().stream().map(entry -> {
            String hour = entry.getKey();
            List<StationHourlyThroughputVO> hourDataList = entry.getValue();

            // 构建区域列表
            List<AreaFlowVO> areaList = hourDataList.stream().map(stats -> {
                AreaFlowVO areaFlowVO = new AreaFlowVO();
                areaFlowVO.setAreaId(stats.getAreaId());
                areaFlowVO.setAreaFlow(stats.getAreaFlow());
                return areaFlowVO;
            }).collect(Collectors.toList());

            // 构建小时分组对象
            StationHourlyThroughputGroupResVO groupVO = new StationHourlyThroughputGroupResVO();
            groupVO.setHourOfDay(hour);
            // 总人流量（取第一条即可，同小时数据总流量一致）
            groupVO.setTotalHourFlow(hourDataList.get(0).getTotalHourFlow());
            // 区域信息
            groupVO.setAreaList(areaList);
            return groupVO;
        }).collect(Collectors.toList());
    }
}
