package com.zhongdao.jlr.business.dao.impl;

import com.dubh.common.dao.hibernate.BaseDaoHibernate;
import com.zhongdao.jlr.business.dao.BaseDao;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import com.zhongdao.jlr.pojo.BaseMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/***
 * 登录
 */
@Repository("baseDaoImpl")
public class BaseDaoImpl extends BaseDaoHibernate implements BaseDao {

    @Autowired
    private HttpServletRequest request;

    @Override
    public List<BaseMenu> getMenuInfo() {
        //获取登录信息
        LoginUserVo logininfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
        List<BaseMenu> lists=new ArrayList<>();
        List list=null;
        try {
                    String sql = "select id, parent_id, name, action " +
                            "from jlr_menu " +
                            "where id in (" +
                            "select menu_id from jlr_role_authority " +
                            "where role_id ="+logininfo.getRole_id()+")";
                    //获取菜单信息
                    list = getRsBySql(sql);
                    //遍历菜单信息
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            //声明一个菜单对象并将信息放入对象里面
                            BaseMenu baseMenu = new BaseMenu();
                            Object[] objects = (Object[]) list.get(i);
                            baseMenu.setId(Integer.valueOf(objects[0].toString()));
                            baseMenu.setParent_id(Integer.valueOf(objects[1] == null ? "0" : objects[1].toString()));
                            baseMenu.setName(objects[2] == null ? "" : objects[2].toString());
                            baseMenu.setAction(objects[3] == null ? "" : objects[3].toString());
                            //将对象放入集合中
                            lists.add(baseMenu);
                        }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        return lists;
    }

    @Override
    public List<BaseMenu> getParentMenuInfo() {
        //获取登录信息
        LoginUserVo logininfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
        //将角色编号存入集合中
        List<BaseMenu> lists=new ArrayList<>();
        List list=null;
        try {
                    String sql = "select b.id,b.parent_id,b.name,b.action " +
                            "from (select parent_id " +
                            "from (select r.id," +
                            "r.role_id,r.menu_id,p.parent_id parent_id,p.id n,p.name,p.action" +
                            " from jlr_role_authority r " +
                            "inner  join jlr_menu p on" +
                            " r.menu_id=p.id  " +
                            "where role_id="+logininfo.getRole_id()+") m  " +
                            "group by " +
                            "m.parent_id) m " +
                            "inner  join jlr_menu b on m.parent_id=b.id";
                    //获取父级菜单信息
                    list = getRsBySql(sql);
                    //遍历菜单信息
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            //声明一个菜单对象并将信息放入对象里面
                            BaseMenu baseMenu = new BaseMenu();
                            Object[] objects = (Object[]) list.get(i);
                            baseMenu.setId(Integer.valueOf(objects[0].toString()));
                            baseMenu.setParent_id(Integer.valueOf(objects[1] == null ? "0" : objects[1].toString()));
                            baseMenu.setName(objects[2] == null ? "" : objects[2].toString());
                            baseMenu.setAction(objects[3] == null ? "" : objects[3].toString());
                            //将对象放入集合中
                            lists.add(baseMenu);
                        }
                    }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        return lists;
    }
}
