ConfigClassifyRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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($where)
  55. {
  56. $query = $this->dao->search($where);
  57. $list = $query->select();
  58. $count = $query->count();
  59. return compact('list', 'count');
  60. }
  61. /**
  62. * @param int $id
  63. * @param int $status
  64. * @return int
  65. * @throws DbException
  66. * @author xaboy
  67. * @day 2020-03-31
  68. */
  69. public function switchStatus(int $id, int $status)
  70. {
  71. return $this->dao->update($id, compact('status'));
  72. }
  73. /**
  74. * @param int|null $id
  75. * @param array $formData
  76. * @return Form
  77. * @throws FormBuilderException
  78. * @author xaboy
  79. * @day 2020-03-31
  80. */
  81. public function form(?int $id = null, array $formData = []): Form
  82. {
  83. $form = Elm::createForm(is_null($id) ? Route::buildUrl('configClassifyCreate')->build() : Route::buildUrl('configClassifyUpdate', ['id' => $id])->build());
  84. $form->setRule([
  85. Elm::select('pid', '上级分类', 0)->options(function () {
  86. $data = $this->dao->getTopOptions();
  87. $options = [['value' => 0, 'label' => '顶级分类']];
  88. foreach ($data as $value => $label) {
  89. $options[] = compact('value', 'label');
  90. }
  91. return $options;
  92. }),
  93. Elm::input('classify_name', '配置分类名称')->required(),
  94. Elm::input('classify_key', '配置分类key')->required(),
  95. Elm::input('info', '配置分类说明'),
  96. Elm::frameInput('icon', '配置分类图标', '/' . config('admin.admin_prefix') . '/setting/icons?field=icon')->icon('el-icon-circle-plus-outline')->height('338px')->width('700px')->modal(['modal' => false]),
  97. Elm::number('sort', '排序', 0)->precision(0)->max(99999),
  98. Elm::switches('status', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  99. ]);
  100. return $form->setTitle(is_null($id) ? '添加配置分类' : '编辑配置分类')->formData($formData);
  101. }
  102. /**
  103. * @return Form
  104. * @throws FormBuilderException
  105. * @author xaboy
  106. * @day 2020-03-30
  107. */
  108. public function createForm()
  109. {
  110. return $this->form();
  111. }
  112. /**
  113. * @param $id
  114. * @return Form
  115. * @throws DataNotFoundException
  116. * @throws DbException
  117. * @throws FormBuilderException
  118. * @throws ModelNotFoundException
  119. * @author xaboy
  120. * @day 2020-03-31
  121. */
  122. public function updateForm($id)
  123. {
  124. return $this->form($id, $this->dao->get($id)->toArray());
  125. }
  126. }