package com.ruoyi.system.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysMqttUser;
import com.ruoyi.system.mapper.SysMqttUserMapper;
import com.ruoyi.system.service.ISysMqttUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * mqtt用户Service业务层处理
 *
 * @author Duke_yzl
 * @date 2023-06-30
 */
@Service
public class SysMqttUserServiceImpl extends MPJBaseServiceImpl<SysMqttUserMapper, SysMqttUser> implements ISysMqttUserService
{
    @Resource
    private SysMqttUserMapper sysMqttUserMapper;


    /**
     * 查询mqtt用户
     *
     * @param id mqtt用户主键
     * @return mqtt用户
     */
    @Override
    public SysMqttUser selectSysMqttUserById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询mqtt用户列表
     *
     * @param sysMqttUser mqtt用户
     * @return mqtt用户
     */
    @Override
    public List<SysMqttUser> selectSysMqttUserList(SysMqttUser sysMqttUser)
    {
        MPJLambdaWrapper<SysMqttUser> selectSysMqttUserList = new MPJLambdaWrapper<SysMqttUser>()
                .selectAll(SysMqttUser.class)
                .like(StringUtils.isNotBlank(sysMqttUser.getUsername()),SysMqttUser::getUsername,sysMqttUser.getUsername())
                .eq(StringUtils.isNotBlank(sysMqttUser.getPasswordHash()),SysMqttUser::getPasswordHash,sysMqttUser.getPasswordHash())
                .eq(StringUtils.isNotBlank(sysMqttUser.getSalt()),SysMqttUser::getSalt,sysMqttUser.getSalt())
                .eq(StringUtils.isNotLongNull(sysMqttUser.getIsSuperuser()),SysMqttUser::getIsSuperuser,sysMqttUser.getIsSuperuser());

        return selectJoinList(SysMqttUser.class, selectSysMqttUserList);
    }

    /**
     * 新增mqtt用户
     *
     * @param sysMqttUser mqtt用户
     * @return 结果
     */
    @Override
    public boolean insertSysMqttUser(SysMqttUser sysMqttUser)
    {
        return save(sysMqttUser);
    }

    /**
     * 修改mqtt用户
     *
     * @param sysMqttUser mqtt用户
     * @return 结果
     */
    @Override
    public boolean updateSysMqttUser(SysMqttUser sysMqttUser)
    {
        return updateById(sysMqttUser);
    }

    /**
     * 批量删除mqtt用户
     *
     * @param ids 需要删除的mqtt用户主键
     * @return 结果
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public boolean deleteSysMqttUserByIds(Long[] ids)
    {
        return removeBatchByIds(Arrays.asList(ids));
    }

    /**
     * 删除mqtt用户信息
     *
     * @param id mqtt用户主键
     * @return 结果
     */
    @Override
    public boolean deleteSysMqttUserById(Long id)
    {
        return removeById(id);
    }
}
