StoreActivityRelatedRepository.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\store;
  12. use app\common\dao\store\StoreActivityDao;
  13. use app\common\dao\store\StoreActivityRelatedDao;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\system\form\FormRepository;
  16. use think\exception\ValidateException;
  17. use think\facade\Db;
  18. /**
  19. * @mixin StoreActivityRelatedDao
  20. */
  21. class StoreActivityRelatedRepository extends BaseRepository
  22. {
  23. const ACTIVITY_TYPE_FORM = 1;
  24. /**
  25. * @var StoreActivityRelatedDao
  26. */
  27. protected $dao;
  28. /**
  29. * StoreActivityDao constructor.
  30. * @param StoreActivityRelatedDao $dao
  31. */
  32. public function __construct(StoreActivityRelatedDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取活动列表
  38. * 根据给定的条件和分页信息,从数据库中检索活动列表。
  39. * 这个方法封装了查询逻辑,包括计算总数和获取特定页面的列表项。
  40. *
  41. * @param string $where 查询条件,用于筛选活动。
  42. * @param int $page 当前的页码。
  43. * @param int $limit 每页显示的活动数量。
  44. * @return array 返回包含总数和活动列表的数组。
  45. */
  46. public function getList($where, $page, $limit)
  47. {
  48. // 构建查询,包括关联查询和选择字段。
  49. $query = $this->dao->search($where)->with([
  50. 'activity' => function ($query) {
  51. // 对活动查询进行进一步限制,只选择特定的字段,并添加计算字段"time_status"。
  52. $query->field('activity_id,activity_name,status,activity_type,pic,start_time,end_time')->append(['time_status']);
  53. }
  54. ]);
  55. // 计算满足条件的活动总数。
  56. $count = $query->count();
  57. // 根据当前页码和每页的限制,获取活动列表。
  58. // 并按照"id"和"create_time"降序排序。
  59. $list = $query->page($page, $limit)->order('id DESC,create_time DESC')->select();
  60. // 返回包含总数和活动列表的数组。
  61. return compact('count', 'list');
  62. }
  63. /**
  64. * 根据ID和用户ID显示信息
  65. *
  66. * 此方法用于根据提供的ID和用户ID检索特定的数据,并确保数据及其相关活动存在。
  67. * 如果数据或活动不存在,将抛出一个验证异常。
  68. *
  69. * @param int $id 数据的唯一标识ID
  70. * @param int $uid 用户的唯一标识ID
  71. * @return array 包含数据和活动信息的数组
  72. * @throws ValidateException 如果数据或活动不存在,则抛出此异常
  73. */
  74. public function show($id, $uid)
  75. {
  76. // 定义查询条件
  77. $where['id'] = $id;
  78. $where['uid'] = $uid;
  79. // 查询数据及其关联的活动信息,附加时间状态信息
  80. $data = $this->dao->getSearch($where)
  81. ->with([
  82. 'activity' => function ($query) {
  83. $query->append(['time_status']);
  84. }
  85. ])
  86. ->find();
  87. // 如果数据不存在,则抛出异常
  88. if (!$data) throw new ValidateException('数据不存在');
  89. // 从查询结果中提取活动信息
  90. $form = $data['activity'];
  91. // 如果活动信息不存在或用户无权查看,则抛出异常
  92. if (!$form) throw new ValidateException('活动不存在或无法查看');
  93. // 返回包含数据和活动信息的数组
  94. return compact('data');
  95. }
  96. /**
  97. * 保存提交信息,增加已提交数量
  98. * @param array $data
  99. * @return mixed
  100. * @author Qinii
  101. * @day 2023/11/22
  102. */
  103. public function save(int $activity_id, array $data)
  104. {
  105. return Db::transaction(function () use ($activity_id, $data) {
  106. $this->dao->create($data);
  107. app()->make(StoreActivityRepository::class)->incTotal($activity_id);
  108. });
  109. }
  110. }