V2Count.java 10.8 KB
Newer Older
NoSubject's avatar
NoSubject 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
package com.x.processplatform.assemble.surface.jaxrs.readcompleted;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

import org.apache.commons.lang3.BooleanUtils;

import com.google.gson.JsonElement;
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.bean.NameValueCountPair;
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 已提交
28 29
import com.x.processplatform.core.entity.content.ReadCompleted;
import com.x.processplatform.core.entity.content.ReadCompleted_;
NoSubject's avatar
NoSubject 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

class V2Count extends V2Base {

	public ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
		ActionResult<Wo> result = new ActionResult<>();
		Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
		Wo wo = new Wo();
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
			Predicate p = this.toFilterPredicate(effectivePerson, business, wi);
			wo.setCount(this.count(business, p));
			if (BooleanUtils.isTrue(wi.getGroupByApplication())) {
				wo.setApplicationList(this.groupByApplication(business, p));
			}
			if (BooleanUtils.isTrue(wi.getGroupByProcess())) {
				wo.setProcessList(this.groupByProcess(business, p));
			}
			if (BooleanUtils.isTrue(wi.getGroupByCreatorPerson())) {
				wo.setCreatorPersonList(this.groupByCreatorPerson(business, p));
			}
			if (BooleanUtils.isTrue(wi.getGroupByCreatorUnit())) {
				wo.setCreatorUnitList(this.groupByCreatorUnit(business, p));
			}
			if (BooleanUtils.isTrue(wi.getGroupByStartTimeMonth())) {
				wo.setStartTimeMonthList(this.groupByStartTimeMonth(business, p));
			}
		}
		result.setData(wo);
		return result;
	}

	private Long count(Business business, Predicate predicate) throws Exception {
Z
zhourui 已提交
62
		return business.entityManagerContainer().count(ReadCompleted.class, predicate);
NoSubject's avatar
NoSubject 已提交
63 64 65
	}

	private List<NameValueCountPair> groupByApplication(Business business, Predicate predicate) throws Exception {
Z
zhourui 已提交
66
		EntityManager em = business.entityManagerContainer().get(ReadCompleted.class);
NoSubject's avatar
NoSubject 已提交
67 68
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
Z
zhourui 已提交
69 70 71
		Root<ReadCompleted> root = cq.from(ReadCompleted.class);
		Path<String> pathApplication = root.get(ReadCompleted_.application);
		Path<String> pathApplicationName = root.get(ReadCompleted_.applicationName);
NoSubject's avatar
NoSubject 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		cq.multiselect(pathApplication, pathApplicationName, cb.count(root)).where(predicate).groupBy(pathApplication);
		List<Tuple> os = em.createQuery(cq).getResultList();
		List<NameValueCountPair> list = new ArrayList<>();
		NameValueCountPair pair = null;
		for (Tuple o : os) {
			pair = new NameValueCountPair();
			pair.setName(o.get(pathApplicationName));
			pair.setValue(o.get(pathApplication));
			pair.setCount(o.get(2, Long.class));
			list.add(pair);
		}
		return list.stream().sorted(Comparator.comparing(NameValueCountPair::getCount).reversed())
				.collect(Collectors.toList());
	}

	private List<NameValueCountPair> groupByProcess(Business business, Predicate predicate) throws Exception {
Z
zhourui 已提交
88
		EntityManager em = business.entityManagerContainer().get(ReadCompleted.class);
NoSubject's avatar
NoSubject 已提交
89 90
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
Z
zhourui 已提交
91 92 93
		Root<ReadCompleted> root = cq.from(ReadCompleted.class);
		Path<String> pathProcess = root.get(ReadCompleted_.process);
		Path<String> pathProcessName = root.get(ReadCompleted_.processName);
NoSubject's avatar
NoSubject 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		cq.multiselect(pathProcess, pathProcessName, cb.count(root)).where(predicate).groupBy(pathProcess);
		List<Tuple> os = em.createQuery(cq).getResultList();
		List<NameValueCountPair> list = new ArrayList<>();
		NameValueCountPair pair = null;
		for (Tuple o : os) {
			pair = new NameValueCountPair();
			pair.setName(o.get(pathProcessName));
			pair.setValue(o.get(pathProcess));
			pair.setCount(o.get(2, Long.class));
			list.add(pair);
		}
		return list.stream().sorted(Comparator.comparing(NameValueCountPair::getCount).reversed())
				.collect(Collectors.toList());
	}

	private List<NameValueCountPair> groupByCreatorPerson(Business business, Predicate predicate) throws Exception {
Z
zhourui 已提交
110
		EntityManager em = business.entityManagerContainer().get(ReadCompleted.class);
NoSubject's avatar
NoSubject 已提交
111 112
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
Z
zhourui 已提交
113 114
		Root<ReadCompleted> root = cq.from(ReadCompleted.class);
		Path<String> pathCreatorPerson = root.get(ReadCompleted_.creatorPerson);
NoSubject's avatar
NoSubject 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		cq.multiselect(pathCreatorPerson, cb.count(root)).where(predicate).groupBy(pathCreatorPerson);
		List<Tuple> os = em.createQuery(cq).getResultList();
		List<NameValueCountPair> list = new ArrayList<>();
		NameValueCountPair pair = null;
		for (Tuple o : os) {
			pair = new NameValueCountPair();
			pair.setName(o.get(pathCreatorPerson));
			pair.setValue(o.get(pathCreatorPerson));
			pair.setCount(o.get(1, Long.class));
			list.add(pair);
		}
		return list.stream().sorted(Comparator.comparing(NameValueCountPair::getCount).reversed())
				.collect(Collectors.toList());
	}

	private List<NameValueCountPair> groupByCreatorUnit(Business business, Predicate predicate) throws Exception {
Z
zhourui 已提交
131
		EntityManager em = business.entityManagerContainer().get(ReadCompleted.class);
NoSubject's avatar
NoSubject 已提交
132 133
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
Z
zhourui 已提交
134 135
		Root<ReadCompleted> root = cq.from(ReadCompleted.class);
		Path<String> pathCreatorUnit = root.get(ReadCompleted_.creatorUnit);
NoSubject's avatar
NoSubject 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		cq.multiselect(pathCreatorUnit, cb.count(root)).where(predicate).groupBy(pathCreatorUnit);
		List<Tuple> os = em.createQuery(cq).getResultList();
		List<NameValueCountPair> list = new ArrayList<>();
		NameValueCountPair pair = null;
		for (Tuple o : os) {
			pair = new NameValueCountPair();
			pair.setName(o.get(pathCreatorUnit));
			pair.setValue(o.get(pathCreatorUnit));
			pair.setCount(o.get(1, Long.class));
			list.add(pair);
		}
		return list.stream().sorted(Comparator.comparing(NameValueCountPair::getCount).reversed())
				.collect(Collectors.toList());
	}

	private List<NameValueCountPair> groupByStartTimeMonth(Business business, Predicate predicate) throws Exception {
Z
zhourui 已提交
152
		EntityManager em = business.entityManagerContainer().get(ReadCompleted.class);
NoSubject's avatar
NoSubject 已提交
153 154
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
Z
zhourui 已提交
155 156
		Root<ReadCompleted> root = cq.from(ReadCompleted.class);
		Path<String> pathStartTimeMonth = root.get(ReadCompleted_.startTimeMonth);
NoSubject's avatar
NoSubject 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		cq.multiselect(pathStartTimeMonth, cb.count(root)).where(predicate).groupBy(pathStartTimeMonth);
		List<Tuple> os = em.createQuery(cq).getResultList();
		List<NameValueCountPair> list = new ArrayList<>();
		NameValueCountPair pair = null;
		for (Tuple o : os) {
			pair = new NameValueCountPair();
			pair.setName(o.get(pathStartTimeMonth));
			pair.setValue(o.get(pathStartTimeMonth));
			pair.setCount(o.get(1, Long.class));
			list.add(pair);
		}
		return list.stream()
				.sorted((o1, o2) -> Objects.toString(o2.getName(), "").compareTo(Objects.toString(o1.getName(), "")))
				.collect(Collectors.toList());
	}

	public static class Wi extends FilterWi {

		@FieldDescribe("按应用分类")
		private Boolean groupByApplication;

		@FieldDescribe("按流程分类")
		private Boolean groupByProcess;

		@FieldDescribe("按创建人分类")
		private Boolean groupByCreatorPerson;

		@FieldDescribe("按创建人分类")
		private Boolean groupByCreatorUnit;

		@FieldDescribe("按创建年月分类")
		private Boolean groupByStartTimeMonth;

		public Boolean getGroupByApplication() {
			return groupByApplication;
		}

		public void setGroupByApplication(Boolean groupByApplication) {
			this.groupByApplication = groupByApplication;
		}

		public Boolean getGroupByProcess() {
			return groupByProcess;
		}

		public void setGroupByProcess(Boolean groupByProcess) {
			this.groupByProcess = groupByProcess;
		}

		public Boolean getGroupByCreatorPerson() {
			return groupByCreatorPerson;
		}

		public void setGroupByCreatorPerson(Boolean groupByCreatorPerson) {
			this.groupByCreatorPerson = groupByCreatorPerson;
		}

		public Boolean getGroupByCreatorUnit() {
			return groupByCreatorUnit;
		}

		public void setGroupByCreatorUnit(Boolean groupByCreatorUnit) {
			this.groupByCreatorUnit = groupByCreatorUnit;
		}

		public Boolean getGroupByStartTimeMonth() {
			return groupByStartTimeMonth;
		}

		public void setGroupByStartTimeMonth(Boolean groupByStartTimeMonth) {
			this.groupByStartTimeMonth = groupByStartTimeMonth;
		}

	}

	public static class Wo extends GsonPropertyObject {

		@FieldDescribe("总数量")
		private Long count;

		@FieldDescribe("按应用分类数量")
		private List<NameValueCountPair> applicationList = new ArrayList<>();

		@FieldDescribe("按流程分类数量")
		private List<NameValueCountPair> processList = new ArrayList<>();

		@FieldDescribe("按创建人分类数量")
		private List<NameValueCountPair> creatorPersonList = new ArrayList<>();

		@FieldDescribe("按创建组织分类数量")
		private List<NameValueCountPair> creatorUnitList = new ArrayList<>();

		@FieldDescribe("按创建的年月分类")
		private List<NameValueCountPair> startTimeMonthList = new ArrayList<>();

		public Long getCount() {
			return count;
		}

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

		public List<NameValueCountPair> getApplicationList() {
			return applicationList;
		}

		public void setApplicationList(List<NameValueCountPair> applicationList) {
			this.applicationList = applicationList;
		}

		public List<NameValueCountPair> getProcessList() {
			return processList;
		}

		public void setProcessList(List<NameValueCountPair> processList) {
			this.processList = processList;
		}

		public List<NameValueCountPair> getCreatorPersonList() {
			return creatorPersonList;
		}

		public void setCreatorPersonList(List<NameValueCountPair> creatorPersonList) {
			this.creatorPersonList = creatorPersonList;
		}

		public List<NameValueCountPair> getCreatorUnitList() {
			return creatorUnitList;
		}

		public void setCreatorUnitList(List<NameValueCountPair> creatorUnitList) {
			this.creatorUnitList = creatorUnitList;
		}

		public List<NameValueCountPair> getStartTimeMonthList() {
			return startTimeMonthList;
		}

		public void setStartTimeMonthList(List<NameValueCountPair> startTimeMonthList) {
			this.startTimeMonthList = startTimeMonthList;
		}

	}
}