/* * Copyright (C), 2018-2021 * Project: starter * FileName: DepartmentServiceKit * Author: Louis * Date: 2021/5/14 9:38 */ package com.eco.plugin.xx.jbsync.kit; import com.eco.plugin.xx.jbsync.utils.Utils; import com.fr.decision.authority.AuthorityContext; import com.fr.decision.authority.base.constant.type.operation.ManualOperationType; import com.fr.decision.authority.data.Department; import com.fr.decision.webservice.exception.general.DuplicatedNameException; import com.fr.decision.webservice.v10.user.DepartmentService; import com.fr.general.ComparatorUtils; import com.fr.stable.StableUtils; import com.fr.stable.query.QueryFactory; import com.fr.stable.query.condition.QueryCondition; import com.fr.stable.query.restriction.Restriction; import com.fr.stable.query.restriction.RestrictionFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** *
* * * @author xx * @since 1.0.0 */ public class DepartmentServiceKit extends DepartmentService { public static final String DECISION_DEP_ROOT = "decision-dep-root"; private static volatile DepartmentServiceKit departmentServiceKit = null; public DepartmentServiceKit() { } public static DepartmentServiceKit getInstance() { if (departmentServiceKit == null) { departmentServiceKit = new DepartmentServiceKit(); } return departmentServiceKit; } /** * 根据id获取部门 * @param id * @return * @throws Exception */ public Department getByid(String id) throws Exception { return AuthorityContext.getInstance().getDepartmentController().getById(id); } /** * 添加部门 * @param id * @param pId * @param depName * @return * @throws Exception */ public Department addDepartment(String id, String pId, String depName) throws Exception { if ((Utils.isNotNullStr(pId) && pId.equals(DECISION_DEP_ROOT)) || Utils.isNullStr(pId)) { pId = null; } this.checkDuplicatedDepartmentName(pId, depName); Department department = (new Department()).id(id).name(depName).parentId(pId).creationType(ManualOperationType.KEY).lastOperationType(ManualOperationType.KEY).enable(true); AuthorityContext.getInstance().getDepartmentController().add(department); return department; } private void checkDuplicatedDepartmentName(String parentId, String depName) throws Exception { QueryCondition condition = QueryFactory.create().addRestriction(RestrictionFactory.and(new Restriction[]{RestrictionFactory.eq("name", depName), RestrictionFactory.eq("parentId", parentId)})); Department sameNameDep = AuthorityContext.getInstance().getDepartmentController().findOne(condition); if (sameNameDep != null) { throw new DuplicatedNameException(); } } private String getDepartmentFullPath(String pId, String depName, String splitter) throws Exception { List paths = new ArrayList<>(); paths.add(depName); while (!ComparatorUtils.equals(pId, DECISION_DEP_ROOT) && pId != null) { Department parentDepartment = AuthorityContext.getInstance().getDepartmentController().getById(pId); paths.add(parentDepartment.getName()); pId = parentDepartment.getParentId(); } Collections.reverse(paths); return StableUtils.join(paths.toArray(new String[0]), splitter); } public void editDepartment(String departmentId, String depName, String pId) throws Exception { if (Utils.isNotNullStr(pId) && pId.equals(DECISION_DEP_ROOT)) { pId = null; } Department department = AuthorityContext.getInstance().getDepartmentController().getById(departmentId); String departmentFullPath = DepartmentService.getInstance().getDepartmentFullPath(departmentId); if (!ComparatorUtils.equals(department.getName(), depName)) { this.checkDuplicatedDepartmentName(department.getParentId(), depName); department.setName(depName); department.setParentId(pId); AuthorityContext.getInstance().getDepartmentController().update(department); } } }