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_audit.ExamRecordDataAuditTask;
import cn.com.qmth.examcloud.tool.service.export_student_photo.ExportStudentPhotoTask;
import cn.com.qmth.examcloud.tool.service.query_paper_struct.QueryPaperStructTask;
import cn.com.qmth.examcloud.tool.service.reset_student_password.ResetStudentPasswordTask;
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 CommonService commonService;

    @Autowired
    private SysProperty sysProperty;

    @Autowired
    private ResetStudentPasswordTask resetStudentPasswordTask;

    @Autowired
    private ExportStudentPhotoTask exportStudentPhotoTask;

    @Autowired
    private QueryPaperStructTask queryPaperStructTask;

    @Autowired
    private ExamRecordDataAuditTask examRecordDataAuditTask;

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

        // this.testTask(user);
        // resetStudentPasswordTask.start(user, 0L);
        // queryPaperStructTask.start(user, 0L);
        // exportStudentPhotoTask.start(user);
        // examRecordDataAuditTask.start(user);
    }

    private void testTask(User user) {
        Map<String, Object> params = new HashMap<>();
        params.put("examId", 84);
        params.put("dataFilePath", "C:/Users/deason/Desktop/批量导入考生模板.xlsx");
        // params.put("dataFilePath", "C:/Users/deason/Desktop/批量创建用户模板.xlsx");

        TaskEntity task = new TaskEntity();
        task.setUserToken(user.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 user = commonService.login(serverUrl, loginName, password, smsCode);
        LoginSessionManager.addLoginSession(user);
        return user;
    }

}
