package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.app.domain.Supplier;
import com.ruoyi.app.mapper.DistributeBatchMapper;
import com.ruoyi.app.mapper.EntranceInspectionMapper;
import com.ruoyi.app.mapper.HarmlessTreatmentMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.EntranceBatchMapper;
import com.ruoyi.app.domain.EntranceBatch;
import com.ruoyi.app.service.IEntranceBatchService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入场批次Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class EntranceBatchServiceImpl implements IEntranceBatchService 
{
    @Autowired
    private EntranceBatchMapper entranceBatchMapper;

    @Autowired
    private DistributeBatchMapper distributeBatchMapper;

    @Autowired
    private EntranceInspectionMapper entranceInspectionMapper;

    @Autowired
    private HarmlessTreatmentMapper harmlessTreatmentMapper;

    /**
     * 查询入场批次
     * 
     * @param id 入场批次主键
     * @return 入场批次
     */
    @Override
    public EntranceBatch selectEntranceBatchById(Long id)
    {
        return entranceBatchMapper.selectEntranceBatchDetailById(id);
    }

    /**
     * 查询入场批次列表
     * 
     * @param entranceBatch 入场批次
     * @return 入场批次
     */
    @Override
    public List<EntranceBatch> selectEntranceBatchList(EntranceBatch entranceBatch)
    {
        return entranceBatchMapper.selectEntranceBatchList(entranceBatch);
    }

    /**
     * 校验检疫证号是否唯一
     *
     * @param entranceBatch 入场批次
     * @return 结果
     */
    @Override
    public boolean checkAnimalCertNoUnique(EntranceBatch entranceBatch)
    {
        Long entranceBatchId = StringUtils.isNull(entranceBatch.getId()) ? -1L : entranceBatch.getId();
        EntranceBatch info = entranceBatchMapper.checkAnimalCertNoUnique(entranceBatch.getAnimalCertNo());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != entranceBatchId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 新增入场批次
     * 
     * @param entranceBatch 入场批次
     * @return 结果
     */
    @Override
    public int insertEntranceBatch(EntranceBatch entranceBatch)
    {
        entranceBatch.setCreateTime(DateUtils.getNowDate());
        return entranceBatchMapper.insertEntranceBatch(entranceBatch);
    }

    /**
     * 修改入场批次
     * 
     * @param entranceBatch 入场批次
     * @return 结果
     */
    @Override
    public int updateEntranceBatch(EntranceBatch entranceBatch)
    {
        entranceBatch.setUpdateTime(DateUtils.getNowDate());
        return entranceBatchMapper.updateEntranceBatch(entranceBatch);
    }

    /**
     * 批量删除入场批次
     * 
     * @param ids 需要删除的入场批次主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEntranceBatchByIds(Long[] ids)
    {
        for (Long entranceBatchId : ids)
        {
            if(entranceBatchId !=null){
                if (distributeBatchMapper.countBatchByEntrance(entranceBatchId) > 0)
                {
                    throw new ServiceException("有关联分销批次，不能删除");
                }

            }
        }
        //删除无害化、检查信息
        entranceInspectionMapper.deleteEntranceInspectionByIds(ids);
        harmlessTreatmentMapper.deleteHarmlessTreatmentByIds(ids);
        return entranceBatchMapper.deleteEntranceBatchByIds(ids);
    }

    /**
     * 删除入场批次信息
     * 
     * @param id 入场批次主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEntranceBatchById(Long id)
    {
        if(id !=null){
            if (distributeBatchMapper.countBatchByEntrance(id) > 0)
            {
                throw new ServiceException("有关联分销批次，不能删除");
            }
        }
        //删除无害化、检查信息
        entranceInspectionMapper.deleteEntranceInspectionById(id);
        harmlessTreatmentMapper.deleteHarmlessTreatmentById(id);
        return entranceBatchMapper.deleteEntranceBatchById(id);
    }

    /**
     * 获取最近一条记录
     * @return 结果
     */
    public EntranceBatch selectLatelyEntrance(){
        return entranceBatchMapper.selectLatelyEntrance();
    }
}
