/*
 * *************************************************
 * Copyright (c) 2019 QMTH. All Rights Reserved.
 * Created by Deason on 2019-03-07 13:53:38.
 * *************************************************
 */

package cn.com.qmth.examcloud.core.questions.api.controller;

import cn.com.qmth.examcloud.core.questions.base.enums.ExamFileType;
import cn.com.qmth.examcloud.core.questions.base.enums.ExportWay;
import cn.com.qmth.examcloud.core.questions.dao.entity.Course;
import cn.com.qmth.examcloud.core.questions.service.ExportPaperService;
import cn.com.qmth.examcloud.core.questions.service.ExtractConfigFileService;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.ExportPaperInfoModel;
import cn.com.qmth.examcloud.core.questions.service.temp.CqdxService;
import cn.com.qmth.examcloud.core.questions.service.temp.DdExcelService;
import cn.com.qmth.examcloud.web.support.Naked;
import cn.com.qmth.examcloud.web.support.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("${api_cqb}/")
public class IndexController {
    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Naked
    @ResponseBody
    @RequestMapping(value = "/paper/init", method = RequestMethod.GET)
    public String init(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /* 重庆大学xml转paper对象入库 */
        CqdxService cqdxService = SpringContextHolder.getBean(CqdxService.class);
        //cqdxService.bulidPaper();

        DdExcelService ddExcelService = SpringContextHolder.getBean(DdExcelService.class);
        //ddExcelService.exportPaperStructExcel("1407");
        //ddExcelService.exporExcel("0");

        //exportExtractExcel(response);
        //exportQuestionExcel();
        //exportQuestionStatistic(response);

        return "ok";
    }

    private void exportExtractExcel(HttpServletResponse response) {
        //用来导出调卷规则中，生成的主观题和客观题的Excel(导出调卷规则)
        ExtractConfigFileService service = SpringContextHolder.getBean(ExtractConfigFileService.class);
        ExportPaperInfoModel model = new ExportPaperInfoModel();
        model.setExamId("606");//for export online
        model.setExportWay(ExportWay.BATCH);
        List<String> list = new ArrayList<>();
        list.add(ExamFileType.PAPER_STRUCTURE_OBJECTIVE.name());
        model.setExportContentList(list);
        try {
            service.exportExamPaperInfo(model, response, "QMTH", null, null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private void exportQuestionExcel() {
        //用来导出给定课程关联中的主观题信息的Excel(导出课程下的主观题试题分布)
        DdExcelService ddExcelService = SpringContextHolder.getBean(DdExcelService.class);
        List<String> codes = new ArrayList<>();
//        codes.add("D02020001");
//        codes.add("D07020005");
//        codes.add("D07020022");
//        codes.add("D08020006");
//        codes.add("D08020008");
//        codes.add("D08020009");
//        codes.add("D08020013");

        codes.add("D17020004");
        codes.add("D17020003");
        codes.add("F06020036");
        codes.add("D16020003");
        codes.add("F46020001");
        codes.add("F32020009");
        codes.add("D17020007");
        codes.add("D15020003");
        codes.add("D15020002");
        codes.add("D12020003");
        codes.add("D11020015");
        codes.add("D08020016");
        codes.add("D06020024");
        codes.add("D06020009");
        codes.add("D05020025");
        codes.add("D05020020");
        codes.add("D05020001");

        try {
            ddExcelService.exportSubQues("1627", 481L, codes);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private void exportQuestionStatistic(HttpServletResponse response) throws Exception {
        ExportPaperService exportPaperService = SpringContextHolder.getBean(ExportPaperService.class);

        List<Course> courses = new ArrayList<>();
        courses.add(new Course("课程0627", "course_0627"));
        courses.add(new Course("课程0628", "course_0628"));

//        courses.add(new Course("仓储与配送管理", "183007"));
//        courses.add(new Course("操作系统原理", "021008"));
//        courses.add(new Course("弹性理论", "051020"));
//        courses.add(new Course("电子商务信息安全", "193004"));
//        courses.add(new Course("供应链管理", "183010"));
//        courses.add(new Course("国际经济法学", "071014"));
//        courses.add(new Course("行政事业管理", "212003"));
//        courses.add(new Course("汇编语言", "021009"));
//        courses.add(new Course("基于INTERNET的管理信息系统", "163001"));
//        courses.add(new Course("计算机软件技术基础(1)", "041020"));
//        courses.add(new Course("建设工程计量", "282002"));
//        courses.add(new Course("软件工程", "021014"));
//        courses.add(new Course("商品学概论", "183002"));
//        courses.add(new Course("信息经济与信息管理", "011009"));
//        courses.add(new Course("中国法制史学", "071008"));
//        courses.add(new Course("财务会计", "061009"));
//        courses.add(new Course("成本会计", "061007"));
//        courses.add(new Course("大学语文", "001030"));
//        courses.add(new Course("法理学", "071002"));
//        courses.add(new Course("房地产开发与经营", "092005"));
//        courses.add(new Course("房屋建筑学(工业厂房)", "051018"));
//        courses.add(new Course("房屋建筑学(民用建筑)", "051017"));
//        courses.add(new Course("分析化学", "532004"));
//        courses.add(new Course("工程地质学", "051003"));
//        courses.add(new Course("工程概预算", "051015"));
//        courses.add(new Course("工程力学", "423002"));
//        courses.add(new Course("工程造价案例分析", "282003"));

        //exportPaperService.downQuestionDistribute(courses.get(0).getCode(), response);
        exportPaperService.downQuestionDistributes(courses);
    }

}