houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.demo;
H 2
3 import org.springframework.stereotype.Service;
4 import javax.annotation.Resource;
5 import org.springframework.validation.annotation.Validated;
6 import org.springframework.transaction.annotation.Transactional;
7
8 import java.util.*;
9 import com.iailab.module.infra.controller.admin.demo.vo.*;
10 import com.iailab.module.infra.dal.dataobject.demo.InfraStudentDO;
11 import com.iailab.module.infra.dal.dataobject.demo.InfraStudentContactDO;
12 import com.iailab.module.infra.dal.dataobject.demo.InfraStudentTeacherDO;
13 import com.iailab.framework.common.pojo.PageResult;
14 import com.iailab.framework.common.pojo.PageParam;
15 import com.iailab.framework.common.util.object.BeanUtils;
16
17 import com.iailab.module.infra.dal.mysql.demo.InfraStudentMapper;
18 import com.iailab.module.infra.dal.mysql.demo.InfraStudentContactMapper;
19 import com.iailab.module.infra.dal.mysql.demo.InfraStudentTeacherMapper;
20
21 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
22 import static com.iailab.module.infra.enums.ErrorCodeConstants.*;
23
24 /**
25  * 学生 Service 实现类
26  *
27  * @author iailab
28  */
29 @Service
30 @Validated
31 public class InfraStudentServiceImpl implements InfraStudentService {
32
33     @Resource
34     private InfraStudentMapper studentMapper;
35     @Resource
36     private InfraStudentContactMapper studentContactMapper;
37     @Resource
38     private InfraStudentTeacherMapper studentTeacherMapper;
39
40     @Override
41     @Transactional(rollbackFor = Exception.class)
42     public Long createStudent(InfraStudentSaveReqVO createReqVO) {
43         // 插入
44         InfraStudentDO student = BeanUtils.toBean(createReqVO, InfraStudentDO.class);
45         studentMapper.insert(student);
46
47         // 插入子表
48         createStudentContactList(student.getId(), createReqVO.getStudentContacts());
49         createStudentTeacher(student.getId(), createReqVO.getStudentTeacher());
50         // 返回
51         return student.getId();
52     }
53
54     @Override
55     @Transactional(rollbackFor = Exception.class)
56     public void updateStudent(InfraStudentSaveReqVO updateReqVO) {
57         // 校验存在
58         validateStudentExists(updateReqVO.getId());
59         // 更新
60         InfraStudentDO updateObj = BeanUtils.toBean(updateReqVO, InfraStudentDO.class);
61         studentMapper.updateById(updateObj);
62
63         // 更新子表
64         updateStudentContactList(updateReqVO.getId(), updateReqVO.getStudentContacts());
65         updateStudentTeacher(updateReqVO.getId(), updateReqVO.getStudentTeacher());
66     }
67
68     @Override
69     @Transactional(rollbackFor = Exception.class)
70     public void deleteStudent(Long id) {
71         // 校验存在
72         validateStudentExists(id);
73         // 删除
74         studentMapper.deleteById(id);
75
76         // 删除子表
77         deleteStudentContactByStudentId(id);
78         deleteStudentTeacherByStudentId(id);
79     }
80
81     private void validateStudentExists(Long id) {
82         if (studentMapper.selectById(id) == null) {
83             throw exception(STUDENT_NOT_EXISTS);
84         }
85     }
86
87     @Override
88     public InfraStudentDO getStudent(Long id) {
89         return studentMapper.selectById(id);
90     }
91
92     @Override
93     public PageResult<InfraStudentDO> getStudentPage(InfraStudentPageReqVO pageReqVO) {
94         return studentMapper.selectPage(pageReqVO);
95     }
96
97     // ==================== 子表(学生联系人) ====================
98
99     @Override
100     public List<InfraStudentContactDO> getStudentContactListByStudentId(Long studentId) {
101         return studentContactMapper.selectListByStudentId(studentId);
102     }
103
104     private void createStudentContactList(Long studentId, List<InfraStudentContactDO> list) {
105         list.forEach(o -> o.setStudentId(studentId));
106         studentContactMapper.insertBatch(list);
107     }
108
109     private void updateStudentContactList(Long studentId, List<InfraStudentContactDO> list) {
110         deleteStudentContactByStudentId(studentId);
111         list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新
112         createStudentContactList(studentId, list);
113     }
114
115     private void deleteStudentContactByStudentId(Long studentId) {
116         studentContactMapper.deleteByStudentId(studentId);
117     }
118
119     // ==================== 子表(学生班主任) ====================
120
121     @Override
122     public InfraStudentTeacherDO getStudentTeacherByStudentId(Long studentId) {
123         return studentTeacherMapper.selectByStudentId(studentId);
124     }
125
126     private void createStudentTeacher(Long studentId, InfraStudentTeacherDO studentTeacher) {
127         if (studentTeacher == null) {
128             return;
129         }
130         studentTeacher.setStudentId(studentId);
131         studentTeacherMapper.insert(studentTeacher);
132     }
133
134     private void updateStudentTeacher(Long studentId, InfraStudentTeacherDO studentTeacher) {
135         if (studentTeacher == null) {
136             return;
137         }
138         studentTeacher.setStudentId(studentId);
139         studentTeacher.setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新
140         studentTeacherMapper.insertOrUpdate(studentTeacher);
141     }
142
143     private void deleteStudentTeacherByStudentId(Long studentId) {
144         studentTeacherMapper.deleteByStudentId(studentId);
145     }
146
147 }