package com.ruoyi.app.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.app.model.DistributeBatch;
import com.ruoyi.app.model.EntranceBatch;
import com.ruoyi.app.model.PigCategory;
import com.ruoyi.app.mapper.DistributeBatchMapper;
import com.ruoyi.app.mapper.EntranceBatchMapper;
import com.ruoyi.app.mapper.PigCategoryMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.HarmlessTreatmentMapper;
import com.ruoyi.app.model.HarmlessTreatment;
import com.ruoyi.app.service.IHarmlessTreatmentService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 无害化处理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;

    @Resource
    private PigCategoryMapper pigCategoryMapper;

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

    @Override
    public List<Map<String, String>> selectHarmlessDetail(Long id) {

        EntranceBatch entranceInfo = entranceBatchMapper.selectEntranceBatchById(id);

        if(!ObjectUtils.isEmpty(entranceInfo) && !(ObjectUtils.isEmpty(entranceInfo.getAnimalEartags()))) {
            int defaultWeight = 0;
            PigCategory pigCategory = new PigCategory();
            pigCategory.setCategoryName(entranceInfo.getVariety());
            List<PigCategory> categoryList = pigCategoryMapper.selectPigCategoryList(pigCategory);
            if(!ObjectUtils.isEmpty(categoryList)) {
                defaultWeight = Integer.parseInt(categoryList.get(0).getDefaultWeight());
            }
            String[] ears = entranceInfo.getAnimalEartags().split(",");
            String finalDefaultWeight = String.valueOf(defaultWeight);
            return Collections.singletonList(Arrays.stream(ears).collect(Collectors.toMap(
                    ear -> ear, ear -> finalDefaultWeight, (oldVal, newVal) -> newVal))
            );
        }
        return null;
    }

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

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