package com.x.organization.assemble.control.jaxrs.unit; import java.util.List; import java.util.stream.Collectors; 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.entity.JpaObject; import com.x.base.core.project.annotation.FieldDescribe; import com.x.base.core.project.bean.WrapCopier; import com.x.base.core.project.bean.WrapCopierFactory; import com.x.base.core.project.cache.ApplicationCache; import com.x.base.core.project.http.ActionResult; import com.x.base.core.project.http.EffectivePerson; import com.x.organization.assemble.control.Business; import com.x.organization.core.entity.Identity; import com.x.organization.core.entity.Unit; import net.sf.ehcache.Element; class ActionListSupNestedWithType extends BaseAction { ActionResult> execute(EffectivePerson effectivePerson, String flag, String type) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { ActionResult> result = new ActionResult<>(); Business business = new Business(emc); String cacheKey = ApplicationCache.concreteCacheKey(this.getClass(), flag, type); Element element = business.cache().get(cacheKey); if (null != element && (null != element.getObjectValue())) { result.setData((List) element.getObjectValue()); } else { List wos = this.list(business, flag, type); business.cache().put(new Element(cacheKey, wos)); result.setData(wos); } this.updateControl(effectivePerson, business, result.getData()); return result; } } public static class Wo extends WoAbstractUnit { private static final long serialVersionUID = -125007357898871894L; @FieldDescribe("直接下级组织数量") private Long subDirectUnitCount = 0L; @FieldDescribe("直接下级身份数量") private Long subDirectIdentityCount = 0L; static WrapCopier copier = WrapCopierFactory.wo(Unit.class, Wo.class, null, JpaObject.FieldsInvisible); public Long getSubDirectUnitCount() { return subDirectUnitCount; } public void setSubDirectUnitCount(Long subDirectUnitCount) { this.subDirectUnitCount = subDirectUnitCount; } public Long getSubDirectIdentityCount() { return subDirectIdentityCount; } public void setSubDirectIdentityCount(Long subDirectIdentityCount) { this.subDirectIdentityCount = subDirectIdentityCount; } } private List list(Business business, String flag, String type) throws Exception { Unit unit = business.unit().pick(flag); if (null == unit) { throw new ExceptionUnitNotExist(flag); } List os = business.unit().listSupNestedObject(unit); List wos = Wo.copier.copy(os); if (StringUtils.isNotEmpty(type)) { wos = wos.stream().filter(o -> { return o.getTypeList().contains(type); }).collect(Collectors.toList()); } for (Wo wo : wos) { wo.setSubDirectUnitCount( business.entityManagerContainer().countEqual(Unit.class, Unit.superior_FIELDNAME, wo.getId())); wo.setSubDirectIdentityCount( business.entityManagerContainer().countEqual(Identity.class, Identity.unit_FIELDNAME, wo.getId())); } wos = business.unit().sort(wos); return wos; } }