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

import cn.estsh.i3plus.core.apiservice.dao.IUserPermissionDao;
import cn.estsh.i3plus.pojo.platform.bean.*;
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.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author : wei.peng
 * @Date : 2018-10-25 20:32
 * @Modify :
 **/
@Service
public class UserPermissionDaoImpl implements IUserPermissionDao {

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

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SysDepartment> findDepartmentByUser(SysUserInfo userInfo) {
        LOGGER.info(" 查询用户所有部门 user id:{}", userInfo.getId());
        String hql = "select d from SysRefUserDepartment as rud " +
                        " left join SysDepartment as d on d.id = rud.departmentId " +
                        " where rud.userId =:userId";
        return entityManager.createQuery(hql).setParameter("userId",userInfo.getId()).getResultList();
    }

    @Override
    public List<SysRole> findSysRoleByUser(SysUser user) {
        LOGGER.info(" 查询用户所有角色  user id:{}", user.getId());

        String hql = "select sr from SysRefUserRole as rur " +
                    " left join SysRole as sr on sr.id = rur.roleId " +
                    " where rur.userId =:userId";
        //hql = "select rur from RefUserRole as rur,SysRole as where rur.userId =:userId and sr.id = rur.roleId";
        return entityManager.createQuery(hql).setParameter("userId",user.getId()).getResultList();
    }

    @Override
    public List<SysMenu> findSysMenuByUser(SysUser user) {
        LOGGER.info(" 查询用户所有菜单  user id:{}", user.getId());
        String hql = "select sm from SysRefUserRole as rur " +
                " left join SysRefRoleMenu as rrm on rrm.roleId = rur.roleId" +
                " left join SysMenu as sm on sm.id = rrm.menuId" +
                " where rur.userId =:userId";
        List<SysMenu> list = entityManager.createQuery(hql).setParameter("userId", user.getId()).getResultList();
        List<SysMenu> result = new ArrayList<>();
        if(list != null && list.size() > 0){
            for (SysMenu menu : list) {
                if(!result.contains(menu)){
                    result.add(menu);
                }
            }
        }
        return result;
    }

    @Override
    public List<SysMenu> findSysMenuByUserTree(SysUser user) {
        return null;
    }

    @Override
    public List<SysPosition> findPositionByUser(SysUserInfo userInfo) {
        LOGGER.info(" 查询用户所有岗位  user id:{}", userInfo.getId());
        String hql = "select p from SysRefUserPosition as rup " +
                " left join SysPosition as p on p.id = rup.positionId" +
                " where rup.userId =:userId";
        return entityManager.createQuery(hql).setParameter("userId",userInfo.getId()).getResultList();
    }


}
