package com.palmnest.dao;

import com.palmnest.application.bean.Page;
import com.palmnest.model.*;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * User Data Access Object (GenericDao) interface.
 *
 * @author <a href="mailto:chasel.java@gmail.com">Chasel Li</a>
 */
public interface UserDao extends GenericDao<User, Long> {

	/**
	 * Gets users information based on login name.
	 *
	 * @param username the user's username
	 * @return userDetails populated userDetails object
	 * @throws org.springframework.security.core.userdetails.UsernameNotFoundException thrown when user not
	 *                                                                                 found in database
	 */
	@Transactional
	UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;

	User getUserByUsername2(String username);

	/**
	 * Gets a list of users ordered by the uppercase version of their username.
	 *
	 * @return List populated list of users
	 */
	List<User> getUsers();

	/**
	 * Saves a user's information.
	 *
	 * @param user the object to be saved
	 * @return the persisted User object
	 */
	User saveUser(User user);

	/**
	 * Retrieves the password in DB for a user
	 *
	 * @param userId the user's id
	 * @return the password in DB, if the user is already persisted
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	String getUserPassword(Long userId);

	/**
	 * 分页查询 用户信息
	 *
	 * @param pageNo   页数
	 * @param pageSize 每页条数
	 * @param whereHql 查询条件
	 * @return
	 */
	public Page queryUserPage(int pageNo, int pageSize, String whereHql);

	/**
	 * 更新用户信息
	 *
	 * @param user
	 */
	public void updateUser(User user);


	/**
	 * 根据用户名和密码查询用户
	 *
	 * @param username 登录用户名
	 * @param password 登录密码
	 * @return
	 */
	public User getUser(String username, String password);

	/**
	 * @param userid
	 * @return
	 */
//	public List<AppUserOrgRelation> getAppUserOrgByUserId(int userid);


	/**
	 * @param userid
	 * @return
	 */
	public List<AppUserOrgnizationRelation> getAppUserOrgByUserId(int userid);

	/**
	 * @param userid
	 * @return
	 */
	public List<AppUserCompanyRelation> getAppUserCompanyByUserId(int userid);

	/**
	 * @param userid
	 * @return
	 */
	public List<AppUserOrgnizationRelation> getUserOrgByUserId(Long userid);

	/**
	 * @param userid
	 * @return
	 */
	public List<AppUserFloorRelation> getAppUserFloorByUserId(int userid);

	/**
	 * @param userid 用户ID 
	 * @param type   关联组织的类型
	 * @return
	 */
//	public List<AppUserOrgRelation> getAppUserOrgByUserId(int userid,String type);

	/**
	 * @param userid
	 * @param orgType
	 */
//	public void deleteAppUserOrgByUserId(int userid, String orgType);
	public void deleteAppUserOrgByUserId(Long userid);

	public void deleteAppUserFloorByUserId(int userid, String orgType);

//	public void deleteAppUserOrgByOrgId(int orgid,String orgType);

//	public void saveAppUserOrgRelation(AppUserOrgRelation model);

	public void saveAppUserOrgRelation(AppUserOrgnizationRelation model);

	public void saveAppUserOrgRelation(AppUserFloorRelation model);

	public ArrayList<User> getUserByType(int type);

	List<User> findByFunction(String systemCode);

	/**
	 * 后台保存更新系统更新通知时更新后台用户通知状态
	 */
	public void updateAllState();

	List<User> findByFunction(Integer orgId, String systemCode);

	List<User> findByFunctionGeneral(Integer orgId, String systemCode);

	List<User> findByRoleId(Integer roleId,Integer orgId);

	User findByFunctionAndUser(String systemCode, Long userId);

	List<User> findByFunctionAndUserList(String systemCode, Long userId);

	List<Map<String, Object>> findMeetingAuditRoleByOrdId(String OrdId);

	User findByTelephone(String telephone);

	List<User> queryUserBySql(String querySql);

	Role findRoleByFunctionAndUser(String systemCode, Long userId);

	//根据 systemCode roleIds 查符合条件的角色
	List<Role> findRoleBySystemCodeAndRoleIds(String systemCode, String roleIds);

	//根据systemCode 和  roleId 获得对应用户 和 floor_id
	List<User> findByFunAndRoleAndFloor(String systemCode, String floorId);

	List<AppUserFloorRelation> getAppUserFloorByUserId(int userid, int roleId);

	List<Map<String, Object>> getPower(String orgId, int sysCode);

	List<Map<String, Object>> getPowerByOrgIds(String orgIds, int sysCode);

}
