package com.qiaoba.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.qiaoba.api.system.entity.SysUserPost;
import com.qiaoba.common.base.constant.BaseConstant;
import com.qiaoba.module.system.mapper.SysUserPostMapper;
import com.qiaoba.module.system.service.SysUserPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户岗位关联 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/10 13:03
 */
@Service
@RequiredArgsConstructor
public class SysUserPostServiceImpl implements SysUserPostService {

    private final SysUserPostMapper sysUserPostMapper;

    @Override
    public void insertBatch(List<SysUserPost> list) {
        Db.saveBatch(list);
    }

    @Override
    public void deleteByUserId(String userId) {
        sysUserPostMapper.delete(createWrapper(userId));
    }

    @Override
    public void deleteByUserIds(List<String> userIds) {
        sysUserPostMapper.deleteByUserIds(userIds);
    }

    @Override
    public List<String> selectPostIdsByUserId(String userId) {
        return sysUserPostMapper.selectPostIdsByUserId(userId);
    }

    @Override
    public List<String> selectUsedPostNameByIds(List<String> postIds) {
        return sysUserPostMapper.selectUsedPostNameByIds(postIds);
    }

    @Override
    public String selectPostGroup(String userId) {
        List<String> list = sysUserPostMapper.selectPostNamesByUserId(userId);
        return CollUtil.isNotEmpty(list) ? StrUtil.join(BaseConstant.LINE_JOIN_STR, list) : StrUtil.EMPTY;
    }

    private QueryWrapper<SysUserPost> createWrapper(String userId) {
        QueryWrapper<SysUserPost> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(SysUserPost::getUserId, userId);
        return wrapper;
    }
}
