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

import java.util.List;

import com.zhyc.common.annotation.DataScope;
import com.zhyc.common.exception.ServiceException;
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.mapper.BasSheepMapper;
import com.zhyc.module.base.service.IDaSheepfoldService;
import com.zhyc.module.produce.other.domain.ScFixHoof;
import com.zhyc.module.produce.other.mapper.ScFixHoofMapper;
import com.zhyc.module.produce.other.service.IScFixHoofService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 修蹄Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@Service
public class ScFixHoofServiceImpl implements IScFixHoofService {
    @Autowired
    private ScFixHoofMapper scFixHoofMapper;

    @Autowired
    private BasSheepMapper basSheepMapper;

    @Autowired
    private IDaSheepfoldService daSheepfoldService;

    /**
     * 查询修蹄
     *
     * @param id 修蹄主键
     * @return 修蹄
     */
    @Override
    public ScFixHoof selectScFixHoofById(Integer id) {
        return scFixHoofMapper.selectScFixHoofById(id);
    }

    /**
     * 查询修蹄列表
     *
     * @param scFixHoof 修蹄
     * @return 修蹄
     */
    @Override
    @DataScope(deptAlias = "fh", userAlias = "fh")
    public List<ScFixHoof> selectScFixHoofList(ScFixHoof scFixHoof) {
        return scFixHoofMapper.selectScFixHoofList(scFixHoof,
                scFixHoof.getManageTagsList());
    }


    /**
     * 新增修蹄
     *
     * @param scFixHoof 修蹄
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)  // ✅ 添加事务
    public int insertScFixHoof(ScFixHoof scFixHoof) {
        // ✅ 查羊只（已有）
        BasSheep sheep = basSheepMapper.selectBasSheepById(scFixHoof.getSheepId().longValue());
        if (sheep == null) {
            throw new ServiceException("该羊只ID不存在，请检查后再添加");
        }

        // ✅ 关键：设置 sheepfold 和 varietyId（你漏了！）
        scFixHoof.setSheepfold(sheep.getSheepfoldId().intValue());
        scFixHoof.setVarietyId(sheep.getVarietyId());

        scFixHoof.setCreateTime(DateUtils.getNowDate());
        scFixHoof.setCreateBy(SecurityUtils.getUsername());

        return scFixHoofMapper.insertScFixHoof(scFixHoof);
    }

    /**
     * 修改修蹄
     *
     * @param scFixHoof 修蹄
     * @return 结果
     */
    @Override
    public int updateScFixHoof(ScFixHoof scFixHoof) {
        return scFixHoofMapper.updateScFixHoof(scFixHoof);
    }

    /**
     * 批量删除修蹄
     *
     * @param ids 需要删除的修蹄主键
     * @return 结果
     */
    @Override
    public int deleteScFixHoofByIds(Integer[] ids) {
        return scFixHoofMapper.deleteScFixHoofByIds(ids);
    }

    /**
     * 删除修蹄信息
     *
     * @param id 修蹄主键
     * @return 结果
     */
    @Override
    public int deleteScFixHoofById(Integer id) {
        return scFixHoofMapper.deleteScFixHoofById(id);
    }


    /**
     * 根据管理耳号查询
     */
    @Override
    public Long findIdByManageTags(String manageTags) {
        if (StringUtils.isBlank(manageTags))
            throw new ServiceException("管理耳号不能为空");
        BasSheep sheep = basSheepMapper.selectBasSheepByManageTags(manageTags.trim());
        if (sheep == null)
            throw new ServiceException("管理耳号不存在：" + manageTags);
        return sheep.getId();
    }

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