LuckLotteryDao.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\dao\activity\lottery;
  13. use app\dao\BaseDao;
  14. use app\model\activity\lottery\LuckLottery;
  15. /**
  16. *
  17. * Class LuckLotteryDao
  18. * @package app\dao\activity\lottery
  19. */
  20. class LuckLotteryDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return LuckLottery::class;
  29. }
  30. /**
  31. * 抽奖搜索
  32. * @param array $data
  33. * @param bool $search
  34. * @return \crmeb\basic\BaseModel
  35. * @throws \ReflectionException
  36. * @author 吴汐
  37. * @email 442384644@qq.com
  38. * @date 2023/03/20
  39. */
  40. public function search(array $where = [], bool $search = false)
  41. {
  42. return parent::search($where, $search)->when(isset($where['id']) && $where['id'], function ($query) use ($where) {
  43. $query->where('id', $where['id']);
  44. })->when(isset($where['start']) && $where['start'] !== '', function ($query) use ($where) {
  45. $time = time();
  46. switch ($where['start']) {
  47. case 0:
  48. $query->where('start_time', '>', $time)->where('status', 1);
  49. break;
  50. case -1:
  51. $query->where(function ($query1) use ($time) {
  52. $query1->where('end_time', '<', $time)->whereOr('status', 0);
  53. });
  54. break;
  55. case 1:
  56. $query->where('status', 1)->where(function ($query1) use ($time) {
  57. $query1->where(function ($query2) use ($time) {
  58. $query2->where('start_time', '<=', $time)->where('end_time', '>=', $time);
  59. })->whereOr(function ($query3) {
  60. $query3->where('start_time', 0)->where('end_time', 0);
  61. });
  62. });
  63. break;
  64. }
  65. });
  66. }
  67. /**
  68. * 抽奖活动列表
  69. * @param array $where
  70. * @param string $field
  71. * @param string $order
  72. * @param int $page
  73. * @param int $limit
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function getList(array $where, string $field = '*', string $order = 'id desc', int $page = 0, int $limit = 0)
  80. {
  81. $model = $this->getModel()->when($where['is_del'] !== '', function ($query) use ($where) {
  82. $query->where('is_del', $where['is_del']);
  83. })->when($where['factor'] !== '', function ($query) use ($where) {
  84. $query->where('factor', $where['factor']);
  85. })->when($where['start'] !== '', function ($query) use ($where) {
  86. if ($where['start'] == 0) {
  87. $query->where('start_time', '>', time());
  88. } elseif ($where['start'] == 1) {
  89. $query->where('start_time', '<', time())->where('end_time', '>', time());
  90. } else {
  91. $query->where('end_time', '<', time());
  92. }
  93. })->when($where['status'] !== '', function ($query) use ($where) {
  94. $query->where('status', $where['status']);
  95. })->when(count($where['time']) == 2, function ($query) use ($where) {
  96. $query->where('start_time', '<=', $where['time'][0])->where('end_time', '>=', $where['time'][1]);
  97. })->when($where['keyword'] !== '', function ($query) use ($where) {
  98. $query->where('name|content', 'like', '%' . $where['keyword'] . '%');
  99. });
  100. $count = $model->count();
  101. $list = $model->with(['records' => function ($query) {
  102. $query->field([
  103. 'lottery_id',
  104. 'COUNT(DISTINCT uid) AS total_user', // 总参与人数
  105. 'COUNT(DISTINCT CASE WHEN type != 1 THEN uid END) AS wins_user', // 中奖人数
  106. 'COUNT(*) AS total_num', // 总参与次数
  107. 'SUM(type != 1) AS wins_num', // 中奖次数
  108. ])->group('lottery_id');
  109. }])->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  110. $query->page($page, $limit);
  111. })->order($order)->select()->toArray();
  112. return compact('count', 'list');
  113. }
  114. /**
  115. * 获取单个活动
  116. * @param int $id
  117. * @param string $field
  118. * @param array|string[] $with
  119. * @param bool $is_doing
  120. * @return array|\think\Model|null
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function getLottery(int $id, string $field = '*', array $with = ['prize'], bool $is_doing = false)
  126. {
  127. $where = ['id' => $id];
  128. $where['is_del'] = 0;
  129. if ($is_doing) $where['start'] = 1;
  130. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  131. $query->with($with);
  132. })->find();
  133. }
  134. /**
  135. * 获取某个抽奖类型的一条抽奖数据
  136. * @param int $factor
  137. * @param string $field
  138. * @param array|string[] $with
  139. * @param bool $is_doing
  140. * @return array|\think\Model|null
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function getFactorLottery(int $factor = 1, string $field = '*', array $with = ['prize'], bool $is_doing = false)
  146. {
  147. $where = ['factor' => $factor, 'is_del' => 0, 'is_use' => 1];
  148. if ($is_doing) $where['start'] = 1;
  149. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  150. $query->with($with);
  151. })->order('id desc')->find();
  152. }
  153. }