package com.lesingle.modules.biz.contest.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lesingle.modules.biz.contest.entity.BizContest;
import com.lesingle.modules.biz.contest.entity.BizContestNotice;
import com.lesingle.modules.biz.contest.mapper.ContestNoticeMapper;
import com.lesingle.modules.biz.contest.service.IContestNoticeService;
import com.lesingle.modules.biz.contest.service.IContestService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ContestNoticeServiceImpl extends ServiceImpl<ContestNoticeMapper, BizContestNotice> implements IContestNoticeService {

    private final IContestService contestService;

    @Override
    public void fillContestInfo(List<BizContestNotice> notices) {
        if (notices == null || notices.isEmpty()) {
            return;
        }
        Set<Long> contestIds = notices.stream()
                .map(BizContestNotice::getContestId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if (contestIds.isEmpty()) {
            return;
        }
        List<BizContest> contests = contestService.listByIds(contestIds);
        Map<Long, BizContest> map = contests.stream()
                .collect(Collectors.toMap(BizContest::getId, Function.identity(), (a, b) -> a));
        for (BizContestNotice notice : notices) {
            Long cid = notice.getContestId();
            if (cid == null) {
                continue;
            }
            BizContest c = map.get(cid);
            if (c != null) {
                BizContest brief = new BizContest();
                brief.setId(c.getId());
                brief.setContestName(c.getContestName());
                notice.setContest(brief);
            }
        }
    }
}
