package com.mindskip.xzs.controller.admin; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.mindskip.xzs.base.BaseApiController; import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.domain.ExamPaperAnswer; import com.mindskip.xzs.domain.ExamTemplates; import com.mindskip.xzs.domain.ExamTemplatesUserCount; import com.mindskip.xzs.domain.User; import com.mindskip.xzs.domain.vo.AnswerVO; import com.mindskip.xzs.domain.vo.ExamTemplatesUserCountVO; import com.mindskip.xzs.domain.vo.UserCountExcelVO; import com.mindskip.xzs.repository.ExamTemplatesMapper; import com.mindskip.xzs.service.ExamPaperAnswerService; import com.mindskip.xzs.service.ExamTemplatesUserCountService; import com.mindskip.xzs.service.UserService; import com.mindskip.xzs.utility.convert.UserCountExcelClassConvert; import com.mindskip.xzs.utility.excel.ExcelSheet; import com.mindskip.xzs.utility.excel.ExcelUtils; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM; import lombok.SneakyThrows; import org.springframework.web.bind.annotation.*; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @RestController("AdminExamTemplatesUserCountController") @RequestMapping(value = "/api/admin/exam/count") public class ExamTemplatesUserCountController extends BaseApiController { private final ExamTemplatesUserCountService examTemplatesUserCountService; private final ExamTemplatesMapper examTemplatesMapper; private final UserService userService; private final ExamPaperAnswerService examPaperAnswerService; public ExamTemplatesUserCountController(ExamTemplatesUserCountService examTemplatesUserCountService, ExamTemplatesMapper examTemplatesMapper, UserService userService, ExamPaperAnswerService examPaperAnswerService) { this.examTemplatesUserCountService = examTemplatesUserCountService; this.examTemplatesMapper = examTemplatesMapper; this.userService = userService; this.examPaperAnswerService = examPaperAnswerService; } @RequestMapping(value = "/list", method = RequestMethod.POST) public RestResponse> select(@RequestBody ExamPaperPageRequestVM model) throws Exception { if (model.getTemplatesName() != null && !"".equals(model.getTemplatesName())) { ExamTemplates examTemplates = examTemplatesMapper.getByName(model.getTemplatesName()); model.setTemplatesId(examTemplates != null ? examTemplates.getId() : 0); } if (model.getUserName() != null && !"".equals(model.getUserName())) { User user = userService.getUserByRealName(model.getUserName()); model.setUserId(user != null ? user.getId() : 0); } PageInfo info = examTemplatesUserCountService.list(model); info.setList(info.getList().stream().map(e -> { e.setName(examTemplatesMapper.getById(e.getId()).getName()); e.setUserName(userService.getUserById(e.getUserId()).getRealName()); return e; }).collect(Collectors.toList())); return RestResponse.ok(info); } @RequestMapping(value = "/sourceList", method = RequestMethod.POST) public RestResponse> selectSource(@RequestBody ExamTemplatesUserCountVO examTemplatesUserCountVO) throws Exception { List pageInfo = examTemplatesUserCountService.getByUserIdAndTemplatesId(examTemplatesUserCountVO); PageInfo idDesc = PageHelper.startPage(examTemplatesUserCountVO.getPageIndex(), examTemplatesUserCountVO.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperAnswerService.getByExamPaperIdAndUserId(pageInfo)); return RestResponse.ok(idDesc); } @RequestMapping(value = "/exportTemplatesSheet/{id}", method = RequestMethod.GET) public void exportTemplatesSheet(@PathVariable("id") Integer id, HttpServletResponse response, HttpServletRequest request){ ExamPaperPageRequestVM vm = new ExamPaperPageRequestVM(); vm.setTemplatesId(id); List list = examTemplatesUserCountService.userCountList(vm) .stream().map(e -> { e.setName(examTemplatesMapper.getById(e.getId()).getName()); e.setUserName(userService.getUserById(e.getUserId()).getRealName()); return e; }).collect(Collectors.toList()); String[] sheetHead1 = {"名称","用户名","数量"}; String[] sheetHead2 = {"总分数","得分","时间(秒)"}; List userCountExcelVOS = UserCountExcelClassConvert.INSTANCE.ExamTemplatesUserCountVOListToUserCountExcelVOList(list); List byTemplates = examTemplatesUserCountService.getByTemplates(id); List answerList = examPaperAnswerService.getByExamPaperIdAndUserId(byTemplates); List answerVOS = UserCountExcelClassConvert.INSTANCE.ExamPaperAnswerListToAnswerVOList(answerList); List listEntities = ExcelUtils.listEntitiesToStringArray(userCountExcelVOS); List strings = ExcelUtils.answerToString(answerVOS); ExcelSheet excelSheet = new ExcelSheet("次数统计",sheetHead1,listEntities); ExcelSheet excelSheet1 = new ExcelSheet("详细分数",sheetHead2,strings); List excelSheets = new ArrayList<>(); excelSheets.add(excelSheet); excelSheets.add(excelSheet1); ExcelUtils.exportManySheetExcel("1",excelSheets,response,request); } @RequestMapping(value = "/exportTemplatesId/{id}", method = RequestMethod.GET) @SneakyThrows public void exportTemplatesId(@PathVariable("id") Integer id, HttpServletResponse response, HttpServletRequest request) { List list = examTemplatesUserCountService.getByExamTemplates(id); ExcelUtils.export(response,"个人练习",list,UserCountExcelVO.class); } }