package com.srs.routine.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
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.RtClassMettingStuUploadMapper;
import com.srs.routine.domain.RtClassMettingStuUpload;
import com.srs.routine.service.IRtClassMettingStuUploadService;

import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 指定学生上传Service业务层处理
 *
 * @author srs
 * @date 2025-02-09
 */
@Service
public class RtClassMettingStuUploadServiceImpl extends ServiceImpl<RtClassMettingStuUploadMapper,RtClassMettingStuUpload> implements IRtClassMettingStuUploadService {
    @Autowired
    private RtClassMettingStuUploadMapper rtClassMettingStuUploadMapper;

    @Override
    public AjaxResult boolStuHasUploadRole(){
        try{
            String stuNo  = getUsername();
            QueryWrapper<RtClassMettingStuUpload> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stu_no",stuNo);
            List<RtClassMettingStuUpload> list = rtClassMettingStuUploadMapper.selectList(queryWrapper);
            if(list.size() == 0){
                throw new Exception("您未被指定上传");
            }

            return AjaxResult.success();
        }catch(Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public List<Map< String, Object >> listStuClass(String stuNo){
        return rtClassMettingStuUploadMapper.listStuClass(stuNo);
    }


    @Override
    public AjaxResult cancelStu( String stuNo){
        try {
            QueryWrapper<RtClassMettingStuUpload> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stu_no", stuNo);
            RtClassMettingStuUpload res = rtClassMettingStuUploadMapper.selectOne(queryWrapper);
            if(res == null){
                throw new Exception("该学生未被指定");
            }
            long id = res.getId();
            int res2 = rtClassMettingStuUploadMapper.deleteById(id);
            if(res2 == 0){
                throw new Exception("取消失败");
            }

            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public List<Map< String, Object >> listFdyStuUpload(){
        return rtClassMettingStuUploadMapper.listFdyStuUpload(getUsername());
    }

    @Override
    public AjaxResult fdyAdd(String stuNo){
        try{

            QueryWrapper<RtClassMettingStuUpload> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stu_no", stuNo);
            List<RtClassMettingStuUpload> list = rtClassMettingStuUploadMapper.selectList(queryWrapper);
            if(list.size() > 0){
                throw new Exception("该学生已经被指定");
            }

            RtClassMettingStuUpload dto = new RtClassMettingStuUpload();
            dto.setStuNo(stuNo);
            dto.setTNo(getUsername());
            dto.setCreateTime(DateUtils.getNowDate());
            dto.setCreateBy(getUsername());

            int res = rtClassMettingStuUploadMapper.insert(dto);
            if(res == 0){
                throw new Exception("新增失败");
            }

            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询指定学生上传
     *
     * @param id 指定学生上传主键
     * @return 指定学生上传
     */
    @Override
    public RtClassMettingStuUpload selectRtClassMettingStuUploadById(Long id) {
        return rtClassMettingStuUploadMapper.selectRtClassMettingStuUploadById(id);
    }

    /**
     * 查询指定学生上传列表
     *
     * @param rtClassMettingStuUpload 指定学生上传
     * @return 指定学生上传
     */
    @Override
    public List<RtClassMettingStuUpload> selectRtClassMettingStuUploadList(RtClassMettingStuUpload rtClassMettingStuUpload) {
        return rtClassMettingStuUploadMapper.selectRtClassMettingStuUploadList(rtClassMettingStuUpload);
    }

    /**
     * 新增指定学生上传
     *
     * @param rtClassMettingStuUpload 指定学生上传
     * @return 结果
     */
    @Override
    public int insertRtClassMettingStuUpload(RtClassMettingStuUpload rtClassMettingStuUpload) {
                rtClassMettingStuUpload.setCreateTime(DateUtils.getNowDate());
            return rtClassMettingStuUploadMapper.insertRtClassMettingStuUpload(rtClassMettingStuUpload);
    }

    /**
     * 修改指定学生上传
     *
     * @param rtClassMettingStuUpload 指定学生上传
     * @return 结果
     */
    @Override
    public int updateRtClassMettingStuUpload(RtClassMettingStuUpload rtClassMettingStuUpload) {
                rtClassMettingStuUpload.setUpdateTime(DateUtils.getNowDate());
        return rtClassMettingStuUploadMapper.updateRtClassMettingStuUpload(rtClassMettingStuUpload);
    }

    /**
     * 批量删除指定学生上传
     *
     * @param ids 需要删除的指定学生上传主键
     * @return 结果
     */
    @Override
    public int deleteRtClassMettingStuUploadByIds(Long[] ids) {
        return rtClassMettingStuUploadMapper.deleteRtClassMettingStuUploadByIds(ids);
    }

    /**
     * 删除指定学生上传信息
     *
     * @param id 指定学生上传主键
     * @return 结果
     */
    @Override
    public int deleteRtClassMettingStuUploadById(Long id) {
        return rtClassMettingStuUploadMapper.deleteRtClassMettingStuUploadById(id);
    }
}
