package com.chushang.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.util.StringUtils;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.datascope.annotation.DataScope;
import com.chushang.system.entity.po.SysPost;

import java.util.List;

/**
 * @auther: zhao
 * @date: 2024/5/9 15:53
 */
public interface ISysPostService extends IService<SysPost> {
    @DataScope(deptAlias = "d")
    default PageResult pagePostList(SysPost sysPost, CommonParam commonParam){
        IPage<SysPost> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                buildWrapper(sysPost, commonParam)
        );
        return new PageResult(page);
    }
    LambdaQueryWrapper<SysPost> buildWrapper(SysPost sysPost, CommonParam commonParam);

    default List<SysPost> allPostList(SysPost post, CommonParam commonParam){
        return this.list(buildWrapper(post, commonParam));
    }

    default List<SysPost> allPostList(){
        SysPost sysPost = new SysPost();
        sysPost.setStatus(true);
        return allPostList(sysPost, CommonParam.buildAllRequest());
    }


    default boolean checkPostUnique(SysPost post){
        long postId = ObjectUtil.isNull(post.getPostId()) ? -1 : post.getPostId();
        SysPost info = getOne(new LambdaQueryWrapper<SysPost>()
                .eq(StringUtils.isNotEmpty(post.getPostName()),SysPost::getPostName, post.getPostName())
                .eq(StringUtils.isNotEmpty(post.getPostCode()),SysPost::getPostCode, post.getPostCode())
                .last(Operator.LIMIT_ONE.getCharacter()));
        return ObjectUtil.isNotNull(info) && info.getPostId() != postId;
    }

    List<Long> selectPostListByUserId(Long userId);

    String selectUserPostGroup(String username);
}
