SyncOrganization.java 25.2 KB
Newer Older
R
roo00 已提交
1
package com.x.program.center.dingding;
caixiangyi's avatar
caixiangyi 已提交
2 3 4 5

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
R
roo00 已提交
6
import java.util.HashMap;
caixiangyi's avatar
caixiangyi 已提交
7
import java.util.List;
R
roo00 已提交
8 9
import java.util.Map;
import java.util.Map.Entry;
caixiangyi's avatar
caixiangyi 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

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 javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;

import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.entity.annotation.CheckRemoveType;
import com.x.base.core.entity.type.GenderType;
import com.x.base.core.project.cache.ApplicationCache;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.organization.core.entity.Identity;
import com.x.organization.core.entity.Identity_;
import com.x.organization.core.entity.Person;
import com.x.organization.core.entity.PersonAttribute;
import com.x.organization.core.entity.PersonAttribute_;
import com.x.organization.core.entity.Person_;
import com.x.organization.core.entity.Unit;
import com.x.organization.core.entity.UnitAttribute;
import com.x.organization.core.entity.UnitDuty;
import com.x.organization.core.entity.Unit_;
import com.x.program.center.Business;

R
roo00 已提交
50
public class SyncOrganization {
caixiangyi's avatar
caixiangyi 已提交
51

R
roo00 已提交
52
	private static Logger logger = LoggerFactory.getLogger(SyncOrganization.class);
caixiangyi's avatar
caixiangyi 已提交
53 54 55

	private ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
	private ScriptEngine engine = scriptEngineManager.getEngineByName("nashorn");
R
roo00 已提交
56
	private Pattern pattern = Pattern.compile(com.x.base.core.project.config.Person.REGULAREXPRESSION_SCRIPT);
caixiangyi's avatar
caixiangyi 已提交
57 58

	public PullResult execute(Business business) throws Exception {
R
roo00 已提交
59
		logger.print("钉钉进行人员同步.");
caixiangyi's avatar
caixiangyi 已提交
60
		PullResult result = new PullResult();
R
roo00 已提交
61
		String accessToken = Config.dingding().corpAccessToken();
caixiangyi's avatar
caixiangyi 已提交
62 63 64 65
		List<Unit> units = new ArrayList<>();
		List<Person> people = new ArrayList<>();
		List<PersonAttribute> personAttributes = new ArrayList<>();
		List<Identity> identities = new ArrayList<>();
R
roo00 已提交
66 67
		DingdingFactory factory = new DingdingFactory(accessToken);
		for (Department root : factory.roots()) {
caixiangyi's avatar
caixiangyi 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
			this.check(business, result, units, people, personAttributes, identities, accessToken, factory, null, root);
		}
		this.clean(business, result, units, people, identities);
		ApplicationCache.notify(Person.class);
		ApplicationCache.notify(PersonAttribute.class);
		ApplicationCache.notify(Unit.class);
		ApplicationCache.notify(UnitAttribute.class);
		ApplicationCache.notify(UnitDuty.class);
		ApplicationCache.notify(Identity.class);
		result.end();
		if (!result.getCreateUnitList().isEmpty()) {
			logger.print("创建组织({}):{}.", result.getCreateUnitList().size(),
					StringUtils.join(result.getCreateUnitList(), ","));
		}
		if (!result.getUpdateUnitList().isEmpty()) {
			logger.print("修改组织({}):{}.", result.getUpdateUnitList().size(),
					StringUtils.join(result.getUpdateUnitList(), ","));
		}
		if (!result.getRemoveUnitList().isEmpty()) {
			logger.print("删除组织({}):{}.", result.getRemoveUnitList().size(),
					StringUtils.join(result.getRemoveUnitList(), ","));
		}
		if (!result.getCreatePersonList().isEmpty()) {
			logger.print("创建个人({}):{}.", result.getCreatePersonList().size(),
					StringUtils.join(result.getCreatePersonList(), ","));
		}
		if (!result.getUpdatePersonList().isEmpty()) {
			logger.print("修改个人({}):{}.", result.getUpdatePersonList().size(),
					StringUtils.join(result.getUpdatePersonList(), ","));
		}
		if (!result.getRemovePersonList().isEmpty()) {
			logger.print("删除个人({}):{}.", result.getRemovePersonList().size(),
					StringUtils.join(result.getRemovePersonList(), ","));
		}
		if (!result.getCreateIdentityList().isEmpty()) {
			logger.print("创建身份({}):{}.", result.getCreateIdentityList().size(),
					StringUtils.join(result.getCreateIdentityList(), ","));
		}
		if (!result.getUpdateIdentityList().isEmpty()) {
			logger.print("修改身份({}):{}.", result.getUpdateIdentityList().size(),
					StringUtils.join(result.getUpdateIdentityList(), ","));
		}
		if (!result.getRemoveIdentityList().isEmpty()) {
			logger.print("删除身份({}):{}.", result.getRemoveIdentityList().size(),
					StringUtils.join(result.getRemoveIdentityList(), ","));
		}
R
roo00 已提交
114
		logger.print("从钉钉同步人员结束.");
caixiangyi's avatar
caixiangyi 已提交
115 116 117 118 119
		return result;
	}

	private void check(Business business, PullResult result, List<Unit> units, List<Person> people,
			List<PersonAttribute> personAttributes, List<Identity> identities, String accessToken,
R
roo00 已提交
120
			DingdingFactory factory, Unit sup, Department org) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
121 122 123 124 125 126 127 128 129
		Unit unit = this.checkUnit(business, result, sup, org);
		units.add(unit);
		for (User o : factory.listUser(org)) {
			Person person = this.checkPerson(business, result, accessToken, o);
			/* 如果人员没有手机号,那么就先跳过这个人 */
			if (null != person) {
				people.add(person);
				Identity identity = this.checkIdentity(business, result, person, unit, o);
				identities.add(identity);
R
roo00 已提交
130 131 132 133 134 135 136
				if ((null != o.getExtattr()) && (!o.getExtattr().isEmpty())) {
					for (Entry<String, String> en : o.getExtattr().entrySet()) {
						PersonAttribute personAttribute = this.checkPersonAttribute(business, result, person, o,
								en.getKey(), en.getValue());
						personAttributes.add(personAttribute);
					}
				}
caixiangyi's avatar
caixiangyi 已提交
137 138
			}
		}
R
roo00 已提交
139
		for (Department o : factory.listSub(org)) {
caixiangyi's avatar
caixiangyi 已提交
140 141 142 143
			this.check(business, result, units, people, personAttributes, identities, accessToken, factory, unit, o);
		}
	}

R
roo00 已提交
144 145
	private Unit checkUnit(Business business, PullResult result, Unit sup, Department org) throws Exception {
		Unit unit = business.unit().getWithDingdingIdObject(Objects.toString(org.getId()));
caixiangyi's avatar
caixiangyi 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
		if (null != unit) {
			if ((null == sup) && (StringUtils.isNotEmpty(unit.getSuperior()))) {
				/* 不是一个顶层组织所以只能删除重建 */
				removeUnit(business, result, unit);
				unit = null;
			}
			if ((null != sup) && (!StringUtils.equals(sup.getId(), unit.getSuperior()))) {
				/* 指定的上级部门和预期不符 */
				removeUnit(business, result, unit);
				unit = null;
			}
		}
		if (null == unit) {
			unit = this.createUnit(business, result, sup, org);
		} else {
R
roo00 已提交
161
			if (!StringUtils.equals(unit.getDingdingHash(), DigestUtils.sha256Hex(XGsonBuilder.toJson(org)))) {
caixiangyi's avatar
caixiangyi 已提交
162 163 164 165 166 167
				unit = this.updateUnit(business, result, unit, org);
			}
		}
		return unit;
	}

R
roo00 已提交
168
	private Unit createUnit(Business business, PullResult result, Unit sup, Department org) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
169 170 171 172
		EntityManagerContainer emc = business.entityManagerContainer();
		Unit unit = new Unit();
		emc.beginTransaction(Unit.class);
		unit.setName(org.getName());
R
roo00 已提交
173 174 175
		unit.setUnique(org.getId().toString());
		unit.setDingdingId(org.getId().toString());
		unit.setDingdingHash(DigestUtils.sha256Hex(XGsonBuilder.toJson(org)));
caixiangyi's avatar
caixiangyi 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
		if (null != sup) {
			unit.setSuperior(sup.getId());
		}
		if (null != org.getOrder()) {
			unit.setOrderNumber(org.getOrder().intValue());
		}
		business.unit().adjustInherit(unit);
		emc.persist(unit);
		emc.commit();
		result.getCreateUnitList().add(unit.getDistinguishedName());
		return unit;
	}

R
roo00 已提交
189
	private Unit updateUnit(Business business, PullResult result, Unit unit, Department department) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
190 191
		EntityManagerContainer emc = business.entityManagerContainer();
		emc.beginTransaction(Unit.class);
R
roo00 已提交
192
		unit.setDingdingHash(DigestUtils.sha256Hex(XGsonBuilder.toJson(department)));
caixiangyi's avatar
caixiangyi 已提交
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
		unit.setName(department.getName());
		emc.check(unit, CheckPersistType.all);
		emc.commit();
		result.getUpdateUnitList().add(unit.getDistinguishedName());
		return unit;
	}

	private void removeUnit(Business business, PullResult result, Unit unit) throws Exception {
		logger.print("正在删除组织{}.", unit.getDistinguishedName());
		List<Unit> os = new ArrayList<>();
		os.add(unit);
		os.addAll(business.unit().listSupNestedObject(unit));
		os = os.stream().sorted(Comparator.comparing(Unit::getLevel, Comparator.nullsLast(Integer::compareTo)))
				.collect(Collectors.toList());
		for (Unit o : os) {
			this.removeSingleUnit(business, result, o);
		}
	}

	private void removeSingleUnit(Business business, PullResult result, Unit unit) throws Exception {
		logger.print("正在删除单个组织{}.", unit.getDistinguishedName());
		EntityManagerContainer emc = business.entityManagerContainer();
		emc.beginTransaction(UnitAttribute.class);
		emc.beginTransaction(UnitDuty.class);
		emc.beginTransaction(Identity.class);
		emc.beginTransaction(Unit.class);
		for (UnitAttribute o : emc.listEqual(UnitAttribute.class, UnitAttribute.unit_FIELDNAME, unit.getId())) {
			emc.remove(o, CheckRemoveType.all);
			result.getRemoveUnitAttributeList().add(o.getDistinguishedName());
		}
		for (UnitDuty o : emc.listEqual(UnitDuty.class, UnitDuty.unit_FIELDNAME, unit.getId())) {
			emc.remove(o, CheckRemoveType.all);
			result.getRemoveUnitDutyList().add(o.getDistinguishedName());
		}
		for (Identity o : emc.listEqual(Identity.class, Identity.unit_FIELDNAME, unit.getId())) {
			emc.remove(o, CheckRemoveType.all);
			result.getRemoveIdentityList().add(o.getDistinguishedName());
		}
		emc.remove(unit, CheckRemoveType.all);
		emc.commit();
		result.getRemoveUnitList().add(unit.getDistinguishedName());
	}

	private Person checkPerson(Business business, PullResult result, String accessToken, User user) throws Exception {
R
roo00 已提交
237
		Person person = business.person().getWithDingdingIdObject(user.getUserid());
caixiangyi's avatar
caixiangyi 已提交
238
		if (null == person) {
R
roo00 已提交
239
			if ((StringUtils.isNotEmpty(user.getMobile())) && StringUtils.isNotEmpty(user.getName())) {
caixiangyi's avatar
caixiangyi 已提交
240 241 242
				person = this.createOrLinkPerson(business, result, user);
			}
		} else {
R
roo00 已提交
243
			if (!StringUtils.equals(DigestUtils.sha256Hex(XGsonBuilder.toJson(user)), person.getDingdingHash())) {
caixiangyi's avatar
caixiangyi 已提交
244 245 246 247 248 249 250 251 252 253 254
				person = this.updatePerson(business, result, person, user);
			}
		}
		return person;
	}

	private Person createOrLinkPerson(Business business, PullResult result, User user) throws Exception {
		EntityManagerContainer emc = business.entityManagerContainer();
		emc.beginTransaction(Person.class);
		Person person = emc.flag(user.getMobile(), Person.class);
		if (null != person) {
R
roo00 已提交
255 256 257
			person.setDingdingId(Objects.toString(user.getUserid()));
			person.setDingdingHash(DigestUtils.sha256Hex(XGsonBuilder.toJson(user)));
			person.setName(user.getName());
caixiangyi's avatar
caixiangyi 已提交
258
			person.setMobile(user.getMobile());
R
roo00 已提交
259
			person.setUnique(user.getUserid());
caixiangyi's avatar
caixiangyi 已提交
260
			person.setMail(user.getEmail());
R
roo00 已提交
261
			person.setOfficePhone(user.getMobile());
caixiangyi's avatar
caixiangyi 已提交
262 263 264 265 266
			person.setGenderType(GenderType.d);
			emc.check(person, CheckPersistType.all);
			result.getUpdatePersonList().add(person.getDistinguishedName());
		} else {
			person = new Person();
R
roo00 已提交
267 268 269
			person.setDingdingId(Objects.toString(user.getUserid()));
			person.setDingdingHash(DigestUtils.sha256Hex(XGsonBuilder.toJson(user)));
			person.setName(user.getName());
caixiangyi's avatar
caixiangyi 已提交
270
			person.setMobile(user.getMobile());
R
roo00 已提交
271 272
			person.setUnique(user.getUserid());
			// person.setEmployee(user.getJobNumber());
caixiangyi's avatar
caixiangyi 已提交
273
			person.setMail(user.getEmail());
R
roo00 已提交
274 275
			person.setOfficePhone(user.getMobile());
			person.setGenderType(GenderType.d);
caixiangyi's avatar
caixiangyi 已提交
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
			/* 新增人员需要增加密码 */
			business.person().setPassword(person, this.getPassword(engine, pattern, person));
			emc.persist(person, CheckPersistType.all);
			result.getCreatePersonList().add(person.getDistinguishedName());
		}
		emc.commit();
		return person;
	}

	private String getPassword(ScriptEngine engine, Pattern pattern, Person person) throws Exception {
		String str = Config.person().getPassword();
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			String eval = matcher.group(1);
			engine.put("person", person);
			String pass = engine.eval(eval).toString();
			return pass;
		} else {
			return str;
		}
	}

	private Person updatePerson(Business business, PullResult result, Person person, User user) throws Exception {
		EntityManagerContainer emc = business.entityManagerContainer();
		emc.beginTransaction(Person.class);
R
roo00 已提交
301 302 303
		person.setDingdingHash(DigestUtils.sha256Hex(XGsonBuilder.toJson(user)));
		// person.setEmployee(user.getJobNumber());
		person.setName(user.getName());
caixiangyi's avatar
caixiangyi 已提交
304 305
		person.setMobile(user.getMobile());
		person.setMail(user.getEmail());
R
roo00 已提交
306
		person.setOfficePhone(user.getMobile());
caixiangyi's avatar
caixiangyi 已提交
307 308 309 310 311 312
		emc.check(person, CheckPersistType.all);
		emc.commit();
		result.getUpdatePersonList().add(person.getDistinguishedName());
		return person;
	}

R
roo00 已提交
313 314
	private PersonAttribute checkPersonAttribute(Business business, PullResult result, Person person, User user,
			String name, String value) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
315 316 317 318 319 320
		EntityManagerContainer emc = business.entityManagerContainer();
		EntityManager em = emc.get(PersonAttribute.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<PersonAttribute> cq = cb.createQuery(PersonAttribute.class);
		Root<PersonAttribute> root = cq.from(PersonAttribute.class);
		Predicate p = cb.equal(root.get(PersonAttribute_.person), person.getId());
R
roo00 已提交
321
		p = cb.and(p, cb.equal(root.get(PersonAttribute_.name), name));
caixiangyi's avatar
caixiangyi 已提交
322 323 324
		List<PersonAttribute> os = em.createQuery(cq.select(root).where(p)).setMaxResults(1).getResultList();
		PersonAttribute personAttribute = null;
		if (os.size() == 0) {
R
roo00 已提交
325
			personAttribute = this.createPersonAttribute(business, result, person, name, value);
caixiangyi's avatar
caixiangyi 已提交
326 327
		} else {
			personAttribute = os.get(0);
R
roo00 已提交
328 329
			if (!StringUtils.equals(personAttribute.getAttributeList().get(0), value)) {
				personAttribute = this.updatePersonAttribute(business, result, personAttribute, name, value);
caixiangyi's avatar
caixiangyi 已提交
330 331 332 333 334
			}
		}
		return personAttribute;
	}

R
roo00 已提交
335 336
	private PersonAttribute createPersonAttribute(Business business, PullResult result, Person person, String name,
			String value) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
337 338 339
		business.entityManagerContainer().beginTransaction(PersonAttribute.class);
		PersonAttribute personAttribute = new PersonAttribute();
		personAttribute.setPerson(person.getId());
R
roo00 已提交
340 341
		personAttribute.setName(name);
		personAttribute.setAttributeList(ListTools.toList(value));
caixiangyi's avatar
caixiangyi 已提交
342 343 344 345 346 347 348
		business.entityManagerContainer().persist(personAttribute, CheckPersistType.all);
		business.entityManagerContainer().commit();
		result.getCreatePersonAttributeList().add(personAttribute.getDistinguishedName());
		return personAttribute;
	}

	private PersonAttribute updatePersonAttribute(Business business, PullResult result, PersonAttribute personAttribute,
R
roo00 已提交
349
			String name, String value) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
350
		business.entityManagerContainer().beginTransaction(PersonAttribute.class);
R
roo00 已提交
351
		personAttribute.setAttributeList(ListTools.toList(value));
caixiangyi's avatar
caixiangyi 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
		business.entityManagerContainer().commit();
		result.getUpdatePersonAttributeList().add(personAttribute.getDistinguishedName());
		return personAttribute;
	}

	private void removePerson(Business business, PullResult result, Person person) throws Exception {
		EntityManagerContainer emc = business.entityManagerContainer();
		emc.beginTransaction(Person.class);
		emc.beginTransaction(PersonAttribute.class);
		for (PersonAttribute o : emc.listEqual(PersonAttribute.class, PersonAttribute.person_FIELDNAME,
				person.getId())) {
			result.getRemovePersonAttributeList().add(o.getDistinguishedName());
			emc.remove(o, CheckRemoveType.all);
		}
		emc.remove(person, CheckRemoveType.all);
		emc.commit();
		result.getRemovePersonList().add(person.getDistinguishedName());
	}

R
roo00 已提交
371
	@SuppressWarnings("unchecked")
caixiangyi's avatar
caixiangyi 已提交
372 373 374 375 376 377 378 379 380 381 382
	private Identity checkIdentity(Business business, PullResult result, Person person, Unit unit, User user)
			throws Exception {
		EntityManagerContainer emc = business.entityManagerContainer();
		EntityManager em = emc.get(Identity.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Identity> cq = cb.createQuery(Identity.class);
		Root<Identity> root = cq.from(Identity.class);
		Predicate p = cb.equal(root.get(Identity_.person), person.getId());
		p = cb.and(p, cb.equal(root.get(Identity_.unit), unit.getId()));
		List<Identity> os = em.createQuery(cq.select(root).where(p).distinct(true)).setMaxResults(1).getResultList();
		Identity identity = null;
R
roo00 已提交
383 384 385 386 387 388 389 390 391 392
		Long order = null;
		if (StringUtils.isNotEmpty(user.getOrderInDepts())) {
			Map<Long, Long> map = new HashMap<Long, Long>();
			map = XGsonBuilder.instance().fromJson(user.getOrderInDepts(), map.getClass());
			for (Entry<Long, Long> en : map.entrySet()) {
				if (Objects.equals(Long.parseLong(unit.getDingdingId()), en.getKey())) {
					order = en.getValue();
				}
			}
		}
caixiangyi's avatar
caixiangyi 已提交
393
		if (os.size() == 0) {
R
roo00 已提交
394
			identity = this.createIdentity(business, result, person, unit, user, order);
caixiangyi's avatar
caixiangyi 已提交
395 396
		} else {
			identity = os.get(0);
R
roo00 已提交
397
			identity = this.updateIdentity(business, result, unit, identity, user, order);
caixiangyi's avatar
caixiangyi 已提交
398 399 400 401
		}
		return identity;
	}

R
roo00 已提交
402 403
	private Identity createIdentity(Business business, PullResult result, Person person, Unit unit, User user,
			Long order) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
404 405 406 407 408 409 410 411 412
		EntityManagerContainer emc = business.entityManagerContainer();
		emc.beginTransaction(Identity.class);
		Identity identity = new Identity();
		identity.setName(person.getName());
		identity.setPerson(person.getId());
		identity.setUnit(unit.getId());
		identity.setUnitLevel(unit.getLevel());
		identity.setUnitLevelName(unit.getLevelName());
		identity.setUnitName(unit.getName());
R
roo00 已提交
413 414
		if (order != null) {
			identity.setOrderNumber(order.intValue());
caixiangyi's avatar
caixiangyi 已提交
415 416 417 418 419 420 421
		}
		emc.persist(identity, CheckPersistType.all);
		emc.commit();
		result.getCreateIdentityList().add(identity.getDistinguishedName());
		return identity;
	}

R
roo00 已提交
422 423 424 425 426 427 428 429 430 431 432
	private Identity updateIdentity(Business business, PullResult result, Unit unit, Identity identity, User user,
			Long order) throws Exception {
		if (null != order) {
			if (!StringUtils.equals(Objects.toString(identity.getOrderNumber(), ""), Objects.toString(order, ""))) {
				EntityManagerContainer emc = business.entityManagerContainer();
				emc.beginTransaction(Identity.class);
				if (order != null) {
					identity.setOrderNumber(order.intValue());
				}
				emc.commit();
				result.getUpdateIdentityList().add(identity.getDistinguishedName());
caixiangyi's avatar
caixiangyi 已提交
433 434 435 436 437 438 439 440 441 442 443 444
			}
		}
		return identity;
	}

	private void clean(Business business, PullResult result, List<Unit> units, List<Person> people,
			List<Identity> identities) throws Exception {
		EntityManagerContainer emc = business.entityManagerContainer();
		List<Identity> allIdentities = this.listIdentity(business);
		/* 删除身份 */
		for (Identity identity : ListUtils.subtract(allIdentities, identities)) {
			Person person = emc.find(identity.getPerson(), Person.class);
R
roo00 已提交
445 446 447 448 449 450 451 452 453 454 455
			if (null == person || StringUtils.isNotEmpty(person.getQiyeweixinId())) {
				List<UnitDuty> uds = emc.listIsMember(UnitDuty.class, UnitDuty.identityList_FIELDNAME,
						identity.getId());
				if (ListTools.isNotEmpty(uds)) {
					emc.beginTransaction(UnitDuty.class);
					uds.stream().forEach(o -> {
						o.getIdentityList().remove(identity.getId());
					});
					emc.commit();
				}
				emc.beginTransaction(Identity.class);
caixiangyi's avatar
caixiangyi 已提交
456
				emc.remove(identity, CheckRemoveType.all);
R
roo00 已提交
457
				emc.commit();
caixiangyi's avatar
caixiangyi 已提交
458 459 460
			}
		}
		/* 组织单独方法删除 */
R
roo00 已提交
461
		List<Unit> allUnit = this.listUnit(business);
caixiangyi's avatar
caixiangyi 已提交
462 463 464 465 466 467
		List<Unit> removeUnits = ListUtils.subtract(allUnit, units).stream()
				.sorted(Comparator.comparing(Unit::getLevel, Comparator.nullsLast(Integer::compareTo)))
				.collect(Collectors.toList());
		for (Unit unit : removeUnits) {
			this.removeSingleUnit(business, result, unit);
		}
R
roo00 已提交
468
		List<Person> allPeople = this.listPerson(business);
caixiangyi's avatar
caixiangyi 已提交
469 470 471 472 473 474
		/* 删除个人 */
		for (Person person : ListUtils.subtract(allPeople, people)) {
			this.removePerson(business, result, person);
		}
	}

R
roo00 已提交
475
	private List<Unit> listUnit(Business business) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
476 477 478 479 480
		EntityManagerContainer emc = business.entityManagerContainer();
		EntityManager em = emc.get(Unit.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Unit> cq = cb.createQuery(Unit.class);
		Root<Unit> root = cq.from(Unit.class);
R
roo00 已提交
481 482
		Predicate p = cb.notEqual(root.get(Unit_.dingdingId), "");
		p = cb.and(p, cb.isNotNull(root.get(Unit_.dingdingId)));
caixiangyi's avatar
caixiangyi 已提交
483 484 485 486
		List<Unit> os = em.createQuery(cq.select(root).where(p).distinct(true)).getResultList();
		return os;
	}

R
roo00 已提交
487
	private List<Person> listPerson(Business business) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
488 489 490 491 492
		EntityManagerContainer emc = business.entityManagerContainer();
		EntityManager em = emc.get(Person.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Person> cq = cb.createQuery(Person.class);
		Root<Person> root = cq.from(Person.class);
R
roo00 已提交
493 494
		Predicate p = cb.notEqual(root.get(Person_.dingdingId), "");
		p = cb.and(p, cb.isNotNull(root.get(Person_.dingdingId)));
caixiangyi's avatar
caixiangyi 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
		List<Person> os = em.createQuery(cq.select(root).where(p).distinct(true)).getResultList();
		return os;
	}

	private List<Identity> listIdentity(Business business) throws Exception {
		return business.entityManagerContainer().listAll(Identity.class);
	}

	public static class PullResult extends GsonPropertyObject {

		private Date start = new Date();

		private Date end;

		private Long elapsed;

		private List<String> createUnitList = new ArrayList<>();
		private List<String> updateUnitList = new ArrayList<>();
		private List<String> removeUnitList = new ArrayList<>();

		private List<String> createPersonList = new ArrayList<>();
		private List<String> updatePersonList = new ArrayList<>();
		private List<String> removePersonList = new ArrayList<>();

		private List<String> createIdentityList = new ArrayList<>();
		private List<String> updateIdentityList = new ArrayList<>();
		private List<String> removeIdentityList = new ArrayList<>();

		private List<String> createPersonAttributeList = new ArrayList<>();
		private List<String> updatePersonAttributeList = new ArrayList<>();
		private List<String> removePersonAttributeList = new ArrayList<>();

		private List<String> removeUnitDutyList = new ArrayList<>();
		private List<String> removeUnitAttributeList = new ArrayList<>();

		public void end() {
			this.end = new Date();
			this.elapsed = end.getTime() - start.getTime();
		}

		public Date getStart() {
			return start;
		}

		public void setStart(Date start) {
			this.start = start;
		}

		public Date getEnd() {
			return end;
		}

		public void setEnd(Date end) {
			this.end = end;
		}

		public List<String> getCreateUnitList() {
			return createUnitList;
		}

		public void setCreateUnitList(List<String> createUnitList) {
			this.createUnitList = createUnitList;
		}

		public List<String> getUpdateUnitList() {
			return updateUnitList;
		}

		public void setUpdateUnitList(List<String> updateUnitList) {
			this.updateUnitList = updateUnitList;
		}

		public List<String> getCreatePersonList() {
			return createPersonList;
		}

		public void setCreatePersonList(List<String> createPersonList) {
			this.createPersonList = createPersonList;
		}

		public List<String> getUpdatePersonList() {
			return updatePersonList;
		}

		public void setUpdatePersonList(List<String> updatePersonList) {
			this.updatePersonList = updatePersonList;
		}

		public List<String> getCreateIdentityList() {
			return createIdentityList;
		}

		public void setCreateIdentityList(List<String> createIdentityList) {
			this.createIdentityList = createIdentityList;
		}

		public List<String> getRemoveUnitList() {
			return removeUnitList;
		}

		public void setRemoveUnitList(List<String> removeUnitList) {
			this.removeUnitList = removeUnitList;
		}

		public List<String> getRemovePersonList() {
			return removePersonList;
		}

		public void setRemovePersonList(List<String> removePersonList) {
			this.removePersonList = removePersonList;
		}

		public List<String> getRemoveIdentityList() {
			return removeIdentityList;
		}

		public void setRemoveIdentityList(List<String> removeIdentityList) {
			this.removeIdentityList = removeIdentityList;
		}

		public List<String> getRemoveUnitDutyList() {
			return removeUnitDutyList;
		}

		public void setRemoveUnitDutyList(List<String> removeUnitDutyList) {
			this.removeUnitDutyList = removeUnitDutyList;
		}

		public List<String> getRemoveUnitAttributeList() {
			return removeUnitAttributeList;
		}

		public void setRemoveUnitAttributeList(List<String> removeUnitAttributeList) {
			this.removeUnitAttributeList = removeUnitAttributeList;
		}

		public List<String> getRemovePersonAttributeList() {
			return removePersonAttributeList;
		}

		public void setRemovePersonAttributeList(List<String> removePersonAttributeList) {
			this.removePersonAttributeList = removePersonAttributeList;
		}

		public Long getElapsed() {
			return elapsed;
		}

		public void setElapsed(Long elapsed) {
			this.elapsed = elapsed;
		}

		public List<String> getUpdateIdentityList() {
			return updateIdentityList;
		}

		public void setUpdateIdentityList(List<String> updateIdentityList) {
			this.updateIdentityList = updateIdentityList;
		}

		public List<String> getCreatePersonAttributeList() {
			return createPersonAttributeList;
		}

		public void setCreatePersonAttributeList(List<String> createPersonAttributeList) {
			this.createPersonAttributeList = createPersonAttributeList;
		}

		public List<String> getUpdatePersonAttributeList() {
			return updatePersonAttributeList;
		}

		public void setUpdatePersonAttributeList(List<String> updatePersonAttributeList) {
			this.updatePersonAttributeList = updatePersonAttributeList;
		}
	}
}