package cn.estsh.i3plus.core.apiservice.daoimpl;

import cn.estsh.i3plus.core.apiservice.dao.ISysMenuDao;
import cn.estsh.i3plus.pojo.platform.bean.SysMenu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author : Adair Peng
 * @CreateDate : 2018-12-18 15:39
 * @Modify:
 **/
@Service
public class SysMenuDaoImpl implements ISysMenuDao {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysMenuDaoImpl.class);

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SysMenu> findByParentIdAndRoleId(Long parentId, Long roleId) {
        LOGGER.info(" 根据角色ID 查询权限 parentId:{} roleId:{}",parentId,roleId);
        String hql = " select sm from SysMenu as sm " +
                "left join SysRefRoleMenu as srrm on sm.id = srrm.menuId and srrm.roleId =:roleId " +
                "where sm.parentId =:parentId";

        return entityManager.createQuery(hql)
                .setParameter("parentId", parentId)
                .setParameter("roleId", roleId)
                .getResultList();
    }

    @Override
    public List<SysMenu> findByParentIdAndUserId(Long parentId, Long userId) {
        LOGGER.info(" 根据账号ID 查询权限 parentId:{} userId:{}",parentId,userId);

        String hql = " select sm from SysMenu as sm " +
                "left join SysRefRoleMenu as srrm on srrm.menuId = sm.id " +
                "left join SysRefUserRole as srur on srur.roleId = srrm.roleId and srur.userId =:userId " +
                "where sm.parentId =:parentId";

        return entityManager.createQuery(hql)
                .setParameter("parentId", parentId)
                .setParameter("userId", userId)
                .getResultList();
    }
}
