LuckLotteryDao.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 array $with
  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 = '*', array $with = [], int $page = 0, int $limit = 0)
  80. {
  81. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  82. $query->with($with);
  83. })->when($page && $limit, function ($query) use ($page, $limit) {
  84. $query->page($page, $limit);
  85. })->order('add_time desc')->select()->toArray();
  86. }
  87. /**
  88. * 获取单个活动
  89. * @param int $id
  90. * @param string $field
  91. * @param array|string[] $with
  92. * @param bool $is_doing
  93. * @return array|\think\Model|null
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function getLottery(int $id, string $field = '*', array $with = ['prize'], bool $is_doing = false)
  99. {
  100. $where = ['id' => $id];
  101. $where['is_del'] = 0;
  102. if ($is_doing) $where['start'] = 1;
  103. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  104. $query->with($with);
  105. })->find();
  106. }
  107. /**
  108. * 获取某个抽奖类型的一条抽奖数据
  109. * @param int $factor
  110. * @param string $field
  111. * @param array|string[] $with
  112. * @param bool $is_doing
  113. * @return array|\think\Model|null
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function getFactorLottery(int $factor = 1, string $field = '*', array $with = ['prize'], bool $is_doing = false)
  119. {
  120. $where = ['factor' => $factor, 'is_del' => 0];
  121. if ($is_doing) $where['start'] = 1;
  122. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  123. $query->with($with);
  124. })->find();
  125. }
  126. }