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.IAdminDao;
import org.cs.mgr.admin.model.Admin;
import org.cs.util.StringUtil;
import org.springframework.stereotype.Repository;

@Repository
public class AdminDao extends BaseDao<Admin>  implements IAdminDao{

	@Override
	public List<Map<String, Object>> findPermissions(String userCode) {
//		// TODO Auto-generated method stub
//		StringBuffer sb = new StringBuffer("SELECT tmur.role_code,tmr.name AS role_name"
//				+ ",tmm.id AS menu_id, tmm.name AS menu_name, tma.id as user_id,tmrm.action_val"
//				+ ",menu_code, pid, url,weight,icon"
//				+ " FROM mgr_user_role tmur "
//				+ " LEFT JOIN  mgr_admin tma ON (tma.code = tmur.user_code)");
//		sb.append(" LEFT JOIN  mgr_role tmr ON (tmur.role_code = tmr.code) ");
//		sb.append(" RIGHT JOIN mgr_role_menu tmrm ON (tmur.role_code = tmrm.role_code) ");
//		sb.append(" LEFT JOIN mgr_menu tmm ON(tmm.code = tmrm.`menu_code`) ");
//		sb.append(" WHERE tmur.user_code='" + userCode + "'");
//		sb.append(" ORDER BY tmm.code, weight ");
//		return (List<Map<String, Object>>) this.findBySql(sb.toString());
//		
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT a.role_code AS \"role_code\", a.role_name AS \"role_name\", a.menu_id AS \"menu_id\", a.menu_name AS \"menu_name\", a.user_id AS \"user_id\", a.action_val AS \"action_val\",a.menu_code AS \"menu_code\", a.pid AS \"pid\", a.url AS \"url\",a.weight AS \"weight\",a.icon AS \"icon\"");
		sb.append(",a.one_category_id AS \"one_category_id\", a.two_category_id AS \"two_category_id\",a.three_category_id AS \"three_category_id\",a.num AS \"num\"" );
		sb.append("  FROM");
		sb.append("  ( ");
		sb.append("  SELECT tmur.role_code,tmr.name AS role_name, tmm.id AS menu_id, tmm.name AS menu_name,tma.id as user_id,tmrm.action_val,menu_code,pid,url,weight,icon,tmm.code");
		sb.append(",tmr.one_category_id,tmr.two_category_id, tmr.three_category_id,tmr.num");
		sb.append("    FROM mgr_user_role tmur");
		sb.append("    LEFT JOIN mgr_admin tma ON (tma.code = tmur.user_code)");
		sb.append("    LEFT JOIN mgr_role tmr ON (tmur. role_code = tmr.code)");
		sb.append("    RIGHT JOIN mgr_role_menu tmrm ON (tmur.role_code = tmrm.role_code)");
		sb.append("    LEFT JOIN mgr_menu tmm ON (tmm.code = tmrm.menu_code)");
		sb.append("    WHERE tmur.user_code='" + userCode + "'");
		sb.append("  ) a ");
		sb.append("   ORDER BY a.code, a.weight");
		sb.append("");
		sb.append("");
		sb.append("");
		return (List<Map<String, Object>>) this.findBySql(sb.toString());
	}


	@Override
	public List<Map<String, Object>> findRoleList(int userId) {
		// TODO Auto-generated method stub
		
		StringBuffer sb = new StringBuffer(
				" SELECT tmr.id AS \"role_id\", tmr.code AS \"role_code\", tmr.name AS \"role_name\",  ");
		sb.append(
				" (SELECT nvl(count(tmur.id), 0) FROM mgr_user_role tmur LEFT JOIN mgr_admin tma ON tma.code = tmur.user_code WHERE tma.id = " + userId);
		sb.append(" AND tmr.code = tmur.role_code) AS \"set\" ");
		sb.append(" FROM mgr_role tmr  ");
		return (List<Map<String, Object>>) this.findBySql(sb.toString());
	}
	
	@Override
	public List<Map<String, Object>> findRoleList(int userId, String name) {
		// TODO Auto-generated method stub
		
		StringBuffer sb = new StringBuffer(
				" SELECT tmr.id AS \"role_id\", tmr.code AS \"role_code\", tmr.name AS \"role_name\",  ");
		sb.append(
				" (SELECT nvl(count(tmur.id), 0) FROM mgr_user_role tmur LEFT JOIN mgr_admin tma ON tma.code = tmur.user_code WHERE tma.id = " + userId);
		sb.append(" AND tmr.code = tmur.role_code) AS \"set\" ");
		sb.append(" FROM mgr_role tmr  ");
		if(StringUtil.isNotBlank(name)){
			sb.append(" where tmr.name like '%"+name+"%'");
		}
		return (List<Map<String, Object>>) this.findBySql(sb.toString());
	}

}
