package cn.cslg.pas.service.upLoadPatent;

import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
import cn.cslg.pas.common.model.vo.UploadParamsVO;
import cn.cslg.pas.common.model.vo.UploadSettingVO;
import cn.cslg.pas.common.utils.FileUtils;
import cn.cslg.pas.common.utils.JsonUtils;
import cn.cslg.pas.common.utils.ReadExcelUtils;
import cn.cslg.pas.domain.PatentData;
import cn.cslg.pas.domain.Task;
import cn.cslg.pas.service.TaskService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Excel类型任务解析获取专利类
 *
 * @Author chenyu
 * @Date 2023/6/16
 */
@Service
@RequiredArgsConstructor
public class ExcutePatentDataExcel implements IExcutePatentData {
    private final FileUtils fileUtils;
    private final ExcuteUploadSettingService excuteUploadSettingService;
    private final ExcuteDataToVOService excuteDataToVOService;
    private final PantentQueueService pantentQueueService;
    private final TaskService taskService;
    private Integer pTaskId = 0;
    private Integer pTaskStatus = 0;

    /**
     * 解析获取专利数据
     *
     * @param task 任务
     * @throws IOException 抛出IO异常
     */
    @Override
    public void startExcute(Task task) throws IOException {
        try {
            //从任务中取出文件路径、总条数、成功条数、前台参数json
            String filePath = fileUtils.getPath(task.getUrl());
            Integer total = task.getTotal();
            int lastIndex = task.getSuccessNum();
            ProjectImportPatentVO projectImportPatentVO = JsonUtils.jsonToPojo(task.getPramJson(), ProjectImportPatentVO.class);

            //解析数据源类，通过数据来源id（如1：智慧芽）解析数据源配置文件，获得数据源配置文件对象jsonData
            List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(projectImportPatentVO.getSourceId());
            //解析Excel文件获得工作簿
            Sheet sheet = ReadExcelUtils.readExcel(filePath);
            //遍历专利总数量，在循环中将专利一个一个存入消费者专利队列
            for (int i = lastIndex; i < total; i++) {
                //判断若任务状态为已暂停，则结束生产
                if (pTaskId.equals(task.getId()) && pTaskStatus == 4) {
                    return;
                }
                PatentData patentData = ReadExcelUtils.readExcelOneRow(filePath, sheet, i + 1);
                //调用装载数据类（根据数据源配置文件对象和专利源数据生成专利数据）
                UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);

                //专利丢入消费者队列，并唤醒消费者线程
                pantentQueueService.patentToQueue(task, uploadParamsVO);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //生产消费到一半时，发生错误异常，将任务状态置为完成
            task = taskService.getById(task.getId());
            if (task.getStatus().equals(1)) {
                task.setStatus(2);
                taskService.updateById(task);
            }
        }
    }

    public void setTaskStatus(Integer taskId, Integer taskStatus) {
        this.pTaskId = taskId;
        this.pTaskStatus = taskStatus;
    }

    public Integer getpTaskId() {
        return pTaskId;
    }

    public Integer getpTaskStatus() {
        return pTaskStatus;
    }

}
