package cn.cecep.talroad.data.analyse.task.service;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.domain.PcStationBuildingStat;
import cn.cecep.talroad.mapper.PcStationBuildingStatMapper;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class StationBuildingStatTaskService extends ServiceImpl<PcStationBuildingStatMapper, PcStationBuildingStat> implements IService<PcStationBuildingStat> {

    @Autowired
    private PcStationBuildingStatMapper pcStationBuildingStatMapper;

    public Boolean dayTask(Date startTime, Date endTime) {
        // 获取当天的数据
        String startDate = DateUtils.parseDateToStr("yyyy-MM-dd 00:00:00", startTime);
        String endDate = DateUtils.parseDateToStr("yyyy-MM-dd 23:59:59", endTime);
        List<PcStationBuildingStat> statList = pcStationBuildingStatMapper.selectStatisticsData(startDate, endDate);

        if (CollectionUtil.isNotEmpty(statList)) {
            statList.forEach(stat -> {
                stat.setStatisticalTime(startTime);
                stat.setCreateTime(new Date());
            });

            this.saveBatch(statList);
        }
        return true;
    }
}
