12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace common\services;
- use common\models\Dictionary;
- use common\models\DictionaryRelationship;
- use common\util\DeriveObj;
- use common\util\DictionaryObj;
- class DictionaryService
- {
- /**
- * @return DictionaryObj[]
- */
- public static function getDictionaryObjShowData(): array
- {
- $info = Dictionary::find()
- ->where(["status" => 1])
- ->orderBy("sort asc,id asc")
- ->all();
- return self::getDictionaryObjData($info);
- }
- /**
- * @return DictionaryObj[]
- */
- public static function getDictionaryObjAllData(): array
- {
- $info = Dictionary::find()
- ->orderBy("sort asc,id asc")
- ->all();
- return self::getDictionaryObjData($info);
- }
- /**
- * @param $info
- * @return DictionaryObj[]
- */
- protected static function getDictionaryObjData($info): array
- {
- $data = [];
- $objList = [];
- $relationshipIds = [];
- $relationshipList = DictionaryRelationship::find()->all();
- foreach ($relationshipList as $relationship) {
- /** @var DictionaryRelationship $relationship */
- $relationshipIds[$relationship->dictionary_id][] = $relationship->relation_id;
- }
- foreach ($info as $value) {
- /** @var Dictionary $value */
- $obj = new DictionaryObj($value->id, $value->pid, $value->name, $value->status, $value->multiple, $value->has_children, $value->can_change, $value->can_children, $value->can_multiple, $value->sort);
- $objList[$obj->getId()] = $obj;
- if ($value->has_children > 0) {
- $data[] = $obj;
- }
- }
- foreach ($objList as $key => $obj) {
- if ($obj->getPid() > 0) {
- if (isset($objList[$obj->getPid()])) {
- $objList[$obj->getPid()]->addChildrenToTop($obj);
- }
- }
- if (isset($relationshipIds[$key])) {
- foreach ($relationshipIds[$key] as $id) {
- if (isset($objList[$id])) {
- $obj->addDeriveToBottom(new DeriveObj($id));
- }
- }
- }
- }
- return $data;
- }
- }
|