package com.hz.pm.api.expert.service.impl;

import cn.hutool.core.collection.CollUtil;
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.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.expert.mapper.ExpertReviewMapper;
import com.hz.pm.api.expert.model.entity.ExpertReview;
import com.hz.pm.api.expert.service.IExpertReviewService;
import com.hz.pm.api.meeting.entity.domain.Meeting;
import com.hz.pm.api.meeting.mapper.MeetingMapper;
import com.ningdatech.basic.util.CollUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

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

    private final MeetingMapper meetingMapper;

    @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);
    }

    @Override
    public ExpertReview getFinalReview(String projectCode, Integer meetingType) {
        LambdaQueryWrapper<Meeting> mQuery = Wrappers.lambdaQuery(Meeting.class)
                .select(Meeting::getId)
                .eq(Meeting::getType, meetingType);
        List<Meeting> constructMeetings = meetingMapper.selectList(mQuery);
        List<Long> meetingIds = constructMeetings.stream()
                .map(Meeting::getId).collect(Collectors.toList());
        if (CollUtil.isEmpty(meetingIds)) {
            return null;
        }
        LambdaQueryWrapper<ExpertReview> erQuery = Wrappers.lambdaQuery(ExpertReview.class)
                .eq(ExpertReview::getProjectCode, projectCode)
                .eq(ExpertReview::getIsFinal, Boolean.TRUE)
                .in(ExpertReview::getMeetingId, meetingIds)
                .last(BizConst.LIMIT_1);
        return this.getOne(erQuery);
    }

}
