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

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.BizYakAsset;
import com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning;
import com.ruoyi.web.modules.industryservice.domain.vo.YakAssetSyncResultVo;
import com.ruoyi.web.modules.industryservice.mapper.BizYakAssetMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakDiseaseWarningMapper;
import com.ruoyi.web.modules.industryservice.service.IBizYakDiseaseWarningService;
import com.ruoyi.web.modules.industryservice.service.YakDiseaseWarningSyncService;
import com.ruoyi.web.modules.industryservice.support.YakDiseaseWarningRules;
import com.ruoyi.web.modules.industryservice.support.YakDiseaseWarningValidation;

@Service
public class BizYakDiseaseWarningServiceImpl implements IBizYakDiseaseWarningService
{
    @Autowired
    private BizYakDiseaseWarningMapper warningMapper;

    @Autowired
    private BizYakAssetMapper bizYakAssetMapper;

    @Autowired
    private YakDiseaseWarningSyncService yakDiseaseWarningSyncService;

    @Override
    public List<BizYakDiseaseWarning> selectBizYakDiseaseWarningList(BizYakDiseaseWarning query)
    {
        BizYakDiseaseWarning criteria = prepareQuery(query);
        List<BizYakDiseaseWarning> list = warningMapper.selectBizYakDiseaseWarningList(criteria);
        return list != null ? list : new ArrayList<>();
    }

    @Override
    public BizYakDiseaseWarning selectBizYakDiseaseWarningById(Long id, List<Long> allowedPastureIds)
    {
        if (id == null)
        {
            throw new ServiceException(YakDiseaseWarningRules.MSG_NOT_FOUND);
        }
        BizYakDiseaseWarning row = warningMapper.selectBizYakDiseaseWarningById(id);
        if (row == null)
        {
            throw new ServiceException(YakDiseaseWarningRules.MSG_NOT_FOUND);
        }
        assertPastureAccess(row, allowedPastureIds);
        BizYakAsset asset = bizYakAssetMapper.selectBizYakAssetByYakNo(row.getYakNo());
        if (asset != null)
        {
            row.setYakAssetId(asset.getId());
        }
        return row;
    }

    @Override
    public YakAssetSyncResultVo syncFromThirdParty()
    {
        return yakDiseaseWarningSyncService.syncFromThirdParty();
    }

    private BizYakDiseaseWarning prepareQuery(BizYakDiseaseWarning query)
    {
        BizYakDiseaseWarning criteria = query != null ? query : new BizYakDiseaseWarning();
        YakDiseaseWarningValidation.applyDateRange(criteria);
        return criteria;
    }

    private void assertPastureAccess(BizYakDiseaseWarning row, List<Long> allowedPastureIds)
    {
        if (allowedPastureIds == null || allowedPastureIds.isEmpty())
        {
            return;
        }
        if (row.getPastureId() == null)
        {
            throw new ServiceException(YakDiseaseWarningRules.MSG_FORBIDDEN);
        }
        if (!allowedPastureIds.contains(row.getPastureId()))
        {
            throw new ServiceException(YakDiseaseWarningRules.MSG_FORBIDDEN);
        }
    }
}
