package com.zhangmeng.admin.manager.service.impl;

import com.zhangmeng.admin.manager.service.PermissionService;
import com.zhangmeng.admin.manager.service.RolePermissionService;
import com.zhangmeng.admin.manager.service.RoleService;
import com.zhangmeng.model.base.baseService.impl.AbstractBaseServiceImpl;
import com.zhangmeng.model.base.baseUtil.CommonUtil;
import com.zhangmeng.model.entity.Permission;
import com.zhangmeng.model.entity.Role;
import com.zhangmeng.model.entity.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangmeng
 * @version 1.0
 * @date 2021年1月7日22:32:54
 */
@Service
public class RoleServiceImpl extends AbstractBaseServiceImpl<Role> implements RoleService {

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public List<Permission> getRolePermission(String roleId) {
        if (CommonUtil.isNotNull(roleId)) {
            List<RolePermission> rolePermissionList = this.rolePermissionService.findByRoleId(Long.parseLong(roleId));
            List<Long> pIds = p_ids(rolePermissionList);
            List<Permission> permissionList = this.permissionService.findAll();
            if (permissionList.size() > 0){
                for (Permission permission : permissionList) {
                    if (pIds.contains(permission.getId())){
                            permission.setCheckArr("1");
                    }
                }
            }
            return permissionList;
        }
        return null;
    }

    private List<Long> p_ids(List<RolePermission> rolePermissionList){
        List<Long> list = new ArrayList<>();
        if (rolePermissionList.size() > 0){
            for (RolePermission rolePermission : rolePermissionList) {
                list.add(rolePermission.getPermission_id());
            }
        }
        return list;
    }
}
