QuestionController.java 13.4 KB
package com.skua.modules.edu.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.context.BaseContextHandler;
import com.skua.core.query.QueryGenerator;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.edu.entity.Question;
import com.skua.modules.edu.entity.QuestionDB;
import com.skua.modules.edu.service.IQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skua.modules.edu.vo.QuestionExcel;
import com.skua.modules.erp.entity.MaterialIN;
import com.skua.modules.erp.vo.MaterialINVO;
import com.skua.modules.system.service.ISysDepartService;
import com.skua.tool.util.JSUtils;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <pre>
 * 在线考试-试题
 * </pre>
 * @author 开发者姓名
 * @version V0.1, 开发时间
 */
@Slf4j
@Api(tags="在线考试-试题")
@RestController("webQuestionController")
@RequestMapping("/web/edu/question")
public class QuestionController {
	@Autowired
	private IQuestionService questionService;

	@AutoLog(value = "在线考试-试题-分页列表查询")
	@ApiOperation(value="在线考试-试题-分页列表查询", notes="在线考试-试题-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Question>> queryPageList(Question question,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<Question>> result = new Result<IPage<Question>>();
		QueryWrapper<Question> queryWrapper = QueryGenerator.initQueryWrapper(question, req.getParameterMap());
		Page<Question> page = new Page<Question>(pageNo, pageSize);
		queryWrapper.orderByDesc("create_time");

		IPage<Question> pageList = questionService.page(page, queryWrapper);
		//IPage<Question> pageList = questionService.queryPageList(page, question);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	@AutoLog(value = "在线考试-试题-审批列表")
	@ApiOperation(value="在线考试-试题-审批列表", notes="在线考试-试题-审批列表")
	@GetMapping(value = "/handleList")
	public Result<IPage<Question>> handleList(Question question,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req) {
		Result<IPage<Question>> result = new Result<IPage<Question>>();
		QueryWrapper<Question> queryWrapper = QueryGenerator.initQueryWrapper(question, req.getParameterMap());
		Page<Question> page = new Page<Question>(pageNo, pageSize);
		//queryWrapper.gt("audit_status","0");//>0
		//queryWrapper.lt("audit_status","3");// < 3
		if(StringUtils.isEmpty(question.getAuditStatus())){
			queryWrapper.eq("audit_status","1");
		}
		queryWrapper.orderByDesc("create_time");

		IPage<Question> pageList = questionService.page(page, queryWrapper);
		//IPage<Question> pageList = questionService.queryPageList(page, question);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	@AutoLog(value = "在线考试-题库-题库下试题列表")
	@ApiOperation(value="在线考试-题库下试题列表", notes="在线考试-题库下试题列表")
	@GetMapping(value = "/questionList/{dbId}")
	public Result<List<Question>> queryPageList(@PathVariable("dbId") String dbId) {
		Result<List<Question>> result = new Result<List<Question>>();
		//QueryWrapper<QuestionDB> queryWrapper = QueryGenerator.initQueryWrapper(questionDB, req.getParameterMap());
		//List<QuestionDB> questionDBList = questionDBService.list(queryWrapper);
		/*QueryWrapper<Question> queryWrapper = new QueryWrapper();
		queryWrapper.eq("question_db_id",dbId);
		queryWrapper.orderByDesc("create_time");*/
		Question question = new Question();
		question.setQuestionDbId(dbId);
		List<Question> questionDBList = questionService.selectQuestionList(question ) ;

		result.setSuccess(true);
		result.setResult(questionDBList);
		return result;
	}

	@AutoLog(value = "在线考试-试题-添加")
	@ApiOperation(value="在线考试-试题-添加", notes="在线考试-试题-添加")
	@PostMapping(value = "/add")
	public Result<Question> add(@RequestBody Question question)throws Exception {
		Result<Question> result = new Result<Question>();
		try {
			question.setCreateBy(BaseContextHandler.getUserId());
			question.setCreateDept(BaseContextHandler.getRealDepartId());
			questionService.saveQuestion(question);
			result.success("添加成功!");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	@AutoLog(value = "在线考试-试题-编辑")
	@ApiOperation(value="在线考试-试题-编辑", notes="在线考试-试题-编辑")
	@PutMapping(value = "/edit")
	public Result<Question> edit(@RequestBody Question question) throws Exception {
		Result<Question> result = new Result<Question>();
		Question questionEntity = questionService.getById(question.getId());
		if(questionEntity==null) {
			result.error500("未找到对应实体");
		}else {
			question.setUpdateBy(BaseContextHandler.getUserId());
			boolean ok = questionService.updateQuestion(question);
			//TODO 返回false说明什么?
			if(ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}

	@AutoLog(value = "在线考试-试题-通过id删除")
	@ApiOperation(value="在线考试-试题-通过id删除", notes="在线考试-试题-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			boolean  isUse = questionService.checkByPaper(id);
			if(isUse){//true被使用
				return Result.error("删除失败!试题被试卷使用");
			}else{
				questionService.removeById(id);
			}

		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}

	@AutoLog(value = "在线考试-试题-批量删除")
	@ApiOperation(value="在线考试-试题-批量删除", notes="在线考试-试题-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<Question> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<Question> result = new Result<Question>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别!");
		}else {
			this.questionService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}

	@AutoLog(value = "在线考试-试题-送审")
	@ApiOperation(value="在线考试-试题-送审", notes="在线考试-试题-送审")
	@GetMapping(value = "/sendAudit")
	public Result<Question> sendAudit(@RequestParam(name="id",required=true) String id) {
		Result<Question> result = new Result<Question>();
		Question  question = null;
		try {
			question  = questionService.getById(id);
			if(question == null) {
				result.error500("未找到对应实体");
			}else{
				question.setAuditStatus("1");//提交审核
				//question.setApplyUser(BaseContextHandler.getUserId());
				questionService.updateById(question);
			}
		} catch (Exception e) {
			log.error("送审失败",e.getMessage());
			result.setResult(question);
			result.setSuccess(true);
		}
		return result;
	}

	@AutoLog(value = "在线考试-试题-批量送审")
	@ApiOperation(value="在线考试-试题-批量送审", notes="在线考试-试题-批量送审")
	@DeleteMapping(value = "/batchSendAudit")
	public Result<Question> batchSendAudit(@RequestParam(name="ids",required=true) String ids) {
		Result<Question> result = new Result<Question>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别!");
		}else {
			String auditStatus = "1";
			this.questionService.batchSendAudit(JSUtils.quoteEach(ids,","),auditStatus);
			result.success("删除成功!");
		}
		return result;
	}

	@AutoLog(value = "在线考试-试题-审批")
	@ApiOperation(value="在线考试-试题-审批", notes="在线考试-试题-审批")
	@PutMapping(value = "/audit")
	public Result<Question> auditQuestion(@RequestBody Question question) {
		Result<Question> result = new Result<Question>();
		try {
			questionService.auditQuestion(question);
			result.setResult(question);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error("审批失败",e.getMessage());

			result.error500("审批失败:"+e.getMessage());
		}
		return result;
	}

	@AutoLog(value = "在线考试-试题-批量审批")
	@ApiOperation(value="在线考试-试题-批量审批", notes="在线考试-试题-批量审批")
	@GetMapping(value = "/auditBatch")
	public Result<String> auditBatchMaterialINVO(@RequestParam(name="ids",required=true) String ids,String approveState,String approveMessage) {
		Result<String> result = new Result<String>();
		String errMsg = null;
		try {
			errMsg = questionService.auditBatchQuestion(ids,approveState,approveMessage);
			//result.setResult(materialINVO);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error("审批失败",errMsg);
			result.error500("审批失败:"+ errMsg);
		}
		return result;
	}


	@AutoLog(value = "在线考试-试题-通过id查询")
	@ApiOperation(value="在线考试-试题-通过id查询", notes="在线考试-试题-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Question> queryById(@RequestParam(name="id",required=true) String id) {
		Result<Question> result = new Result<Question>();
		Question question = questionService.queryById(id);
		if(question==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(question);
			result.setSuccess(true);
		}
		return result;
	}




  @RequestMapping(value = "/exportXlsTemplate")
  public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
      // Step.1 组装查询条件
      //Step.2 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      List<QuestionExcel> pageList = new ArrayList<>();
	  pageList.add( new QuestionExcel("单选", "车辆在较窄的山路上行驶时,如果靠山体的一方不让行,应怎样做?", "鸣喇叭催其让行", "保持正常车速行驶", "提前减速或停车避让", "向左占道,谨慎驶过", "A", ""));
	  pageList.add( new QuestionExcel("多选", "交通信号灯由( )组成。", "鸣喇叭催其让行", "红灯", "绿灯", "黄灯", "蓝", "ABC"));
      //导出文件名称
      mv.addObject(NormalExcelConstants.FILE_NAME, "选择题_批量导入模板");
      mv.addObject(NormalExcelConstants.CLASS, QuestionExcel.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("选择题_批量导入模板", "导出人", ""));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
  }

   /**
	 * <pre>
	 * 	 通过excel导入数据
	 * </pre>
	 * @param request
	 * @param response
	 * @return
	 * @author 开发者姓名, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
          	String dbId = request.getParameter("dbId");
              List<QuestionExcel> listQuestions = ExcelImportUtil.importExcel(file.getInputStream(), QuestionExcel.class, params);
              questionService.batchImportExcel(listQuestions,dbId);
              return Result.ok("文件导入成功!数据行数:" + listQuestions.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败!");
  }

}