/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
package com.crunii.micro.service.sysmgr.jdbc;

import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.domain.common.jdbc.BaseJdbcTemplate;
import com.crunii.micro.domain.common.jdbc.BaseNPJdbcTemplate;
import com.crunii.micro.domain.entity.main.TsmMenu;
import com.crunii.micro.domain.entity.main.TsmRole;
import com.crunii.micro.domain.entity.main.TsmRoleApp;
import com.crunii.micro.domain.entity.main.TsmUrl;
import com.crunii.micro.service.sysmgr.dto.sysmgr.MenuInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 田平 create 2017年8月11日下午12:44:04
 */
@Repository(value = "authJdbcDao")
public class AuthJdbcDao {
	@Resource(name = "mainJT")
	protected BaseJdbcTemplate JT;

	@Resource(name = "mainNPJT")
	protected BaseNPJdbcTemplate NPJT;

	private static final String getAuthRolesSql = "select * from tsm_role where role_id  "
			+ " in(select role_id from tsm_worker_role where login_id=?) and state=1 ";

	private static final String getAuthMenusSql = "select * from tsm_menu where menu_code in(select menu_code from tsm_role_menu where role_id in "
			+ " (select a.role_id from tsm_worker_role a,tsm_role b where a.role_id=b.role_id and a.login_id=? and b.state=1 )) and state=1 order by parent_menu_code,sort_id ";

	public List<TsmRole> getAuthRoles(int loginId) {
		List<TsmRole> roles = JT.queryForBeans(TsmRole.class, getAuthRolesSql, loginId);
		return roles;
	}

	public List<TsmMenu> getAuthMenus(int loginId) {
		List<TsmMenu> menus = JT.queryForBeans(TsmMenu.class, getAuthMenusSql, loginId);
		return menus;
	}

//	private static final String getAuthUrlsSql = "select a.* from tsm_url a where a.url_id in("
//			+ " select d.url_id from tsm_role_url_group b,tsm_url_group c,tsm_url_group_url d"
//			+ " where b.role_id in (select e.role_id from tsm_worker_role e,tsm_role f "
//			+ " where e.role_id=f.role_id and e.login_id=? and f.state=1 ) "
//			+ " and b.group_id= c.group_id and c.group_id=d.group_id and c.state=1 ) and a.state=1";

	private static final String getAuthUrlsSql = "select a.* from tsm_url a where a.url_id in( "
			+ " select d.url_id from tsm_role_url_group b,tsm_url_group c,tsm_url_group_url d "
			+ " where b.role_id in (select e.role_id from tsm_worker_role e,tsm_role f "
			+ " where e.role_id=f.role_id and e.login_id=? and f.state=1 ) "
			+ " and b.group_id= c.group_id and c.group_id=d.group_id and c.state=1 ) and a.state=1 "
			+ " union select a.* from tsm_url a where a.url_id in( "
			+ " select e.button_url_id from tsm_worker_role b,tsm_role c,tsm_role_menu d,tsm_menu e "
			+ " where b.role_id=c.role_id and b.role_id=d.role_id and d.menu_code=e.menu_code AND e.button_url_id>0 "
			+ " and b.login_id=? and c.state=1 and e.state=1 ) and a.state=1 " ;

	public List<TsmUrl> getAuthUrls(int loginId) {
		List<TsmUrl> urls = JT.queryForBeans(TsmUrl.class, getAuthUrlsSql, loginId, loginId);
		return urls;
	}

	private static final String getRoleMenusSql = "select a.* from tsm_menu a,tsm_role_menu b where a.menu_code=b.menu_code and b.role_id=? and a.state=1 order by a.parent_menu_code,a.sort_id ";

	public List<MenuInfo> getRoleMenus(int roleId) {
		List<MenuInfo> menus = JT.queryForBeans(MenuInfo.class, getRoleMenusSql, roleId);
		return menus;
	}

	private static final String getCheckRoleBtn = "select btn.btn_id from tsm_app_button btn,tsm_role_button rbtn where btn.state = 1 and rbtn.btn_id = btn.btn_id and rbtn.role_id = ?";
	public List<String> getCheckRoleBtn(int roleId) {
		 return JT.queryForRawObjects(String.class, getCheckRoleBtn, roleId);
	}


	private static final String getUserRoleBtnCode = "select distinct code from (\n"
			+ "select btn.* from tsm_worker_role rol,tsm_role_button btn where  rol.login_id = ? AND btn.role_id = rol.role_id \n"
			+ ") r_btn, tsm_app_button btn where r_btn.btn_id = btn.btn_id AND btn.type = 2";
	public List<String> getUserRoleBtnCode(int loginId) {
		return JT.queryForRawObjects(String.class, getUserRoleBtnCode, loginId);
	}

	public List<TsmUrl> findPublicAuth(List<Integer> groupIds) {
		String sql = "SELECT * FROM TSM_URL WHERE url_id IN(SELECT url_id FROM TSM_URL_GROUP_URL  WHERE group_id in(:groupIds))";
		Map<String,Object> params = new HashMap<>(2);
		params.put("groupIds",groupIds);
		return NPJT.queryForBeans(TsmUrl.class,sql,params);
	}
}
