package com.ruoyi.web.core.support;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.diagnosis.mapper.BizMedicalResourceMapper;
import com.ruoyi.web.modules.farming.mapper.BizTechResourceMapper;

/**
 * 用户删除前检查是否被畜牧医疗/科技资源引用。
 */
@Component
public class SysUserResourceReferenceChecker
{
    @Autowired
    private BizMedicalResourceMapper bizMedicalResourceMapper;

    @Autowired
    private BizTechResourceMapper bizTechResourceMapper;

    public void assertDeletable(Long userId)
    {
        if (userId == null)
        {
            return;
        }
        if (bizMedicalResourceMapper.countBySysUserId(userId) > 0)
        {
            throw new ServiceException("该用户已被畜牧医疗资源引用，不允许删除");
        }
        if (bizTechResourceMapper.countBySysUserId(userId) > 0)
        {
            throw new ServiceException("该用户已被畜牧科技资源引用，不允许删除");
        }
    }

    public void assertDeletable(Long[] userIds)
    {
        if (userIds == null)
        {
            return;
        }
        for (Long userId : userIds)
        {
            assertDeletable(userId);
        }
    }
}
