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.common.utils.StringUtils;
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;

/**
 * 牦牛疾病预警查询与同步入口实现。
 * <p>
 * 列表/详情读 {@code biz_yak_disease_warning}；同步委托 {@link YakDiseaseWarningSyncService}。<br>
 * 详情可按 {@code yakNo} 反查 {@code biz_yak_asset} 填充 {@code yakAssetId}（非强外键）。
 * </p>
 */
@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);
        // 按耳标/牦牛编号关联档案，供移动端/详情跳转
        if (StringUtils.isNotEmpty(row.getYakNo()))
        {
            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;
    }

    /**
     * 牧场数据权限：传入非空可见牧场集合时，记录必须落在集合内；
     * {@code pastureId} 为空视为不可见（无法映射牧场的集中预警）。
     */
    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);
        }
    }
}
