InfraStudentServiceImpl 5.2 KB

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