ConfigClassifyRepository.php 3.7 KB

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