package com.srs.dormitory.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.dormitory.mapper.DmsOutsideAccommodationApprovalMapper;
import com.srs.dormitory.domain.DmsOutsideAccommodationApproval;
import com.srs.dormitory.service.IDmsOutsideAccommodationApprovalService;

/**
 * 外宿申请审批记录Service业务层处理
 *
 * @author srs
 * @date 2025-12-05
 */
@Service
public class DmsOutsideAccommodationApprovalServiceImpl extends ServiceImpl<DmsOutsideAccommodationApprovalMapper,DmsOutsideAccommodationApproval> implements IDmsOutsideAccommodationApprovalService {
    @Autowired
    private DmsOutsideAccommodationApprovalMapper dmsOutsideAccommodationApprovalMapper;

    /**
     * 查询外宿申请审批记录
     *
     * @param id 外宿申请审批记录主键
     * @return 外宿申请审批记录
     */
    @Override
    public DmsOutsideAccommodationApproval selectDmsOutsideAccommodationApprovalById(Long id) {
        return dmsOutsideAccommodationApprovalMapper.selectDmsOutsideAccommodationApprovalById(id);
    }

    /**
     * 查询外宿申请审批记录列表
     *
     * @param dmsOutsideAccommodationApproval 外宿申请审批记录
     * @return 外宿申请审批记录
     */
    @Override
    public List<DmsOutsideAccommodationApproval> selectDmsOutsideAccommodationApprovalList(DmsOutsideAccommodationApproval dmsOutsideAccommodationApproval) {
        return dmsOutsideAccommodationApprovalMapper.selectDmsOutsideAccommodationApprovalList(dmsOutsideAccommodationApproval);
    }

    /**
     * 新增外宿申请审批记录
     *
     * @param dmsOutsideAccommodationApproval 外宿申请审批记录
     * @return 结果
     */
    @Override
    public int insertDmsOutsideAccommodationApproval(DmsOutsideAccommodationApproval dmsOutsideAccommodationApproval) {
            return dmsOutsideAccommodationApprovalMapper.insertDmsOutsideAccommodationApproval(dmsOutsideAccommodationApproval);
    }

    /**
     * 修改外宿申请审批记录
     *
     * @param dmsOutsideAccommodationApproval 外宿申请审批记录
     * @return 结果
     */
    @Override
    public int updateDmsOutsideAccommodationApproval(DmsOutsideAccommodationApproval dmsOutsideAccommodationApproval) {
        return dmsOutsideAccommodationApprovalMapper.updateDmsOutsideAccommodationApproval(dmsOutsideAccommodationApproval);
    }

    /**
     * 批量删除外宿申请审批记录
     *
     * @param ids 需要删除的外宿申请审批记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsOutsideAccommodationApprovalByIds(Long[] ids) {
        return dmsOutsideAccommodationApprovalMapper.deleteDmsOutsideAccommodationApprovalByIds(ids);
    }

    /**
     * 删除外宿申请审批记录信息
     *
     * @param id 外宿申请审批记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsOutsideAccommodationApprovalById(Long id) {
        return dmsOutsideAccommodationApprovalMapper.deleteDmsOutsideAccommodationApprovalById(id);
    }

    @Override
    public int addOrUpdateAccommodationApproval(DmsOutsideAccommodationApproval dmsOutsideAccommodationApproval) {
        // 空值校验：避免空指针
        if (dmsOutsideAccommodationApproval == null
                || dmsOutsideAccommodationApproval.getStudentName() == null
                || dmsOutsideAccommodationApproval.getStudentNo() == null
                || dmsOutsideAccommodationApproval.getApproverId() == null) {
            return 0;
        }

        int result = 0;
        try {
            // 1. 根据学生姓名、学号、审批人ID查询已有记录
            DmsOutsideAccommodationApproval accommodationApproval = dmsOutsideAccommodationApprovalMapper
                    .selectOutsideAccommodationApprovalByStuName(
                            dmsOutsideAccommodationApproval.getStudentName(),
                            dmsOutsideAccommodationApproval.getStudentNo(),
                            dmsOutsideAccommodationApproval.getApproverId()
                    );

            // 2. 无记录则新增，有记录则更新
            if (accommodationApproval == null) {
                result = dmsOutsideAccommodationApprovalMapper.insertDmsOutsideAccommodationApproval(dmsOutsideAccommodationApproval);
            } else {
                // 设置已有记录的ID，用于更新
                dmsOutsideAccommodationApproval.setId(accommodationApproval.getId());
                result = dmsOutsideAccommodationApprovalMapper.updateDmsOutsideAccommodationApproval(dmsOutsideAccommodationApproval);
            }
        } catch (Exception e) {
            log.error("新增/更新外宿审批记录失败", e);
            // 抛出运行时异常，触发事务回滚
            throw new RuntimeException("审批记录操作失败：" + e.getMessage());
        }
        return result; // 返回操作结果（1=成功，0=失败）
    }
}
