package com.njcn.influx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.influx.bo.param.TableEnum;
import com.njcn.influx.bo.po.JobDetailInfluxDB;
import com.njcn.influx.mapper.JobDetailInfluxDBMapper;
import com.njcn.influx.service.JobDetailInfluxDBService;
import com.njcn.oracle.bo.param.JobQueryParam;
import com.njcn.oracle.util.LocalDateUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/1/8  12:27【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class JobDetailInfluxDBServiceImpl extends MppServiceImpl<JobDetailInfluxDBMapper, JobDetailInfluxDB> implements JobDetailInfluxDBService {


    @Override
    public JobDetailInfluxDB select(String tableName, LocalDate date) {
        return this.lambdaQuery()
                .eq(JobDetailInfluxDB::getTableName, tableName)
                .eq(JobDetailInfluxDB::getExcuteDate, date)
                .one();
    }

    @Override
    public IPage<JobDetailInfluxDB> selectByParam(JobQueryParam jobQueryParam) {
        IPage<JobDetailInfluxDB> page = new Page<>(jobQueryParam.getCurrentPage(), jobQueryParam.getPageSize());
        QueryWrapper<JobDetailInfluxDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().between(JobDetailInfluxDB::getExcuteDate, jobQueryParam.getStartTime(), jobQueryParam.getEndTime()).
                in(!CollectionUtils.isEmpty(jobQueryParam.getStates()), JobDetailInfluxDB::getState, jobQueryParam.getStates()).
                in(!CollectionUtils.isEmpty(jobQueryParam.getTableNames()), JobDetailInfluxDB::getTableName, jobQueryParam.getTableNames());

        List<JobDetailInfluxDB> jobDetails = this.getBaseMapper().selectList(queryWrapper);

        List<String> tableNames;
        if (CollectionUtils.isEmpty(jobQueryParam.getTableNames())) {
            tableNames = TableEnum.getExecutableTypes();
        } else {
            tableNames = jobQueryParam.getTableNames();
        }
        List<LocalDate> dateList = LocalDateUtil.getDateList(jobQueryParam.getStartTime(), jobQueryParam.getEndTime());
        List<String> finalTableNames = tableNames;
        dateList.stream().forEach(localDate -> {
            finalTableNames.stream().forEach(temp -> {
                JobDetailInfluxDB jobDetail = new JobDetailInfluxDB();
                jobDetail.setTableName(temp);
                jobDetail.setExcuteDate(localDate);
                if (jobDetails.contains(jobDetail)) {
                    return;
                }
                jobDetail.setTableName_CN(TableEnum.getValueByCode(temp));
                jobDetail.setState(3);
                jobDetails.add(jobDetail);
            });
        });

        List<JobDetailInfluxDB> jobDetailIPage = jobDetails.stream().filter(temp->jobQueryParam.getStates().contains(temp.getState()+"")).sorted(Comparator.comparing(JobDetailInfluxDB::getExcuteDate).reversed()).
                skip((jobQueryParam.getCurrentPage() - 1) * jobQueryParam.getPageSize()).
                limit(jobQueryParam.getPageSize()).collect(Collectors.toList());
        jobDetailIPage.stream().forEach(temp -> temp.setTableName_CN(TableEnum.getValueByCode(temp.getTableName())));

        page.setRecords(jobDetailIPage);
        page.setTotal(Long.parseLong(jobDetails.size() + ""));
        return page;
    }

    @Override
    public boolean jobRemove(JobDetailInfluxDB jobDetail) {
        QueryWrapper<JobDetailInfluxDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(JobDetailInfluxDB::getTableName, jobDetail.getTableName())
                .eq(JobDetailInfluxDB::getExcuteDate, jobDetail.getExcuteDate());
        return this.remove(queryWrapper);
    }


}
