package cn.cslg.report.service.business;

import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.ImportTask;

import cn.cslg.report.mapper.ImportTaskMapper;

import cn.cslg.report.service.OutInterfaceService;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 李仁杰
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@SuppressWarnings({"all"})
public class ImportTaskService extends ServiceImpl<ImportTaskMapper, ImportTask> {
    /**
     * @function 根据导入任务报告id和类型查询：导入任务
     * @param importTask
     * @return importTask（导入任务）
     */
    private final ImportTaskMapper mapper;
    private final OutInterfaceService outInterfaceService;

    public String reImportTask(ImportTask importTask) throws IOException {
        LambdaQueryWrapper<ImportTask> queryWrapper = new LambdaQueryWrapper<>();
        if (importTask.getReportId() != null) {
            queryWrapper.eq(ImportTask::getReportId, importTask.getReportId());
        }
        if (importTask.getCreateUserName() != null && importTask.getCreateUserName() != "") {
            String res = outInterfaceService.getPersonIdByNamePCS(importTask.getCreateUserName());
            List<Integer> createIds = JSONArray.parseArray(res, Integer.class);
            //当未查询到时直接返回空页
            if (createIds == null || createIds.size() == 0) {
                createIds = new ArrayList<>(Arrays.asList(-1));
            }
            queryWrapper.in(ImportTask::getCreateUserId, createIds);
        }
        //根据状态查询报告
        if(importTask.getState()!=null){
            queryWrapper.eq(ImportTask::getState,importTask.getState());
        }
        queryWrapper.orderByDesc(ImportTask::getCreateDate);
        if (importTask.getCurrent() != null && importTask.getSize() != null) {
            IPage<ImportTask> importTaskIPage = this.page(new Page<>(importTask.getCurrent(), importTask.getSize()), queryWrapper);
            return Response.success(importTaskIPage);
        }
        return Response.success(this.list(queryWrapper));


    }

    /**
     * @param id
     * @return
     * @function 根据导入任务的id删除导入任务
     */
    public Boolean delImportTask(Integer id) {
        Boolean flag = this.removeById(id);
        return flag;
    }

    //删除报告时删除导入任务
    @Transactional(rollbackFor = Exception.class)
    public String deleteByRortID(Integer id) {
        LambdaQueryWrapper<ImportTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportTask::getReportId, id);
        remove(queryWrapper);
        return Response.success();

    }

    /**
     * @param date
     * @return
     * @function :根据时间去获取文件的路径并且删除文件
     */
    public List<String> findPath(Date date) {
        List<String> stringList = new ArrayList<>();
        List<ImportTask> importTasks = mapper.find(date);
        stringList = importTasks.stream().map(ImportTask::getFilePath).collect(Collectors.toList());
        mapper.dele(date);
        return stringList;
    }


}