package com.njcn.oracle.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
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.oracle.bo.param.JobQueryParam;
import com.njcn.oracle.bo.param.ServiceTypeEnum;
import com.njcn.oracle.util.LocalDateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.oracle.mybatis.mapper.JobDetailMapper;
import com.njcn.oracle.bo.po.JobDetail;
import com.njcn.oracle.service.JobDetailService;
import org.springframework.util.CollectionUtils;

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


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

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

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

        List<String> tableNames;
        if (CollectionUtils.isEmpty(jobQueryParam.getTableNames())) {
            tableNames = ServiceTypeEnum.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 -> {
                JobDetail jobDetail = new JobDetail();
                jobDetail.setTableName(temp);
                jobDetail.setExcuteDate(localDate);
                if (jobDetails.contains(jobDetail)) {
                    return;
                }
                jobDetail.setTableName_CN(ServiceTypeEnum.getValueByCode(temp));
                jobDetail.setState(3);
                jobDetails.add(jobDetail);
            });
        });

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

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

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


}
