ActionTaskCountWithPerson.java 2.1 KB
Newer Older
Z
zhourui 已提交
1 2
package com.x.processplatform.assemble.surface.jaxrs.anonymous;

Z
zhourui 已提交
3 4 5 6 7 8
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

Z
zhourui 已提交
9 10 11 12 13 14 15 16 17
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.annotation.FieldDescribe;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.processplatform.assemble.surface.Business;
Z
zhourui 已提交
18 19
import com.x.processplatform.core.entity.content.Task;
import com.x.processplatform.core.entity.content.Task_;
Z
zhourui 已提交
20 21 22 23 24 25 26 27 28 29

class ActionTaskCountWithPerson extends BaseAction {

	ActionResult<Wo> execute(EffectivePerson effectivePerson, String credential) throws Exception {
		ActionResult<Wo> result = new ActionResult<>();
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Wo wo = new Wo();
			Business business = new Business(emc);
			String person = business.organization().person().get(credential);
			if (StringUtils.isNotEmpty(person)) {
Z
zhourui 已提交
30
				Long count = this.countWithPerson(business, person);
Z
zhourui 已提交
31 32 33 34 35 36 37
				wo.setCount(count);
			}
			result.setData(wo);
			return result;
		}
	}

Z
zhourui 已提交
38 39 40 41 42 43 44 45 46 47
	private Long countWithPerson(Business business, String person) throws Exception {
		EntityManager em = business.entityManagerContainer().get(Task.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Long> cq = cb.createQuery(Long.class);
		Root<Task> root = cq.from(Task.class);
		Predicate p = cb.equal(root.get(Task_.person), person);
		cq.select(cb.count(root)).where(p);
		return em.createQuery(cq).getSingleResult();
	}

Z
zhourui 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	public static class Wo extends GsonPropertyObject {

		private static final long serialVersionUID = 8792811593252273112L;

		@FieldDescribe("待办数量")
		private Long count = 0L;

		public Long getCount() {
			return count;
		}

		public void setCount(Long count) {
			this.count = count;
		}

	}
}