package com.zhyc.module.produce.breed.service.impl;

import java.util.List;
import java.util.Map;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.module.biosafety.domain.Diagnosis;
import com.zhyc.module.biosafety.domain.Treatment;
import com.zhyc.module.biosafety.mapper.DiagnosisMapper;
import com.zhyc.module.biosafety.mapper.TreatmentMapper;
import com.zhyc.module.biosafety.service.impl.TreatmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.produce.breed.mapper.ScSheepDeathMapper;
import com.zhyc.module.produce.breed.domain.ScSheepDeath;
import com.zhyc.module.produce.breed.service.IScSheepDeathService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 羊只死淘记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class ScSheepDeathServiceImpl implements IScSheepDeathService
{
    @Autowired
    private ScSheepDeathMapper scSheepDeathMapper;
    @Autowired
    private DiagnosisMapper diagnosisMapper;
    @Autowired
    private TreatmentServiceImpl treatmentService;




    /**
     * 查询羊只死淘记录
     *
     * @param id 羊只死淘记录主键
     * @return 羊只死淘记录
     */
    @Override
    public ScSheepDeath selectScSheepDeathById(Long id)
    {
        ScSheepDeath scSheepDeath = scSheepDeathMapper.selectScSheepDeathById(id);
        // 查询时也需要填充显示字段
        if (scSheepDeath != null && scSheepDeath.getManageTags() != null) {
            Map<String, Object> sheepInfo = selectSheepFileByManageTags(scSheepDeath.getManageTags());
            if (sheepInfo != null) {
                scSheepDeath.setVariety(sheepInfo.get("variety") != null ? sheepInfo.get("variety").toString() : null);
                scSheepDeath.setSheepType(sheepInfo.get("sheepType") != null ? sheepInfo.get("sheepType").toString() : null);
                scSheepDeath.setGender(sheepInfo.get("gender") != null ? Integer.valueOf(sheepInfo.get("gender").toString()) : null);
                scSheepDeath.setDayAge(sheepInfo.get("dayAge") != null ? Long.valueOf(sheepInfo.get("dayAge").toString()) : null);
                scSheepDeath.setParity(sheepInfo.get("parity") != null ? Integer.valueOf(sheepInfo.get("parity").toString()) : null);
                scSheepDeath.setSheepfoldName(sheepInfo.get("sheepfoldName") != null ? sheepInfo.get("sheepfoldName").toString() : null);
                scSheepDeath.setBreedStatus(sheepInfo.get("breedStatus") != null ? sheepInfo.get("breedStatus").toString() : null);
                scSheepDeath.setPostLambingDay(sheepInfo.get("postLambingDay") != null ? Integer.valueOf(sheepInfo.get("postLambingDay").toString()) : null);
                scSheepDeath.setLactationDay(sheepInfo.get("lactationDay") != null ? Integer.valueOf(sheepInfo.get("lactationDay").toString()) : null);
                scSheepDeath.setGestationDay(sheepInfo.get("gestationDay") != null ? Integer.valueOf(sheepInfo.get("gestationDay").toString()) : null);
            }
        }
        return scSheepDeath;
    }

    /**
     * 查询羊只死淘记录列表
     *
     * @param scSheepDeath 羊只死淘记录
     * @return 羊只死淘记录
     */
    @Override
    public List<ScSheepDeath> selectScSheepDeathList(ScSheepDeath scSheepDeath)
    {
        List<ScSheepDeath> list = scSheepDeathMapper.selectScSheepDeathList(scSheepDeath);
        // 为列表中的每条记录填充显示字段
        for (ScSheepDeath death : list) {
            if (death.getManageTags() != null) {
                Map<String, Object> sheepInfo = selectSheepFileByManageTags(death.getManageTags());
                if (sheepInfo != null) {
                    death.setVariety(sheepInfo.get("variety") != null ? sheepInfo.get("variety").toString() : null);
                    death.setSheepType(sheepInfo.get("sheepType") != null ? sheepInfo.get("sheepType").toString() : null);
                    death.setGender(sheepInfo.get("gender") != null ? Integer.valueOf(sheepInfo.get("gender").toString()) : null);
                    death.setDayAge(sheepInfo.get("dayAge") != null ? Long.valueOf(sheepInfo.get("dayAge").toString()) : null);
                    death.setParity(sheepInfo.get("parity") != null ? Integer.valueOf(sheepInfo.get("parity").toString()) : null);
                    death.setSheepfoldName(sheepInfo.get("sheepfoldName") != null ? sheepInfo.get("sheepfoldName").toString() : null);
                    death.setBreedStatus(sheepInfo.get("breedStatus") != null ? sheepInfo.get("breedStatus").toString() : null);
                    death.setPostLambingDay(sheepInfo.get("postLambingDay") != null ? Integer.valueOf(sheepInfo.get("postLambingDay").toString()) : null);
                    death.setLactationDay(sheepInfo.get("lactationDay") != null ? Integer.valueOf(sheepInfo.get("lactationDay").toString()) : null);
                    death.setGestationDay(sheepInfo.get("gestationDay") != null ? Integer.valueOf(sheepInfo.get("gestationDay").toString()) : null);
                }
            }
        }
        return list;
    }

    /**
     * 根据管理耳号查询sheep_file视图信息
     *
     * @param manageTags 管理耳号
     * @return 羊只信息
     */
    @Override
    public Map<String, Object> selectSheepFileByManageTags(String manageTags)
    {
        return scSheepDeathMapper.selectSheepFileByManageTags(manageTags);
    }

    /**
     * 新增羊只死淘记录
     *
     * @param scSheepDeath 羊只死淘记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScSheepDeath(ScSheepDeath scSheepDeath)
    {
        // 设置事件类型默认为"死亡"
        if (scSheepDeath.getEventType() == null || scSheepDeath.getEventType().isEmpty()) {
            scSheepDeath.setEventType("死亡");
        }

        // 如果有管理耳号，查询并设置羊只ID
        if (scSheepDeath.getManageTags() != null && !scSheepDeath.getManageTags().isEmpty()) {
            Map<String, Object> sheepInfo = selectSheepFileByManageTags(scSheepDeath.getManageTags());
            if (sheepInfo != null) {
                Long sheepId = sheepInfo.get("sheepId") != null ? Long.valueOf(sheepInfo.get("sheepId").toString()) : null;
                scSheepDeath.setSheepId(sheepId);

                // 插入死淘记录后，同时更新羊只在群状态为"不在群"（状态ID为2）
                if (sheepId != null) {
                    scSheepDeathMapper.updateSheepStatus(sheepId, "2");
                }
            }
        }

        treatmentService.updateTreatmentStatus(scSheepDeath.getSheepId());

        scSheepDeath.setCreateTime(DateUtils.getNowDate());

        return scSheepDeathMapper.insertScSheepDeath(scSheepDeath);
    }

    /**
     * 修改羊只死淘记录
     *
     * @param scSheepDeath 羊只死淘记录
     * @return 结果
     */
    @Override
    public int updateScSheepDeath(ScSheepDeath scSheepDeath)
    {
        // 如果管理耳号发生变化，重新查询并设置羊只ID
        if (scSheepDeath.getManageTags() != null && !scSheepDeath.getManageTags().isEmpty()) {
            Map<String, Object> sheepInfo = selectSheepFileByManageTags(scSheepDeath.getManageTags());
            if (sheepInfo != null) {
                Long sheepId = sheepInfo.get("sheepId") != null ? Long.valueOf(sheepInfo.get("sheepId").toString()) : null;
                scSheepDeath.setSheepId(sheepId);

                // 修改死淘记录时，同时更新羊只在群状态为"不在群"（状态ID为2）
                if (sheepId != null) {
                    scSheepDeathMapper.updateSheepStatus(sheepId, "2");
                }
            }
        }
        scSheepDeath.setUpdateTime(DateUtils.getNowDate());
        return scSheepDeathMapper.updateScSheepDeath(scSheepDeath);
    }

    /**
     * 批量删除羊只死淘记录
     *
     * @param ids 需要删除的羊只死淘记录主键
     * @return 结果
     */
    @Override
    public int deleteScSheepDeathByIds(Long[] ids)
    {
        // 删除死淘记录前，将对应羊只在群状态改回"在群"
        for (Long id : ids) {
            ScSheepDeath scSheepDeath = scSheepDeathMapper.selectScSheepDeathById(id);
            if (scSheepDeath != null && scSheepDeath.getSheepId() != null) {
                // 恢复羊只在群状态为"在群"（状态ID为1）
                scSheepDeathMapper.updateSheepStatus(scSheepDeath.getSheepId(), "1");
            }
        }

        return scSheepDeathMapper.deleteScSheepDeathByIds(ids);
    }

    /**
     * 删除羊只死淘记录信息
     *
     * @param id 羊只死淘记录主键
     * @return 结果
     */
    @Override
    public int deleteScSheepDeathById(Long id)
    {
        // 删除死淘记录前，将对应羊只在群状态改回"在群"
        ScSheepDeath scSheepDeath = scSheepDeathMapper.selectScSheepDeathById(id);
        if (scSheepDeath != null && scSheepDeath.getSheepId() != null) {
            // 恢复羊只在群状态为"在群"（状态ID为1）
            scSheepDeathMapper.updateSheepStatus(scSheepDeath.getSheepId(), "1");
        }

        return scSheepDeathMapper.deleteScSheepDeathById(id);
    }
}