package com.srs.comprehensive.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.SrsKnrdTimeMapper;
import com.srs.comprehensive.domain.SrsKnrdTime;
import com.srs.comprehensive.service.ISrsKnrdTimeService;

/**
 * 困难认定审核时间Service业务层处理
 *
 * @author srs
 * @date 2024-06-05
 */
@Service
public class SrsKnrdTimeServiceImpl extends ServiceImpl<SrsKnrdTimeMapper,SrsKnrdTime> implements ISrsKnrdTimeService {
    @Autowired
    private SrsKnrdTimeMapper srsKnrdTimeMapper;


    @Override
    public AjaxResult doImport(List<SrsKnrdTime> param){
        try{
            if(param.size() == 0){
                throw new Exception("无数据导入");
            }

            Long stuYearId = param.get(0).getStuYearId();
            String term = param.get(0).getTerm();

            Set<String> stuNoSet = param.stream().map(SrsKnrdTime::getStuNo).collect(Collectors.toSet());

            QueryWrapper<SrsKnrdTime> query = new QueryWrapper<>();
            query.eq("stu_year_id",stuYearId)
                    .eq("term",term)
                    .in("stu_no",stuNoSet);

            List<SrsKnrdTime> isInStu = srsKnrdTimeMapper.selectList(query);

            if(isInStu.size() != 0){
                param.forEach(x->{
                    SrsKnrdTime find = isInStu.stream()
                            .filter(y-> Objects.equals(y.getStuNo(), x.getStuNo())
                                && Objects.equals(y.getStuYearId(), x.getStuYearId())
                                    && Objects.equals(y.getTerm(), x.getTerm())
                            ).findFirst().orElse(null);
                    if(find != null){
                        x.setId(find.getId());
                    }
                });
            }

            boolean res = this.saveOrUpdateBatch(param);
            if(!res){
                throw new Exception("保存失败");
            }

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




    /**
     * 查询困难认定审核时间
     *
     * @param id 困难认定审核时间主键
     * @return 困难认定审核时间
     */
    @Override
    public SrsKnrdTime selectSrsKnrdTimeById(Long id) {
        return srsKnrdTimeMapper.selectSrsKnrdTimeById(id);
    }

    /**
     * 查询困难认定审核时间列表
     *
     * @param srsKnrdTime 困难认定审核时间
     * @return 困难认定审核时间
     */
    @Override
    public List<SrsKnrdTime> selectSrsKnrdTimeList(SrsKnrdTime srsKnrdTime) {
        return srsKnrdTimeMapper.selectSrsKnrdTimeList(srsKnrdTime);
    }

    /**
     * 新增困难认定审核时间
     *
     * @param srsKnrdTime 困难认定审核时间
     * @return 结果
     */
    @Override
    public int insertSrsKnrdTime(SrsKnrdTime srsKnrdTime) {
            return srsKnrdTimeMapper.insertSrsKnrdTime(srsKnrdTime);
    }

    /**
     * 修改困难认定审核时间
     *
     * @param srsKnrdTime 困难认定审核时间
     * @return 结果
     */
    @Override
    public int updateSrsKnrdTime(SrsKnrdTime srsKnrdTime) {
        return srsKnrdTimeMapper.updateSrsKnrdTime(srsKnrdTime);
    }

    /**
     * 批量删除困难认定审核时间
     *
     * @param ids 需要删除的困难认定审核时间主键
     * @return 结果
     */
    @Override
    public int deleteSrsKnrdTimeByIds(Long[] ids) {
        return srsKnrdTimeMapper.deleteSrsKnrdTimeByIds(ids);
    }

    /**
     * 删除困难认定审核时间信息
     *
     * @param id 困难认定审核时间主键
     * @return 结果
     */
    @Override
    public int deleteSrsKnrdTimeById(Long id) {
        return srsKnrdTimeMapper.deleteSrsKnrdTimeById(id);
    }
}
