package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.app.DTO.SlaughterRelationDTO;
import com.ruoyi.app.domain.HookBind;
import com.ruoyi.app.domain.Pigpen;
import com.ruoyi.app.domain.request.ExportSlaughterRelation;
import com.ruoyi.app.domain.request.ReqGetSlaughterRelation;
import com.ruoyi.app.domain.request.ReqSlaughterRelation;
import com.ruoyi.app.domain.response.RespGetSlaughterRelation;
import com.ruoyi.app.mapper.DistributeBatchMapper;
import com.ruoyi.app.mapper.HookBindMapper;
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.SlaughterRelationMapper;
import com.ruoyi.app.domain.SlaughterRelation;
import com.ruoyi.app.service.ISlaughterRelationService;

/**
 * 血码关系Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class SlaughterRelationServiceImpl implements ISlaughterRelationService 
{
    @Autowired
    private SlaughterRelationMapper slaughterRelationMapper;

    @Autowired
    private DistributeBatchMapper distributeBatchMapper;

    @Autowired
    private HookBindMapper hookBindMapper;

    /**
     * 查询血码关系
     * 
     * @param id 血码关系主键
     * @return 血码关系
     */
    @Override
    public SlaughterRelation selectSlaughterRelationById(Long id)
    {
        return slaughterRelationMapper.selectSlaughterRelationById(id);
    }

    /**
     * 查询血码关系列表
     * 
     * @param req 血码关系
     * @return 血码关系
     */
    @Override
    public List<SlaughterRelation> selectSlaughterRelationList(ReqSlaughterRelation req)
    {
        return slaughterRelationMapper.selectSlaughterRelationList(req);
    }

    /**
     * 导出血码关系列表
     *
     * @return 血码关系
     */
    @Override
    public List<SlaughterRelationDTO> exportSlaughterRelationList(ExportSlaughterRelation req)
    {
        return slaughterRelationMapper.exportSlaughterRelationList(req);
    }

    /**
     * 查询供应商血码数量列表
     *
     * @param req 查询条件
     * @return 供应商血码数量列表
     */
    @Override
    public List<RespGetSlaughterRelation> selectSupplierList(ReqGetSlaughterRelation req)
    {
        return slaughterRelationMapper.selectSupplierList(req);
    }

    /**
     * 校验血码是否唯一
     *
     * @param slaughterRelation 血码关系
     * @return 结果
     */
    @Override
    public boolean checkCodeUnique(SlaughterRelation slaughterRelation)
    {
        Long id = StringUtils.isNull(slaughterRelation.getId()) ? -1L : slaughterRelation.getId();
        SlaughterRelation info = slaughterRelationMapper.checkCodeUnique(slaughterRelation.getSlaughterCode());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 校验供应商的肉商是否唯一
     *
     * @param slaughterRelation 血码关系
     * @return 结果
     */
    @Override
    public SlaughterRelation checkPurchaserUnique(SlaughterRelation slaughterRelation)
    {
        return slaughterRelationMapper.checkPurchaserUnique(slaughterRelation);

    }


    /**
     * 新增血码关系
     * 
     * @param slaughterRelation 血码关系
     * @return 结果
     */
    @Override
    public int insertSlaughterRelation(SlaughterRelation slaughterRelation)
    {
        slaughterRelation.setCreateTime(DateUtils.getNowDate());
        return slaughterRelationMapper.insertSlaughterRelation(slaughterRelation);
    }

    /**
     * 批量新增血码关系
     *
     * @param list 血码关系
     * @return 结果
     */
    @Override
    public int insertSlaughterRelationBatch(List<SlaughterRelation> list)
    {

        return slaughterRelationMapper.insertSlaughterRelationBatch(list);
    }

    /**
     * 修改血码关系
     * 
     * @param slaughterRelation 血码关系
     * @return 结果
     */
    @Override
    public int updateSlaughterRelation(SlaughterRelation slaughterRelation)
    {
        slaughterRelation.setUpdateTime(DateUtils.getNowDate());
        return slaughterRelationMapper.updateSlaughterRelation(slaughterRelation);
    }

    /**
     * 批量删除血码关系
     * 
     * @param ids 需要删除的血码关系主键
     * @return 结果
     */
    @Override
    public int deleteSlaughterRelationByIds(Long[] ids)
    {
        for (Long codeId : ids)
        {
            SlaughterRelation slaughterRelation = selectSlaughterRelationById(codeId);
            if(slaughterRelation !=null){
                if (distributeBatchMapper.countBatchBySlaughterCode(slaughterRelation.getSlaughterCode()) > 0)
                {
                    throw new ServiceException("有关联分销批次，不能删除");
                }

                if (hookBindMapper.countHookBindBySlaughterCode(slaughterRelation.getSlaughterCode()) > 0)
                {
                    throw new ServiceException("有关联绑定吊钩，不能删除");
                }
            }
        }
        return slaughterRelationMapper.deleteSlaughterRelationByIds(ids);
    }

    /**
     * 删除血码关系信息
     * 
     * @param id 血码关系主键
     * @return 结果
     */
    @Override
    public int deleteSlaughterRelationById(Long id)
    {
        SlaughterRelation slaughterRelation = selectSlaughterRelationById(id);
        if(slaughterRelation !=null){
            if (distributeBatchMapper.countBatchBySlaughterCode(slaughterRelation.getSlaughterCode()) > 0)
            {
                throw new ServiceException("有关联分销批次，不能删除");
            }

            if (hookBindMapper.countHookBindBySlaughterCode(slaughterRelation.getSlaughterCode()) > 0)
            {
                throw new ServiceException("有关联绑定吊钩，不能删除");
            }
        }
        return slaughterRelationMapper.deleteSlaughterRelationById(id);
    }
}
