package com.gyee.runeconomy.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.runeconomy.entity.StationInfoMonth;
import com.gyee.runeconomy.mapper.StationInfoMonthMapper;
import com.gyee.runeconomy.model.auto.PointInfo;
import com.gyee.runeconomy.service.IStationInfoMonthService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gfhd
 * @since 2024-03-05
 */
@Service
public class StationInfoMonthServiceImpl extends ServiceImpl<StationInfoMonthMapper, StationInfoMonth> implements IStationInfoMonthService {

    @Override
    public boolean saveOrUpdateBatch(Collection<StationInfoMonth> entityList) {
        return super.saveOrUpdateBatch(entityList);
    }

    @Override
    public List<StationInfoMonth> list(Wrapper<StationInfoMonth> queryWrapper) {
        return super.list(queryWrapper);
    }

    public List<StationInfoMonth> getInfoByDate(Date date) {
        QueryWrapper<StationInfoMonth> wrapper = new QueryWrapper<>();
        wrapper.eq("record_date", date);
        return super.list(wrapper);
    }

    public List<StationInfoMonth> getInfoByDate(Date date, List<PointInfo> entity) {
        List<StationInfoMonth> list = getInfoByDate(date);
        if (CollUtil.isEmpty(list)) {
            entity.forEach(pi -> {
                StationInfoMonth day = new StationInfoMonth();
                day.setStationId(pi.getStationId());
                day.setRecordDate(date);
                list.add(day);
            });
        }
        return list;
    }

    public Map<String, StationInfoMonth> getInfoByDateMap(Date date, List<PointInfo> entity) {
        List<StationInfoMonth> list = getInfoByDate(date, entity);
        return list.stream().collect(Collectors.toMap(StationInfoMonth::getStationId, Function.identity()));
    }
}
