package com.srs.comprehensive.service.impl;

import java.util.List;
import java.util.Objects;
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.SrsKnrdTypeMapper;
import com.srs.comprehensive.domain.SrsKnrdType;
import com.srs.comprehensive.service.ISrsKnrdTypeService;

/**
 * 困难类型Service业务层处理
 *
 * @author srs
 * @date 2024-06-05
 */
@Service
public class SrsKnrdTypeServiceImpl extends ServiceImpl<SrsKnrdTypeMapper,SrsKnrdType> implements ISrsKnrdTypeService {
    @Autowired
    private SrsKnrdTypeMapper srsKnrdTypeMapper;


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

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

            QueryWrapper<SrsKnrdType> query = new QueryWrapper<>();
            query.in("stu_no", stuNoSet)
                    .eq("stu_year_id", param.get(0).stuYearId);

            List<SrsKnrdType> isInStu = srsKnrdTypeMapper.selectList(query);

            if (isInStu.size() != 0) {
                param.forEach(x -> {
                    SrsKnrdType find = isInStu.stream()
                            .filter(y -> Objects.equals(y.getStuNo(), x.getStuNo())
                            ).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 SrsKnrdType selectSrsKnrdTypeById(Long id) {
        return srsKnrdTypeMapper.selectSrsKnrdTypeById(id);
    }

    /**
     * 查询困难类型列表
     *
     * @param srsKnrdType 困难类型
     * @return 困难类型
     */
    @Override
    public List<SrsKnrdType> selectSrsKnrdTypeList(SrsKnrdType srsKnrdType) {
        return srsKnrdTypeMapper.selectSrsKnrdTypeList(srsKnrdType);
    }

    /**
     * 新增困难类型
     *
     * @param srsKnrdType 困难类型
     * @return 结果
     */
    @Override
    public int insertSrsKnrdType(SrsKnrdType srsKnrdType) throws Exception {
        QueryWrapper<SrsKnrdType> query = new QueryWrapper<>();
        query.eq("stu_no", srsKnrdType.getStuNo())
                .eq("stu_year_id", srsKnrdType.stuYearId);
        List<SrsKnrdType> find = srsKnrdTypeMapper.selectList(query);
        if (find.size() != 0) {
            throw new Exception("学生学年标签冲突");
        }
        return srsKnrdTypeMapper.insertSrsKnrdType(srsKnrdType);
    }

    /**
     * 修改困难类型
     *
     * @param srsKnrdType 困难类型
     * @return 结果
     */
    @Override
    public int updateSrsKnrdType(SrsKnrdType srsKnrdType) throws Exception {
        QueryWrapper<SrsKnrdType> query = new QueryWrapper<>();
        query.eq("stu_no", srsKnrdType.getStuNo())
                .eq("stu_year_id", srsKnrdType.stuYearId)
                .ne("id", srsKnrdType.getId());
        List<SrsKnrdType> find = srsKnrdTypeMapper.selectList(query);
        if (find.size() != 0) {
            throw new Exception("学生学年标签冲突");
        }

        return srsKnrdTypeMapper.updateSrsKnrdType(srsKnrdType);
    }

    /**
     * 批量删除困难类型
     *
     * @param ids 需要删除的困难类型主键
     * @return 结果
     */
    @Override
    public int deleteSrsKnrdTypeByIds(Long[] ids) {
        return srsKnrdTypeMapper.deleteSrsKnrdTypeByIds(ids);
    }

    /**
     * 删除困难类型信息
     *
     * @param id 困难类型主键
     * @return 结果
     */
    @Override
    public int deleteSrsKnrdTypeById(Long id) {
        return srsKnrdTypeMapper.deleteSrsKnrdTypeById(id);
    }

    @Override
    public SrsKnrdType queryByStuNo(SrsKnrdType srsKnrdType) {
        //根据学号查询困难类型
        QueryWrapper<SrsKnrdType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no",srsKnrdType.getStuNo());
        queryWrapper.eq("stu_year_id",srsKnrdType.getStuYearId());
        return srsKnrdTypeMapper.selectOne(queryWrapper);
    }
}
