package cn.palmte.work.service;


import cn.palmte.work.model.SysRolePermission;
import cn.palmte.work.model.SysRolePermissionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author
 */
@Service
public class SysRolePermissionService {

    @Autowired
    private SysRolePermissionRepository sysRolePermissionRepository;


    private static final Logger logger = LoggerFactory.getLogger(SysRolePermissionService.class);


    /**
     * 批量删除 角色权限关联表
     */

    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSysRolePermissionByIDs(String[] ids){
        boolean deleted = true;
        for(String id : ids){
            try {
                sysRolePermissionRepository.deleteSysRolePermissionByPermissionId(Integer.parseInt(id));
            } catch (Exception e) {
                e.printStackTrace();
                deleted = false;
                break;
            }
        }
        return deleted;
    }


    public List<Integer> getPermissionIds(int roleId){

        List<SysRolePermission> list =sysRolePermissionRepository.findPerListByRoleId(roleId);
        List<Integer> permIdList = new ArrayList<Integer>();
        for(SysRolePermission permission : list){

            permIdList.add(permission.getPermissionId());
        }
        return permIdList;
    }


}
