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

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.produce.breed.mapper.ScDryMilkMapper;
import com.zhyc.module.produce.breed.domain.ScDryMilk;
import com.zhyc.module.produce.breed.service.IScDryMilkService;
import com.zhyc.common.utils.StringUtils;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.exception.ServiceException;

/**
 * 干奶记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class ScDryMilkServiceImpl implements IScDryMilkService
{
    @Autowired
    private ScDryMilkMapper scDryMilkMapper;

    /**
     * 查询干奶记录
     *
     * @param id 干奶记录主键
     * @return 干奶记录
     */
    @Override
    public ScDryMilk selectScDryMilkById(Long id)
    {
        return scDryMilkMapper.selectScDryMilkById(id);
    }

    /**
     * 查询干奶记录列表
     *
     * @param scDryMilk 干奶记录
     * @return 干奶记录
     */
    @Override
    public List<ScDryMilk> selectScDryMilkList(ScDryMilk scDryMilk)
    {
        return scDryMilkMapper.selectScDryMilkList(scDryMilk);
    }

    /**
     * 根据耳号查询羊只ID
     *
     * @param manageTags 管理耳号
     * @return 羊只ID
     */
    @Override
    public Long selectSheepIdByManageTags(String manageTags)
    {
        return scDryMilkMapper.selectSheepIdByManageTags(manageTags);
    }

    /**
     * 新增干奶记录
     *
     * @param scDryMilk 干奶记录
     * @return 结果
     */
    @Override
    public int insertScDryMilk(ScDryMilk scDryMilk)
    {
        // 如果传入的是耳号，需要转换为羊只ID
        if (StringUtils.isNotEmpty(scDryMilk.getManageTags()) && StringUtils.isEmpty(scDryMilk.getSheepId()))
        {
            Long sheepId = scDryMilkMapper.selectSheepIdByManageTags(scDryMilk.getManageTags());
            if (sheepId == null)
            {
                throw new ServiceException("未找到对应耳号的羊只信息");
            }
            scDryMilk.setSheepId(String.valueOf(sheepId));
        }

        // 自动设置创建时间（精确到分钟）
        scDryMilk.setCreateTime(DateUtils.getNowDate());
        // 如果有获取当前用户的方法，可以设置创建人
        // scDryMilk.setCreateBy(SecurityUtils.getUsername());

        return scDryMilkMapper.insertScDryMilk(scDryMilk);
    }

    /**
     * 修改干奶记录
     *
     * @param scDryMilk 干奶记录
     * @return 结果
     */
    @Override
    public int updateScDryMilk(ScDryMilk scDryMilk)
    {
        // 如果传入的是耳号，需要转换为羊只ID
        if (StringUtils.isNotEmpty(scDryMilk.getManageTags()) && StringUtils.isEmpty(scDryMilk.getSheepId()))
        {
            Long sheepId = scDryMilkMapper.selectSheepIdByManageTags(scDryMilk.getManageTags());
            if (sheepId == null)
            {
                throw new ServiceException("未找到对应耳号的羊只信息");
            }
            scDryMilk.setSheepId(String.valueOf(sheepId));
        }

        // 设置更新时间
        scDryMilk.setUpdateTime(DateUtils.getNowDate());
        // 如果有获取当前用户的方法，可以设置更新人
        // scDryMilk.setUpdateBy(SecurityUtils.getUsername());

        return scDryMilkMapper.updateScDryMilk(scDryMilk);
    }

    /**
     * 批量删除干奶记录
     *
     * @param ids 需要删除的干奶记录主键
     * @return 结果
     */
    @Override
    public int deleteScDryMilkByIds(Long[] ids)
    {
        return scDryMilkMapper.deleteScDryMilkByIds(ids);
    }

    /**
     * 删除干奶记录信息
     *
     * @param id 干奶记录主键
     * @return 结果
     */
    @Override
    public int deleteScDryMilkById(Long id)
    {
        return scDryMilkMapper.deleteScDryMilkById(id);
    }
}