package com.mooctest.service;

import com.mooctest.dao.MasterReportDao;
import com.mooctest.model.MasterReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BugReviewService {

    @Autowired
    MasterReportDao masterReportDao;

    public boolean isBugReviewed(String bugId) {
        MasterReport mr = masterReportDao.findByBugId(bugId);
        return (mr != null) && (mr.getStatus() == 1);
    }
    public void aggReportReview(String masterId) {
        List<MasterReport> mrs = masterReportDao.findByMasterId(masterId);
        List<String> bugIds = mrs.stream().map(MasterReport::getBugId).collect(Collectors.toList());

        updateBugReviewed(bugIds);
    }

    private void updateBugReviewed(List<String> bugIds) {
        List<MasterReport> mrs = masterReportDao.findByBugIdIn(bugIds);
        mrs.forEach(mr -> {
            mr.setStatus(1);
            mr.setReviewerId(1l);
        });
        masterReportDao.save(mrs);
    }


}
