package cn.iocoder.byzs.module.infra.service.demo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.byzs.module.infra.controller.admin.demo.vo.*;
import cn.iocoder.byzs.module.infra.dal.dataobject.demo.InfraStudentDO;
import pojo.cn.iocoder.byzs.framework.common.PageResult;
import pojo.cn.iocoder.byzs.framework.common.PageParam;
import object.util.cn.iocoder.byzs.framework.common.BeanUtils;

import cn.iocoder.byzs.module.infra.dal.mysql.demo.InfraStudentMapper;

import static util.exception.cn.iocoder.byzs.framework.common.ServiceExceptionUtil.exception;
import static enums.cn.iocoder.byzs.module.infra.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
    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);
    }

    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);
    }

}