package com.chushang.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.system.entity.po.SysDictType;
import com.chushang.system.entity.po.SysPost;
import com.chushang.system.mapper.SysPostMapper;
import com.chushang.system.service.ISysPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @auther: zhao
 * @date: 2024/5/9 18:33
 */
@Slf4j
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    public LambdaQueryWrapper<SysPost> buildWrapper(SysPost sysPost, CommonParam commonParam) {
        return new QueryWrapper<SysPost>()
                .orderBy(true, "asc".equals(commonParam.getIsAsc()), commonParam.getOrderBy())
                .lambda()
                .eq(null != sysPost.getPostId(), SysPost::getPostId, sysPost.getPostId())
                .eq(StringUtils.isNotEmpty(sysPost.getPostCode()), SysPost::getPostCode, sysPost.getPostCode())
                .eq(StringUtils.isNotEmpty(sysPost.getPostName()), SysPost::getPostName, sysPost.getPostName())
                .eq(null != sysPost.getStatus(), SysPost::getStatus, sysPost.getStatus());
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return baseMapper.selectPostListByUserId(userId);
    }

    @Override
    public String selectUserPostGroup(String username) {
        List<SysPost> list = baseMapper.selectPostsByUserName(username);
        if (CollectionUtils.isEmpty(list))
        {
            return StringUtils.EMPTY;
        }
        return list.stream().map(SysPost::getPostName).collect(Collectors.joining(","));
    }

}
