UserGroupRepository.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserGroupDao;
  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 UserGroupRepository
  14. * @package app\common\repositories\user
  15. * @author zfy
  16. * @day 2020-05-07
  17. * @mixin UserGroupDao
  18. */
  19. class UserGroupRepository extends BaseRepository
  20. {
  21. /**
  22. * @var UserGroupDao
  23. */
  24. protected $dao;
  25. /**
  26. * UserGroupRepository constructor.
  27. * @param UserGroupDao $dao
  28. */
  29. public function __construct(UserGroupDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * @param array $where
  35. * @param $page
  36. * @param $limit
  37. * @return array
  38. * @throws DataNotFoundException
  39. * @throws DbException
  40. * @throws ModelNotFoundException
  41. * @author zfy
  42. * @day 2020-05-07
  43. */
  44. public function getList(array $where, $page, $limit)
  45. {
  46. $query = $this->dao->search($where);
  47. $count = $query->count($this->dao->getPk());
  48. $list = $query->page($page, $limit)->select();
  49. return compact('count', 'list');
  50. }
  51. /**
  52. * @param null $id
  53. * @param array $formData
  54. * @return Form
  55. * @throws FormBuilderException
  56. * @author zfy
  57. * @day 2020-05-07
  58. */
  59. public function form($id = null, array $formData = [])
  60. {
  61. $isCreate = is_null($id);
  62. $action = Route::buildUrl($isCreate ? 'systemUserGroupCreate' : 'systemUserGroupUpdate', $isCreate ? [] : compact('id'))->build();
  63. return Elm::createForm($action, [
  64. Elm::input('group_name', '用户分组名称')->required()
  65. ])->setTitle($isCreate ? '添加用户分组' : '编辑用户分组')->formData($formData);
  66. }
  67. /**
  68. * @param $id
  69. * @return Form
  70. * @throws FormBuilderException
  71. * @throws DataNotFoundException
  72. * @throws DbException
  73. * @throws ModelNotFoundException
  74. * @author zfy
  75. * @day 2020-05-07
  76. */
  77. public function updateForm($id)
  78. {
  79. return $this->form($id, $this->dao->get($id)->toArray());
  80. }
  81. }