package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.app.domain.Pigpen;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.HookMapper;
import com.ruoyi.app.domain.Hook;
import com.ruoyi.app.service.IHookService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 吊钩Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class HookServiceImpl implements IHookService 
{
    @Autowired
    private HookMapper hookMapper;

    /**
     * 查询吊钩
     * 
     * @param id 吊钩主键
     * @return 吊钩
     */
    @Override
    public Hook selectHookById(Long id)
    {
        return hookMapper.selectHookById(id);
    }

    /**
     * 查询吊钩列表
     * 
     * @param hook 吊钩
     * @return 吊钩
     */
    @Override
    public List<Hook> selectHookList(Hook hook)
    {
        return hookMapper.selectHookList(hook);
    }

    /**
     * 校验吊钩名称是否唯一
     *
     * @param hook 吊钩
     * @return 结果
     */
    @Override
    public boolean checkHookNameUnique(Hook hook)
    {
        Long hookId = StringUtils.isNull(hook.getId()) ? -1L : hook.getId();
        Hook info = hookMapper.checkHookNameUnique(hook.getHookName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != hookId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 新增吊钩
     * 
     * @param hook 吊钩
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertHook(Hook hook)
    {
        hook.setCreateTime(DateUtils.getNowDate());
        //是否设置为默认吊钩
        if (hook.getDefaultFlag()){
            //将其他吊钩设置为非默认
            hookMapper.updateHookDefault();
        }
        return hookMapper.insertHook(hook);
    }

    /**
     * 修改吊钩
     * 
     * @param hook 吊钩
     * @return 结果
     */
    @Override
    public int updateHook(Hook hook)
    {
        hook.setUpdateTime(DateUtils.getNowDate());
        //是否设置为默认吊钩
        if (hook.getDefaultFlag()){
            //将其他吊钩设置为非默认
            hookMapper.updateHookDefault();
        }
        return hookMapper.updateHook(hook);
    }

    /**
     * 批量删除吊钩
     * 
     * @param ids 需要删除的吊钩主键
     * @return 结果
     */
    @Override
    public int deleteHookByIds(Long[] ids)
    {
        return hookMapper.deleteHookByIds(ids);
    }

    /**
     * 删除吊钩信息
     * 
     * @param id 吊钩主键
     * @return 结果
     */
    @Override
    public int deleteHookById(Long id)
    {
        return hookMapper.deleteHookById(id);
    }
}
