StoreActivityDao.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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;
  13. use app\dao\BaseDao;
  14. use app\model\activity\StoreActivity;
  15. /**
  16. * 活动
  17. * Class StoreActivityDao
  18. * @package app\dao\activity
  19. */
  20. class StoreActivityDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreActivity::class;
  29. }
  30. /**
  31. * 搜索
  32. * @param array $where
  33. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  34. */
  35. protected function search(array $where = [])
  36. {
  37. return parent::search($where)->when(isset($where['activityTime']), function ($query) use ($where) {
  38. [$startTime, $stopTime] = is_array($where['activityTime']) ? $where['activityTime'] : [time(), time() - 86400];
  39. $query->where('start_day', '<=', $startTime)->where('end_day', '>=', $stopTime);
  40. })->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  41. $time = time();
  42. switch ($where['start_status']) {
  43. case -1:
  44. $query->where(function ($q) use ($time) {
  45. $q->where('end_day', '<', $time - 86400)->whereOr('status', '0');
  46. });
  47. break;
  48. case 0:
  49. $query->where('start_day', '>', $time)->where('status', 1);
  50. break;
  51. case 1:
  52. $query->where('start_day', '<=', $time)->where('end_day', '>=', $time - 86400)->where('status', 1);
  53. break;
  54. }
  55. });
  56. }
  57. /**
  58. * 获取活动列表
  59. * @param array $where
  60. * @param string $field
  61. * @param int $page
  62. * @param int $limit
  63. * @param array $with
  64. * @param string $order
  65. * @return array
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, array $with = [], string $order = 'start_time asc, id desc')
  71. {
  72. return $this->search($where)->field($field)
  73. ->when($with, function ($query) use ($with) {
  74. $query->with($with);
  75. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  76. $query->page($page, $limit);
  77. })->order($order)->select()->toArray();
  78. }
  79. /**
  80. * 验证是否有活动
  81. * @param array $where
  82. * @return bool
  83. * @throws \think\db\exception\DbException
  84. */
  85. public function checkActivity(array $where)
  86. {
  87. return $this->search($where)->where('end_day', '>', time())->count() > 0;
  88. }
  89. }