DictionaryService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace common\services;
  3. use common\models\Dictionary;
  4. use common\models\DictionaryRelationship;
  5. use common\util\DeriveObj;
  6. use common\util\DictionaryObj;
  7. class DictionaryService
  8. {
  9. /**
  10. * @return DictionaryObj[]
  11. */
  12. public static function getDictionaryObjShowData(): array
  13. {
  14. $info = Dictionary::find()
  15. ->where(["status" => 1])
  16. ->orderBy("sort asc,id asc")
  17. ->all();
  18. return self::getDictionaryObjData($info);
  19. }
  20. /**
  21. * @return DictionaryObj[]
  22. */
  23. public static function getDictionaryObjAllData(): array
  24. {
  25. $info = Dictionary::find()
  26. ->orderBy("sort asc,id asc")
  27. ->all();
  28. return self::getDictionaryObjData($info);
  29. }
  30. /**
  31. * @param $info
  32. * @return DictionaryObj[]
  33. */
  34. protected static function getDictionaryObjData($info): array
  35. {
  36. $data = [];
  37. $objList = [];
  38. $relationshipIds = [];
  39. $relationshipList = DictionaryRelationship::find()->all();
  40. foreach ($relationshipList as $relationship) {
  41. /** @var DictionaryRelationship $relationship */
  42. $relationshipIds[$relationship->dictionary_id][] = $relationship->relation_id;
  43. }
  44. foreach ($info as $value) {
  45. /** @var Dictionary $value */
  46. $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);
  47. $objList[$obj->getId()] = $obj;
  48. if ($value->has_children > 0) {
  49. $data[] = $obj;
  50. }
  51. }
  52. foreach ($objList as $key => $obj) {
  53. if ($obj->getPid() > 0) {
  54. if (isset($objList[$obj->getPid()])) {
  55. $objList[$obj->getPid()]->addChildrenToTop($obj);
  56. }
  57. }
  58. if (isset($relationshipIds[$key])) {
  59. foreach ($relationshipIds[$key] as $id) {
  60. if (isset($objList[$id])) {
  61. $obj->addDeriveToBottom(new DeriveObj($id));
  62. }
  63. }
  64. }
  65. }
  66. return $data;
  67. }
  68. }