/**
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sifang.modules.sys.dao;


import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.Role;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 角色DAO接口
 * @version 2019-11-15
 */
@Repository(value="roleDao")
public class RoleDao extends BaseDao<Role> {

	public Role findByName(String name){
		return getByHql("from Role where status = :p1 and name = :p2", new Parameter(Role.DEL_FLAG_NORMAL, name));
	}

	public String getUserRole(String userId){
	String roles=Db.queryStr(" SELECT r.role_ids FROM `crm_platform_employee_role_rel` r " +
				"LEFT JOIN  `crm_platform_employee` e ON e.id=r.employee_id " +
				"WHERE e.user_id=? ",userId);
		return StringUtil.isBlank(roles)?"-1":roles;
	}

//	@Query("from Role where status='" + Role.DEL_FLAG_NORMAL + "' order by name")
//	public List<Role> findAllList();
//
//	@Query("select distinct r from Role r, User u where r in elements (u.roleList) and r.status='" + Role.DEL_FLAG_NORMAL +
//			"' and u.status='" + User.DEL_FLAG_NORMAL + "' and u.id=?1 or (r.user.id=?1 and r.status='" + Role.DEL_FLAG_NORMAL +
//			"') order by r.name")
//	public List<Role> findByUserId(Long userId);

}
