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

import cn.com.qmth.examcloud.api.commons.enums.AdminOperateType;
import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
import cn.com.qmth.examcloud.core.questions.base.enums.RandomGenPaperPolicy;
import cn.com.qmth.examcloud.core.questions.service.PaperService;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.GenPaperDto;
import cn.com.qmth.examcloud.core.questions.service.impl.GenPaperService;
import cn.com.qmth.examcloud.reports.commons.bean.AdminOperateReport;
import cn.com.qmth.examcloud.reports.commons.util.ReportsUtil;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * Created by weiwenhai on 16/12/28.
 * update by wwh on 2018/5/21
 *
 * @describle 重写简易组卷逻辑
 */
@RestController
@RequestMapping("${api_cqb}/")
public class GenPaperController extends ControllerSupport {

    @Autowired
    GenPaperService genPaperService;

    @Autowired
    PaperService paperService;

    @ApiOperation(value = "简易随机组卷", notes = "简易随机组卷")
    @PostMapping("/genPaper/simple")
    public ResponseEntity<Object> genPaperSimple(@RequestBody GenPaperDto genPaperDto) {
        User user = getAccessUser();
        genPaperDto.setOrgId(user.getRootOrgId().toString());
        genPaperDto.setCreator(user.getDisplayName());
        Map<String, Object> paperMap = new HashMap<>();
        String paperName = genPaperDto.getPaperName();
        try {
            //判断试卷名称是否一样
            boolean result = false;
            if (genPaperDto.getGenNumber() > 1) {
                for (int i = 1; i <= genPaperDto.getGenNumber(); i++) {
                    genPaperDto.setPaperName(paperName + "_" + i);
                    result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
                    if (!result) {
                        paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                        return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                }
            }
            result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
            if (!result) {
                paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
            }
            //如果是组一套试卷
            if (genPaperDto.getGenNumber() == 1) {
                //判断抽题类型  按数量  按分数
                if (genPaperDto.getSimpleGenPaperPolicy() == RandomGenPaperPolicy.BY_QUESTIONNUM) {
                    //按题目数量组卷
                    paperMap = genPaperService.genPaperByQuestionNum(genPaperDto, user);
                } else {
                    // 按分数 组卷
                    paperMap = genPaperService.genPaperByScore(genPaperDto, user);
                }
                if (paperMap.get("msg").equals("success")) {
	                StringBuilder paperInfo=new StringBuilder();
	                paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
	                ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE25.getDesc(),paperInfo.toString()));
                }
            } else {
                //如果是组多套试卷
                for (int i = 1; i <= genPaperDto.getGenNumber(); i++) {
                    genPaperDto.setPaperName(paperName + "_" + i);
                    //判断抽题类型  按数量  按分数
                    if (genPaperDto.getSimpleGenPaperPolicy() == RandomGenPaperPolicy.BY_QUESTIONNUM) {
                        paperMap.putAll(genPaperService.genPaperByQuestionNum(genPaperDto, user));
                    } else {
                        paperMap.putAll(genPaperService.genPaperByScore(genPaperDto, user));
                    }
                    if (!paperMap.get("msg").equals("success")) {
                        break;
                    }
                    StringBuilder paperInfo=new StringBuilder();
                    paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
                    ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE25.getDesc(),paperInfo.toString()));
                }
            }
            if (!paperMap.get("msg").equals("success")) {
                return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>(paperMap, HttpStatus.OK);
    }

    @ApiOperation(value = "精细组卷", notes = "精细组卷")
    @PostMapping("/genPaper/normal")
    public ResponseEntity<Object> genPaperNormal(@RequestBody GenPaperDto genPaperDto) {
        User user = getAccessUser();
        genPaperDto.setOrgId(user.getRootOrgId().toString());
        genPaperDto.setCreator(user.getDisplayName());
        Map<String, Object> paperMap = new HashMap<>();
        String paperName = genPaperDto.getPaperName();
        //判断试卷名称是否一样
        try {
            //判断试卷名称是否一样
            boolean result = false;
            if (genPaperDto.getGenNumber() > 1) {
                for (int i = 1; i <= genPaperDto.getGenNumber(); i++) {
                    genPaperDto.setPaperName(paperName + "_" + i);
                    result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
                    if (!result) {
                        paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                        return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                }
            }
            result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
            if (!result) {
                paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if (genPaperDto.getGenNumber() > 1) {
            for (int i = 1; i <= genPaperDto.getGenNumber(); i++) {
                genPaperDto.setPaperName(paperName + "_" + i);
                paperMap.putAll(genPaperService.genPaper(genPaperDto));
                if (!paperMap.get("msg").equals("success")) {
                    break;
                }
                StringBuilder paperInfo=new StringBuilder();
                paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
                ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE26.getDesc(),paperInfo.toString()));
            }
        } else if (genPaperDto.getGenNumber() == 1) {
            paperMap = genPaperService.genPaper(genPaperDto);
            if (paperMap.get("msg").equals("success")) {
	            StringBuilder paperInfo=new StringBuilder();
	            paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
	            ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE26.getDesc(),paperInfo.toString()));
            }
        }
        if (paperMap.get("msg").equals("success")) {
            return new ResponseEntity<>(paperMap, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "蓝图组卷", notes = "蓝图组卷")
    @PostMapping("/genPaper/blue")
    public ResponseEntity<Object> genPaperBlue(@RequestBody GenPaperDto genPaperDto) {
        User user = getAccessUser();
        genPaperDto.setOrgId(user.getRootOrgId().toString());
        genPaperDto.setCreator(user.getDisplayName());
        Map<String, Object> paperMap = new HashMap<>();
        String paperName = genPaperDto.getPaperName();
        //判断试卷名称是否一样
        try {
            //判断试卷名称是否一样
            boolean result = false;
            if (genPaperDto.getGenNumber() > 1) {
                for (int i = 1; i <= genPaperDto.getGenNumber(); i++) {
                    genPaperDto.setPaperName(paperName + "_" + i);
                    result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
                    if (!result) {
                        paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                        return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                }
            }
            result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
            if (!result) {
                paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if (genPaperDto.getGenNumber() > 1) {
            for (int i = 1; i <= genPaperDto.getGenNumber(); i++) {
                genPaperDto.setPaperName(paperName + "_" + i);
                paperMap.putAll(genPaperService.genBluePaper(genPaperDto));
                if (!paperMap.get("msg").equals("success")) {
                    break;
                }
	            StringBuilder paperInfo=new StringBuilder();
	            paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
	            ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE27.getDesc(),paperInfo.toString()));
            }
        } else if (genPaperDto.getGenNumber() == 1) {
            paperMap = genPaperService.genBluePaper(genPaperDto);
            if (paperMap.get("msg").equals("success")) {
	            StringBuilder paperInfo=new StringBuilder();
	            paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
	            ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE27.getDesc(),paperInfo.toString()));
            }
        }
        if (paperMap.get("msg").equals("success")) {
            return new ResponseEntity<>(paperMap, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    @ApiOperation(value = "手动组卷", notes = "手动组卷")
    @PostMapping("/genPaper/manual")
    public ResponseEntity<Object> genPaperManual(@RequestBody GenPaperDto genPaperDto) {
        User user = getAccessUser();
        genPaperDto.setOrgId(user.getRootOrgId().toString());
        genPaperDto.setCreator(user.getDisplayName());
        Map<String, Object> paperMap = new HashMap<>();
        //判断试卷名称是否一样
        try {
            //判断试卷名称是否一样
            boolean result = false;
            result = paperService.checkPaperName(genPaperDto.getPaperName(), PaperType.GENERATE, user.getRootOrgId() + "");
            if (!result) {
                paperMap.put("msg", "考试试卷:" + genPaperDto.getPaperName() + "已经存在");
                return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        paperMap = genPaperService.genPaperManual(genPaperDto);
        if (paperMap.get("msg").equals("success")) {
            StringBuilder paperInfo=new StringBuilder();
            paperInfo.append("课程:"+genPaperDto.getCourseName()+"("+genPaperDto.getCourseNo()+") 试卷名称:"+genPaperDto.getPaperName());
            ReportsUtil.report(new AdminOperateReport(user.getRootOrgId(), user.getUserId(), AdminOperateType.TYPE28.getDesc(),paperInfo.toString()));
        }
        if (paperMap.get("msg").equals("success")) {
            return new ResponseEntity<>(paperMap, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(paperMap, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
