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

import java.util.ArrayList;
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.customUpload.assemble.control.AbstractFactory;
import org.apache.commons.lang3.StringUtils;

import com.x.base.core.project.exception.ExceptionWhen;
import com.x.base.core.project.tools.ListTools;
import com.x.customUpload.assemble.control.Business;
import com.x.customUpload.core.entity.UploadEntity;
import com.x.customUpload.core.entity.UploadEntity_;

public class UploadEntityFactory extends AbstractFactory {

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

    public UploadEntity get(String id) throws Exception {
        return this.entityManagerContainer().find(id, UploadEntity.class, ExceptionWhen.none);
    }

    public List<String> listAll() throws Exception {
        EntityManager em = this.entityManagerContainer().get(UploadEntity.class);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<String> cq = cb.createQuery(String.class);
        Root<UploadEntity> root = cq.from(UploadEntity.class);
        cq.select(root.get(UploadEntity_.id));
        return em.createQuery(cq).getResultList();
    }

    public List<UploadEntity> list(List<String> ids) throws Exception {
        if (ListTools.isEmpty(ids)) {
            return new ArrayList<UploadEntity>();
        }
        EntityManager em = this.entityManagerContainer().get(UploadEntity.class);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<UploadEntity> cq = cb.createQuery(UploadEntity.class);
        Root<UploadEntity> root = cq.from(UploadEntity.class);
        Predicate p = root.get(UploadEntity_.id).in(ids);
        return em.createQuery(cq.where(p)).getResultList();
    }

//    public List<UploadEntity> listAttachmentWithProject(String pid) throws Exception {
//        if (StringUtils.isEmpty(pid)) {
//            return new ArrayList<UploadEntity>();
//        }
//        EntityManager em = this.entityManagerContainer().get(UploadEntity.class);
//        CriteriaBuilder cb = em.getCriteriaBuilder();
//        CriteriaQuery<UploadEntity> cq = cb.createQuery(UploadEntity.class);
//        Root<UploadEntity> root = cq.from(UploadEntity.class);
//        Predicate p = cb.equal(root.get(UploadEntity_.projectId), pid);
//        p = cb.and(p, cb.equal(root.get(UploadEntity_.bundleObjType), "PROJECT"));
//        return em.createQuery(cq.where(p)).getResultList();
//    }

    public List<UploadEntity> listAttachmentWithProject(String project) throws Exception {
        if (StringUtils.isEmpty(project)) {
            return new ArrayList<UploadEntity>();
        }
        EntityManager em = this.entityManagerContainer().get(UploadEntity.class);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<UploadEntity> cq = cb.createQuery(UploadEntity.class);
        Root<UploadEntity> root = cq.from(UploadEntity.class);
        Predicate p = cb.equal(root.get(UploadEntity_.projectId), project);
        p = cb.and(p, cb.equal(root.get(UploadEntity_.bundleObjType), "PROJECT"));
        return em.createQuery(cq.where(p)).getResultList();
    }

    public List<UploadEntity> listAttachmentWithTask(String taskId) throws Exception {
        if (StringUtils.isEmpty(taskId)) {
            return new ArrayList<UploadEntity>();
        }
        EntityManager em = this.entityManagerContainer().get(UploadEntity.class);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<UploadEntity> cq = cb.createQuery(UploadEntity.class);
        Root<UploadEntity> root = cq.from(UploadEntity.class);
        Predicate p = cb.equal(root.get(UploadEntity_.taskId), taskId);
        p = cb.and(p, cb.equal(root.get(UploadEntity_.bundleObjType), "TASK"));
        return em.createQuery(cq.where(p)).getResultList();
    }
}
