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

import java.util.List;

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;

/**
 * 去势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
    public List<ScCastrate> selectScCastrateList(ScCastrate scCastrate) {
        return scCastrateMapper.selectScCastrateList(scCastrate);
    }

    /**
     * 新增去势
     *
     * @param scCastrate 去势
     * @return 结果
     */
    @Override
    public int insertScCastrate(ScCastrate scCastrate) {
        scCastrate.setCreateTime(DateUtils.getNowDate());
        scCastrate.setCreateBy(SecurityUtils.getUsername());
        int result = scCastrateMapper.insertScCastrate(scCastrate);

        if (result > 0 && scCastrate.getSheepId() != null) {
            try {
                BasSheep sheep = new BasSheep();
                sheep.setId(Long.parseLong(scCastrate.getSheepId()));
                sheep.setGender(3L);
                basSheepService.updateBasSheep(sheep);
            } catch (Exception e) {
                throw new RuntimeException("去势成功，但更新羊只性别失败，请重试");
            }
        }

        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);
    }
}
