package org.jeecg.modules.database.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.database.constant.ExperimentReportStatus;
import org.jeecg.modules.database.entity.ExperimentReport;
import org.jeecg.modules.database.mapper.ExperimentReportMapper;
import org.jeecg.modules.database.service.IExperimentReportService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 试验报告
 * @Author: jeecg-boot
 * @Date: 2024-08-30
 * @Version: V1.0
 */
@Service
public class ExperimentReportServiceImpl extends ServiceImpl<ExperimentReportMapper, ExperimentReport> implements IExperimentReportService {

    @Override
    public List<ExperimentReport> getByExperimentId(String experimentId) {
        return list(new LambdaQueryWrapper<ExperimentReport>().eq(ExperimentReport::getExperimentId, experimentId));
    } @Override
    public List<ExperimentReport> getPassedByExperimentId(String experimentId) {
        return list(new LambdaQueryWrapper<ExperimentReport>().eq(ExperimentReport::getExperimentId, experimentId).eq(ExperimentReport::getStatus,ExperimentReportStatus.PASSED));
    }

    @Override
    public ExperimentReport getLatestReport(String experimentId) {
        LambdaQueryWrapper<ExperimentReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExperimentReport::getExperimentId, experimentId)
                .eq(ExperimentReport::getStatus, ExperimentReportStatus.PASSED);
        queryWrapper.orderByDesc(ExperimentReport::getCreateTime);
        return getOne(queryWrapper, false);
    }

    @Override
    public IPage<ExperimentReport> join(Page page, ExperimentReport report, QueryWrapper<ExperimentReport> wrapper) {
        if (StringUtils.isBlank(report.getStatus())) {
            return super.page(page, wrapper);
        }
        IPage<ExperimentReport> join = baseMapper.join(page, report);
        return join;
    }
}
