package com.ruoyi.tuya.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tuya.domain.TuyaCategoryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tuya.mapper.TuyaChannelInfoMapper;
import com.ruoyi.tuya.domain.TuyaChannelInfo;
import com.ruoyi.tuya.service.ITuyaChannelInfoService;

/**
 * 模块信息Service业务层处理
 *
 * @author zf
 * @date 2022-07-06
 */
@Service
public class TuyaChannelInfoServiceImpl implements ITuyaChannelInfoService
{
    @Autowired
    private TuyaChannelInfoMapper tuyaChannelInfoMapper;

    @Override
    public String checkChannelNameUnique(TuyaChannelInfo tuyaChannelInfo) {
        Long channelId = StringUtils.isNull(tuyaChannelInfo.getChannelId()) ? -1L : tuyaChannelInfo.getChannelId();
        TuyaChannelInfo info = tuyaChannelInfoMapper.checkChannelNameUnique(tuyaChannelInfo);
        if (StringUtils.isNotNull(info) && info.getChannelId() != channelId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询模块信息
     *
     * @param channelId 模块信息主键
     * @return 模块信息
     */
    @Override
    public TuyaChannelInfo selectTuyaChannelInfoByChannelId(Long channelId)
    {
        return tuyaChannelInfoMapper.selectTuyaChannelInfoByChannelId(channelId);
    }

    /**
     * 查询模块信息列表
     *
     * @param tuyaChannelInfo 模块信息
     * @return 模块信息
     */
    @Override
    public List<TuyaChannelInfo> selectTuyaChannelInfoList(TuyaChannelInfo tuyaChannelInfo)
    {
        return tuyaChannelInfoMapper.selectTuyaChannelInfoList(tuyaChannelInfo);
    }

    /**
     * 新增模块信息
     *
     * @param tuyaChannelInfo 模块信息
     * @return 结果
     */
    @Override
    public int insertTuyaChannelInfo(TuyaChannelInfo tuyaChannelInfo)
    {
        tuyaChannelInfo.setCreateTime(DateUtils.getNowDate());
        return tuyaChannelInfoMapper.insertTuyaChannelInfo(tuyaChannelInfo);
    }

    /**
     * 修改模块信息
     *
     * @param tuyaChannelInfo 模块信息
     * @return 结果
     */
    @Override
    public int updateTuyaChannelInfo(TuyaChannelInfo tuyaChannelInfo)
    {
        tuyaChannelInfo.setUpdateTime(DateUtils.getNowDate());
        return tuyaChannelInfoMapper.updateTuyaChannelInfo(tuyaChannelInfo);
    }

    /**
     * 批量删除模块信息
     *
     * @param channelIds 需要删除的模块信息主键
     * @return 结果
     */
    @Override
    public int deleteTuyaChannelInfoByChannelIds(Long[] channelIds)
    {
        return tuyaChannelInfoMapper.deleteTuyaChannelInfoByChannelIds(channelIds);
    }

    /**
     * 删除模块信息信息
     *
     * @param channelId 模块信息主键
     * @return 结果
     */
    @Override
    public int deleteTuyaChannelInfoByChannelId(Long channelId)
    {
        return tuyaChannelInfoMapper.deleteTuyaChannelInfoByChannelId(channelId);
    }
    /**
     * 是否存在子节点
     *
     * @param channelId ID
     * @return 结果
     */
    @Override
    public boolean hasChildByChannelId(Long channelId)
    {
        int result = tuyaChannelInfoMapper.hasChildByChannelId(channelId);
        return result > 0;
    }
}
