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

import java.util.Date;
import java.util.List;

import com.zhyc.common.annotation.DataScope;
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.ScChangeCommentMapper;
import com.zhyc.module.produce.manage_sheep.domain.ScChangeComment;
import com.zhyc.module.produce.manage_sheep.service.IScChangeCommentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 改备注Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-24
 */
@Service
public class ScChangeCommentServiceImpl implements IScChangeCommentService 
{
    @Autowired
    private ScChangeCommentMapper scChangeCommentMapper;

    @Autowired
    private IBasSheepService basSheepService;
    /**
     * 查询改备注
     * 
     * @param id 改备注主键
     * @return 改备注
     */
    @Override
    public ScChangeComment selectScChangeCommentById(Long id)
    {
        return scChangeCommentMapper.selectScChangeCommentById(id);
    }

    /**
     * 查询改备注列表
     * 
     * @param scChangeComment 改备注
     * @return 改备注
     */
    @Override
    @DataScope(deptAlias = "scc", userAlias = "scc")
    public List<ScChangeComment> selectScChangeCommentList(ScChangeComment scChangeComment)
    {
        // 把实体和独立参数一起传过去
        return scChangeCommentMapper.selectScChangeCommentList(
                scChangeComment,
                scChangeComment.getManageTagsList());
    }
    @Override
    public List<String> searchEarNumbers(String query) {
        return scChangeCommentMapper.searchEarNumbers(query);
    }
    /**
     * 新增改备注
     * 
     * @param scChangeComment 改备注
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertScChangeComment(ScChangeComment scChangeComment)
    {
        if (StringUtils.isBlank(scChangeComment.getTechnician())) {
            throw new RuntimeException("技术员不能为空");
        }
        if (scChangeComment.getEventDate() == null) {
            throw new RuntimeException("事件日期不能为空");
        }

        String manageTags = scChangeComment.getManageTags();
        BasSheep sheep = basSheepService.selectBasSheepByManageTags(manageTags);
        if (sheep == null) {
            throw new RuntimeException("未找到耳号为【" + manageTags + "】的羊只，无法更新备注");
        }

        scChangeComment.setSheepId(String.valueOf(sheep.getId()));
        scChangeComment.setOldComment(sheep.getComment());
        scChangeComment.setCreateTime(new Date());
        scChangeComment.setCreateBy(SecurityUtils.getUsername());

        int rows = scChangeCommentMapper.insertScChangeComment(scChangeComment);
        if (rows <= 0) {
            return rows;
        }

        sheep.setComment(scChangeComment.getNewComment());
        basSheepService.updateBasSheep(sheep);

        return rows;
    }

    /**
     * 修改改备注
     * 
     * @param scChangeComment 改备注
     * @return 结果
     */
    @Override
    public int updateScChangeComment(ScChangeComment scChangeComment)
    {
        return scChangeCommentMapper.updateScChangeComment(scChangeComment);
    }

    /**
     * 批量删除改备注
     * 
     * @param ids 需要删除的改备注主键
     * @return 结果
     */
    @Override
    public int deleteScChangeCommentByIds(Long[] ids)
    {
        return scChangeCommentMapper.deleteScChangeCommentByIds(ids);
    }

    /**
     * 删除改备注信息
     * 
     * @param id 改备注主键
     * @return 结果
     */
    @Override
    public int deleteScChangeCommentById(Long id)
    {
        return scChangeCommentMapper.deleteScChangeCommentById(id);
    }


}
