package cn.com.qmth.examcloud.tool.service.batch_delete_exam_student;

import cn.com.qmth.examcloud.tool.config.SysProperty;
import cn.com.qmth.examcloud.tool.entity.TaskEntity;
import cn.com.qmth.examcloud.tool.service.CommonService;
import cn.com.qmth.examcloud.tool.service.TaskService;
import cn.com.qmth.examcloud.tool.service.batch_delete_exam_student.vo.ExamStudentExcelVO;
import cn.com.qmth.examcloud.tool.service.update_correct_answer_and_re_fix_score.vo.ExamStudentVO;
import cn.com.qmth.examcloud.tool.vo.user.User;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class BatchDeleteExamStudentTask implements TaskService {

    private final static Logger log = LoggerFactory.getLogger(BatchDeleteExamStudentTask.class);

    @Autowired
    private SysProperty sysProperty;

    @Autowired
    private CommonService commonService;

    @Override
    public void start(TaskEntity task) {
        // this.execute(loginUser, task);
    }

    /**
     * 批量删除某个考试的考生列表
     */
    public void deleteExamStudentForExcel(User loginUser) {
        String dataFilePath = "D:/home/data/delete_exam_students.xlsx";

        long startTime = System.currentTimeMillis();
        List<ExamStudentExcelVO> list = new ArrayList<>();
        try {
            EasyExcel.read(dataFilePath, ExamStudentExcelVO.class, new AnalysisEventListener<ExamStudentExcelVO>() {
                        @Override
                        public void invoke(ExamStudentExcelVO data, AnalysisContext analysisContext) {
                            list.add(data);
                        }

                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            // ignore
                        }
                    }
            ).sheet().doRead();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("Excel内容解析错误，请使用标准模板！");
        }

        log.info("待删除条数:{}", list.size());

        for (ExamStudentExcelVO vo : list) {
            List<ExamStudentVO> examStudents = commonService.getExamStudentList(loginUser, vo.getExamId(), null, vo.getIdentityNumber());
            for (ExamStudentVO examStudent : examStudents) {
                log.info("examId:{} identityNumber:{} examStudentId:{}", examStudent.getExamId(), examStudent.getIdentityNumber(), examStudent.getExamStudentId());
            }
        }

        log.info("删除完成，耗时:{}ms", System.currentTimeMillis() - startTime);
    }

}
