ConfigClassifyRepository.php 4.3 KB

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