/* * Copyright (C), 2018-2021 * Project: starter * FileName: DepartmentServiceKit * Author: Louis * Date: 2021/5/14 9:38 */ package com.fr.plugin.hdmu.kit; import com.fanruan.api.util.StringKit; 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.plugin.hdmu.config.SsoConfig; 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 static com.fr.decision.authority.base.AuthorityConstants.DECISION_DEP_ROOT; /** *
* * * @author fr.open * @since 1.0.0 */ public class DepartmentServiceKit extends DepartmentService { private static volatile DepartmentServiceKit departmentServiceKit = null; public DepartmentServiceKit() { } public static DepartmentServiceKit getInstance() { if (departmentServiceKit == null) { departmentServiceKit = new DepartmentServiceKit(); } return departmentServiceKit; } /** * 根部门与FR根部门转换 * * @param parentId * @return */ public String changeRootId(String parentId) { if (StringKit.isBlank(parentId) || StringKit.equals(parentId, SsoConfig.ROOT_DEP_ID)) { return DECISION_DEP_ROOT; } return parentId; } public void addDepartment(String id, String pId, String depName) throws Exception { if (StringKit.equals(pId, DECISION_DEP_ROOT)) { 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); } 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(); } } public void editDepartment(String departmentId, String depName, String pId) throws Exception { if (StringKit.equals(pId, DECISION_DEP_ROOT)) { pId = null; } Department department = AuthorityContext.getInstance().getDepartmentController().getById(departmentId); if (!ComparatorUtils.equals(department.getName(), depName) || !ComparatorUtils.equals(department.getParentId(), pId)) { this.checkDuplicatedDepartmentName(pId, depName); department.setName(depName); department.setParentId(pId); AuthorityContext.getInstance().getDepartmentController().update(department); } } }