package com.fjs.scenic.service.system.impl;

import com.fjs.scenic.dto.PermissionDto;
import com.fjs.scenic.entity.system.Permission;
import com.fjs.scenic.entity.system.RolePermission;
import com.fjs.scenic.mapper.system.PermissionMapper;
import com.fjs.scenic.mapper.system.RolePermissionMapper;
import com.fjs.scenic.service.system.PermissionService;
import com.fjs.scenic.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public ReturnResult getPermissionTree() {

        return ReturnResult.ok(permissionMapper.selectTree());
    }

    @Override
    public ReturnResult getParent() {

        return ReturnResult.ok(permissionMapper.getParent());
    }

    @Override
    public ReturnResult add(Permission permission) {
        if(permission.getPid()==null){
            permission.setPid(0);
        }
        int row = permissionMapper.insertSelective(permission);
        if (row>0) {
            return new ReturnResult(200,"ok",null);
        }
        return ReturnResult.build(500,"添加失败！");
    }

    @Override
    public ReturnResult get(Integer id){
        return ReturnResult.ok(permissionMapper.selectByPrimaryKey(id));
    }

    @Override
    public ReturnResult update(Permission permission){
        if(permission.getPid()==null){
            permission.setPid(0);
        }
        int row = permissionMapper.updateByPrimaryKeySelective(permission);
        if (row>0) {
            return new ReturnResult(200,"ok",null);
        }
        return ReturnResult.build(500,"修改失败！");
    }

    @Override
    public ReturnResult del(int id) {
        List<RolePermission> list=rolePermissionMapper.selectByPermissionId(id);
        if(list.size()>0){
            return ReturnResult.build(500,"该菜单已分配权限，不能删除！");
        }
        permissionMapper.deleteByPrimaryKey(id);
        return ReturnResult.ok(null);
    }

    @Override
    public List<PermissionDto> getListByManageId(Integer id) {
        return permissionMapper.getListByManageId(id);
    }

    @Override
    public List<Permission> getAll() {
        return permissionMapper.getAll();
    }

	@Override
	public List<String> getPermissionAllNotParent() {
		return permissionMapper.getPermissionAll();
	}

	@Override
	public List<String> getPermissionsByManageId(Integer managerId) {
		return permissionMapper.getPermissionsByManageId(managerId);
	}

    /**
     * 通过账号获取登录用户权限
     * @param account
     * @return
     */
    @Override
    public List<PermissionDto> getListByManageAccount(String account) {
        return permissionMapper.getListByManageAccount(account);
    }
}
