From d69a9a79a82c8a2f93efcd2b43b0a03ca57a031c Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期一, 08 七月 2024 17:37:19 +0800 Subject: [PATCH] 课目重构 --- src/main/java/com/mindskip/xzs/domain/form/EditSubjectForm.java | 30 +++++ src/main/java/com/mindskip/xzs/domain/vo/DeptSubjectCascaderVO.java | 46 +++++++ src/main/java/com/mindskip/xzs/service/SubjectService.java | 16 ++ src/main/java/com/mindskip/xzs/controller/admin/EducationController.java | 114 +++++++++++++----- src/main/java/com/mindskip/xzs/domain/form/AddSubjectForm.java | 29 ++++ src/main/java/com/mindskip/xzs/repository/SubjectDeptMapper.java | 16 ++ src/main/resources/mapper/SubjectDeptMapper.xml | 13 ++ src/main/java/com/mindskip/xzs/service/impl/SubjectServiceImpl.java | 40 ++++++ 8 files changed, 270 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/mindskip/xzs/controller/admin/EducationController.java b/src/main/java/com/mindskip/xzs/controller/admin/EducationController.java index d7ab8b0..18bae61 100644 --- a/src/main/java/com/mindskip/xzs/controller/admin/EducationController.java +++ b/src/main/java/com/mindskip/xzs/controller/admin/EducationController.java @@ -6,7 +6,12 @@ import com.mindskip.xzs.domain.ExamPaperSubject; import com.mindskip.xzs.domain.Subject; import com.mindskip.xzs.domain.SubjectDept; +import com.mindskip.xzs.domain.form.AddSubjectForm; +import com.mindskip.xzs.domain.form.EditSubjectForm; +import com.mindskip.xzs.domain.vo.CascaderDataVO; +import com.mindskip.xzs.domain.vo.DeptSubjectCascaderVO; import com.mindskip.xzs.domain.vo.SubjectDeptVO; +import com.mindskip.xzs.repository.DepartmentMapper; import com.mindskip.xzs.repository.SubjectDeptMapper; import com.mindskip.xzs.service.*; import com.mindskip.xzs.utility.PageInfoHelper; @@ -14,14 +19,16 @@ import com.mindskip.xzs.viewmodel.admin.education.SubjectPageRequestVM; import com.mindskip.xzs.viewmodel.admin.education.SubjectResponseVM; import com.github.pagehelper.PageInfo; +import com.mindskip.xzs.vo.SubjectVO; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @RestController("AdminEducationController") @@ -35,12 +42,26 @@ private final ExamPaperSubjectService examPaperSubjectService; private final ExamPaperDepartmentService examPaperDepartmentService; private final ExamPaperService examPaperService; + private final DepartmentService departmentService; + private final DepartmentMapper departmentMapper; @RequestMapping(value = "/subject/list", method = RequestMethod.POST) public RestResponse<List<Subject>> list() { List<Subject> subjects = subjectService.allSubject(); return RestResponse.ok(subjects); + } + + @PostMapping("/subject/add") + public RestResponse<Object> add(@RequestBody @Validated AddSubjectForm form) { + subjectService.add(form); + return RestResponse.ok(); + } + + @PostMapping("/subject/edit") + public RestResponse<Object> edit(@RequestBody @Validated EditSubjectForm form) { + subjectService.edit(form); + return RestResponse.ok(); } @RequestMapping(value = "/subject/select/dept/{deptId}", method = RequestMethod.GET) @@ -50,43 +71,70 @@ } @RequestMapping(value = "/subject/page", method = RequestMethod.POST) - public RestResponse<PageInfo<SubjectResponseVM>> pageList(@RequestBody SubjectPageRequestVM model) { - PageInfo<Subject> pageInfo = subjectService.page(model); - PageInfo<SubjectResponseVM> page = PageInfoHelper.copyMap(pageInfo, item -> { - SubjectResponseVM vo = new SubjectResponseVM(); - BeanUtils.copyProperties(item, vo); - List<SubjectDeptVO> subjectDeptVOS = subjectDeptMapper.deptBySubjectId(item.getId()); - List<Integer> deptIds = subjectDeptVOS.stream().map(SubjectDeptVO::getDeptId).collect(Collectors.toList()); - String deptNames = subjectDeptVOS.stream().map(SubjectDeptVO::getDeptName).collect(Collectors.joining("銆�")); - vo.setDeptIds(deptIds); - vo.setDeptNames(deptNames); - return vo; - }); - return RestResponse.ok(page); + public RestResponse<Object> pageList(@RequestBody SubjectPageRequestVM model) { + List<CascaderDataVO> deptList = departmentMapper.list(); + List<DeptSubjectCascaderVO> data = deptList.stream().map(dept -> { + DeptSubjectCascaderVO deptCascaderVO = new DeptSubjectCascaderVO(); + deptCascaderVO.setId(dept.getValue()); + deptCascaderVO.setName(dept.getLabel()); + deptCascaderVO.setParentId(dept.getParentId()); + List<SubjectVO> subjectDeptVOS = subjectDeptMapper.getByDeptId(dept.getValue()); + List<Long> subjectIds = subjectDeptVOS.stream().map(SubjectVO::getId).collect(Collectors.toList()); + deptCascaderVO.setSubjectIds(subjectIds); + String subjectNames = subjectDeptVOS.stream().map(SubjectVO::getSubjectName).collect(Collectors.joining(",")); + deptCascaderVO.setSubjectNames(subjectNames); + return deptCascaderVO; + }).collect(Collectors.toList()); + // 鏋勫缓鏍� + return RestResponse.ok(this.buildTree(data)); } - @Transactional(rollbackFor = Exception.class) - @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); + public List<DeptSubjectCascaderVO> buildTree(List<DeptSubjectCascaderVO> departments) { + // 鐢ㄤ簬瀛樺偍宸茬粡鏀惧叆鏍戠殑鑺傜偣锛屼互value涓洪敭 + Map<Integer, DeptSubjectCascaderVO> map = new HashMap<>(); + List<DeptSubjectCascaderVO> rootNodes = new ArrayList<>(); + + // 绗竴姝ワ細灏嗘墍鏈夎妭鐐规斁鍏ap涓紝骞跺垵濮嬪寲children鍒楄〃 + for (DeptSubjectCascaderVO dept : departments) { + map.put(dept.getId(), dept); + dept.setChildren(new ArrayList<>()); } - // 澶勭悊閮ㄩ棬 - subjectDeptMapper.removeBySubjectId(subject.getId()); - List<SubjectDept> subjectDeptList = model.getDeptIds().stream().map(deptId -> { - SubjectDept subjectDept = new SubjectDept(); - subjectDept.setSubjectId(subject.getId()); - subjectDept.setDeptId(deptId); - return subjectDept; - }).collect(Collectors.toList()); - subjectDeptMapper.add(subjectDeptList); - return RestResponse.ok(); + // 绗簩姝ワ細鏋勫缓鏍戝舰缁撴瀯 + for (DeptSubjectCascaderVO dept : departments) { + if (dept.getParentId() == null) { // 鍋囪椤剁骇閮ㄩ棬鐨刾arentId涓簄ull鎴�0 + rootNodes.add(dept); + } else { + DeptSubjectCascaderVO parent = map.get(dept.getParentId()); + if (parent != null) { + parent.getChildren().add(dept); + } + } + } + return rootNodes; } +// @Transactional(rollbackFor = Exception.class) +// @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); +// } +// // 澶勭悊閮ㄩ棬 +// subjectDeptMapper.removeBySubjectId(subject.getId()); +// List<SubjectDept> subjectDeptList = model.getDeptIds().stream().map(deptId -> { +// SubjectDept subjectDept = new SubjectDept(); +// subjectDept.setSubjectId(subject.getId()); +// subjectDept.setDeptId(deptId); +// return subjectDept; +// }).collect(Collectors.toList()); +// subjectDeptMapper.add(subjectDeptList); +// return RestResponse.ok(); +// } + @RequestMapping(value = "/subject/select/{id}", method = RequestMethod.POST) public RestResponse<SubjectEditRequestVM> select(@PathVariable Integer id) { Subject subject = subjectService.selectById(id); diff --git a/src/main/java/com/mindskip/xzs/domain/form/AddSubjectForm.java b/src/main/java/com/mindskip/xzs/domain/form/AddSubjectForm.java new file mode 100644 index 0000000..f2d5f32 --- /dev/null +++ b/src/main/java/com/mindskip/xzs/domain/form/AddSubjectForm.java @@ -0,0 +1,29 @@ +package com.mindskip.xzs.domain.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @author锛歺p + * @date锛�2024/7/8 14:32 + */ +@Data +public class AddSubjectForm { + + /** + * 閮ㄩ棬ID + * + */ + @NotNull(message = "璇烽�夋嫨閮ㄩ棬") + private Integer deptId; + + /** + * 璇剧洰鍚嶇О + * + */ + @NotBlank(message = "璇疯緭鍏ヨ鐩悕绉�") + private String subjectName; + +} diff --git a/src/main/java/com/mindskip/xzs/domain/form/EditSubjectForm.java b/src/main/java/com/mindskip/xzs/domain/form/EditSubjectForm.java new file mode 100644 index 0000000..32b312c --- /dev/null +++ b/src/main/java/com/mindskip/xzs/domain/form/EditSubjectForm.java @@ -0,0 +1,30 @@ +package com.mindskip.xzs.domain.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.util.List; + +/** + * @author锛歺p + * @date锛�2024/7/8 14:32 + */ +@Data +public class EditSubjectForm { + + /** + * 閮ㄩ棬ID + * + */ + @NotNull(message = "璇烽�夋嫨閮ㄩ棬") + private Integer deptId; + + /** + * 璇剧洰ids + * + */ + private List<Integer> subjectIds; + +} diff --git a/src/main/java/com/mindskip/xzs/domain/vo/DeptSubjectCascaderVO.java b/src/main/java/com/mindskip/xzs/domain/vo/DeptSubjectCascaderVO.java new file mode 100644 index 0000000..794c976 --- /dev/null +++ b/src/main/java/com/mindskip/xzs/domain/vo/DeptSubjectCascaderVO.java @@ -0,0 +1,46 @@ +package com.mindskip.xzs.domain.vo; + +import com.mindskip.xzs.vo.SubjectVO; +import lombok.Data; + +import java.util.List; + +/** + * 绾ц仈閫夋嫨鍣ㄦ暟鎹� + * + * @author锛歺p + * @date锛�2024/7/7 20:50 + */ +@Data +public class DeptSubjectCascaderVO { + + /** + * 閫変腑鐨勫�硷紝id + */ + private Integer id; + + /** + * 灞曠ず鐨勫唴瀹� + */ + private String name; + + /** + * 閮ㄩ棬瀵瑰簲鐨勮鐩垪琛� + */ + private List<SubjectVO> subjectList; + + private List<Long> subjectIds; + + private String subjectNames; + + /** + * 鐖剁骇 + */ + private Integer parentId; + + /** + * 瀛愮骇鏁版嵁 + */ + private List<DeptSubjectCascaderVO> children; + +} diff --git a/src/main/java/com/mindskip/xzs/repository/SubjectDeptMapper.java b/src/main/java/com/mindskip/xzs/repository/SubjectDeptMapper.java index 66683f2..889931c 100644 --- a/src/main/java/com/mindskip/xzs/repository/SubjectDeptMapper.java +++ b/src/main/java/com/mindskip/xzs/repository/SubjectDeptMapper.java @@ -2,6 +2,7 @@ import com.mindskip.xzs.domain.SubjectDept; import com.mindskip.xzs.domain.vo.SubjectDeptVO; +import com.mindskip.xzs.vo.SubjectVO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @@ -19,4 +20,19 @@ void removeBySubjectId(@Param("subjectId") Integer subjectId); List<SubjectDeptVO> deptBySubjectId(@Param("subjectId") Integer subjectId); + + /** + * 鑾峰彇閮ㄩ棬涓嬬殑璇剧洰 + * + * @param deptId + * @return + */ + List<SubjectVO> getByDeptId(@Param("deptId") Integer deptId); + + /** + * 鏍规嵁鍒犻櫎閮ㄩ棬鎵�鏈夎鐩� + * + * @param deptId + */ + void deleteByDeptId(@Param("deptId") Integer deptId); } diff --git a/src/main/java/com/mindskip/xzs/service/SubjectService.java b/src/main/java/com/mindskip/xzs/service/SubjectService.java index 1069765..cf449a4 100644 --- a/src/main/java/com/mindskip/xzs/service/SubjectService.java +++ b/src/main/java/com/mindskip/xzs/service/SubjectService.java @@ -1,6 +1,8 @@ package com.mindskip.xzs.service; import com.mindskip.xzs.domain.Subject; +import com.mindskip.xzs.domain.form.AddSubjectForm; +import com.mindskip.xzs.domain.form.EditSubjectForm; import com.mindskip.xzs.viewmodel.admin.education.SubjectPageRequestVM; import com.github.pagehelper.PageInfo; import org.apache.ibatis.annotations.Param; @@ -28,4 +30,18 @@ * @return */ List<Subject> listByDeptId(Integer deptId); + + /** + * 鏂板璇剧洰 + * + * @param form + */ + void add(AddSubjectForm form); + + /** + * 淇敼閮ㄩ棬璇剧洰 + * + * @param form + */ + void edit(EditSubjectForm form); } diff --git a/src/main/java/com/mindskip/xzs/service/impl/SubjectServiceImpl.java b/src/main/java/com/mindskip/xzs/service/impl/SubjectServiceImpl.java index 609b7b1..feeecc0 100644 --- a/src/main/java/com/mindskip/xzs/service/impl/SubjectServiceImpl.java +++ b/src/main/java/com/mindskip/xzs/service/impl/SubjectServiceImpl.java @@ -2,9 +2,13 @@ import com.mindskip.xzs.context.WebContext; import com.mindskip.xzs.domain.Subject; +import com.mindskip.xzs.domain.SubjectDept; import com.mindskip.xzs.domain.enums.RoleEnum; +import com.mindskip.xzs.domain.form.AddSubjectForm; +import com.mindskip.xzs.domain.form.EditSubjectForm; import com.mindskip.xzs.repository.BaseMapper; import com.mindskip.xzs.repository.DepartmentMapper; +import com.mindskip.xzs.repository.SubjectDeptMapper; import com.mindskip.xzs.repository.SubjectMapper; import com.mindskip.xzs.service.SubjectService; import com.mindskip.xzs.viewmodel.admin.education.SubjectPageRequestVM; @@ -12,9 +16,11 @@ import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @Service public class SubjectServiceImpl extends BaseServiceImpl<Subject> implements SubjectService { @@ -22,12 +28,14 @@ private final SubjectMapper subjectMapper; private final WebContext webContext; private final DepartmentMapper departmentMapper; + private final SubjectDeptMapper subjectDeptMapper; - public SubjectServiceImpl(BaseMapper<Subject> baseMapper, SubjectMapper subjectMapper, WebContext webContext, DepartmentMapper departmentMapper) { + public SubjectServiceImpl(BaseMapper<Subject> baseMapper, SubjectMapper subjectMapper, WebContext webContext, DepartmentMapper departmentMapper, SubjectDeptMapper subjectDeptMapper) { super(baseMapper); this.subjectMapper = subjectMapper; this.webContext = webContext; this.departmentMapper = departmentMapper; + this.subjectDeptMapper = subjectDeptMapper; } @Override @@ -79,4 +87,34 @@ public List<Subject> listByDeptId(Integer deptId) { return subjectMapper.listByDeptId(deptId); } + + @Override + @Transactional(rollbackFor = Exception.class) + public void add(AddSubjectForm form) { + Subject subject = new Subject(); + subject.setName(form.getSubjectName()); + subject.setDeleted(false); + subjectMapper.insert(subject); + + SubjectDept subjectDept = new SubjectDept(); + subjectDept.setDeptId(form.getDeptId()); + subjectDept.setSubjectId(subject.getId()); + List<SubjectDept> s = new ArrayList<>(1); + s.add(subjectDept); + subjectDeptMapper.add(s); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void edit(EditSubjectForm form) { + // 鍒犻櫎鍘熷厛鐨勮鐩紝鍐嶆柊澧� + subjectDeptMapper.deleteByDeptId(form.getDeptId()); + List<SubjectDept> toAddList = form.getSubjectIds().stream().map(subjectId -> { + SubjectDept subjectDept = new SubjectDept(); + subjectDept.setDeptId(form.getDeptId()); + subjectDept.setSubjectId(subjectId); + return subjectDept; + }).collect(Collectors.toList()); + subjectDeptMapper.add(toAddList); + } } diff --git a/src/main/resources/mapper/SubjectDeptMapper.xml b/src/main/resources/mapper/SubjectDeptMapper.xml index 4487a63..ed5381d 100644 --- a/src/main/resources/mapper/SubjectDeptMapper.xml +++ b/src/main/resources/mapper/SubjectDeptMapper.xml @@ -25,4 +25,17 @@ WHERE tdq.subject_id = #{subjectId} </select> + <select id="getByDeptId" resultType="com.mindskip.xzs.vo.SubjectVO"> + SELECT + s.id, + s.name as subjectName + FROM + t_subject_dept sd + INNER JOIN t_subject s ON sd.subject_id = s.id AND sd.dept_id = #{deptId} AND s.deleted = 0 + </select> + + <delete id="deleteByDeptId"> + DELETE FROM t_subject_dept WHERE dept_id = #{deptId} + </delete> + </mapper> -- Gitblit v1.8.0