package com.ruoyi.web.modules.epidemic.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.epidemic.domain.BizLabDetection;
import com.ruoyi.web.modules.epidemic.domain.vo.QuarantineStationOptionVo;
import com.ruoyi.web.modules.epidemic.mapper.BizLabDetectionMapper;
import com.ruoyi.web.modules.epidemic.service.IBizLabDetectionService;
import com.ruoyi.web.modules.epidemic.support.LabDetectionQuarantineStationSupport;
import com.ruoyi.web.modules.epidemic.support.LabDetectionValidation;

/**
 * 实验室检测服务实现。
 */
@Service
public class BizLabDetectionServiceImpl implements IBizLabDetectionService
{
    @Autowired
    private BizLabDetectionMapper labDetectionMapper;

    @Autowired
    private LabDetectionQuarantineStationSupport quarantineStationSupport;

    @Override
    public List<QuarantineStationOptionVo> listQuarantineStationOptions()
    {
        return quarantineStationSupport.listNormalStationOptions();
    }

    @Override
    public BizLabDetection selectBizLabDetectionById(Long id)
    {
        return labDetectionMapper.selectBizLabDetectionById(id);
    }

    @Override
    public List<BizLabDetection> selectBizLabDetectionList(BizLabDetection query)
    {
        return labDetectionMapper.selectBizLabDetectionList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizLabDetection(BizLabDetection row)
    {
        prepareForSave(row);
        return labDetectionMapper.insertBizLabDetection(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizLabDetection(BizLabDetection row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        requireRow(row.getId());
        prepareForSave(row);
        return labDetectionMapper.updateBizLabDetection(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizLabDetectionByIds(Long[] ids)
    {
        if (ids == null || ids.length == 0)
        {
            return 0;
        }
        List<Long> cleaned = new ArrayList<>();
        for (Long id : ids)
        {
            if (id != null)
            {
                cleaned.add(id);
            }
        }
        if (cleaned.isEmpty())
        {
            return 0;
        }
        return labDetectionMapper.deleteBizLabDetectionByIds(cleaned.toArray(new Long[0]));
    }

    private void prepareForSave(BizLabDetection row)
    {
        LabDetectionValidation.validateRowForSave(row);
        quarantineStationSupport.validateQuarantineStationId(row.getQuarantineStationId());
        row.setDetectionRate(null);
        LabDetectionValidation.applyDetectionRate(row);
    }

    private BizLabDetection requireRow(Long id)
    {
        BizLabDetection row = labDetectionMapper.selectBizLabDetectionById(id);
        if (row == null)
        {
            throw new ServiceException("记录不存在");
        }
        return row;
    }
}
