package com.ruoyi.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.ruoyi.common.enums.DictEnum;
import com.ruoyi.common.web.exception.ServiceException;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.service.ISysPostService;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * 岗位 服务层实现
 * @author ruoyi
 */
@Component
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Inject
    SysPostMapper sysPostMapper;

    @Inject
    SysUserPostMapper sysUserPostMapper;

    /**
     * 新增岗位
     * @param sysPost
     * @return
     */
    @Override
    public int add(SysPost sysPost) {
        if (!checkPostNameUnique(sysPost)) {
            throw new ServiceException("岗位名称已存在");
        } else if(!checkPostCodeUnique(sysPost)) {
            throw new ServiceException("岗位编码已存在");
        }
        return sysPostMapper.insert(sysPost);
    }

    /**
     * 修改岗位
     * @param sysPost
     * @return
     */
    @Override
    public int edit(SysPost sysPost) {
        if (!checkPostNameUnique(sysPost)) {
            throw new ServiceException("岗位名称已存在");
        } else if(!checkPostCodeUnique(sysPost)) {
            throw new ServiceException("岗位编码已存在");
        }
        return sysPostMapper.update(sysPost);
    }

    /**
     * 删除岗位
     * @param postIds
     * @return
     */
    @Override
    public void delete(List<Long> postIds) {
        List<SysPost> sysPosts = sysPostMapper.selectListByIds(postIds);
        for (SysPost sysPost : sysPosts) {
            if(sysUserPostMapper.countUserPostByPostId(sysPost.getPostId()) > 0) {
                throw new ServiceException("岗位已分配,不能删除");
            }
            sysPostMapper.deleteById(sysPost.getPostId());
        }
    }

    /**
     * 根据用户ID获取岗位选择框列表
     * @param userId 用户ID
     * @return
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return sysPostMapper.selectPostListByUserId(userId, DictEnum.DEL_FLAG.OK.getValue());
    }

    /**
     * 查询用户所属岗位组
     * @param userName 用户名
     * @return
     */
    @Override
    public List<SysPost> selectPostsByUserName(String userName) {
        return sysPostMapper.selectPostsByUserName(userName, DictEnum.DEL_FLAG.OK.getValue());
    }

    /**
     * 校验岗位名称是否唯一
     * @param sysPost
     * @return
     */
    private boolean checkPostNameUnique(SysPost sysPost) {
        long postId = sysPost.getPostId() == null ? -1L : sysPost.getPostId();
        QueryWrapper qw = QueryWrapper.create();
        qw.and(SysPost::getPostName).eq(sysPost.getPostName());
        qw.limit(1);
        SysPost sp = sysPostMapper.selectOneByQuery(qw);
        return ObjectUtil.isNull(sp) || sp.getPostId() == postId;
    }

    /**
     * 校验岗位编码是否唯一
     * @param sysPost
     * @return
     */
    private boolean checkPostCodeUnique(SysPost sysPost) {
        long postId = sysPost.getPostId() == null ? -1L : sysPost.getPostId();
        QueryWrapper qw = QueryWrapper.create();
        qw.and(SysPost::getPostCode).eq(sysPost.getPostCode());
        qw.limit(1);
        SysPost sp = sysPostMapper.selectOneByQuery(qw);
        return ObjectUtil.isNull(sp) || sp.getPostId() == postId;
    }

}
