package org.cs.mgr.admin.dao.impl;

import java.util.List;
import java.util.Map;

import org.cs.core.dao.impl.BaseDao;
import org.cs.mgr.admin.dao.IMenuDao;
import org.cs.mgr.admin.model.Menu;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.springframework.stereotype.Repository;

@Repository
public class MenuDao extends BaseDao<Menu> implements IMenuDao {

	public String getNewSuperMenuCode(){
		StringBuffer sb = new StringBuffer("SELECT MAX(to_number(CODE)) AS max_code FROM mgr_menu ");
		Map<String, Object> map = ((List<Map<String,Object>>)this.findBySql(sb.toString())).get(0);
		Integer code = Integer.valueOf(map.get("max_code").toString());
		while(true){
			code = code + 1;
			Menu menu = this.findOneByHql("from Menu where code = '"+ code + "'", null);
			if(menu == null || StringUtil.isBlank(menu.getId().toString())){
				break;
			}
		}
		
		return code + "";
	}
	
	
	public String getNewSecMenuCode(Menu m){
//		int ret ;
		StringBuffer sb = new StringBuffer("select MAX(to_number(CODE)) as max_code from mgr_menu where pid = " + m.getPid());
		List<Map<String, Object>> list = (List<Map<String, Object>>)this.findBySql(sb.toString());
		Menu superMenu = this.findById(m.getPid());
		int maxCode = 0;
		if(list.size() > 0){
			Map<String, Object> map = list.get(0);
			if(map.get("max_code") != null){
				maxCode = Integer.valueOf(map.get("max_code") + "") +  1;
			}else{
				maxCode = Integer.valueOf(superMenu.getCode()) * 10 + 1;
			}
		}
		//闃叉menu琛╟ode閲嶅
		while(true){
			Menu menu = this.findOneByHql("from Menu where code = '"+ maxCode + "'", null);
			if(menu == null || StringUtil.isBlank(menu.getId().toString())){
				break;
			}
			maxCode = maxCode + 1;
		}
		return maxCode + "";
	}


	@Override
	public Pager getSuperMenuList(Pager page) {
		
		// TODO Auto-generated method stub
		return this.findPageByHql("from Menu where pid = 0 and display = true order by weight", page, null);
	}


	@Override
	public List<Menu> getSuperMenuList() {
		// TODO Auto-generated method stub
		return (List<Menu>) this.findByHql("from Menu where pid = 0 and display = true order by weight", null);
	}


	@Override
	public List<Menu> getSecMenuList() {
		// TODO Auto-generated method stub
		return (List<Menu>) this.findByHql("from Menu where pid <> 0 and display = true order by weight ", null);
	}


	@Override
	public List<Map<String, Object>> getRoleMenu(int roleId) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer(" SELECT tmm.id AS menu_id, tmm.code as menu_code,tmm.pid as menu_pid, tmm.name AS menu_name, nvl((SELECT tmrm.action_val FROM mgr_role_menu tmrm LEFT JOIN  mgr_role tmr ON tmr.code = tmrm.role_code WHERE tmr.id =" + roleId + " AND tmrm.menu_code = tmm.code),-1) AS a1a from mgr_menu tmm  order by tmm.code, tmm.weight ");
		return (List<Map<String, Object>>) this.findBySql(sb.toString());
	}


	@Override
	public List<Menu> getMenuList() {
		// TODO Auto-generated method stub
		return (List<Menu>) this.findByHql("from Menu where display = true order by code, weight ", null);
	}
	
	
	
	
	
	
}
