package cn.stylefeng.team.portal.modular.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.team.portal.modular.management.entity.CommonAppLinkUserConfig;
import cn.stylefeng.team.portal.modular.management.mapper.CommonAppLinkUserConfigMapper;
import cn.stylefeng.team.portal.modular.management.service.CommonAppLinkUserConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CommonAppLinkUserConfigServiceImpl extends ServiceImpl<CommonAppLinkUserConfigMapper, CommonAppLinkUserConfig> implements CommonAppLinkUserConfigService {

    @Override
    public Set<Long> getUserSelectedAppLinkIdList(Long userId) {
        LambdaQueryWrapper<CommonAppLinkUserConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommonAppLinkUserConfig::getUserId, userId);
        wrapper.select(CommonAppLinkUserConfig::getAppLinkId);
        List<CommonAppLinkUserConfig> list = this.list(wrapper);
        if (ObjectUtil.isEmpty(list)) {
            return new HashSet<>();
        }

        return list.stream().map(CommonAppLinkUserConfig::getAppLinkId).collect(Collectors.toSet());
    }

    @Override
    public void cleanUserBindLinks(Long userId) {
        LambdaQueryWrapper<CommonAppLinkUserConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommonAppLinkUserConfig::getUserId, userId);
        this.remove(wrapper);
    }

}
