package com.srs.routine.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.srs.common.annotation.Excel;
import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.exception.ServiceException;
import com.srs.common.utils.SecurityUtils;
import com.srs.system.service.ISysUserService;
import io.swagger.annotations.ApiModelProperty;
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.RtStuAtSchoolMapper;
import com.srs.routine.domain.RtStuAtSchool;
import com.srs.routine.service.IRtStuAtSchoolService;

/**
 * 测试Service业务层处理
 *
 * @author srs
 * @date 2024-05-27
 */
@Service
public class RtStuAtSchoolServiceImpl extends ServiceImpl<RtStuAtSchoolMapper,RtStuAtSchool> implements IRtStuAtSchoolService {
    @Autowired
    private RtStuAtSchoolMapper rtStuAtSchoolMapper;

    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询测试
     *
     * @param id 测试主键
     * @return 测试
     */
    @Override
    public RtStuAtSchool selectRtStuAtSchoolById(Integer id) {
        return rtStuAtSchoolMapper.selectRtStuAtSchoolById(id);
    }

    @Override
    public RtStuAtSchool selectRtStuAtSchoolByStudentId(String studentId) {
        return rtStuAtSchoolMapper.selectRtStuAtSchoolByStudentId(studentId);
    }

    /**
     * 查询测试列表
     *
     * @param rtStuAtSchool 测试
     * @return 测试
     */
    @Override
    public List<RtStuAtSchool> selectRtStuAtSchoolList(RtStuAtSchool rtStuAtSchool) {
        return rtStuAtSchoolMapper.selectRtStuAtSchoolList(rtStuAtSchool);
    }

    /**
     * 新增测试
     *
     * @param rtStuAtSchool 测试
     * @return 结果
     */
    @Override
    public int insertRtStuAtSchool(RtStuAtSchool rtStuAtSchool) {
        RtStuAtSchool rtStuAtSchool1 = rtStuAtSchoolMapper.selectRtStuAtSchoolByStudentId(rtStuAtSchool.getStudentId());
        if (rtStuAtSchool1 != null){
            throw new ServiceException("申请已提交，请勿重复提交", 500);
        }
        SysUser sysUser = sysUserService.selectUserByUserName(rtStuAtSchool.getStudentId());
        if (sysUser == null) {
            throw new ServiceException("该学生不存在", 500);
        }
        return rtStuAtSchoolMapper.insertRtStuAtSchool(rtStuAtSchool);
    }

    /**
     * 修改测试
     *
     * @param rtStuAtSchool 测试
     * @return 结果
     */
    @Override
    public int updateRtStuAtSchool(RtStuAtSchool rtStuAtSchool) {
        // 如果学工意见和学工审核状态不为空，去到当前登陆人的姓名赋值给学工姓名，并给学工处审核 时间赋值，取出当前时间
        if (rtStuAtSchool.getXgopinion() != null && rtStuAtSchool.getXgstatus() != null) {
            rtStuAtSchool.setXgname(SecurityUtils.getLoginUser().getUser().getNickName());
            rtStuAtSchool.setXgtime(new Date());
        }

        return rtStuAtSchoolMapper.updateRtStuAtSchool(rtStuAtSchool);
    }

    /**
     * 批量删除测试
     *
     * @param ids 需要删除的测试主键
     * @return 结果
     */
    @Override
    public int deleteRtStuAtSchoolByIds(Integer[] ids) {
        return rtStuAtSchoolMapper.deleteRtStuAtSchoolByIds(ids);
    }

    /**
     * 删除测试信息
     *
     * @param id 测试主键
     * @return 结果
     */
    @Override
    public int deleteRtStuAtSchoolById(Integer id) {
        return rtStuAtSchoolMapper.deleteRtStuAtSchoolById(id);
    }
}
