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

import java.util.List;

import com.x.customUpload.core.entity.UploadEntity;
import org.apache.commons.lang3.StringUtils;

import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.customUpload.assemble.control.Business;

public class AttachmentQueryService {

    /**
     * 根据传入的ID从数据库查询Attachment对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    public UploadEntity get(String id) throws Exception {
        if (id == null || id.isEmpty()) {
            throw new Exception("id is null, return null!");
        }
        try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
            return emc.find(id, UploadEntity.class);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 根据传入的ID从数据库查询Attachment对象
     *
     * @param ids
     * @return
     * @throws Exception
     */
    public List<UploadEntity> list(List<String> ids) throws Exception {
        if (ListTools.isEmpty(ids)) {
            return null;
        }
        Business business = null;
        try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
            business = new Business(emc);
            return business.uploadEntityFactory().list(ids);
        } catch (Exception e) {
            throw e;
        }
    }

    public List<UploadEntity> listAttachmentWithProject(String pid) throws Exception {
        if (StringUtils.isEmpty(pid)) {
            return null;
        }
        Business business = null;
        try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
            business = new Business(emc);
            return business.uploadEntityFactory().listAttachmentWithProject(pid);
        } catch (Exception e) {
            throw e;
        }
    }

}
