package com.mindskip.xzs.controller.admin; import com.mindskip.xzs.base.BaseApiController; import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.domain.ExamPaperSubject; import com.mindskip.xzs.domain.Subject; import com.mindskip.xzs.service.*; import com.mindskip.xzs.utility.PageInfoHelper; import com.mindskip.xzs.viewmodel.admin.education.SubjectEditRequestVM; import com.mindskip.xzs.viewmodel.admin.education.SubjectPageRequestVM; import com.mindskip.xzs.viewmodel.admin.education.SubjectResponseVM; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @RestController("AdminEducationController") @RequestMapping(value = "/api/admin/education") public class EducationController extends BaseApiController { private final SubjectService subjectService; private final QuestionSubjectService questionSubjectService; private final ExamPaperSubjectService examPaperSubjectService; private final ExamPaperDepartmentService examPaperDepartmentService; private final ExamPaperService examPaperService; @Autowired public EducationController(SubjectService subjectService, QuestionSubjectService questionSubjectService, ExamPaperSubjectService examPaperSubjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperService examPaperService) { this.subjectService = subjectService; this.questionSubjectService = questionSubjectService; this.examPaperSubjectService = examPaperSubjectService; this.examPaperDepartmentService = examPaperDepartmentService; this.examPaperService = examPaperService; } @RequestMapping(value = "/subject/list", method = RequestMethod.POST) public RestResponse> list() { List subjects = subjectService.allSubject(); return RestResponse.ok(subjects); } @RequestMapping(value = "/subject/page", method = RequestMethod.POST) public RestResponse> pageList(@RequestBody SubjectPageRequestVM model) { PageInfo pageInfo = subjectService.page(model); PageInfo page = PageInfoHelper.copyMap(pageInfo, e -> modelMapper.map(e, SubjectResponseVM.class)); return RestResponse.ok(page); } @RequestMapping(value = "/subject/edit", method = RequestMethod.POST) public RestResponse edit(@RequestBody @Valid SubjectEditRequestVM model) { Subject subject = modelMapper.map(model, Subject.class); if (model.getId() == null) { subject.setDeleted(false); subjectService.insertByFilter(subject); } else { subjectService.updateByIdFilter(subject); } return RestResponse.ok(); } @RequestMapping(value = "/subject/select/{id}", method = RequestMethod.POST) public RestResponse select(@PathVariable Integer id) { Subject subject = subjectService.selectById(id); SubjectEditRequestVM vm = modelMapper.map(subject, SubjectEditRequestVM.class); return RestResponse.ok(vm); } @RequestMapping(value = "/subject/delete/{id}", method = RequestMethod.POST) public RestResponse delete(@PathVariable Integer id) { Subject subject = subjectService.selectById(id); subject.setDeleted(true); subjectService.updateByIdFilter(subject); questionSubjectService.removeSubjectId(id); Integer[] ids = examPaperSubjectService.getBySubjectId(id) .stream().map(ExamPaperSubject::getExamPaperId).toArray(Integer[]::new); examPaperService.removeByIds(ids); examPaperDepartmentService.removeByExamPaperIds(ids); examPaperSubjectService.removeBySubjectId(id); return RestResponse.ok(); } }