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

import java.util.List;
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.bodyManage.mapper.ScBodyMeasureMapper;
import com.zhyc.module.produce.bodyManage.domain.ScBodyMeasure;
import com.zhyc.module.produce.bodyManage.service.IScBodyMeasureService;

/**
 * 体尺测量Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class ScBodyMeasureServiceImpl implements IScBodyMeasureService 
{
    @Autowired
    private ScBodyMeasureMapper scBodyMeasureMapper;
    @Autowired
    private IBasSheepService basSheepService;
    /**
     * 查询体尺测量
     * 
     * @param id 体尺测量主键
     * @return 体尺测量
     */
    @Override
    public ScBodyMeasure selectScBodyMeasureById(Long id)
    {
        return scBodyMeasureMapper.selectScBodyMeasureById(id);
    }

    /**
     * 查询体尺测量列表
     * 
     * @param scBodyMeasure 体尺测量
     * @return 体尺测量
     */
    @Override
    public List<ScBodyMeasure> selectScBodyMeasureList(ScBodyMeasure scBodyMeasure)
    {
        return scBodyMeasureMapper.selectScBodyMeasureList(scBodyMeasure);
    }

    /**
     * 新增体尺测量
     */
    @Override
    public int insertScBodyMeasure(ScBodyMeasure scBodyMeasure) {
        String manageTags = scBodyMeasure.getManageTags();
        if (StringUtils.isNotBlank(manageTags)) {
            BasSheep sheep = basSheepService.selectBasSheepByManageTags(manageTags);
            if (sheep == null) {
                throw new RuntimeException("耳号不存在，请检查输入");
            }
            scBodyMeasure.setSheepId(sheep.getId());
        }

        if (scBodyMeasure.getCurrentWeight() != null) {
            BasSheep updateSheep = new BasSheep();
            updateSheep.setId(scBodyMeasure.getSheepId());
            updateSheep.setCurrentWeight(scBodyMeasure.getCurrentWeight());
            basSheepService.updateBasSheep(updateSheep);
        }

        scBodyMeasure.setCreateTime(DateUtils.getNowDate());
        scBodyMeasure.setCreateBy(SecurityUtils.getUsername());
        return scBodyMeasureMapper.insertScBodyMeasure(scBodyMeasure);
    }

    /**
     * 修改体尺测量
     */
    @Override
    public int updateScBodyMeasure(ScBodyMeasure scBodyMeasure) {
        String manageTags = scBodyMeasure.getManageTags();
        if (StringUtils.isNotBlank(manageTags)) {
            BasSheep sheep = basSheepService.selectBasSheepByManageTags(manageTags);
            if (sheep == null) {
                throw new RuntimeException("耳号不存在，请检查输入");
            }
            scBodyMeasure.setSheepId(sheep.getId());
        }

        if (scBodyMeasure.getCurrentWeight() != null) {
            BasSheep updateSheep = new BasSheep();
            updateSheep.setId(scBodyMeasure.getSheepId());
            updateSheep.setCurrentWeight(scBodyMeasure.getCurrentWeight());
            basSheepService.updateBasSheep(updateSheep);
        }

        return scBodyMeasureMapper.updateScBodyMeasure(scBodyMeasure);
    }

    /**
     * 批量删除体尺测量
     * 
     * @param ids 需要删除的体尺测量主键
     * @return 结果
     */
    @Override
    public int deleteScBodyMeasureByIds(Long[] ids)
    {
        return scBodyMeasureMapper.deleteScBodyMeasureByIds(ids);
    }

    /**
     * 删除体尺测量信息
     * 
     * @param id 体尺测量主键
     * @return 结果
     */
    @Override
    public int deleteScBodyMeasureById(Long id)
    {
        return scBodyMeasureMapper.deleteScBodyMeasureById(id);
    }
}
