ConfigContributionRepository.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\SystemContributionDao;
  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 ConfigContributionRepository
  22. * @package crmeb\repositories\system\config
  23. * @mixin SystemContributionDao
  24. */
  25. class ConfigContributionRepository extends BaseRepository
  26. {
  27. /**
  28. * ConfigContributionRepository constructor.
  29. * @param SystemContributionDao $dao
  30. */
  31. public function __construct(SystemContributionDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @return array
  37. * @throws DataNotFoundException
  38. * @throws DbException
  39. * @throws ModelNotFoundException
  40. * @author xaboy
  41. * @day 2020-03-31
  42. */
  43. public function lst()
  44. {
  45. $list = $this->dao->all();
  46. $count = $this->dao->count();
  47. return compact('list', 'count');
  48. }
  49. /**
  50. * @param int|null $id
  51. * @param array $formData
  52. * @return Form
  53. * @throws FormBuilderException
  54. * @author xaboy
  55. * @day 2020-03-31
  56. */
  57. public function form(?int $id = null, array $formData = []): Form
  58. {
  59. $form = Elm::createForm(is_null($id) ? Route::buildUrl('configContributionCreate')->build() : Route::buildUrl('configContributionUpdate', ['id' => $id])->build());
  60. $form->setRule([
  61. Elm::input('title', '范围名称')->required(),
  62. Elm::number('start', '范围开始')->required(),
  63. Elm::number('end', '范围结束')->required(),
  64. Elm::input('type', '类型')->required(),
  65. Elm::number('jd', '京豆')->required(),
  66. Elm::number('value', '贡献值')->required(),
  67. ]);
  68. return $form->setTitle(is_null($id) ? '添加' : '编辑')->formData($formData);
  69. }
  70. /**
  71. * @return Form
  72. * @throws FormBuilderException
  73. * @author xaboy
  74. * @day 2020-03-30
  75. */
  76. public function createForm()
  77. {
  78. return $this->form();
  79. }
  80. /**
  81. * @param $id
  82. * @return Form
  83. * @throws DataNotFoundException
  84. * @throws DbException
  85. * @throws FormBuilderException
  86. * @throws ModelNotFoundException
  87. * @author xaboy
  88. * @day 2020-03-31
  89. */
  90. public function updateForm($id)
  91. {
  92. return $this->form($id, $this->dao->get($id)->toArray());
  93. }
  94. }