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.BizEpidemicReport;
import com.ruoyi.web.modules.epidemic.domain.dto.EpidemicReportRelieveRequest;
import com.ruoyi.web.modules.epidemic.mapper.BizEpidemicReportMapper;
import com.ruoyi.web.modules.epidemic.service.IBizEpidemicReportService;
import com.ruoyi.web.modules.epidemic.support.EpidemicReportRules;
import com.ruoyi.web.modules.epidemic.support.EpidemicReportValidation;

/**
 * 疫情上报服务实现。
 */
@Service
public class BizEpidemicReportServiceImpl implements IBizEpidemicReportService
{
    @Autowired
    private BizEpidemicReportMapper epidemicReportMapper;

    @Override
    public BizEpidemicReport selectBizEpidemicReportById(Long id)
    {
        return epidemicReportMapper.selectBizEpidemicReportById(id);
    }

    @Override
    public List<BizEpidemicReport> selectBizEpidemicReportList(BizEpidemicReport query)
    {
        return epidemicReportMapper.selectBizEpidemicReportList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizEpidemicReport(BizEpidemicReport row)
    {
        EpidemicReportValidation.validateRowForSave(row);
        row.setReleaseDate(null);
        return epidemicReportMapper.insertBizEpidemicReport(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizEpidemicReport(BizEpidemicReport row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        EpidemicReportValidation.validateRowForSave(row);
        BizEpidemicReport db = requireRow(row.getId());
        if (!EpidemicReportRules.canEdit(db))
        {
            throw new ServiceException("已解除的疫情上报不可修改");
        }
        row.setReleaseDate(null);
        return epidemicReportMapper.updateBizEpidemicReport(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizEpidemicReportByIds(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 epidemicReportMapper.deleteBizEpidemicReportByIds(cleaned.toArray(new Long[0]));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int relieve(EpidemicReportRelieveRequest request, String updateBy)
    {
        BizEpidemicReport db = requireRow(request.getId());
        if (!EpidemicReportRules.canRelieve(db))
        {
            throw new ServiceException("仅未解除的疫情上报可执行解除");
        }
        EpidemicReportValidation.validateRelieveRequest(request, db.getReportDate());
        BizEpidemicReport update = new BizEpidemicReport();
        update.setId(request.getId());
        update.setReleaseDate(request.getReleaseDate());
        update.setUpdateBy(updateBy);
        return epidemicReportMapper.updateReleaseDate(update);
    }

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