UserLabelRepository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\user;
  12. use app\common\dao\user\UserLabelDao;
  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 UserLabelRepository
  23. * @package app\common\repositories\user
  24. * @author xaboy
  25. * @day 2020-05-07
  26. * @mixin UserLabelDao
  27. */
  28. class UserLabelRepository extends BaseRepository
  29. {
  30. /**
  31. * @var UserLabelDao
  32. */
  33. protected $dao;
  34. /**
  35. * UserGroupRepository constructor.
  36. * @param UserLabelDao $dao
  37. */
  38. public function __construct(UserLabelDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. /**
  43. * 根据条件获取列表数据
  44. *
  45. * 本函数用于根据给定的条件数组 $where,从数据库中检索满足条件的数据列表。
  46. * 它支持分页查询,每页的数据数量由 $limit 指定,查询结果将包含当前页码的数据。
  47. *
  48. * @param array $where 查询条件,以键值对形式表示,用于构建 SQL 查询的 WHERE 子句。
  49. * @param int $page 当前页码,用于指定要返回的数据页。
  50. * @param int $limit 每页的数据数量,用于指定每页返回的记录数。
  51. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 表示满足条件的总记录数,'list' 表示当前页的数据列表。
  52. */
  53. public function getList(array $where, $page, $limit)
  54. {
  55. // 根据 $where 条件搜索数据
  56. $query = $this->dao->search($where);
  57. // 计算满足条件的总记录数
  58. $count = $query->count($this->dao->getPk());
  59. // 按照 'label_id' 降序排序,并根据 $page 和 $limit 获取分页数据
  60. $list = $query->order('label_id DESC')->page($page, $limit)->select();
  61. // 返回包含总记录数和数据列表的数组
  62. return compact('count', 'list');
  63. }
  64. /**
  65. * 创建或编辑用户标签的表单
  66. *
  67. * 本函数用于生成添加或编辑用户标签的表单界面。根据$id$是否存在来判断是创建新标签还是编辑已有的标签。
  68. * 表单的动作(action)根据创建或编辑的状态动态生成相应的URL。返回生成的表单元素。
  69. *
  70. * @param int|null $id 用户标签的ID,如果为null,则表示创建新标签;否则,表示编辑已有的标签。
  71. * @param array $formData 表单的初始数据,用于填充表单字段。
  72. * @return \EasyWeChat\Kernel\Messages\MiniprogramForm|Form
  73. */
  74. public function form($id = null, array $formData = [])
  75. {
  76. // 判断当前操作是创建还是编辑
  77. $isCreate = is_null($id);
  78. // 根据操作类型生成表单的action URL
  79. $action = Route::buildUrl($isCreate ? 'systemUserLabelCreate' : 'systemUserLabelUpdate', $isCreate ? [] : compact('id'))->build();
  80. // 返回生成的表单,设置表单标题和初始数据
  81. return Elm::createForm($action, [
  82. Elm::input('label_name', '用户标签名称:')->placeholder('请输入用户标签名称')->required()
  83. ])->setTitle($isCreate ? '添加用户标签' : '编辑用户标签')->formData($formData);
  84. }
  85. /**
  86. * 更新表单数据。
  87. * 该方法用于根据给定的ID获取数据库中的记录,并使用这些数据来构建一个表单,以便用户可以查看或编辑这些数据。
  88. *
  89. * @param int $id 表单记录的唯一标识符。
  90. * @return array|\EasyWeChat\Kernel\Messages\MiniprogramForm|Form
  91. */
  92. public function updateForm($id)
  93. {
  94. // 通过ID从数据库获取记录,并转换为数组格式,用于填充表单
  95. return $this->form($id, $this->dao->get($id)->toArray());
  96. }
  97. }