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.qiaoba.api.system.entity.SysPost;
import com.qiaoba.api.system.entity.param.SysPostParam;
import com.qiaoba.api.auth.utils.SecurityUtil;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.module.system.mapper.SysPostMapper;
import com.qiaoba.module.system.service.SysPostService;
import com.qiaoba.module.system.service.SysUserPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 岗位管理 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/9 16:09
 */
@Service
@RequiredArgsConstructor
public class SysPostServiceImpl implements SysPostService {

    private final SysPostMapper sysPostMapper;
    private final SysUserPostService sysUserPostService;

    @Override
    public TableDataInfo<SysPost> selectPageList(SysPostParam param, PageQuery pageQuery) {
        return TableDataInfo.build(sysPostMapper.selectPage(pageQuery.build(), param2Wrapper(param)));
    }

    @Override
    public int insert(SysPost sysPost) {
        checkPostNameUnique(sysPost);
        checkPostCodeUnique(sysPost);
        sysPost.setCreateUser(SecurityUtil.getLoginUsername());
        sysPost.setCreateTime(new Date());
        return sysPostMapper.insert(sysPost);
    }

    @Override
    public int updateById(SysPost sysPost) {
        checkPostNameUnique(sysPost);
        checkPostCodeUnique(sysPost);
        sysPost.setUpdateUser(SecurityUtil.getLoginUsername());
        sysPost.setUpdateTime(new Date());
        return sysPostMapper.updateById(sysPost);
    }

    @Override
    public List<SysPost> selectList(SysPostParam param) {
        return sysPostMapper.selectList(param2Wrapper(param));
    }

    @Override
    public SysPost selectById(String deptId) {
        return sysPostMapper.selectById(deptId);
    }

    @Override
    public int deleteById(String deptId) {
        checkAllowDelete(CollUtil.toList(deptId));
        return sysPostMapper.deleteById(deptId);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        checkAllowDelete(ids);
        return sysPostMapper.deleteBatchIds(ids);
    }

    private QueryWrapper<SysPost> param2Wrapper(SysPostParam param) {
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByAsc(SysPost::getPostSort)
                .like(StrUtil.isNotBlank(param.getPostName()), SysPost::getPostName, param.getPostName())
                .like(StrUtil.isNotBlank(param.getPostCode()), SysPost::getPostCode, param.getPostCode())
                .eq(StrUtil.isNotBlank(param.getStatus()), SysPost::getStatus, param.getStatus());
        return wrapper;
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     */
    private void checkPostNameUnique(SysPost post) {
        if (StrUtil.isNotBlank(sysPostMapper.checkPostNameUnique(post))) {
            throw new ServiceException(StrUtil.format("处理岗位{}失败, 岗位名称已存在", post.getPostName()));
        }
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     */
    private void checkPostCodeUnique(SysPost post) {
        if (StrUtil.isNotBlank(sysPostMapper.checkPostCodeUnique(post))) {
            throw new ServiceException(StrUtil.format("处理岗位{}失败, 岗位编码已存在", post.getPostName()));
        }
    }

    /**
     * 检查岗位是否允许删除
     *
     * @param ids ids
     */
    private void checkAllowDelete(List<String> ids) {
        List<String> names = sysUserPostService.selectUsedPostNameByIds(ids);
        if (CollUtil.isNotEmpty(names)) {
            throw new ServiceException(StrUtil.format("删除岗位{}失败, 岗位已绑定用户", names.toString()));
        }
    }
}
