package cn.com.liandisys.ycjt.ggcx.admin.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

import cn.com.liandisys.ycjt.ggcx.admin.model.User;

/**
 * UserDaoCustom的扩展行为实现类.
 */
@Component
public class UserDaoImpl implements UserDaoCustom {

    private static final String QUERY_ALL_USER          = "select user from User user where user.avaliable='Y' ";

    private static final String QUERY_USER_BY_LOGINNAME = "select user from User user where user.avaliable='Y' and user.loginName = :loginName";

    private static final String QUERY_USER_BY_ORG       = "select user from User user where user.specialOrg.id = :orgId";
    
    private static final String QUERY_USER_BY_NAME = "select user from User user where user.avaliable='Y' and user.name = :name";
    
    private static final String QUERY_USER_BY_WORKNO = "select user from User user where user.avaliable='Y' and user.workno = :workno";

    @PersistenceContext
    private EntityManager       em;

    @SuppressWarnings("unchecked")
    public List<User> findAllUsers() {
        List<User> list = em.createQuery(QUERY_ALL_USER).getResultList();
        return list;
    }

    public User findByLoginName(String loginName) {
        User User = (User) em.createQuery(QUERY_USER_BY_LOGINNAME)
                .setParameter("loginName", loginName).getSingleResult();
        return User;
    }
    
    @SuppressWarnings("unchecked")
	public List<User> findUserByName(String name) {
    	List<User> list = em.createQuery(QUERY_USER_BY_NAME)
                .setParameter("name", name).getResultList();
        return list;
    }
    
    @SuppressWarnings("unchecked")
	public List<User> findUserByWorkno(String workno) {
    	List<User> list = em.createQuery(QUERY_USER_BY_WORKNO)
                .setParameter("workno", workno).getResultList();
        return list;
    }

    @SuppressWarnings("unchecked")
    public List<User> findByOrgId(String orgId) {
        List<User> list = em.createQuery(QUERY_USER_BY_ORG).setParameter("orgId", orgId)
                .getResultList();
        return list;
    }

}
