package com.srs.routine.service.impl;

import java.util.List;

import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.exception.ServiceException;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.routine.domain.vo.StuInfoReturnVo;
import com.srs.routine.mapper.RtStuLeaveApplicationMapper;
import com.srs.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
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.RtStuIdReissueMapper;
import com.srs.routine.domain.RtStuIdReissue;
import com.srs.routine.service.IRtStuIdReissueService;

/**
 * 学生证补办Service业务层处理
 *
 * @author srs
 * @date 2024-05-16
 */
@Service
@Slf4j
public class RtStuIdReissueServiceImpl extends ServiceImpl<RtStuIdReissueMapper,RtStuIdReissue> implements IRtStuIdReissueService {
    @Autowired
    private RtStuIdReissueMapper rtStuIdReissueMapper;

    @Autowired
    private RtStuLeaveApplicationMapper rtStuLeaveApplicationMapper;

    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询学生证补办
     *
     * @param id 学生证补办主键
     * @return 学生证补办
     */
    @Override
    public RtStuIdReissue selectRtStuIdReissueById(Long id) {
        return rtStuIdReissueMapper.selectRtStuIdReissueById(id);
    }

    @Override
    public RtStuIdReissue selectRtStuIdReissueByStuNo(String stuNo) {
        return rtStuIdReissueMapper.selectRtStuIdReissueByStuNo(stuNo);
    }

    /**
     * 查询学生证补办列表
     *
     * @param rtStuIdReissue 学生证补办
     * @return 学生证补办
     */
    @Override
    public List<RtStuIdReissue> selectRtStuIdReissueList(RtStuIdReissue rtStuIdReissue) {
        return rtStuIdReissueMapper.selectRtStuIdReissueList(rtStuIdReissue);
    }

    /**
     * 新增学生证补办
     *
     * @param rtStuIdReissue 学生证补办
     * @return 结果
     */
    @Override
    public int insertRtStuIdReissue(RtStuIdReissue rtStuIdReissue) {
        RtStuIdReissue rtStuIdReissue1 = rtStuIdReissueMapper.selectRtStuIdReissueByStuNo(rtStuIdReissue.getStuNo());
        if (rtStuIdReissue1 != null) {
            throw new ServiceException("申请已提交，请勿重复提交", 500);
        }
        rtStuIdReissue.setCreateTime(DateUtils.getNowDate());
        // 当前登录用户名字
        // rtStuIdReissue.setCounsellorName(SecurityUtils.getLoginUser().getUser().getNickName());
        // 当前登录用户Id
        // rtStuIdReissue.setCounsellorId(SecurityUtils.getUserId());
        // 获取该学生在user表的id
        SysUser sysUser = sysUserService.selectUserByUserName(rtStuIdReissue.getStuNo());
        if (sysUser == null) {
            throw new ServiceException("该学生不存在", 500);
        }
        // 学生id
        rtStuIdReissue.setStuId(sysUser.getUserId());
        // 学生名字
        rtStuIdReissue.setStuName(sysUser.getNickName());
        // rtStuIdReissue.setStuName(SecurityUtils.getLoginUser().getUser().getNickName());

        return rtStuIdReissueMapper.insertRtStuIdReissue(rtStuIdReissue);
    }

    /**
     * 修改学生证补办
     *
     * @param rtStuIdReissue 学生证补办
     * @return 结果
     */
    @Override
    public int updateRtStuIdReissue(RtStuIdReissue rtStuIdReissue) {
        rtStuIdReissue.setUpdateTime(DateUtils.getNowDate());
        return rtStuIdReissueMapper.updateRtStuIdReissue(rtStuIdReissue);
    }

    /**
     * 批量删除学生证补办
     *
     * @param ids 需要删除的学生证补办主键
     * @return 结果
     */
    @Override
    public int deleteRtStuIdReissueByIds(Long[] ids) {
        return rtStuIdReissueMapper.deleteRtStuIdReissueByIds(ids);
    }

    /**
     * 删除学生证补办信息
     *
     * @param id 学生证补办主键
     * @return 结果
     */
    @Override
    public int deleteRtStuIdReissueById(Long id) {
        return rtStuIdReissueMapper.deleteRtStuIdReissueById(id);
    }


    @Override
    public StuInfoReturnVo getStuInfo(String stuNo) {
        StuInfoReturnVo stuInfoReturnVo = rtStuLeaveApplicationMapper.selectStuInfoReturn(stuNo);
        if (stuInfoReturnVo != null) {
            return stuInfoReturnVo;
        } else {
            return null;
        }
    }
}
