src/CorporateTrainingBundle/Biz/Org/Service/Impl/OrgServiceImpl.php line 16

Open in your IDE?
  1. <?php
  2. namespace CorporateTrainingBundle\Biz\Org\Service\Impl;
  3. use AppBundle\Common\ArrayToolkit;
  4. use AppBundle\Common\Exception\AccessDeniedException;
  5. use Biz\Org\Service\Impl\OrgServiceImpl as BaseOrgServiceImpl;
  6. use Biz\System\Service\CacheService;
  7. use Biz\System\Service\LogService;
  8. use Codeages\Biz\Framework\Service\Exception\NotFoundException;
  9. use CorporateTrainingBundle\Biz\ManagePermission\Service\ManagePermissionOrgService;
  10. use CorporateTrainingBundle\Biz\Org\Service\OrgService;
  11. use CorporateTrainingBundle\Common\Constant\CTConst;
  12. use CorporateTrainingBundle\Common\OrgTreeToolkit;
  13. class OrgServiceImpl extends BaseOrgServiceImpl implements OrgService
  14. {
  15.     protected $visibleOrgTreeDataLocalCacheArray = [];
  16.     public function createAiCoachOrg(array $fields)
  17.     {
  18.         $parent $this->getOrgDao()->get($fields['parentId']);
  19.         if (empty($parent)) {
  20.             throw $this->createNotFoundException('AI Coach parent org not found');
  21.         }
  22.         if ($this->getOrgDao()->getByCode($fields['code'])) {
  23.             throw $this->createInvalidArgumentException('AI Coach org code already exists');
  24.         }
  25.         $org $this->getOrgDao()->create([
  26.             'name' => $fields['name'],
  27.             'code' => strtoupper($fields['code']),
  28.             'parentId' => $parent['id'],
  29.             'description' => $fields['description'] ?? '',
  30.             'syncId' => (string) $fields['syncId'],
  31.             'createdUserId' => 0,
  32.         ]);
  33.         $this->getOrgDao()->wave([$parent['id']], ['childrenNum' => 1]);
  34.         return $this->getOrgDao()->update($org['id'], [
  35.             'orgCode' => $parent['orgCode'].$org['id'].'.',
  36.             'depth' => $parent['depth'] + 1,
  37.         ]);
  38.     }
  39.     public function updateAiCoachOrg($id, array $fields)
  40.     {
  41.         $org $this->getOrgDao()->get($id);
  42.         $parent $this->getOrgDao()->get($fields['parentId']);
  43.         if (empty($org) || empty($parent)) {
  44.             throw $this->createNotFoundException('AI Coach org not found');
  45.         }
  46.         if (in_array($org['id'], array_filter(explode('.'$parent['orgCode'])))) {
  47.             throw $this->createInvalidArgumentException('AI Coach org cannot move below itself');
  48.         }
  49.         $oldPrefix $org['orgCode'];
  50.         $newPrefix $parent['orgCode'].$org['id'].'.';
  51.         $depthDelta $parent['depth'] + $org['depth'];
  52.         $parentChanged = (int) $org['parentId'] !== (int) $parent['id'];
  53.         $this->beginTransaction();
  54.         try {
  55.             if ($parentChanged) {
  56.                 $this->getOrgDao()->wave([$org['parentId']], ['childrenNum' => -1]);
  57.                 $this->getOrgDao()->wave([$parent['id']], ['childrenNum' => 1]);
  58.             }
  59.             $updated $this->getOrgDao()->update($id, [
  60.                 'name' => $fields['name'],
  61.                 'code' => strtoupper($fields['code']),
  62.                 'parentId' => $parent['id'],
  63.                 'description' => $fields['description'] ?? '',
  64.                 'syncId' => (string) $fields['syncId'],
  65.                 'orgCode' => $newPrefix,
  66.                 'depth' => $parent['depth'] + 1,
  67.             ]);
  68.             if ($oldPrefix !== $newPrefix) {
  69.                 foreach ($this->getOrgDao()->findByPrefixOrgCode($oldPrefix) as $descendant) {
  70.                     if ((int) $descendant['id'] === (int) $id) {
  71.                         continue;
  72.                     }
  73.                     $this->getOrgDao()->update($descendant['id'], [
  74.                         'orgCode' => $newPrefix.substr($descendant['orgCode'], strlen($oldPrefix)),
  75.                         'depth' => $descendant['depth'] + $depthDelta,
  76.                     ]);
  77.                 }
  78.             }
  79.             $this->commit();
  80.             return $updated;
  81.         } catch (\Exception $e) {
  82.             $this->rollback();
  83.             throw $e;
  84.         }
  85.     }
  86.     public function updateAiCoachRootOrg($id, array $fields)
  87.     {
  88.         $org $this->getOrgDao()->get($id);
  89.         $root $this->getOrgByOrgCode(CTConst::ROOT_ORG_CODE);
  90.         if (empty($org) || empty($root) || (int) $org['id'] !== (int) $root['id']) {
  91.             throw $this->createInvalidArgumentException('AI Coach org is not the root org');
  92.         }
  93.         $code strtoupper($fields['code']);
  94.         $sameCodeOrg $this->getOrgDao()->getByCode($code);
  95.         if (!empty($sameCodeOrg) && (int) $sameCodeOrg['id'] !== (int) $root['id']) {
  96.             throw $this->createInvalidArgumentException('AI Coach org code already exists');
  97.         }
  98.         return $this->getOrgDao()->update($root['id'], [
  99.             'name' => $fields['name'],
  100.             'code' => $code,
  101.             'description' => $fields['description'] ?? '',
  102.             'syncId' => (string) $fields['syncId'],
  103.         ]);
  104.     }
  105.     public function deleteAiCoachOrg($id)
  106.     {
  107.         $org $this->getOrgDao()->get($id);
  108.         if (empty($org)) {
  109.             return;
  110.         }
  111.         $this->beginTransaction();
  112.         try {
  113.             $this->getOrgDao()->deleteByPrefixOrgCode($org['orgCode']);
  114.             $this->getOrgDao()->wave([$org['parentId']], ['childrenNum' => -1]);
  115.             $this->commit();
  116.         } catch (\Exception $e) {
  117.             $this->rollback();
  118.             throw $e;
  119.         }
  120.     }
  121.     /**
  122.      * @param $id
  123.      * @param $num //正数或负数
  124.      *
  125.      * @return array
  126.      *
  127.      * @throws NotFoundException
  128.      */
  129.     public function waveOrgChildrenNum($id$num)
  130.     {
  131.         $org $this->checkBeforeProccess($id);
  132.         $childrenNum = ($org['childrenNum'] + $num) < $org['childrenNum'] + $num;
  133.         return $this->getOrgDao()->update($id, ['childrenNum' => $childrenNum]);
  134.     }
  135.     public function countOrgs(array $conditions)
  136.     {
  137.         return $this->getOrgDao()->count($conditions);
  138.     }
  139.     public function getOrgBySyncId($syncId)
  140.     {
  141.         return $this->getOrgDao()->getOrgBySyncId($syncId);
  142.     }
  143.     public function findOrgsBySyncIds($syncIds)
  144.     {
  145.         return $this->getOrgDao()->findOrgsBySyncIds($syncIds);
  146.     }
  147.     public function findOrgsByOrgCodes(array $orgCodes)
  148.     {
  149.         return $this->getOrgDao()->findByOrgCodes($orgCodes);
  150.     }
  151.     public function findOrgsByCodes(array $codes)
  152.     {
  153.         return $this->getOrgDao()->findByCodes($codes);
  154.     }
  155.     public function findSelfAndParentOrgsByOrgCode($orgCode)
  156.     {
  157.         $preOrgIdArray explode('.'$orgCode);
  158.         return $this->findOrgsByIds($preOrgIdArray);
  159.     }
  160.     public function findOrgsByPrefixOrgCodes(array $orgCodes = ['1.'], $columns = [])
  161.     {
  162.         $cacheName md5(json_encode($orgCodes).'|'.json_encode($columns));
  163.         $cacheOrgs $this->getCacheService()->get($cacheName);
  164.         if (!empty($cacheOrgs)) {
  165.             return $cacheOrgs;
  166.         }
  167.         $result $this->getOrgDao()->findByPrefixOrgCodes($orgCodes$columns);
  168.         $this->getCacheService()->clear($cacheName);
  169.         $this->getCacheService()->set($cacheName$resulttime() + 60 3);
  170.         return $result;
  171.     }
  172.     public function getOrgMaxDepth($conditions)
  173.     {
  174.         return $this->getOrgDao()->getMaxDepth($conditions);
  175.     }
  176.     public function resetOrg()
  177.     {
  178.         $currentUser $this->getCurrentUser();
  179.         if ($currentUser->isSuperAdmin() || $currentUser->hasPermission('admin_org_sync_department')) {
  180.             $org $this->getOrg(CTConst::ROOT_ORG_CODE);
  181.             $this->getOrgDao()->wave([$org['id']], ['childrenNum' => -$org['childrenNum']]);
  182.             $this->getOrgDao()->update($org['id'], ['syncId' => 0]);
  183.             return $this->getOrgDao()->deleteOrgsWithoutOrgCode(CTConst::ROOT_ORG_CODE);
  184.         }
  185.         return false;
  186.     }
  187.     public function resetOrgSyncId()
  188.     {
  189.         $this->getOrgDao()->resetOrgSyncId();
  190.     }
  191.     public function buildOrgTreeByCode($orgCode)
  192.     {
  193.         if (empty($orgCode)) {
  194.             return [];
  195.         }
  196.         $orgInfo $this->getOrgByOrgCode($orgCode);
  197.         $orgs $this->findOrgsByPrefixOrgCode($orgCode);
  198.         $orgs ArrayToolkit::index($orgs'id');
  199.         for ($i CTConst::ORG_MAX_DEPTH$i >= ($orgInfo['depth'] + 1); --$i) {
  200.             foreach ($orgs as $key => $org) {
  201.                 if (!empty($org['depth']) && $org['depth'] == $i && $org['depth'] > 1) {
  202.                     $orgs[$org['parentId']]['nodes'][] = $org;
  203.                     unset($orgs[$key]);
  204.                 }
  205.             }
  206.         }
  207.         return array_values($orgs);
  208.     }
  209.     public function buildVisibleOrgTreeByOrgCodes(array $orgCodes)
  210.     {
  211.         return array_values(OrgTreeToolkit::makeTree($this->getVisibleOrgTreeDataByOrgCodes($orgCodestrue)));
  212.     }
  213.     /**
  214.      * @param $settingOrgIds
  215.      * 被设置的用户或资源的部门orgIds
  216.      * @param bool $filter
  217.      *
  218.      * @return array|mixed
  219.      *
  220.      * 部门设置特定树结构(展示管理员的管理范围及被操作用户的超出管理员权限部分的部门,部门树中超出我管理范围的部分只展示顶级)
  221.      */
  222.     public function getPermissionOrgTreeData($settingOrgIds$filter false)
  223.     {
  224.         $orgCodes $this->getCurrentUser()->getManageOrgCodes();
  225.         if (empty($orgCodes)) {
  226.             return [];
  227.         }
  228.         $cacheKey $this->getPermissionOrgTreeDataLocalCacheKey($orgCodes$settingOrgIds$filter);
  229.         if (isset($this->visibleOrgTreeDataLocalCacheArray[$cacheKey])) {
  230.             return $this->visibleOrgTreeDataLocalCacheArray[$cacheKey];
  231.         }
  232.         $orgs $this->findOrgsByPrefixOrgCodes($orgCodes);
  233.         $orgCodes ArrayToolkit::column($orgs'orgCode');
  234.         $settingOrgs $this->findOrgsByIds($settingOrgIds);
  235.         $settingOrgCodes ArrayToolkit::column($settingOrgs'orgCode');
  236.         $diffOrgCodes array_diff($settingOrgCodes$orgCodes);
  237.         if (in_array('1.'$diffOrgCodestrue)) {
  238.             $parentOrg $this->getOrgByOrgCode('1.');
  239.             $allOrgs $this->findOrgsByParentId($parentOrg['id']);
  240.             $allOrgs array_merge($allOrgs, [$parentOrg], $orgs);
  241.             if ($filter) {
  242.                 $allOrgs $this->filterOrgs($allOrgs);
  243.             }
  244.             foreach ($allOrgs as &$org) {
  245.                 $org['disableCheckbox'] = true;
  246.                 $org['selectable'] = false;
  247.             }
  248.         } else {
  249.             $diffOrgs $this->findOrgsByOrgCodes(array_values($diffOrgCodes));
  250.             $orgCodeIds $this->exploreOrgCodes($orgCodes);
  251.             $parentOrgIds array_diff($orgCodeIdsArrayToolkit::column($orgs'id'));
  252.             $parentOrgs $this->findOrgsByIds(array_values($parentOrgIds));
  253.             $allOrgs array_merge($orgs$parentOrgs$diffOrgs);
  254.             if ($filter) {
  255.                 $allOrgs $this->filterOrgs($allOrgs);
  256.             }
  257.             $diffOrgTreeOrgCodes = [];
  258.             if (!empty($diffOrgCodes)) {
  259.                 $diffOrgTree $this->findOrgsByPrefixOrgCodes($diffOrgCodes, ['orgCode']);
  260.                 $diffOrgTreeOrgCodes ArrayToolkit::column($diffOrgTree'orgCode');
  261.             }
  262.             foreach ($allOrgs as &$org) {
  263.                 if (in_array($org['id'], $parentOrgIds) || in_array($org['orgCode'], $diffOrgTreeOrgCodes)) {
  264.                     $org['selectable'] = false;
  265.                     $org['disableCheckbox'] = true;
  266.                 } else {
  267.                     $org['selectable'] = true;
  268.                     $org['disableCheckbox'] = false;
  269.                 }
  270.             }
  271.         }
  272.         $indexedAllOrgs ArrayToolkit::index($allOrgs'orgCode');
  273.         ksort($indexedAllOrgs);
  274.         $treeData array_values($indexedAllOrgs);
  275.         $this->visibleOrgTreeDataLocalCacheArray[$cacheKey] = $treeData;
  276.         return $treeData;
  277.     }
  278.     /*
  279.      * 获取可见的部门树组件数据,直线上级及所有下级
  280.      */
  281.     public function getVisibleOrgTreeDataByOrgCodes(array $orgCodes$filter false)
  282.     {
  283.         $cacheKey $this->getVisibleOrgTreeDataLocalCacheKey($orgCodes$filter);
  284.         if (isset($this->visibleOrgTreeDataLocalCacheArray[$cacheKey])) {
  285.             return $this->visibleOrgTreeDataLocalCacheArray[$cacheKey];
  286.         }
  287.         $orgs $this->findOrgsByPrefixOrgCodes($orgCodes);
  288.         $orgCodes ArrayToolkit::column($orgs'orgCode');
  289.         $orgCodeIds $this->exploreOrgCodes($orgCodes);
  290.         $parentOrgIds array_diff($orgCodeIdsArrayToolkit::column($orgs'id'));
  291.         $parentOrgs $this->findOrgsByIds(array_values($parentOrgIds));
  292.         $allOrgs array_merge($orgs$parentOrgs);
  293.         if ($filter) {
  294.             $allOrgs $this->filterOrgs($allOrgs);
  295.         }
  296.         foreach ($allOrgs as &$org) {
  297.             if (in_array($org['id'], $parentOrgIds)) {
  298.                 $org['selectable'] = false;
  299.                 $org['disableCheckbox'] = true;
  300.             } else {
  301.                 $org['selectable'] = true;
  302.                 $org['disableCheckbox'] = false;
  303.             }
  304.         }
  305.         $indexedAllOrgs ArrayToolkit::index($allOrgs'orgCode');
  306.         ksort($indexedAllOrgs);
  307.         $treeData array_values($indexedAllOrgs);
  308.         $this->visibleOrgTreeDataLocalCacheArray[$cacheKey] = $treeData;
  309.         return $treeData;
  310.     }
  311.     public function buildOrgTreeByParentId($parentId$offset$limit$filter false)
  312.     {
  313.         $orgs $this->findOrgsDataByParentId($parentId$offset$limit$filter);
  314.         if (empty($orgs)) {
  315.             return [];
  316.         }
  317.         $orgTree array_values(OrgTreeToolkit::makeTree($orgs));
  318.         $childrenCounts $this->childrenCountsByOrgIds(ArrayToolkit::column($orgs'id'));
  319.         $childrenCounts ArrayToolkit::index($childrenCounts'parentId');
  320.         $orgTree $this->findChildrenTree($orgTree[0], $parentId$childrenCounts);
  321.         return $orgTree;
  322.     }
  323.     public function findOrgsDataByParentId($parentId$offset$limit$filter false)
  324.     {
  325.         $orgs $this->searchOrgs(['parentId' => $parentId], ['orgCode' => 'ASC'], $offset$limit);
  326.         if (empty($orgs)) {
  327.             return [];
  328.         }
  329.         $parentOrg $this->getOrg($parentId);
  330.         $parentIds $this->parentOrgIdsByOrgCodes([$parentOrg['orgCode']]);
  331.         $orgs array_merge($orgs$this->findOrgsByIds(array_merge($parentIds, [$parentId])));
  332.         if ($filter) {
  333.             $orgs $this->filterOrgs($orgs);
  334.         }
  335.         foreach ($orgs as &$org) {
  336.             if (in_array($org['id'], $parentIdstrue)) {
  337.                 $org['selectable'] = false;
  338.                 $org['disableCheckbox'] = true;
  339.             } else {
  340.                 $org['selectable'] = true;
  341.                 $org['disableCheckbox'] = false;
  342.             }
  343.         }
  344.         return $orgs;
  345.     }
  346.     public function buildCanManageOrgTreeByParentId($parentId$settingOrgIds$offset$limit$filter false)
  347.     {
  348.         $orgs $this->findCanManageOrgsDataByParentId($parentId$settingOrgIds$offset$limit$filter);
  349.         if (empty($orgs)) {
  350.             return [];
  351.         }
  352.         $orgTree array_values(OrgTreeToolkit::makeTree($orgs));
  353.         $childrenCounts $this->childrenCountsByOrgIds(ArrayToolkit::column($orgs'id'));
  354.         $childrenCounts ArrayToolkit::index($childrenCounts'parentId');
  355.         $orgTree $this->findChildrenTree($orgTree[0], $parentId$childrenCounts);
  356.         return $orgTree;
  357.     }
  358.     public function findCanManageOrgsDataByParentId($parentId$settingOrgIds$offset$limit$filter false)
  359.     {
  360.         $manageOrgCodes $this->getCurrentUser()->getManageOrgCodes();
  361.         if (empty($manageOrgCodes)) {
  362.             return [];
  363.         }
  364.         list($manageOrgIds$lastOrgIds) = $this->exploreOrgCodesAndLastOrgIds($manageOrgCodes);
  365.         $noPermissionOrgIds array_values(array_filter($this->parentOrgIdsByOrgCodes($manageOrgCodes)));
  366.         $noPermissionSettingOrgIds array_intersect($noPermissionOrgIds$settingOrgIds);
  367.         $parentOrg $this->getOrg($parentId);
  368.         $parentOrgIds $this->parentOrgIdsByOrgCodes([$parentOrg['orgCode']]);
  369.         $conditions = ['parentId' => $parentId];
  370.         if (!in_array('1'$lastOrgIdstrue)) {
  371.             if (empty(array_intersect($manageOrgIds$parentOrgIds)) || (in_array(
  372.                 $parentId,
  373.                 $manageOrgIds,
  374.                 true
  375.             ) && !in_array($parentId$lastOrgIdstrue))) {
  376.                 $conditions['orgIds'] = $manageOrgIds;
  377.             }
  378.         }
  379.         $allOrgs $this->searchOrgs(
  380.             $conditions,
  381.             ['orgCode' => 'ASC'],
  382.             $offset,
  383.             $limit
  384.         );
  385.         if (empty($allOrgs)) {
  386.             return [];
  387.         }
  388.         $parentOrgs $this->findOrgsByIds($parentOrgIds);
  389.         $allOrgs array_merge($allOrgs$parentOrgs, [$parentOrg]);
  390.         if ($filter) {
  391.             $allOrgs $this->filterOrgs($allOrgs);
  392.         }
  393.         foreach ($allOrgs as &$org) {
  394.             $exploreOrgIds $this->exploreOrgCodes([$org['orgCode']]);
  395.             $exploreOrgIds array_diff($exploreOrgIds$noPermissionOrgIds);
  396.             if ((!empty($noPermissionSettingOrgIds) && !empty(
  397.                     array_intersect(
  398.                         $exploreOrgIds,
  399.                         $noPermissionSettingOrgIds
  400.                     )
  401.                     )) || in_array($org['id'], $noPermissionOrgIdstrue)) {
  402.                 $org['selectable'] = false;
  403.                 $org['disableCheckbox'] = true;
  404.             } else {
  405.                 $org['selectable'] = true;
  406.                 $org['disableCheckbox'] = false;
  407.             }
  408.         }
  409.         return $allOrgs;
  410.     }
  411.     public function findOrgsByParentId($orgId)
  412.     {
  413.         return $this->getOrgDao()->findByParentOrgId($orgId);
  414.     }
  415.     public function findOrgsByParentIds($orgIds)
  416.     {
  417.         return $this->getOrgDao()->findByParentOrgIds($orgIds);
  418.     }
  419.     public function findParentOrgIdsByOrgId($orgId)
  420.     {
  421.         $parentOrgIds = [];
  422.         $parentOrgIds $this->findParentOrgIdByIdAndCategories($orgId$parentOrgIds);
  423.         return array_merge($parentOrgIds, [$orgId]);
  424.     }
  425.     protected function findParentOrgIdByIdAndCategories($orgId, &$parentOrgIds)
  426.     {
  427.         $org $this->getOrg($orgId);
  428.         if (!= $org['parentId']) {
  429.             $parentOrg $this->getOrg($org['parentId']);
  430.             $parentOrgIds array_merge($parentOrgIds, [$parentOrg['id']]);
  431.             $this->findParentOrgIdByIdAndCategories($parentOrg['id'], $parentOrgIds);
  432.         }
  433.         return $parentOrgIds;
  434.     }
  435.     /**
  436.      * @param $orgIds
  437.      *
  438.      * @return array 过滤子部门获取顶层部门
  439.      */
  440.     public function wipeOffChildrenOrgIds($orgIds)
  441.     {
  442.         $orgs $this->findOrgsByIds($orgIds);
  443.         $orgs ArrayToolkit::index($orgs'id');
  444.         $orgIds = [];
  445.         foreach ($orgs as $org) {
  446.             if (empty($orgs[$org['parentId']])) {
  447.                 $orgIds[] = $org['id'];
  448.             }
  449.         }
  450.         return $orgIds;
  451.     }
  452.     public function wipeOffChildrenCode($codes)
  453.     {
  454.         if (empty($codes)) {
  455.             return [];
  456.         }
  457.         $orgs $this->findOrgsByCodes($codes);
  458.         $orgs ArrayToolkit::index($orgs'id');
  459.         $orgCodes = [];
  460.         foreach ($orgs as $org) {
  461.             if (empty($orgs[$org['parentId']])) {
  462.                 $orgCodes[] = $org['code'];
  463.             }
  464.         }
  465.         return $orgCodes;
  466.     }
  467.     public function childrenCountsByOrgIds($orgIds)
  468.     {
  469.         return $this->getOrgDao()->childrenCountsByOrgIds($orgIds);
  470.     }
  471.     public function deleteOrgWithoutDeleteChildren($id)
  472.     {
  473.         $org $this->getOrg($id);
  474.         if (empty($org)) {
  475.             return;
  476.         }
  477.         if ('1.' == $org['orgCode']) {
  478.             throw new AccessDeniedException('顶级部门不可删除!');
  479.         }
  480.         try {
  481.             $this->beginTransaction();
  482.             $this->getOrgDao()->delete($id);
  483.             $parentOrg $this->getOrg($org['parentId']);
  484.             $groupChildOrgs ArrayToolkit::group($this->findOrgsByPrefixOrgCode($org['orgCode']), 'parentId');
  485.             $updateFields = [];
  486.             $orgQueue = [$org];
  487.             while ($orgQueue) {
  488.                 $currentOrg array_shift($orgQueue);
  489.                 if ($currentOrg['id'] != $org['id']) {
  490.                     $updateFields[$currentOrg['id']] = [
  491.                         'parentId' => $currentOrg['parentId'] == $org['id'] ? $org['parentId'] : $currentOrg['parentId'],
  492.                         'orgCode' => $currentOrg['parentId'] == $org['id'] ? $parentOrg['orgCode'].$currentOrg['id'].'.' $updateFields[$currentOrg['parentId']]['orgCode'].$currentOrg['id'],
  493.                         'depth' => $currentOrg['depth'] - 1,
  494.                     ];
  495.                 }
  496.                 if (isset($groupChildOrgs[$currentOrg['id']])) {
  497.                     $orgQueue array_merge($orgQueue$groupChildOrgs[$currentOrg['id']]);
  498.                 }
  499.             }
  500.             $this->getOrgDao()->wave([$org['parentId']], ['childrenNum' => $org['childrenNum'] - 1]);
  501.             if ($updateFields) {
  502.                 $this->getOrgDao()->batchUpdate(array_keys($updateFields), $updateFields'id');
  503.             }
  504.             $this->getLogService()->info('org''delete_org'"删除部门{$org['name']}", ['org' => $org]);
  505.             $this->commit();
  506.         } catch (\Exception $e) {
  507.             $this->rollback();
  508.             throw $e;
  509.         }
  510.     }
  511.     protected function findChildrenTree($tree$parentId$childrenCounts)
  512.     {
  513.         if (empty($tree['nodes'])) {
  514.             return $tree;
  515.         }
  516.         $nodes $tree['nodes'];
  517.         foreach ($nodes as &$org) {
  518.             if ($org['id'] == $parentId || == count($org['nodes'])) {
  519.                 return $this->findChildrenTree($org$parentId$childrenCounts);
  520.             }
  521.             if ($org['parentId'] == $parentId && !empty($childrenCounts[$org['id']])) {
  522.                 $org['nodes'] = [['name' => '加载中···''isExist' => $childrenCounts[$org['id']]['count'] > 0]];
  523.             }
  524.         }
  525.         $tree['nodes'] = $nodes;
  526.         return $tree;
  527.     }
  528.     protected function getVisibleOrgTreeDataLocalCacheKey($orgCodes$filter)
  529.     {
  530.         return md5(json_encode($orgCodes).'|'.($filter '1' '0'));
  531.     }
  532.     protected function getPermissionOrgTreeDataLocalCacheKey($orgCodes$settingOrgIds$filter)
  533.     {
  534.         return md5(json_encode($settingOrgIds).'|'.json_encode($orgCodes).'|'.($filter '1' '0'));
  535.     }
  536.     protected function filterOrgs($orgs)
  537.     {
  538.         foreach ($orgs as $key => $org) {
  539.             $orgs[$key] = ArrayToolkit::parts(
  540.                 $org,
  541.                 [
  542.                     'id',
  543.                     'name',
  544.                     'parentId',
  545.                     'seq',
  546.                     'orgCode',
  547.                     'code',
  548.                     'depth',
  549.                 ]
  550.             );
  551.         }
  552.         return $orgs;
  553.     }
  554.     protected function exploreOrgCodes(array $orgCodes)
  555.     {
  556.         $orgIds = [];
  557.         foreach ($orgCodes as $orgCode) {
  558.             $orgIds[] = array_filter(explode('.'$orgCode));
  559.         }
  560.         if (!empty($orgIds)) {
  561.             $orgIds array_merge(...$orgIds);
  562.         }
  563.         return array_unique($orgIds);
  564.     }
  565.     protected function exploreOrgCodesAndLastOrgIds(array $orgCodes)
  566.     {
  567.         $orgGroup = [];
  568.         $lastIds = [];
  569.         foreach ($orgCodes as $orgCode) {
  570.             $orgIds array_filter(explode('.'$orgCode));
  571.             $orgGroup[] = $orgIds;
  572.             $lastIds[] = end($orgIds);
  573.         }
  574.         $orgAllIds = !empty($orgGroup) ? array_merge(...$orgGroup) : [];
  575.         return [array_unique($orgAllIds), $lastIds];
  576.     }
  577.     protected function parentOrgIdsByOrgCodes($orgCodes)
  578.     {
  579.         $orgIds = [];
  580.         foreach ($orgCodes as $orgCode) {
  581.             $orgArr array_filter(explode('.'$orgCode));
  582.             array_pop($orgArr);
  583.             $orgIds[] = $orgArr;
  584.         }
  585.         $orgIds array_merge(...$orgIds);
  586.         return array_unique($orgIds);
  587.     }
  588.     public function filterCurrentUserManageOrgsByOrgs(array $orgs)
  589.     {
  590.         $orgIds $this->getCurrentUser()->getManageOrgIds();
  591.         if (empty($orgIds) || empty($orgs)) {
  592.             return [];
  593.         }
  594.         $manageOrgs = [];
  595.         foreach ($orgs as $org) {
  596.             $orgCodeExplode explode('.'$org['orgCode'] ?? '');
  597.             $intersectIds array_intersect($orgCodeExplode$orgIds);
  598.             if (empty($intersectIds)) {
  599.                 continue;
  600.             }
  601.             $manageOrgs[] = $org;
  602.         }
  603.         return $manageOrgs;
  604.     }
  605.     /**
  606.      * @return array
  607.      */
  608.     public function findCurrentUserManageOrgIds()
  609.     {
  610.         $manageOrgIds $this->getCurrentUser()->getManageOrgIds();
  611.         if (empty($manageOrgIds)) {
  612.             return [];
  613.         }
  614.         $orgs $this->findOrgsByIds($manageOrgIds, ['orgCode']);
  615.         if (empty($orgs)) {
  616.             return [];
  617.         }
  618.         $orgCodes ArrayToolkit::column($orgs'orgCode');
  619.         $manageOrgs $this->findOrgsByPrefixOrgCodes($orgCodes, ['id']);
  620.         if (empty($manageOrgs)) {
  621.             return [];
  622.         }
  623.         return ArrayToolkit::column($manageOrgs'id');
  624.     }
  625.     public function findFullPathOrgsByOrgCodesColumn($list)
  626.     {
  627.         if (empty($list)) {
  628.             return [];
  629.         }
  630.         $orgCodes ArrayToolkit::flatten(ArrayToolkit::column($list'orgCodes'));
  631.         $orgIds ArrayToolkit::flatten(array_map(function ($orgCode) {
  632.             return array_filter(explode('.'$orgCode));
  633.         }, $orgCodes));
  634.         $orgs $this->getOrgDao()->findByIds($orgIds);
  635.         return ArrayToolkit::index($orgs'id');
  636.     }
  637.     public function findFullPathOrgsByOrgCodeColumn($list)
  638.     {
  639.         if (empty($list)) {
  640.             return [];
  641.         }
  642.         $orgCodes ArrayToolkit::column($list'orgCode');
  643.         $orgIds ArrayToolkit::flatten(array_map(function ($orgCode) {
  644.             return array_filter(explode('.'$orgCode));
  645.         }, $orgCodes));
  646.         $orgs $this->getOrgDao()->findByIds($orgIds);
  647.         return ArrayToolkit::index($orgs'id');
  648.     }
  649.     /**
  650.      * @return ManagePermissionOrgService
  651.      */
  652.     protected function getManagePermissionService()
  653.     {
  654.         return $this->createService('CorporateTrainingBundle:ManagePermission:ManagePermissionOrgService');
  655.     }
  656.     /**
  657.      * @return LogService
  658.      */
  659.     protected function getLogService()
  660.     {
  661.         return $this->createService('System:LogService');
  662.     }
  663.     /**
  664.      * @return CacheService
  665.      */
  666.     protected function getCacheService()
  667.     {
  668.         return $this->createService('System:CacheService');
  669.     }
  670. }