| | |
| | | package com.mindskip.xzs.service.impl; |
| | | |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.mindskip.xzs.base.RestResponse; |
| | | 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; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | public class SubjectServiceImpl extends BaseServiceImpl<Subject> implements SubjectService { |
| | |
| | | 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 |
| | |
| | | List<Integer> deptIds = webContext.getAdminDeptIds(); |
| | | Boolean admin = RoleEnum.ADMIN.getCode().equals(webContext.getCurrentUser().getRole()); |
| | | return subjectMapper.allSubject(deptIds, admin); |
| | | } |
| | | |
| | | public List<Subject> studentSubList() { |
| | | // 只查自己部门的课目 |
| | | List<Integer> deptIds = webContext.getAdminDeptIds(); |
| | | return subjectMapper.allSubject(deptIds, Boolean.FALSE); |
| | | } |
| | | |
| | | @Override |
| | |
| | | public List<Subject> listByDeptId(Integer deptId) { |
| | | return subjectMapper.listByDeptId(deptId); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public RestResponse add(AddSubjectForm form) { |
| | | Subject subject = new Subject(); |
| | | subject.setName(form.getSubjectName()); |
| | | subject.setDeleted(false); |
| | | try { |
| | | subjectMapper.insert(subject); |
| | | } catch (Exception e) { |
| | | return RestResponse.fail(500, "课目名不能重复"); |
| | | } |
| | | |
| | | SubjectDept subjectDept = new SubjectDept(); |
| | | subjectDept.setDeptId(form.getDeptId()); |
| | | subjectDept.setSubjectId(subject.getId()); |
| | | List<SubjectDept> s = new ArrayList<>(1); |
| | | s.add(subjectDept); |
| | | subjectDeptMapper.add(s); |
| | | return RestResponse.ok(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void edit(EditSubjectForm form) { |
| | | if (ObjectUtils.isNotEmpty(form.getDeptId())) { |
| | | // 删除原先的课目,再新增 |
| | | 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()); |
| | | if (ObjectUtils.isNotEmpty(toAddList)) { |
| | | subjectDeptMapper.add(toAddList); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查名称是否存在 |
| | | * |
| | | * @param id |
| | | * @param name |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean selectByName(Integer id, String name) { |
| | | Subject subject = subjectMapper.selectByName(id,name); |
| | | return Objects.nonNull(subject); |
| | | } |
| | | } |