dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
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     public Long createStudent(InfraStudentSaveReqVO createReqVO) {
42         // 插入
43         InfraStudentDO student = BeanUtils.toBean(createReqVO, InfraStudentDO.class);
44         studentMapper.insert(student);
45         // 返回
46         return student.getId();
47     }
48
49     @Override
50     public void updateStudent(InfraStudentSaveReqVO updateReqVO) {
51         // 校验存在
52         validateStudentExists(updateReqVO.getId());
53         // 更新
54         InfraStudentDO updateObj = BeanUtils.toBean(updateReqVO, InfraStudentDO.class);
55         studentMapper.updateById(updateObj);
56     }
57
58     @Override
59     @Transactional(rollbackFor = Exception.class)
60     public void deleteStudent(Long id) {
61         // 校验存在
62         validateStudentExists(id);
63         // 删除
64         studentMapper.deleteById(id);
65
66         // 删除子表
67         deleteStudentContactByStudentId(id);
68         deleteStudentTeacherByStudentId(id);
69     }
70
71     private void validateStudentExists(Long id) {
72         if (studentMapper.selectById(id) == null) {
73             throw exception(STUDENT_NOT_EXISTS);
74         }
75     }
76
77     @Override
78     public InfraStudentDO getStudent(Long id) {
79         return studentMapper.selectById(id);
80     }
81
82     @Override
83     public PageResult<InfraStudentDO> getStudentPage(InfraStudentPageReqVO pageReqVO) {
84         return studentMapper.selectPage(pageReqVO);
85     }
86
87     // ==================== 子表(学生联系人) ====================
88
89     @Override
90     public PageResult<InfraStudentContactDO> getStudentContactPage(PageParam pageReqVO, Long studentId) {
91         return studentContactMapper.selectPage(pageReqVO, studentId);
92     }
93
94     @Override
95     public Long createStudentContact(InfraStudentContactDO studentContact) {
96         studentContactMapper.insert(studentContact);
97         return studentContact.getId();
98     }
99
100     @Override
101     public void updateStudentContact(InfraStudentContactDO studentContact) {
102         // 校验存在
103         validateStudentContactExists(studentContact.getId());
104         // 更新
105         studentContactMapper.updateById(studentContact);
106     }
107
108     @Override
109     public void deleteStudentContact(Long id) {
110         // 校验存在
111         validateStudentContactExists(id);
112         // 删除
113         studentContactMapper.deleteById(id);
114     }
115
116     @Override
117     public InfraStudentContactDO getStudentContact(Long id) {
118         return studentContactMapper.selectById(id);
119     }
120
121     private void validateStudentContactExists(Long id) {
122         if (studentContactMapper.selectById(id) == null) {
123             throw exception(STUDENT_CONTACT_NOT_EXISTS);
124         }
125     }
126
127     private void deleteStudentContactByStudentId(Long studentId) {
128         studentContactMapper.deleteByStudentId(studentId);
129     }
130
131     // ==================== 子表(学生班主任) ====================
132
133     @Override
134     public PageResult<InfraStudentTeacherDO> getStudentTeacherPage(PageParam pageReqVO, Long studentId) {
135         return studentTeacherMapper.selectPage(pageReqVO, studentId);
136     }
137
138     @Override
139     public Long createStudentTeacher(InfraStudentTeacherDO studentTeacher) {
140         // 校验是否已经存在
141         if (studentTeacherMapper.selectByStudentId(studentTeacher.getStudentId()) != null) {
142             throw exception(STUDENT_TEACHER_EXISTS);
143         }
144         // 插入
145         studentTeacherMapper.insert(studentTeacher);
146         return studentTeacher.getId();
147     }
148
149     @Override
150     public void updateStudentTeacher(InfraStudentTeacherDO studentTeacher) {
151         // 校验存在
152         validateStudentTeacherExists(studentTeacher.getId());
153         // 更新
154         studentTeacherMapper.updateById(studentTeacher);
155     }
156
157     @Override
158     public void deleteStudentTeacher(Long id) {
159         // 校验存在
160         validateStudentTeacherExists(id);
161         // 删除
162         studentTeacherMapper.deleteById(id);
163     }
164
165     @Override
166     public InfraStudentTeacherDO getStudentTeacher(Long id) {
167         return studentTeacherMapper.selectById(id);
168     }
169
170     private void validateStudentTeacherExists(Long id) {
171         if (studentTeacherMapper.selectById(id) == null) {
172             throw exception(STUDENT_TEACHER_NOT_EXISTS);
173         }
174     }
175
176     private void deleteStudentTeacherByStudentId(Long studentId) {
177         studentTeacherMapper.deleteByStudentId(studentId);
178     }
179
180 }