package com.ningdatech.pmapi.expert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.expert.mapper.ExpertReviewMapper;
import com.ningdatech.pmapi.expert.model.entity.ExpertReview;
import com.ningdatech.pmapi.expert.service.IExpertReviewService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WendyYang
 * @since 2023-02-15
 */
@Service
public class ExpertReviewServiceImpl extends ServiceImpl<ExpertReviewMapper, ExpertReview> implements IExpertReviewService {

    @Override
    public List<ExpertReview> listByProjectIdAndExpertId(Long projectId, Long expertId, Long meetingId) {
        LambdaQueryWrapper<ExpertReview> query = Wrappers.lambdaQuery(ExpertReview.class);
        query.eq(ExpertReview::getProjectId, projectId);
        query.eq(ExpertReview::getCreateBy, expertId);
        query.eq(ExpertReview::getMeetingId, meetingId);
        query.orderByAsc(ExpertReview::getCreateOn);
        return list(query);
    }

    @Override
    public ExpertReview getFinalReview(Long meetingId, Long projectId) {
        LambdaQueryWrapper<ExpertReview> query = Wrappers.lambdaQuery(ExpertReview.class);
        query.eq(ExpertReview::getProjectId, projectId);
        query.eq(ExpertReview::getMeetingId, meetingId);
        query.eq(ExpertReview::getIsFinal, Boolean.TRUE);
        query.last(BizConst.LIMIT_1);
        return getOne(query);
    }

    @Override
    public Map<Long, ExpertReview> listFinalReviewMap(Long meetingId) {
        return CollUtils.listToMap(listFinalReview(meetingId), ExpertReview::getProjectId);
    }

    @Override
    public List<ExpertReview> listFinalReview(Long meetingId) {
        LambdaQueryWrapper<ExpertReview> query = Wrappers.lambdaQuery(ExpertReview.class);
        query.eq(ExpertReview::getMeetingId, meetingId);
        query.eq(ExpertReview::getIsFinal, Boolean.TRUE);
        return list(query);
    }

}
