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 com.srs.comprehensive.domain.SrsKnrdTime;
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.SrsZxjTimeMapper;
import com.srs.comprehensive.domain.SrsZxjTime;
import com.srs.comprehensive.service.ISrsZxjTimeService;

/**
 * 助学金审核时间Service业务层处理
 *
 * @author srs
 * @date 2024-06-05
 */
@Service
public class SrsZxjTimeServiceImpl extends ServiceImpl<SrsZxjTimeMapper, SrsZxjTime> implements ISrsZxjTimeService {
    @Autowired
    private SrsZxjTimeMapper srsZxjTimeMapper;


    @Override
    public AjaxResult doImport(List<SrsZxjTime> 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(SrsZxjTime::getStuNo).collect(Collectors.toSet());

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

            List<SrsZxjTime> isInStu = srsZxjTimeMapper.selectList(query);

            if (isInStu.size() != 0) {
                param.forEach(x -> {
                    SrsZxjTime 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 SrsZxjTime selectSrsZxjTimeById(Long id) {
        return srsZxjTimeMapper.selectSrsZxjTimeById(id);
    }

    /**
     * 查询助学金审核时间列表
     *
     * @param srsZxjTime 助学金审核时间
     * @return 助学金审核时间
     */
    @Override
    public List<SrsZxjTime> selectSrsZxjTimeList(SrsZxjTime srsZxjTime) {
        return srsZxjTimeMapper.selectSrsZxjTimeList(srsZxjTime);
    }

    /**
     * 新增助学金审核时间
     *
     * @param srsZxjTime 助学金审核时间
     * @return 结果
     */
    @Override
    public int insertSrsZxjTime(SrsZxjTime srsZxjTime) {
        return srsZxjTimeMapper.insertSrsZxjTime(srsZxjTime);
    }

    /**
     * 修改助学金审核时间
     *
     * @param srsZxjTime 助学金审核时间
     * @return 结果
     */
    @Override
    public int updateSrsZxjTime(SrsZxjTime srsZxjTime) {
        return srsZxjTimeMapper.updateSrsZxjTime(srsZxjTime);
    }

    /**
     * 批量删除助学金审核时间
     *
     * @param ids 需要删除的助学金审核时间主键
     * @return 结果
     */
    @Override
    public int deleteSrsZxjTimeByIds(Long[] ids) {
        return srsZxjTimeMapper.deleteSrsZxjTimeByIds(ids);
    }

    /**
     * 删除助学金审核时间信息
     *
     * @param id 助学金审核时间主键
     * @return 结果
     */
    @Override
    public int deleteSrsZxjTimeById(Long id) {
        return srsZxjTimeMapper.deleteSrsZxjTimeById(id);
    }
}
