package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.app.domain.Supplier;
import com.ruoyi.app.domain.request.ReqPurchaser;
import com.ruoyi.app.mapper.DistributeBatchMapper;
import com.ruoyi.app.mapper.SlaughterRelationMapper;
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.PurchaserMapper;
import com.ruoyi.app.domain.Purchaser;
import com.ruoyi.app.service.IPurchaserService;

/**
 * 肉商Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class PurchaserServiceImpl implements IPurchaserService 
{
    @Autowired
    private PurchaserMapper purchaserMapper;

    @Autowired
    private DistributeBatchMapper distributeBatchMapper;

    @Autowired
    private SlaughterRelationMapper slaughterRelationMapper;
    /**
     * 查询肉商
     * 
     * @param id 肉商主键
     * @return 肉商
     */
    @Override
    public Purchaser selectPurchaserById(Long id)
    {
        return purchaserMapper.selectPurchaserById(id);
    }

    /**
     * 查询肉商列表
     * 
     * @param purchaser 肉商
     * @return 肉商
     */
    @Override
    public List<Purchaser> selectPurchaserList(ReqPurchaser purchaser)
    {
        return purchaserMapper.selectPurchaserList(purchaser);
    }

    /**
     * 校验肉商名称是否唯一
     *
     * @param purchaser 肉商
     * @return 结果
     */
    @Override
    public boolean checkPurchaserNameUnique(Purchaser purchaser)
    {
        Long purchaserId = StringUtils.isNull(purchaser.getId()) ? -1L : purchaser.getId();
        Purchaser info = purchaserMapper.checkPurchaserNameUnique(purchaser.getPurchaserName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != purchaserId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 校验肉商身份证号是否唯一
     *
     * @param purchaser 肉商
     * @return 结果
     */
    @Override
    public boolean checkIDNumberUnique(Purchaser purchaser)
    {
        Long purchaserId = StringUtils.isNull(purchaser.getId()) ? -1L : purchaser.getId();
        if(purchaser.getIdNumber() != null && purchaser.getIdNumber().length() == 18){
            Purchaser info = purchaserMapper.checkIDNumberUnique(purchaser.getIdNumber());
            if (StringUtils.isNotNull(info) && info.getId().longValue() != purchaserId.longValue())
            {
                return false;
            }
        }

        return true;
    }

    /**
     * 校验肉商编号是否唯一
     *
     * @param purchaser 肉商
     * @return 结果
     */
    @Override
    public boolean checkPurchaserNoUnique(Purchaser purchaser)
    {
        Long purchaserId = StringUtils.isNull(purchaser.getId()) ? -1L : purchaser.getId();
        Purchaser info = purchaserMapper.checkPurchaserNoUnique(purchaser.getPurchaserNo());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != purchaserId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 查询全部肉商
     *
     *
     * @return 肉商
     */
    @Override
    public List<Purchaser> selectAllPurchaserList()
    {
        return purchaserMapper.selectPurchaserAll();
    }

    /**
     * 新增肉商
     * 
     * @param purchaser 肉商
     * @return 结果
     */
    @Override
    public int insertPurchaser(Purchaser purchaser)
    {
        purchaser.setCreateTime(DateUtils.getNowDate());
        return purchaserMapper.insertPurchaser(purchaser);
    }

    /**
     * 修改肉商
     * 
     * @param purchaser 肉商
     * @return 结果
     */
    @Override
    public int updatePurchaser(Purchaser purchaser)
    {
        purchaser.setUpdateTime(DateUtils.getNowDate());
        return purchaserMapper.updatePurchaser(purchaser);
    }

    /**
     * 批量删除肉商
     * 
     * @param ids 需要删除的肉商主键
     * @return 结果
     */
    @Override
    public int deletePurchaserByIds(Long[] ids)
    {
        for (Long purchaserId : ids)
        {
            if(purchaserId !=null){

                if (distributeBatchMapper.countBatchByPurchaser(purchaserId) > 0)
                {
                    throw new ServiceException("有关联分销批次，不能删除");
                }

                if (slaughterRelationMapper.countSlaughterRelationByPurchaser(purchaserId) > 0)
                {
                    throw new ServiceException("有关联血码，不能删除");
                }
            }
        }
        return purchaserMapper.deletePurchaserByIds(ids);
    }

    /**
     * 删除肉商信息
     * 
     * @param id 肉商主键
     * @return 结果
     */
    @Override
    public int deletePurchaserById(Long id)
    {
        if(id !=null){
            if (distributeBatchMapper.countBatchByPurchaser(id) > 0)
            {
                throw new ServiceException("有关联分销批次，不能删除");
            }

            if (slaughterRelationMapper.countSlaughterRelationByPurchaser(id) > 0)
            {
                throw new ServiceException("有关联血码，不能删除");
            }
        }
        return purchaserMapper.deletePurchaserById(id);
    }
}
