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

import java.util.List;

import com.zhyc.common.annotation.DataScope;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.utils.SecurityUtils;
import com.zhyc.common.utils.StringUtils;
import com.zhyc.module.base.domain.BasSheep;
import com.zhyc.module.base.service.IBasSheepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.produce.manage_sheep.mapper.ScChangeEarMapper;
import com.zhyc.module.produce.manage_sheep.domain.ScChangeEar;
import com.zhyc.module.produce.manage_sheep.service.IScChangeEarService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 修改电子耳号记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@Service
public class ScChangeEarServiceImpl implements IScChangeEarService
{
    @Autowired
    private ScChangeEarMapper scChangeEarMapper;

    @Autowired
    private IBasSheepService basSheepService;
    /**
     * 查询修改电子耳号记录
     *
     * @param id 修改电子耳号记录主键
     * @return 修改电子耳号记录
     */
    @Override
    public ScChangeEar selectScChangeEarById(Integer id) {
        ScChangeEar changeEar = scChangeEarMapper.selectScChangeEarById(id);
        if (changeEar != null && changeEar.getSheepId() != null) {
            System.out.println("当前要查询羊只信息的sheepId: " + changeEar.getSheepId());
            BasSheep sheep = basSheepService.selectBasSheepById(changeEar.getSheepId());
            System.out.println("查询到的羊只信息: " + sheep);
            if (sheep != null) {
                if (changeEar.getEarType() != null) {
                    if (changeEar.getEarType() == 0) {
                        changeEar.setOldTag(sheep.getElectronicTags());
                    } else {
                        changeEar.setOldTag(sheep.getManageTags());
                    }
                }
                changeEar.setManageTags(sheep.getManageTags());
            } else {
                System.out.println("根据sheepId: " + changeEar.getSheepId() + " 未查询到对应的羊只信息");
            }
        } else {
            System.out.println("changeEar为null或者sheepId为null");
        }
        System.out.println("设置旧耳号: " + changeEar.getOldTag());
        return changeEar;
    }

    /**
     * 查询修改电子耳号记录列表
     *
     * @param scChangeEar 修改电子耳号记录
     * @return 修改电子耳号记录
     */
    @Override
    @DataScope(deptAlias = "sce", userAlias = "sce")
    public List<ScChangeEar> selectScChangeEarList(ScChangeEar scChangeEar) {
        return scChangeEarMapper.selectScChangeEarList(scChangeEar,
                scChangeEar.getManageTagsList());
    }

    /**
     * 新增修改电子耳号记录
     *
     * @param scChangeEar 修改电子耳号记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertScChangeEar(ScChangeEar scChangeEar) {
        scChangeEar.setCreateTime(DateUtils.getNowDate());
        scChangeEar.setCreateBy(SecurityUtils.getUsername());
        if (StringUtils.isBlank(scChangeEar.getTechnician())) {
            throw new RuntimeException("技术员不能为空");
        }
        if (scChangeEar.getEventDate() == null) {
            throw new RuntimeException("事件日期不能为空");
        }
        if (scChangeEar.getSheepId() == null) {
            throw new RuntimeException("未找到对应的羊只ID");
        }

        BasSheep sheep = basSheepService.selectBasSheepById(scChangeEar.getSheepId());
        if (sheep == null) {
            throw new RuntimeException("未找到ID为【" + scChangeEar.getSheepId() + "】的羊只");
        }

        // 设置旧耳号为原始耳号的值
        if (scChangeEar.getEarType() == 0) {
            scChangeEar.setOldTag(sheep.getElectronicTags());
        } else if (scChangeEar.getEarType() == 1) {
            scChangeEar.setOldTag(sheep.getManageTags());
        } else {
            throw new RuntimeException("无效的耳号类型：" + scChangeEar.getEarType());
        }

        int rows = scChangeEarMapper.insertScChangeEar(scChangeEar);
        System.out.println(scChangeEar);
        if (rows <= 0) {
            return rows;
        }

        // 更新羊只基本信息表中的管理耳号或电子耳号
        if (scChangeEar.getEarType() == 0) {
            sheep.setElectronicTags(scChangeEar.getNewTag());
        } else if (scChangeEar.getEarType() == 1) {
            sheep.setManageTags(scChangeEar.getNewTag());
        }

        basSheepService.updateBasSheep(sheep);
        return rows;
    }

    /**
     * 修改修改电子耳号记录
     *
     * @param scChangeEar 修改电子耳号记录
     * @return 结果
     */
    @Override
    public int updateScChangeEar(ScChangeEar scChangeEar)
    {
        return scChangeEarMapper.updateScChangeEar(scChangeEar);
    }

    /**
     * 批量删除修改电子耳号记录
     *
     * @param ids 需要删除的修改电子耳号记录主键
     * @return 结果
     */
    @Override
    public int deleteScChangeEarByIds(Integer[] ids)
    {
        return scChangeEarMapper.deleteScChangeEarByIds(ids);
    }

    /**
     * 删除修改电子耳号记录信息
     *
     * @param id 修改电子耳号记录主键
     * @return 结果
     */
    @Override
    public int deleteScChangeEarById(Integer id)
    {
        return scChangeEarMapper.deleteScChangeEarById(id);
    }


    @Override
    public List<String> searchEarNumbers(String query) {
        return scChangeEarMapper.searchEarNumbers(query.trim());
    }
}
