package com.srs.routine.service.impl;

import java.util.List;

import com.srs.common.exception.ServiceException;
import com.srs.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.routine.mapper.RtEnlistmentReserveApprovalMapper;
import com.srs.routine.domain.RtEnlistmentReserveApproval;
import com.srs.routine.service.IRtEnlistmentReserveApprovalService;

/**
 * 保留学籍审批记录Service业务层处理
 *
 * @author srs
 * @date 2025-10-31
 */
@Service
public class RtEnlistmentReserveApprovalServiceImpl extends ServiceImpl<RtEnlistmentReserveApprovalMapper,RtEnlistmentReserveApproval> implements IRtEnlistmentReserveApprovalService {
    @Autowired
    private RtEnlistmentReserveApprovalMapper rtEnlistmentReserveApprovalMapper;

    /**
     * 查询保留学籍审批记录
     *
     * @param id 保留学籍审批记录主键
     * @return 保留学籍审批记录
     */
    @Override
    public RtEnlistmentReserveApproval selectRtEnlistmentReserveApprovalById(Long id) {
        return rtEnlistmentReserveApprovalMapper.selectRtEnlistmentReserveApprovalById(id);
    }

    /**
     * 查询保留学籍审批记录列表
     *
     * @param rtEnlistmentReserveApproval 保留学籍审批记录
     * @return 保留学籍审批记录
     */
    @Override
    public List<RtEnlistmentReserveApproval> selectRtEnlistmentReserveApprovalList(RtEnlistmentReserveApproval rtEnlistmentReserveApproval) {
        return rtEnlistmentReserveApprovalMapper.selectRtEnlistmentReserveApprovalList(rtEnlistmentReserveApproval);
    }

    /**
     * 新增保留学籍审批记录
     *
     * @param rtEnlistmentReserveApproval 保留学籍审批记录
     * @return 结果
     */
    @Override
    public int insertRtEnlistmentReserveApproval(RtEnlistmentReserveApproval rtEnlistmentReserveApproval) {
            return rtEnlistmentReserveApprovalMapper.insertRtEnlistmentReserveApproval(rtEnlistmentReserveApproval);
    }

    /**
     * 修改保留学籍审批记录
     *
     * @param rtEnlistmentReserveApproval 保留学籍审批记录
     * @return 结果
     */
    @Override
    public int updateRtEnlistmentReserveApproval(RtEnlistmentReserveApproval rtEnlistmentReserveApproval) {
        return rtEnlistmentReserveApprovalMapper.updateRtEnlistmentReserveApproval(rtEnlistmentReserveApproval);
    }

    /**
     * 批量删除保留学籍审批记录
     *
     * @param ids 需要删除的保留学籍审批记录主键
     * @return 结果
     */
    @Override
    public int deleteRtEnlistmentReserveApprovalByIds(Long[] ids) {
        return rtEnlistmentReserveApprovalMapper.deleteRtEnlistmentReserveApprovalByIds(ids);
    }

    /**
     * 删除保留学籍审批记录信息
     *
     * @param id 保留学籍审批记录主键
     * @return 结果
     */
    @Override
    public int deleteRtEnlistmentReserveApprovalById(Long id) {
        return rtEnlistmentReserveApprovalMapper.deleteRtEnlistmentReserveApprovalById(id);
    }

    /**
     * 根据学生姓名、学号、审批人ID判断，存在则更新，不存在则新增
     * @param approval 审批记录入参
     * @return 数据库影响行数（新增/更新成功返回1，无操作返回0）
     */
    @Override
    public int insertOrUpdateByStuAndApprover(RtEnlistmentReserveApproval approval) {
        approval.setApprovalTime(DateUtils.getNowDate());
        // 入参非空校验
        if (approval == null) {
            throw new ServiceException("审批记录入参不能为空", 500);
        }

        // 查询已有审批记录
        RtEnlistmentReserveApproval existingApproval = rtEnlistmentReserveApprovalMapper.selectRtEnlistmentReserveApprovalByStuName(
                approval.getStudentName(),
                approval.getStudentNo(),
                approval.getApproverId()
        );

        // 存在则更新，不存在则新增
        if (existingApproval != null) {
            // 为更新操作设置主键ID（保证MyBatis更新时能定位到数据）
            approval.setId(existingApproval.getId());
            return rtEnlistmentReserveApprovalMapper.updateRtEnlistmentReserveApproval(approval);
        } else {
            return rtEnlistmentReserveApprovalMapper.insertRtEnlistmentReserveApproval(approval);
        }
    }
}
