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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.database.entity.ExperimentReviewDetail;
import org.jeecg.modules.database.mapper.ExperimentReviewDetailMapper;
import org.jeecg.modules.database.service.IExperimentReviewDetailService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 试验评定真值表
 * @Author: jeecg-boot
 * @Date: 2025-06-16
 * @Version: V1.0
 */
@Service
public class ExperimentReviewDetailServiceImpl extends ServiceImpl<ExperimentReviewDetailMapper, ExperimentReviewDetail>
        implements IExperimentReviewDetailService {
    @Override
    public List<ExperimentReviewDetail> getBySample(String experimentId, String sampleId) {
        return list(new LambdaQueryWrapper<ExperimentReviewDetail>().eq(ExperimentReviewDetail::getExperimentId, experimentId)
                .eq(ExperimentReviewDetail::getSampleId, sampleId).orderByAsc(ExperimentReviewDetail::getPriority));
    }

    @Override
    public ExperimentReviewDetail getByRule(String experimentId, String sampleId, String ruleId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ExperimentReviewDetail>().eq(ExperimentReviewDetail::getExperimentId,
                experimentId).eq(ExperimentReviewDetail::getSampleId, sampleId).eq(ExperimentReviewDetail::getRuleId, ruleId), false);

    }

    @Override
    public boolean saveOrUpdate(ExperimentReviewDetail entity) {
        ExperimentReviewDetail experimentReviewDetail = getByRule(entity.getExperimentId(), entity.getSampleId(), entity.getRuleId());
        if (experimentReviewDetail != null) {
            entity.setId(experimentReviewDetail.getId());
        }
        return super.saveOrUpdate(entity);
    }
}
