package com.zhongdao.jlr.business.dao.impl;
import com.dubh.common.dao.hibernate.BaseDaoHibernate;
import com.util.UtilTools;
import com.util.Vo;
import com.zhongdao.jlr.business.dao.RoleDao;
import com.zhongdao.jlr.business.vo.JLRMenu;
import com.zhongdao.jlr.business.vo.JLRRMenu;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import com.zhongdao.jlr.business.vo.PageInfo;
import com.zhongdao.jlr.pojo.BaseMenu;
import com.zhongdao.jlr.pojo.JLRAuthority;
import com.zhongdao.jlr.pojo.JLRRole;
import org.apache.commons.lang.StringUtils;
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
public class RoleDaoImpl extends BaseDaoHibernate implements RoleDao{

    @Autowired
    HttpServletRequest request;

    @Override
    public Vo getRoleInfo(HttpServletRequest request, String name) {
        //获取sessions用户信息
        LoginUserVo logininfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
        Vo vo=new Vo();
        //声明一个分页对象
        PageInfo pageInfo=new PageInfo();
        //获取页数和一页显示多少行并将其放入对象中
        pageInfo.setPage(UtilTools.getPage(request));
        pageInfo.setRows(UtilTools.getRows(request));
        //sql动态拼接
        StringBuffer sql=new StringBuffer("select j.id,j.name,j.update_time from jlr_role j where j.delete_flag != 1 and j.id != "+ logininfo.getRole_id());
        if (StringUtils.isNotBlank(name)) {
            sql.append("  and j.name like '%" + name + "%'");
        }
        String countSql=sql.toString();
        String info=sql.append(" order by j.update_time desc limit "+pageInfo.getCurrentIndex()+","+pageInfo.getRows()).toString();
        try {
            //获取角色信息总数
            List lists=getRsBySql(countSql);
            //分页获取角色信息
            List list = getRsBySql(info);
            if (list != null && list.size() > 0) {
                //将角色信息放入vo对象中
                vo.setRows(UtilTools.listTojsonArray(list, new String[]{"ID","NAME","UPDATETIME"}));
            }
            if(lists != null && lists.size() > 0) {
                vo.setTotal(lists.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    @Override
    public List<JLRMenu> getTreeInfo() {
        List<JLRMenu> listes=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  ) m  " +
                    "group by " +
                    "m.parent_id) m " +
                    "inner join jlr_menu b on m.parent_id=b.id";
            //获取父级菜单信息
            list = getRsBySql(sql);
            //获取子菜单
            List<JLRRMenu> listess=getMenuInfo();
            //遍历菜单信息
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    //将角色编号存入集合中
                    List<JLRRMenu> lists=new ArrayList<>();
                    //声明一个菜单对象并将信息放入对象里面
                    BaseMenu baseMenu = new BaseMenu();
                    Object[] objects = (Object[]) list.get(i);
                    //声明一个tree对象
                    JLRMenu  jlrMenu= new JLRMenu();
                    //将id和名称分别存入tree对象中
                    jlrMenu.setId(Integer.valueOf(objects[0].toString()));
                    jlrMenu.setText(objects[2] == null ? "" : objects[2].toString());
                    if(listess != null && list.size()>0) {
                        for (JLRRMenu listt : listess) {
                            //如果是父节点的子节点
                            if(listt.getParent_id() == jlrMenu.getId()){
                                //则将字节点放如集合中
                                lists.add(listt);
                            }
                        }
                    }
                    //将集合放入tree对象中
                    jlrMenu.setChildren(lists);
                    //将树对象放入集合中
                    listes.add(jlrMenu);
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        return listes;
    }

    @Override
    public boolean updateRoleInfo(JLRRole jlrRole) {
            try {
                //添加或更新用户信息
                saveOrUpdate(jlrRole);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
    }

    @Override
    public boolean updateRolesInfo(JLRAuthority jlrAuthority) {
        try {
            //添加或更新权限信息
            saveOrUpdate(jlrAuthority);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public JLRRole getUser(String id) {
            JLRRole jlrUser=null;
            try {
                //获取单个用户对象信息
                jlrUser=(JLRRole) getObjectHql("from JLRRole where id = "+id);
            } catch (Exception e) {
                e.printStackTrace();
                return  null;
            }
            return jlrUser;
    }

    @Override
    public List<BaseMenu> getMenu(String id) {
        List<BaseMenu> lists=new ArrayList<>();
        List list=null;
        try {
            String sql = "select j.id,j.parent_id,j.name,j.action from jlr_menu j inner join jlr_role_authority l on j.id = l.menu_id where role_id ="+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());
                    //将对象放入集合中
                    lists.add(baseMenu);
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        return lists;
    }

    public List<JLRRMenu> getMenuInfo() {
        List<JLRRMenu> lists=new ArrayList<>();
        List list=null;
        try {
            String sql = "select l.id,l.parent_id,l.name,l.action from jlr_menu j inner join jlr_menu l on j.id = l.parent_id";
            //获取菜单信息
            list = getRsBySql(sql);
            //遍历菜单信息
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    //声明一个菜单对象并将信息放入对象里面
                    JLRRMenu baseMenu = new JLRRMenu();
                    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.setText(objects[2] == null ? "" : objects[2].toString());
                    //将对象放入集合中
                    lists.add(baseMenu);
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        return lists;
    }

    @Override
    public boolean deleteSupplier(Integer id) {
        try {
            deleteHql("delete JLRAuthority where role_id="+id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
