package com.qmth.exam.reserve.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.boot.core.collection.PageResult;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.boot.tools.excel.ExcelReader;
import com.qmth.boot.tools.excel.enums.ExcelType;
import com.qmth.boot.tools.excel.model.DataMap;
import com.qmth.exam.reserve.bean.PagerReq;
import com.qmth.exam.reserve.bean.studentimport.StudentImportTaskExport;
import com.qmth.exam.reserve.bean.studentimport.StudentImportTaskVO;
import com.qmth.exam.reserve.dao.StudentImportTaskDao;
import com.qmth.exam.reserve.entity.ApplyTaskEntity;
import com.qmth.exam.reserve.entity.StudentImportTaskEntity;
import com.qmth.exam.reserve.enums.ImportStatus;
import com.qmth.exam.reserve.enums.ImportType;
import com.qmth.exam.reserve.service.ApplyTaskService;
import com.qmth.exam.reserve.service.StudentImportAsyncService;
import com.qmth.exam.reserve.service.StudentImportTaskService;
import com.qmth.exam.reserve.util.PageUtil;

@Service
public class StudentImportTaskServiceImpl extends ServiceImpl<StudentImportTaskDao, StudentImportTaskEntity> implements StudentImportTaskService {

    @Autowired
    private StudentImportAsyncService asyncImportService;

    @Autowired
    private StudentImportTaskService studentTaskService;

    @Autowired
    private ApplyTaskService applyTaskService;

    @Override
    public void importStudent(Long operateId, String filePath, InputStream inputStream) {
        List<DataMap> lineList = null;
        ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
        try {
            lineList = reader.getDataMapList();
        } catch (Exception e) {
            throw new StatusException("Excel 解析失败");
        }
        if (CollectionUtils.isEmpty(lineList)) {
            throw new StatusException("Excel无内容");
        }
        StudentImportTaskEntity studentTask = new StudentImportTaskEntity();
        ApplyTaskEntity task = applyTaskService.findApplyTask();
        if (task == null) {
            throw new StatusException("没有开启的任务");
        }
        studentTask.setOperateId(operateId);
        studentTask.setApplyTaskId(task.getId());
        studentTask.setType(ImportType.STUDENT.toString());
        studentTask.setStatus(ImportStatus.RUNNING.toString());
        studentTask.setFilePath(filePath);
        studentTaskService.save(studentTask);
        // TODO 原始文件上传到OSS
        asyncImportService.asyncImportStudent(studentTask, task.getOrgId(), lineList);
    }

    @Override
    public PageResult<StudentImportTaskVO> page(PagerReq req) {
        IPage<StudentImportTaskVO> page = this.baseMapper.page(new Page<StudentImportTaskVO>(req.getPageNumber(), req.getPageSize()), req);
        return PageUtil.of(page);
    }

    @Override
    public List<StudentImportTaskExport> exportPage(Long id) {
        List<StudentImportTaskExport> exportList = new ArrayList<StudentImportTaskExport>();
        StudentImportTaskEntity importTask = getById(id);
        if (importTask != null && StringUtils.isNotEmpty(importTask.getMessage())) {
            ApplyTaskEntity task = applyTaskService.getById(importTask.getApplyTaskId());
            StudentImportTaskExport taskExport = new StudentImportTaskExport();
            taskExport.setMessage(importTask.getMessage());
            taskExport.setTaskName(task.getName());
            exportList.add(taskExport);
        }
        return exportList;
    }

}
