package com.x.sso.assemble.control.factory;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.x.base.core.project.exception.ExceptionWhen;
import com.x.sso.assemble.control.AbstractFactory;
import com.x.sso.assemble.control.Business;
import com.x.sso.core.entity.SsoEntityClassName;

/**
 * 示例数据表基础功能服务类
 * @author sword
 */
public class SsoEntityClassNameFactory extends AbstractFactory {

	public SsoEntityClassNameFactory(Business business) throws Exception {
		super(business);
	}

	/**
	 * 获取指定Id的SampleEntityClassName信息对象
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public SsoEntityClassName get( String id ) throws Exception {
		return this.entityManagerContainer().find(id, SsoEntityClassName.class, ExceptionWhen.none);
	}

	/**
	 * 根据ID列示指定的SampleEntityClassName信息列表
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public List<SsoEntityClassName> list( List<String> ids, Integer maxCount ) throws Exception {
		EntityManager em = this.entityManagerContainer().get(SsoEntityClassName.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<SsoEntityClassName> cq = cb.createQuery(SsoEntityClassName.class);
		Root<SsoEntityClassName> root = cq.from( SsoEntityClassName.class);
		Predicate p = root.get(SsoEntityClassName.id_FIELDNAME).in(ids);
		return em.createQuery(cq.where(p)).setMaxResults(maxCount).getResultList();
	}

	/**
	 * 列示全部的SsoEntityClassName信息列表
	 * @param maxCount  返回的最大条目数
	 * @return
	 * @throws Exception
	 */
	public List<SsoEntityClassName> listAll( Integer maxCount ) throws Exception {
		EntityManager em = this.entityManagerContainer().get(SsoEntityClassName.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<SsoEntityClassName> cq = cb.createQuery(SsoEntityClassName.class);
		Root<SsoEntityClassName> root = cq.from( SsoEntityClassName.class);
		//根据数据更新时间降序
		cq.orderBy( cb.desc( root.get( SsoEntityClassName.updateTime_FIELDNAME ) ) );
		return em.createQuery(cq).setMaxResults(maxCount).getResultList();
	}


}
