package com.ruoyi.web.modules.epidemic.support;

import java.util.Date;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.epidemic.domain.BizLabDetection;

/**
 * 实验室检测入参校验。
 */
public final class LabDetectionValidation
{
    private LabDetectionValidation()
    {
    }

    public static void trimFieldsForSave(BizLabDetection row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getRemark() != null)
        {
            String remark = row.getRemark().trim();
            row.setRemark(remark.isEmpty() ? null : remark);
        }
    }

    public static void validateTestDate(Date testDate)
    {
        if (testDate == null)
        {
            throw new ServiceException("检测日期不能为空");
        }
    }

    public static void validateSampleSource(Integer sampleSource)
    {
        if (!LabDetectionRules.isSampleSourceValid(sampleSource))
        {
            throw new ServiceException("样本来源须为1～4（1场地抽检2疫情上报送检3调运检疫4日常检测）");
        }
    }

    public static void validateTestQuantity(Integer testQuantity)
    {
        if (!LabDetectionRules.isNonNegativeInteger(testQuantity))
        {
            throw new ServiceException("检测量须为非负整数");
        }
    }

    public static void validatePositiveCount(Integer positiveCount)
    {
        if (!LabDetectionRules.isNonNegativeInteger(positiveCount))
        {
            throw new ServiceException("阳性检出数须为非负整数");
        }
    }

    public static void validateQuantityRelation(int testQuantity, int positiveCount)
    {
        if (testQuantity == 0 && positiveCount != 0)
        {
            throw new ServiceException("检测量为0时阳性检出数须为0");
        }
        if (positiveCount > testQuantity)
        {
            throw new ServiceException("阳性检出数不能大于检测量");
        }
    }

    public static void validateQuarantineStationIdRequired(Long quarantineStationId)
    {
        if (quarantineStationId == null)
        {
            throw new ServiceException("检疫点不能为空");
        }
    }

    public static void validateRowForSave(BizLabDetection row)
    {
        if (row == null)
        {
            throw new ServiceException("参数不能为空");
        }
        trimFieldsForSave(row);
        validateTestDate(row.getTestDate());
        validateQuarantineStationIdRequired(row.getQuarantineStationId());
        validateSampleSource(row.getSampleSource());
        validateTestQuantity(row.getTestQuantity());
        validatePositiveCount(row.getPositiveCount());
        validateQuantityRelation(row.getTestQuantity(), row.getPositiveCount());
    }

    public static void applyDetectionRate(BizLabDetection row)
    {
        int testQuantity = row.getTestQuantity() == null ? 0 : row.getTestQuantity();
        int positiveCount = row.getPositiveCount() == null ? 0 : row.getPositiveCount();
        if (testQuantity == 0)
        {
            row.setPositiveCount(0);
            positiveCount = 0;
        }
        row.setDetectionRate(LabDetectionRules.calcDetectionRate(testQuantity, positiveCount));
    }
}
