2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cn.iocoder.yudao.module.infra.service.demo;
 
import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import cn.iocoder.yudao.module.infra.controller.admin.demo.vo.*;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.InfraStudentDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.InfraStudentContactDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.InfraStudentTeacherDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 
import cn.iocoder.yudao.module.infra.dal.mysql.demo.InfraStudentMapper;
import cn.iocoder.yudao.module.infra.dal.mysql.demo.InfraStudentContactMapper;
import cn.iocoder.yudao.module.infra.dal.mysql.demo.InfraStudentTeacherMapper;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
 
/**
 * 学生 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InfraStudentServiceImpl implements InfraStudentService {
 
    @Resource
    private InfraStudentMapper studentMapper;
    @Resource
    private InfraStudentContactMapper studentContactMapper;
    @Resource
    private InfraStudentTeacherMapper studentTeacherMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createStudent(InfraStudentSaveReqVO createReqVO) {
        // 插入
        InfraStudentDO student = BeanUtils.toBean(createReqVO, InfraStudentDO.class);
        studentMapper.insert(student);
 
 
        // 插入子表
        createStudentContactList(student.getId(), createReqVO.getStudentContacts());
        createStudentTeacher(student.getId(), createReqVO.getStudentTeacher());
        // 返回
        return student.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudent(InfraStudentSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentExists(updateReqVO.getId());
        // 更新
        InfraStudentDO updateObj = BeanUtils.toBean(updateReqVO, InfraStudentDO.class);
        studentMapper.updateById(updateObj);
 
        // 更新子表
        updateStudentContactList(updateReqVO.getId(), updateReqVO.getStudentContacts());
        updateStudentTeacher(updateReqVO.getId(), updateReqVO.getStudentTeacher());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStudent(Long id) {
        // 校验存在
        validateStudentExists(id);
        // 删除
        studentMapper.deleteById(id);
 
        // 删除子表
        deleteStudentContactByStudentId(id);
        deleteStudentTeacherByStudentId(id);
    }
 
    @Override
        @Transactional(rollbackFor = Exception.class)
    public void deleteStudentListByIds(List<Long> ids) {
        // 删除
        studentMapper.deleteByIds(ids);
    
    // 删除子表
            deleteStudentContactByStudentIds(ids);
            deleteStudentTeacherByStudentIds(ids);
    }
 
 
    private void validateStudentExists(Long id) {
        if (studentMapper.selectById(id) == null) {
            throw exception(STUDENT_NOT_EXISTS);
        }
    }
 
    @Override
    public InfraStudentDO getStudent(Long id) {
        return studentMapper.selectById(id);
    }
 
    @Override
    public PageResult<InfraStudentDO> getStudentPage(InfraStudentPageReqVO pageReqVO) {
        return studentMapper.selectPage(pageReqVO);
    }
 
    // ==================== 子表(学生联系人) ====================
 
    @Override
    public List<InfraStudentContactDO> getStudentContactListByStudentId(Long studentId) {
        return studentContactMapper.selectListByStudentId(studentId);
    }
 
    private void createStudentContactList(Long studentId, List<InfraStudentContactDO> list) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        list.forEach(o -> o.setStudentId(studentId).clean());
        studentContactMapper.insertBatch(list);
    }
 
    private void updateStudentContactList(Long studentId, List<InfraStudentContactDO> list) {
        list.forEach(o -> o.setStudentId(studentId).clean());
        List<InfraStudentContactDO> oldList = studentContactMapper.selectListByStudentId(studentId);
        List<List<InfraStudentContactDO>> diffList = diffList(oldList, list, (oldVal, newVal) -> {
            boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId());
            if (same) {
                newVal.setId(oldVal.getId()).clean(); // 解决更新情况下:updateTime 不更新
            }
            return same;
        });
 
        // 第二步,批量添加、修改、删除
        if (CollUtil.isNotEmpty(diffList.get(0))) {
            studentContactMapper.insertBatch(diffList.get(0));
        }
        if (CollUtil.isNotEmpty(diffList.get(1))) {
            studentContactMapper.updateBatch(diffList.get(1));
        }
        if (CollUtil.isNotEmpty(diffList.get(2))) {
            studentContactMapper.deleteByIds(convertList(diffList.get(2), InfraStudentContactDO::getId));
        }
    }
 
    private void deleteStudentContactByStudentId(Long studentId) {
        studentContactMapper.deleteByStudentId(studentId);
    }
 
    private void deleteStudentContactByStudentIds(List<Long> studentIds) {
        studentContactMapper.deleteByStudentIds(studentIds);
    }
 
    // ==================== 子表(学生班主任) ====================
 
    @Override
    public InfraStudentTeacherDO getStudentTeacherByStudentId(Long studentId) {
        return studentTeacherMapper.selectByStudentId(studentId);
    }
 
    private void createStudentTeacher(Long studentId, InfraStudentTeacherDO studentTeacher) {
        if (studentTeacher == null) {
            return;
        }
        studentTeacher.setStudentId(studentId);
        studentTeacherMapper.insert(studentTeacher);
    }
 
    private void updateStudentTeacher(Long studentId, InfraStudentTeacherDO studentTeacher) {
        if (studentTeacher == null) {
            return;
        }
        studentTeacher.setStudentId(studentId).clean();// 解决更新情况下:updateTime 不更新
        studentTeacherMapper.insertOrUpdate(studentTeacher);
    }
 
    private void deleteStudentTeacherByStudentId(Long studentId) {
        studentTeacherMapper.deleteByStudentId(studentId);
    }
 
    private void deleteStudentTeacherByStudentIds(List<Long> studentIds) {
        studentTeacherMapper.deleteByStudentIds(studentIds);
    }
 
}