package com.palmnest.dao.hibernate;

import java.util.List;

import com.palmnest.model.Role;
import com.palmnest.model.RoleFunction;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.palmnest.dao.AppFunctionDao;
import com.palmnest.model.AppFunction;
import com.palmnest.model.User;

/**
 * 系统功能管理底层DAO实现
 *
 * @author yb
 * @since 2014-05-28
 */
@Repository("appFunctionDao")
public class AppFunctionDaoHibernate extends GenericDaoHibernate<AppFunction, Integer> implements AppFunctionDao {
	
	public AppFunctionDaoHibernate() {
		super(AppFunction.class);
	}

	public User getUserById(long id) {
		String strHql = "from User where id=?";
		List<User> users = getSession().createQuery(strHql).setLong(0, id).list();
		User obj = new User();

		User user = (User) getSession().get(User.class, id);
		return user;
	}

	@Override
	public List<AppFunction> getAll() {
		String strHql = "from AppFunction";
		List<AppFunction> functions = getSession().createQuery(strHql).list();
		return functions;
	}

	@Override
	public AppFunction getFuctionBySysCode(String id, String sysCode) {
		StringBuilder strHql = new StringBuilder("from AppFunction where systemcode=" + sysCode + " ");
		if (StringUtils.isNotEmpty(id)) {
			strHql.append(" and id<>" + id);
		}
		List<AppFunction> functions = getSession().createQuery(strHql.toString()).list();
		if (functions != null && functions.size() > 0) {
			return functions.get(0);
		}
		return null;
	}

	@Override
	public AppFunction getFuctionByPerms(String id, String perms) {
		StringBuilder strHql = new StringBuilder("from AppFunction where perms='" + perms + "' ");
		if (StringUtils.isNotEmpty(id)) {
			strHql.append(" and id<>" + id);
		}
		List<AppFunction> functions = getSession().createQuery(strHql.toString()).list();
		if (functions != null && functions.size() > 0) {
			return functions.get(0);
		}
		return null;
	}

	@Override
	public List<AppFunction> getFunctionListByRole(Long roleId, String isApp) {
		String sqlStr = "SELECT * FROM tb_app_function t2 WHERE t2.STATUS=1 AND t2.ID in (SELECT DISTINCT(t1.FUNCTION_ID) FROM tr_app_role_function t1 WHERE t1.ROLEID = " + roleId + ") ";
		if (StringUtils.isNotBlank(isApp)) {
			sqlStr += " AND IS_APP=" + isApp;
		}
		return getSession().createSQLQuery(sqlStr).addEntity(AppFunction.class).list();
	}

	@Override
	public List<AppFunction> getFunctionListByRole(String roleIds) {
		String sqlStr = "SELECT t1.* FROM tb_app_function t1 INNER JOIN tr_app_role_function t2 ON t1.ID=t2.FUNCTION_ID WHERE t1.`STATUS`='1' AND t2.ROLEID in(" + roleIds + ") GROUP BY t1.ID ORDER BY t1.ID ASC ";
		return getSession().createSQLQuery(sqlStr).addEntity(AppFunction.class).list();
	}

	@Override
	public List<AppFunction> getAll(Integer port) {
		String sqlStr = "SELECT * FROM tb_app_function t2 WHERE t2.STATUS=1 AND t2.IS_APP= " + port;
		return getSession().createSQLQuery(sqlStr).addEntity(AppFunction.class).list();
	}

	@Override
	public List<AppFunction> getAll(Integer port, Integer rightType) {
		String sqlStr = "SELECT * FROM tb_app_function t2 WHERE t2.STATUS=1 AND t2.IS_APP= " + port + " AND t2.RIGHT_TYPE=" + rightType;
		return getSession().createSQLQuery(sqlStr).addEntity(AppFunction.class).list();
	}

	@Override
	public List<AppFunction> getLevel3Function(Integer parentFuncId, Long roleId) {
		String sqlStr = "SELECT t1.* FROM tb_app_function t1 INNER JOIN tr_app_role_function t2 ON t1.ID=t2.FUNCTION_ID WHERE t1.RIGHT_TYPE=2 AND t1.`STATUS`='1' AND t2.ROLEID=" + roleId + " AND t1.PARENTCODE=" + parentFuncId;
		return getSession().createSQLQuery(sqlStr).addEntity(AppFunction.class).list();
	}


	@Override
	public List<Role> getRoleList(String funcId) {

		String sql = "SELECT t2.* FROM tr_app_role_function INNER JOIN tb_app_role t2 ON ROLEID=t2.ID WHERE FUNCTION_ID=" + funcId + " AND ROLEID NOT in(SELECT ROLEID FROM tr_app_role_function WHERE FUNCTION_ID in(SELECT ID FROM tb_app_function WHERE PARENTCODE=" + funcId + ")) GROUP BY ROLEID";
		return getSession().createSQLQuery(sql).addEntity(Role.class).list();
	}

	@Override
	public List<AppFunction> getLevel3Function(String funcId) {
		String sql = "SELECT * FROM tb_app_function WHERE PARENTCODE=" + funcId;
		return getSession().createSQLQuery(sql).addEntity(AppFunction.class).list();
	}
}
