package com.nbport.zgwl.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbport.zgwl.common.PageResult;
import com.nbport.zgwl.dto.StudentQueryDto;
import com.nbport.zgwl.dto.StudentSaveDto;
import com.nbport.zgwl.entity.StudentEntity;
import com.nbport.zgwl.entity.SysUserEntity;
import com.nbport.zgwl.exception.ServiceErrorCodeRange;
import com.nbport.zgwl.exception.ServiceException;
import com.nbport.zgwl.mapper.StudentMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class StudentService {

    private final StudentMapper studentMapper;

    public PageResult<Map<String, Object>> queryList(StudentQueryDto queryDto, SysUserEntity user) {
        try {
            log.info("学生分页查询，user={}, queryDto={}", user, queryDto);
            Page<Map<String, Object>> page = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
            Page<Map<String, Object>> resultPage = (Page<Map<String, Object>>) studentMapper.queryList(page, queryDto);
            return new PageResult<>(resultPage);
        } catch (Exception e) {
            log.error("分页查询学生失败", e);
            throw new ServiceException("分页查询失败");
        }
    }

    public StudentEntity detail(Long id) {
        StudentEntity studentEntity = studentMapper.selectById(id);
        if (studentEntity == null) {
            throw new ServiceException(ServiceErrorCodeRange.DATA_NOT_FOUND, "学生信息不存在");
        }
        return studentEntity;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(StudentSaveDto dto) {
        boolean exists = studentMapper.exists(new LambdaQueryWrapper<StudentEntity>()
                .eq(StudentEntity::getStudentNo, dto.getStudentNo()));
        if (exists) {
            throw new ServiceException("学号已存在");
        }
        StudentEntity entity = BeanUtil.copyProperties(dto, StudentEntity.class);
        studentMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(StudentSaveDto dto) {
        if (dto.getId() == null) {
            throw new ServiceException("id 不能为空");
        }
        StudentEntity dbEntity = detail(dto.getId());
        boolean exists = studentMapper.exists(new LambdaQueryWrapper<StudentEntity>()
                .eq(StudentEntity::getStudentNo, dto.getStudentNo())
                .ne(StudentEntity::getId, dto.getId()));
        if (exists) {
            throw new ServiceException("学号已存在");
        }
        BeanUtil.copyProperties(dto, dbEntity);
        studentMapper.updateById(dbEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        detail(id);
        studentMapper.deleteById(id);
    }
}
