package com.jiayue.center.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.center.constant.UserConstants;
import com.jiayue.center.entity.SysDept;
import com.jiayue.center.mapper.SysDeptMapper;
import com.jiayue.center.service.SysDeptService;
import com.jiayue.center.util.RyStringUtils;
import com.jiayue.center.util.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 部门服务类
 *
 * @author xsl
 * @date 2023/2/16
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    @Autowired
    SysDeptMapper deptMapper;

    /**
     * 查询系统菜单列表
     *
     * @param dept 菜单信息
     * @return 菜单列表
     */
    @Override
    public List<SysDept> selectDeptList(SysDept dept) {
        // 管理员显示所有菜单信息
        return deptMapper.selectDeptList(dept);
    }

    /**
     * 校验菜单名称是否唯一
     *
     * @param dept 菜单信息
     * @return 结果
     */
    @Override
    public String checkDeptNameUnique(SysDept dept) {
        Long deptId = RyStringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
        SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
        if (RyStringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存菜单信息
     *
     * @param dept 菜单信息
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertDept(SysDept dept) {
        return deptMapper.insertDept(dept);
    }

    /**
     * 修改保存菜单信息
     *
     * @param dept 菜单信息
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateDept(SysDept dept) {
        dept.setUpdateBy(SecurityContextUtil.getSysUser().getCreateBy());
        return deptMapper.updateDept(dept);
    }

    /**
     * 根据菜单ID查询信息
     *
     * @param deptId 菜单ID
     * @return 菜单信息
     */
    @Override
    public SysDept selectDeptById(Long deptId) {
        return deptMapper.selectDeptById(deptId);
    }
    /**
     * 是否存在菜单子节点
     *
     * @param DeptId 菜单ID
     * @return 结果
     */
    @Override
    public boolean hasChildByDeptId(Long DeptId) {
        int result = deptMapper.hasChildByDeptId(DeptId);
        return result > 0;
    }
    /**
     * 删除菜单管理信息
     *
     * @param DeptId 菜单ID
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteDeptById(Long DeptId) {
        return deptMapper.deleteDeptById(SecurityContextUtil.getSysUser().getUsername(),DeptId);
    }
}
