package com.mindskip.xzs.controller.admin; import com.mindskip.xzs.base.BaseApiController; import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.domain.ExamPaper; import com.mindskip.xzs.domain.ExamPaperSubject; import com.mindskip.xzs.service.ExamPaperDepartmentService; import com.mindskip.xzs.service.ExamPaperService; import com.mindskip.xzs.service.ExamPaperSubjectService; import com.mindskip.xzs.utility.DateTimeUtil; import com.mindskip.xzs.utility.PageInfoHelper; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamResponseVM; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @RestController("AdminExamPaperController") @RequestMapping(value = "/api/admin/exam/paper") public class ExamPaperController extends BaseApiController { private final ExamPaperService examPaperService; private final ExamPaperSubjectService examPaperSubjectService; private final ExamPaperDepartmentService examPaperDepartmentService; @Autowired public ExamPaperController(ExamPaperService examPaperService, ExamPaperSubjectService examPaperSubjectService, ExamPaperDepartmentService examPaperDepartmentService) { this.examPaperService = examPaperService; this.examPaperSubjectService = examPaperSubjectService; this.examPaperDepartmentService = examPaperDepartmentService; } @RequestMapping(value = "/page", method = RequestMethod.POST) public RestResponse> pageList(@RequestBody ExamPaperPageRequestVM model) { PageInfo pageInfo = examPaperService.page(model); PageInfo page = PageInfoHelper.copyMap(pageInfo, e -> { ExamResponseVM vm = modelMapper.map(e, ExamResponseVM.class); vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); vm.setSubjectId(examPaperSubjectService.getByExamPaperId(vm.getId()) .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new)); return vm; }); return RestResponse.ok(page); } @RequestMapping(value = "/taskExamPage", method = RequestMethod.POST) public RestResponse> taskExamPageList(@RequestBody ExamPaperPageRequestVM model) { PageInfo pageInfo = examPaperService.taskExamPage(model); PageInfo page = PageInfoHelper.copyMap(pageInfo, e -> { ExamResponseVM vm = modelMapper.map(e, ExamResponseVM.class); vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); return vm; }); return RestResponse.ok(page); } @RequestMapping(value = "/edit", method = RequestMethod.POST) public RestResponse edit(@RequestBody @Valid ExamPaperEditRequestVM model) { ExamPaper examPaper = examPaperService.savePaperFromVM(model, getCurrentUser()); ExamPaperEditRequestVM newVM = examPaperService.examPaperToVM(examPaper.getId()); return RestResponse.ok(newVM); } @RequestMapping(value = "/select/{id}", method = RequestMethod.POST) public RestResponse select(@PathVariable Integer id) { ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id); return RestResponse.ok(vm); } @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) public RestResponse delete(@PathVariable Integer id) { ExamPaper examPaper = examPaperService.selectById(id); examPaper.setDeleted(true); examPaperService.updateByIdFilter(examPaper); examPaperDepartmentService.removeByExamPaperId(id); examPaperSubjectService.removeByExamPaperId(id); return RestResponse.ok(); } }