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

import java.util.List;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.module.produce.breed.domain.ScWeanRecord;
import com.zhyc.module.produce.breed.mapper.ScWeanRecordMapper;
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 ruoyi
 * @date 2025-07-13
 */
@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) {
        // 如果前端传递的是耳号，需要先获取羊只ID
        if (scWeanRecord.getEarNumber() != null && scWeanRecord.getSheepId() == null) {
            Long sheepId = scWeanRecordMapper.selectSheepIdByEarNumber(scWeanRecord.getEarNumber());
            if (sheepId != null) {
                scWeanRecord.setSheepId(sheepId);
            }
        }
        scWeanRecord.setCreateTime(DateUtils.getNowDate());

        // 插入断奶记录
        int result = scWeanRecordMapper.insertScWeanRecord(scWeanRecord);

        // 同步更新bas_sheep表中的断奶信息
        if (result > 0 && scWeanRecord.getEarNumber() != null) {
            scWeanRecordMapper.updateBasSheepWeaningInfo(scWeanRecord);
        }

        return result;
    }

    /**
     * 修改断奶记录
     *
     * @param scWeanRecord 断奶记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScWeanRecord(ScWeanRecord scWeanRecord) {
        // 如果前端传递的是耳号，需要先获取羊只ID
        if (scWeanRecord.getEarNumber() != null && scWeanRecord.getSheepId() == null) {
            Long sheepId = scWeanRecordMapper.selectSheepIdByEarNumber(scWeanRecord.getEarNumber());
            if (sheepId != null) {
                scWeanRecord.setSheepId(sheepId);
            }
        }

        // 更新断奶记录
        int result = scWeanRecordMapper.updateScWeanRecord(scWeanRecord);

        // 同步更新bas_sheep表中的断奶信息
        if (result > 0 && scWeanRecord.getEarNumber() != null) {
            scWeanRecordMapper.updateBasSheepWeaningInfo(scWeanRecord);
        }

        return result;
    }

    /**
     * 批量删除断奶记录
     *
     * @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
     *
     * @param earNumber 耳号
     * @return 羊只ID
     */
    @Override
    public Long selectSheepIdByEarNumber(String earNumber) {
        return scWeanRecordMapper.selectSheepIdByEarNumber(earNumber);
    }
}