LuckLotteryDao.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. public function search(array $data = [])
  31. {
  32. return parent::search($data)->when(isset($data['id']) && $data['id'], function ($query) use ($data) {
  33. $query->where('id', $data['id']);
  34. })->when(isset($data['start']) && $data['start'] !== '', function ($query) use ($data) {
  35. $time = time();
  36. switch ($data['start']) {
  37. case 0:
  38. $query->where('start_time', '>', $time)->where('status', 1);
  39. break;
  40. case -1:
  41. $query->where(function ($query1) use ($time) {
  42. $query1->where('end_time', '<', $time)->whereOr('status', 0);
  43. });
  44. break;
  45. case 1:
  46. $query->where('status', 1)->where(function ($query1) use ($time) {
  47. $query1->where(function ($query2) use ($time) {
  48. $query2->where('start_time', '<=', $time)->where('end_time', '>=', $time);
  49. })->whereOr(function ($query3) {
  50. $query3->where('start_time', 0)->where('end_time', 0);
  51. });
  52. });
  53. break;
  54. }
  55. });
  56. }
  57. /**
  58. * 抽奖活动列表
  59. * @param array $where
  60. * @param string $field
  61. * @param array $with
  62. * @param int $page
  63. * @param int $limit
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0)
  70. {
  71. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  72. $query->with($with);
  73. })->when($page && $limit, function ($query) use ($page, $limit) {
  74. $query->page($page, $limit);
  75. })->order('add_time desc')->select()->toArray();
  76. }
  77. /**
  78. * 获取单个活动
  79. * @param int $id
  80. * @param string $field
  81. * @param array|string[] $with
  82. * @param bool $is_doing
  83. * @return array|\think\Model|null
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function getLottery(int $id, string $field = '*', array $with = ['prize'], bool $is_doing = false)
  89. {
  90. $where = ['id' => $id];
  91. $where['is_del'] = 0;
  92. if ($is_doing) $where['start'] = 1;
  93. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  94. $query->with($with);
  95. })->find();
  96. }
  97. /**
  98. * 获取某个抽奖类型的一条抽奖数据
  99. * @param int $factor
  100. * @param string $field
  101. * @param array|string[] $with
  102. * @param bool $is_doing
  103. * @return array|\think\Model|null
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public function getFactorLottery(int $factor = 1, string $field = '*', array $with = ['prize'], bool $is_doing = true)
  109. {
  110. $where = ['factor' => $factor, 'is_del' => 0];
  111. if ($is_doing) $where['start'] = 1;
  112. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  113. $query->with($with);
  114. })->order('id desc')->find();
  115. }
  116. }