ConfigClassifyRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\common\repositories\system\config;
  3. use app\common\dao\system\config\SystemConfigClassifyDao;
  4. use app\common\repositories\BaseRepository;
  5. use FormBuilder\Exception\FormBuilderException;
  6. use FormBuilder\Factory\Elm;
  7. use FormBuilder\Form;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\facade\Route;
  12. /**
  13. * Class ConfigClassifyRepository
  14. * @package ln\repositories\system\config
  15. * @mixin SystemConfigClassifyDao
  16. */
  17. class ConfigClassifyRepository extends BaseRepository
  18. {
  19. /**
  20. * ConfigClassifyRepository constructor.
  21. * @param SystemConfigClassifyDao $dao
  22. */
  23. public function __construct(SystemConfigClassifyDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * @return array
  29. * @author zfy
  30. * @day 2020-03-27
  31. */
  32. public function options(): array
  33. {
  34. $options = $this->dao->getOptions();
  35. return formatCascaderData($options, 'classify_name');
  36. }
  37. /**
  38. * @return array
  39. * @throws DataNotFoundException
  40. * @throws DbException
  41. * @throws ModelNotFoundException
  42. * @author zfy
  43. * @day 2020-03-31
  44. */
  45. public function lst()
  46. {
  47. $list = $this->dao->all();
  48. $count = $this->dao->count();
  49. return compact('list', 'count');
  50. }
  51. /**
  52. * @param int $id
  53. * @param int $status
  54. * @return int
  55. * @throws DbException
  56. * @author zfy
  57. * @day 2020-03-31
  58. */
  59. public function switchStatus(int $id, int $status)
  60. {
  61. return $this->dao->update($id, compact('status'));
  62. }
  63. /**
  64. * @param int|null $id
  65. * @param array $formData
  66. * @return Form
  67. * @throws FormBuilderException
  68. * @author zfy
  69. * @day 2020-03-31
  70. */
  71. public function form(?int $id = null, array $formData = []): Form
  72. {
  73. $form = Elm::createForm(is_null($id) ? Route::buildUrl('configClassifyCreate')->build() : Route::buildUrl('configClassifyUpdate', ['id' => $id])->build());
  74. $form->setRule([
  75. Elm::select('pid', '上级分类', 0)->options(function () {
  76. $data = $this->dao->getTopOptions();
  77. $options = [['value' => 0, 'label' => '顶级分类']];
  78. foreach ($data as $value => $label) {
  79. $options[] = compact('value', 'label');
  80. }
  81. return $options;
  82. }),
  83. Elm::input('classify_name', '配置分类名称')->required(),
  84. Elm::input('classify_key', '配置分类key')->required(),
  85. Elm::input('info', '配置分类说明'),
  86. Elm::frameInput('icon', '配置分类图标', '/' . config('admin.admin_prefix') . '/setting/icons?field=icon')->icon('el-icon-circle-plus-outline')->height('338px')->width('700px')->modal(['modal' => false]),
  87. Elm::number('sort', '排序', 0),
  88. Elm::switches('status', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  89. ]);
  90. return $form->setTitle(is_null($id) ? '添加配置分类' : '编辑配置分类')->formData($formData);
  91. }
  92. /**
  93. * @return Form
  94. * @throws FormBuilderException
  95. * @author zfy
  96. * @day 2020-03-30
  97. */
  98. public function createForm()
  99. {
  100. return $this->form();
  101. }
  102. /**
  103. * @param $id
  104. * @return Form
  105. * @throws DataNotFoundException
  106. * @throws DbException
  107. * @throws FormBuilderException
  108. * @throws ModelNotFoundException
  109. * @author zfy
  110. * @day 2020-03-31
  111. */
  112. public function updateForm($id)
  113. {
  114. return $this->form($id, $this->dao->get($id)->toArray());
  115. }
  116. }