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

import java.util.List;
import com.zhyc.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.produce.breed.mapper.ScWeanRecordMapper;
import com.zhyc.module.produce.breed.domain.ScWeanRecord;
import com.zhyc.module.produce.breed.service.IScWeanRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 断奶记录Service业务层处理
 * * @author zhyc
 * @date 2024-01-01
 */
@Service
public class ScWeanRecordServiceImpl implements IScWeanRecordService
{
    @Autowired
    private ScWeanRecordMapper scWeanRecordMapper;

    /**
     * 查询断奶记录
     * * @param id 断奶记录主键
     * @return 断奶记录
     */
    @Override
    public ScWeanRecord selectScWeanRecordById(Long id)
    {
        return scWeanRecordMapper.selectScWeanRecordById(id);
    }

    /**
     * 查询断奶记录列表
     * * @param scWeanRecord 断奶记录
     * @return 断奶记录
     */
    @Override
    public List<ScWeanRecord> selectScWeanRecordList(ScWeanRecord scWeanRecord)
    {
        return scWeanRecordMapper.selectScWeanRecordList(scWeanRecord);
    }

    /**
     * 新增断奶记录
     * * @param scWeanRecord 断奶记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScWeanRecord(ScWeanRecord scWeanRecord)
    {
        scWeanRecord.setCreateTime(DateUtils.getNowDate());
        // 1. 插入断奶记录
        int rows = scWeanRecordMapper.insertScWeanRecord(scWeanRecord);

        // 2. 同步更新羊只档案中的断奶状态（断奶日期、断奶重、电子耳号等）
        // 确保 scWeanRecord 中包含 earNumber (Controller中add方法校验过)
        if (rows > 0 && scWeanRecord.getEarNumber() != null) {
            scWeanRecordMapper.updateBasSheepWeaningInfo(scWeanRecord);
        }
        return rows;
    }

    /**
     * 修改断奶记录
     * * @param scWeanRecord 断奶记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScWeanRecord(ScWeanRecord scWeanRecord)
    {
        // 1. 更新断奶记录
        int rows = scWeanRecordMapper.updateScWeanRecord(scWeanRecord);

        // 2. 同步更新羊只档案信息
        if (rows > 0 && scWeanRecord.getEarNumber() != null) {
            scWeanRecordMapper.updateBasSheepWeaningInfo(scWeanRecord);
        }
        return rows;
    }

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

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

    /**
     * 根据耳号查询羊只ID
     */
    @Override
    public Long selectSheepIdByEarNumber(String earNumber) {
        return scWeanRecordMapper.selectSheepIdByEarNumber(earNumber);
    }

    /**
     * 【新增】模糊查询耳号列表 (用于前端远程搜索)
     * @param query 查询关键字
     * @return 耳号列表
     */
    @Override
    public List<String> searchEarNumbers(String query) {
        return scWeanRecordMapper.searchEarNumbers(query);
    }
}