package com.zhyc.module.produce.other.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.module.base.domain.BasSheep;
import com.zhyc.module.base.service.IBasSheepService;
import com.zhyc.module.produce.other.domain.ScCastrate;
import com.zhyc.module.produce.other.mapper.ScCastrateMapper;
import com.zhyc.module.produce.other.service.IScCastrateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 去势Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-09
 */
@Service
public class ScCastrateServiceImpl implements IScCastrateService {
    @Autowired
    private ScCastrateMapper scCastrateMapper;
    @Autowired
    private IBasSheepService basSheepService;

    /**
     * 查询去势
     *
     * @param id 去势主键
     * @return 去势
     */
    @Override
    public ScCastrate selectScCastrateById(Long id) {
        return scCastrateMapper.selectScCastrateById(id);
    }

    /**
     * 查询去势列表
     *
     * @param scCastrate 去势
     * @return 去势
     */
    @Override
    @DataScope(deptAlias = "sc", userAlias = "sc")
    public List<ScCastrate> selectScCastrateList(ScCastrate scCastrate) {
        return scCastrateMapper.selectScCastrateList(scCastrate,
                scCastrate.getManageTagsList());
    }

    /**
     * 新增去势
     *
     * @param scCastrate 去势
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertScCastrate(ScCastrate scCastrate) {
        // ✅ 第1步：先根据耳号查羊只（确保存在且获取完整信息）
        String manageTags = scCastrate.getManageTags();
        BasSheep sheep = basSheepService.selectBasSheepByManageTags(manageTags);
        if (sheep == null) {
            throw new RuntimeException("未找到耳号为【" + manageTags + "】的羊只");
        }

        // ✅ 第2步：设置 sheepId 和其他字段（关键！）
        scCastrate.setSheepId(String.valueOf(sheep.getId()));
        scCastrate.setVarietyId(sheep.getVarietyId());  // 品种ID也设置
        scCastrate.setSheepfold(sheep.getSheepfoldId()); // 羊舍ID也设置

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

        // ✅ 第3步：再执行 insert（此时所有字段都有值了）
        int result = scCastrateMapper.insertScCastrate(scCastrate);
        if (result <= 0) {
            return result;
        }

        // 第4步：更新羊只性别为去势（3）
        sheep.setGender(3L);
        basSheepService.updateBasSheep(sheep);

        return result;
    }

    /**
     * 修改去势
     *
     * @param scCastrate 去势
     * @return 结果
     */
    @Override
    public int updateScCastrate(ScCastrate scCastrate) {
        return scCastrateMapper.updateScCastrate(scCastrate);
    }

    /**
     * 批量删除去势
     *
     * @param ids 需要删除的去势主键
     * @return 结果
     */
    @Override
    public int deleteScCastrateByIds(Long[] ids) {
        return scCastrateMapper.deleteScCastrateByIds(ids);
    }

    /**
     * 删除去势信息
     *
     * @param id 去势主键
     * @return 结果
     */
    @Override
    public int deleteScCastrateById(Long id) {
        return scCastrateMapper.deleteScCastrateById(id);
    }


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