ConfigCateService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace services\common;
  3. use yii\db\ActiveQuery;
  4. use common\enums\StatusEnum;
  5. use common\helpers\ArrayHelper;
  6. use common\models\common\ConfigCate;
  7. /**
  8. * Class ConfigCateService
  9. * @package services\common
  10. */
  11. class ConfigCateService
  12. {
  13. /**
  14. * @return array
  15. */
  16. public function getDropDown($app_id)
  17. {
  18. $models = ArrayHelper::itemsMerge($this->findAll($app_id));
  19. return ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  20. }
  21. /**
  22. * 获取下拉
  23. *
  24. * @param string $id
  25. * @return array
  26. */
  27. public function getDropDownForEdit($app_id, $id = '')
  28. {
  29. $list = ConfigCate::find()
  30. ->where(['>=', 'status', StatusEnum::DISABLED])
  31. ->andWhere(['app_id' => $app_id])
  32. ->andFilterWhere(['<>', 'id', $id])
  33. ->select(['id', 'title', 'pid', 'level'])
  34. ->orderBy('sort asc')
  35. ->asArray()
  36. ->all();
  37. $models = ArrayHelper::itemsMerge($list);
  38. $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  39. return ArrayHelper::merge([0 => '顶级分类'], $data);
  40. }
  41. /**
  42. * 获取关联配置信息的递归数组
  43. *
  44. * @param $app_id
  45. * @return array
  46. */
  47. public function getItemsMergeForConfig($app_id)
  48. {
  49. return ArrayHelper::itemsMerge($this->findAllWithConfig($app_id));
  50. }
  51. /**
  52. * 关联配置的列表
  53. *
  54. * @return array|\yii\db\ActiveRecord[]
  55. */
  56. public function findAllWithConfig($app_id, $merchant_id = 0)
  57. {
  58. return ConfigCate::find()
  59. ->where(['status' => StatusEnum::ENABLED])
  60. ->andWhere(['app_id' => $app_id])
  61. ->orderBy('sort asc')
  62. ->with([
  63. 'config' => function (ActiveQuery $query) use ($app_id) {
  64. return $query->andWhere(['app_id' => $app_id])->with(['value']);
  65. }
  66. ])
  67. ->asArray()
  68. ->all();
  69. }
  70. /**
  71. * @return array|\yii\db\ActiveRecord[]
  72. */
  73. public function findAll($app_id)
  74. {
  75. return ConfigCate::find()
  76. ->where(['status' => StatusEnum::ENABLED])
  77. ->andWhere(['app_id' => $app_id])
  78. ->asArray()
  79. ->all();
  80. }
  81. }
粤ICP备19079148号