package cn.com.qmth.examcloud.tool;

import cn.com.qmth.examcloud.tool.cache.LoginSessionManager;
import cn.com.qmth.examcloud.tool.config.SpringContextHolder;
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.batch_import_exam_student.BatchImportExamStudentTask;
import cn.com.qmth.examcloud.tool.service.exam_record_data_audit.ExamRecordDataAuditTask;
import cn.com.qmth.examcloud.tool.service.export_exam_capture.ExportExamCaptureTask;
import cn.com.qmth.examcloud.tool.service.export_exam_paper_struct.ExportExamPaperStructTask;
import cn.com.qmth.examcloud.tool.service.export_student_photo.ExportStudentPhotoTask;
import cn.com.qmth.examcloud.tool.service.reset_student_password.ResetStudentPasswordTask;
import cn.com.qmth.examcloud.tool.service.unbind_student_code.UnbindStudentCodeTask;
import cn.com.qmth.examcloud.tool.utils.JsonMapper;
import cn.com.qmth.examcloud.tool.vo.user.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ToolTest {

    @Autowired
    private SysProperty sysProperty;

    @Autowired
    private CommonService commonService;

    @Autowired
    private ResetStudentPasswordTask resetStudentPasswordTask;

    @Autowired
    private UnbindStudentCodeTask unbindStudentCodeTask;

    @Autowired
    private ExportStudentPhotoTask exportStudentPhotoTask;

    @Autowired
    private ExamRecordDataAuditTask examRecordDataAuditTask;

    @Autowired
    private ExportExamCaptureTask exportExamCaptureTask;

    @Autowired
    private ExportExamPaperStructTask exportExamPaperStructTask;

    public static final String DATA_DIR = "D:/home/data";

    @Test
    public void demo() throws Exception {
        // User loginUser = doLogin();

        // this.testTask(loginUser);
        // resetStudentPasswordTask.execute(loginUser, 0L);
        // unbindStudentCodeTask.execute(loginUser);
        // exportExamPaperStructTask.execute(loginUser, 0L, DATA_DIR);
        // examRecordDataAuditTask.execute(loginUser);
        // exportStudentPhotoTask.execute(null, DATA_DIR);
        // exportExamCaptureTask.execute(null, DATA_DIR);
    }

    private void testTask(User loginUser) {
        Map<String, Object> params = new HashMap<>();
        params.put("examId", 89);
        params.put("dataFilePath", DATA_DIR + "/考生导入模板-1w.xlsx");
        // params.put("dataFilePath", DATA_DIR + "/批量创建用户模板.xlsx");

        TaskEntity task = new TaskEntity();
        task.setId(System.currentTimeMillis());
        task.setUserToken(loginUser.getToken());
        task.setParams(new JsonMapper().toJson(params));

        SpringContextHolder.getBean(BatchImportExamStudentTask.class).start(task);
        // SpringContextHolder.getBean(BatchCreateUserTask.class).start(task);
    }

    private User doLogin() {
        String serverUrl = "org0.dev39.qmth.com.cn";
        // String serverUrl = "ecs-test.ea100.com.cn";
        // String serverUrl = "exam.exam-cloud.cn";

        String loginName = "admin5";
        String password = "0";
        String smsCode = "5220"; //选填

        User loginUser = commonService.login(serverUrl, loginName, password, smsCode);
        LoginSessionManager.addLoginSession(loginUser);
        return loginUser;
    }

}
