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

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.exam_record_data_audit.vo.ExamRecordDataVO;
import cn.com.qmth.examcloud.tool.utils.HttpHelper;
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.*;

@Component
public class ExamRecordDataAuditTask implements TaskService {

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

    @Autowired
    private SysProperty sysProperty;

    @Autowired
    private CommonService commonService;

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

    /**
     * 将某些考试记录重审为违纪
     */
    public void execute(User loginUser) {
        String dataFilePath = "D:/home/data/illegally_list.xlsx";
        List<ExamRecordDataVO> list = new ArrayList<>();
        try {
            EasyExcel.read(dataFilePath, ExamRecordDataVO.class, new AnalysisEventListener<ExamRecordDataVO>() {
                        @Override
                        public void invoke(ExamRecordDataVO 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 (ExamRecordDataVO vo : list) {
            Set<Long> examRecordDataIds = new HashSet<>();
            examRecordDataIds.add(vo.getExamRecordDataId());

            String url = loginUser.getServerUrl() + "/api/ecs_oe_admin/exam/audit/redoAudit";
            Map<String, String> headers = new HashMap<>();
            headers.put("key", loginUser.getKey());
            headers.put("token", loginUser.getToken());

            Map<String, Object> params = new HashMap<>();
            params.put("examRecordDataIds", examRecordDataIds);
            params.put("illegallyTypeId", 5L);// 违纪ID，通过页面查询获取
            params.put("disciplineDetail", "视频替考");// 选填
            params.put("isPass", false);// 审核是否通过
            params.put("fromMarking", true);

            String result = HttpHelper.post(url, headers, params);
            log.info("examRecordDataId:{} result:{}", vo.getExamRecordDataId(), result);
        }
    }

}
