package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.OptionalLong;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.app.model.request.ReqSupplierList;
import com.ruoyi.app.mapper.DistributeBatchMapper;
import com.ruoyi.app.mapper.EntranceBatchMapper;
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.SupplierMapper;
import com.ruoyi.app.model.Supplier;
import com.ruoyi.app.service.ISupplierService;
import org.springframework.util.ObjectUtils;

/**
 * 供应商Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class SupplierServiceImpl implements ISupplierService 
{
    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private EntranceBatchMapper entranceBatchMapper;

    @Autowired
    private DistributeBatchMapper distributeBatchMapper;

    @Autowired
    private SlaughterRelationMapper slaughterRelationMapper;
    /**
     * 查询供应商
     * 
     * @param id 供应商主键
     * @return 供应商
     */
    @Override
    public Supplier selectSupplierById(Long id)
    {
        return supplierMapper.selectSupplierById(id);
    }

    /**
     * 查询供应商列表
     * 
     * @param req 供应商
     * @return 供应商
     */
    @Override
    public List<Supplier> selectSupplierList(ReqSupplierList req)
    {
        return supplierMapper.selectSupplierList(req);
    }

    /**
     * 校验供应商名称是否唯一
     *
     * @param supplier 供应商
     * @return 结果
     */
    @Override
    public boolean checkSupplierNameUnique(Supplier supplier)
    {
        Long supplierId = StringUtils.isNull(supplier.getId()) ? -1L : supplier.getId();
        Supplier info = supplierMapper.checkSupplierNameUnique(supplier.getSupplierName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != supplierId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 校验供应商编号是否唯一
     *
     * @param supplier 供应商
     * @return 结果
     */
    @Override
    public boolean checkSupplierNoUnique(Supplier supplier)
    {
        Long supplierId = StringUtils.isNull(supplier.getId()) ? -1L : supplier.getId();
        Supplier info = supplierMapper.checkSupplierNoUnique(supplier.getSupplierNo());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != supplierId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 查询全部供应商列表
     *
     *
     * @return 供应商
     */
    @Override
    public List<Supplier> selectAllSupplierList()
    {
        return supplierMapper.selectSupplierAll();
    }

    /**
     * 新增供应商
     * 
     * @param supplier 供应商
     * @return 结果
     */
    @Override
    public int insertSupplier(Supplier supplier)
    {
        supplier.setCreateTime(DateUtils.getNowDate());
        //如果没有输入，自动生成供应商编号
        if(ObjectUtils.isEmpty(supplier.getSupplierNo())) {
            supplier.setSupplierNo(getNextNo());
        }
        return supplierMapper.insertSupplier(supplier);
    }

    /**
     * 修改供应商
     * 
     * @param supplier 供应商
     * @return 结果
     */
    @Override
    public int updateSupplier(Supplier supplier)
    {
        supplier.setUpdateTime(DateUtils.getNowDate());
        return supplierMapper.updateSupplier(supplier);
    }

    /**
     * 批量删除供应商
     * 
     * @param ids 需要删除的供应商主键
     * @return 结果
     */
    @Override
    public int deleteSupplierByIds(Long[] ids)
    {
        for (Long supplierId : ids)
        {
            if(supplierId !=null){
                if ( entranceBatchMapper.countBatchBySupplier(supplierId) > 0)
                {
                    throw new ServiceException("有关联入场批次，不能删除");
                }

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

                if (slaughterRelationMapper.countSlaughterRelationBySupplier(supplierId) > 0)
                {
                    throw new ServiceException("有关联血码，不能删除");
                }
            }
        }
        return supplierMapper.deleteSupplierByIds(ids);
    }

    /**
     * 删除供应商信息
     * 
     * @param id 供应商主键
     * @return 结果
     */
    @Override
    public int deleteSupplierById(Long id)
    {
        if(id !=null){
            if ( entranceBatchMapper.countBatchBySupplier(id) > 0)
            {
                throw new ServiceException("有关联入场批次，不能删除");
            }

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

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

    @Override
    public String getNextNo() {
        List<Supplier> suppliers = supplierMapper.selectSupplierList(null);
        long number = 1L;
        if(!suppliers.isEmpty() && ObjectUtil.isNotEmpty(suppliers.get(0)) && ObjectUtil.isNotEmpty(suppliers.get(0).getSupplierNo())) {
            number = Long.parseLong(suppliers.get(0).getSupplierNo()) + 1;
        }
        return String.format("%02d", number);
    }
}
