package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.app.domain.DistributeBatch;
import com.ruoyi.app.domain.EntranceBatch;
import com.ruoyi.app.domain.InspectionItem;
import com.ruoyi.app.mapper.DistributeBatchMapper;
import com.ruoyi.app.mapper.EntranceBatchMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.HarmlessTreatmentMapper;
import com.ruoyi.app.domain.HarmlessTreatment;
import com.ruoyi.app.service.IHarmlessTreatmentService;

/**
 * 无害化处理Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class HarmlessTreatmentServiceImpl implements IHarmlessTreatmentService 
{
    @Autowired
    private HarmlessTreatmentMapper harmlessTreatmentMapper;

    @Autowired
    private EntranceBatchMapper entranceBatchMapper;

    @Autowired
    private DistributeBatchMapper distributeBatchMapper;

    /**
     * 查询无害化处理
     * 
     * @param id 无害化处理主键
     * @return 无害化处理
     */
    @Override
    public HarmlessTreatment selectHarmlessTreatmentById(Long id)
    {
        return harmlessTreatmentMapper.selectHarmlessTreatmentById(id);
    }

    /**
     * 查询无害化处理列表
     * 
     * @param harmlessTreatment 无害化处理
     * @return 无害化处理
     */
    @Override
    public List<HarmlessTreatment> selectHarmlessTreatmentList(HarmlessTreatment harmlessTreatment)
    {
        return harmlessTreatmentMapper.selectHarmlessTreatmentList(harmlessTreatment);
    }

    /**
     * 校验数量是否超出了入场数量
     *
     * @param harmlessTreatment 无害化处理
     * @return 无害化处理集合
     */
    public boolean checkTotalLessThanAmount(HarmlessTreatment harmlessTreatment){
        EntranceBatch entranceInfo = entranceBatchMapper.selectEntranceBatchById(harmlessTreatment.getEntranceBatchId());
        if(entranceInfo==null){
            return false;
        }
        DistributeBatch distributeBatch = new DistributeBatch();
        distributeBatch.setEntranceBatchId(harmlessTreatment.getEntranceBatchId());
        //已分销数量
        long hasDistribute = NumberUtil.getLongValue(distributeBatchMapper.getTotalByEntranceBatch(distributeBatch));
        int basic = (int) (NumberUtil.getIntValue(entranceInfo.getAmount()) - hasDistribute);
        //淘汰数量(病死+病害)应该 <= 入场数量-已分销数量
        int total = NumberUtil.getIntValue(harmlessTreatment.getIllnessDeadAmount())+
                NumberUtil.getIntValue(harmlessTreatment.getIllnessDiseaseAmount());
        return basic >= total;
    }

    /**
     * 新增无害化处理
     * 
     * @param harmlessTreatment 无害化处理
     * @return 结果
     */
    @Override
    public int insertHarmlessTreatment(HarmlessTreatment harmlessTreatment)
    {
        harmlessTreatment.setCreateTime(DateUtils.getNowDate());
        return harmlessTreatmentMapper.insertHarmlessTreatment(harmlessTreatment);
    }

    /**
     * 修改无害化处理
     * 
     * @param harmlessTreatment 无害化处理
     * @return 结果
     */
    @Override
    public int updateHarmlessTreatment(HarmlessTreatment harmlessTreatment)
    {
        harmlessTreatment.setUpdateTime(DateUtils.getNowDate());
        return harmlessTreatmentMapper.updateHarmlessTreatment(harmlessTreatment);
    }

    /**
     * 批量删除无害化处理
     * 
     * @param ids 需要删除的无害化处理主键
     * @return 结果
     */
    @Override
    public int deleteHarmlessTreatmentByIds(Long[] ids)
    {
        return harmlessTreatmentMapper.deleteHarmlessTreatmentByIds(ids);
    }

    /**
     * 删除无害化处理信息
     * 
     * @param id 无害化处理主键
     * @return 结果
     */
    @Override
    public int deleteHarmlessTreatmentById(Long id)
    {
        return harmlessTreatmentMapper.deleteHarmlessTreatmentById(id);
    }
}
