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 java.util.concurrent.atomic.AtomicInteger;
|
import cn.iocoder.yudao.module.infra.controller.admin.demo.vo.*;
|
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.InfraStudentDO;
|
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 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;
|
|
@Override
|
public Long createStudent(InfraStudentSaveReqVO createReqVO) {
|
// 插入
|
InfraStudentDO student = BeanUtils.toBean(createReqVO, InfraStudentDO.class);
|
studentMapper.insert(student);
|
|
// 返回
|
return student.getId();
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
public InfraStudentImportRespVO importStudentList(List<InfraStudentImportExcelVO> importList) {
|
if (CollUtil.isEmpty(importList)) {
|
return InfraStudentImportRespVO.builder()
|
.successCount(0).failureCount(0).failureRows(new LinkedHashMap<>()).build();
|
}
|
// 遍历,逐个创建
|
InfraStudentImportRespVO respVO = InfraStudentImportRespVO.builder()
|
.successCount(0).failureCount(0).failureRows(new LinkedHashMap<>()).build();
|
AtomicInteger index = new AtomicInteger(1);
|
importList.forEach(importItem -> {
|
int currentIndex = index.getAndIncrement();
|
try {
|
createStudent(BeanUtils.toBean(importItem, InfraStudentSaveReqVO.class));
|
respVO.setSuccessCount(respVO.getSuccessCount() + 1);
|
} catch (Exception ex) {
|
respVO.getFailureRows().put(currentIndex, ex.getMessage());
|
}
|
});
|
respVO.setFailureCount(respVO.getFailureRows().size());
|
return respVO;
|
}
|
|
@Override
|
public void updateStudent(InfraStudentSaveReqVO updateReqVO) {
|
// 校验存在
|
validateStudentExists(updateReqVO.getId());
|
// 更新
|
InfraStudentDO updateObj = BeanUtils.toBean(updateReqVO, InfraStudentDO.class);
|
studentMapper.updateById(updateObj);
|
}
|
|
@Override
|
public void deleteStudent(Long id) {
|
// 校验存在
|
validateStudentExists(id);
|
// 删除
|
studentMapper.deleteById(id);
|
}
|
|
@Override
|
public void deleteStudentListByIds(List<Long> ids) {
|
// 删除
|
studentMapper.deleteByIds(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);
|
}
|
|
}
|