StoreSeckillTimeDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\seckill;
  13. use app\dao\BaseDao;
  14. use app\model\activity\seckill\StoreSeckillTime;
  15. /**
  16. * 秒杀时间
  17. * Class StoreSeckillTimeDao
  18. * @package app\dao\activity\seckill
  19. */
  20. class StoreSeckillTimeDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreSeckillTime::class;
  29. }
  30. /**
  31. * @param array $where
  32. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  33. */
  34. public function search(array $where = [])
  35. {
  36. return parent::search($where)
  37. ->when(isset($where['start_time']) && $where['start_time'] !== '', function ($query) use ($where) {
  38. $query->whereTime('start_time', '<=', intval($where['start_time']));
  39. })
  40. ->when(isset($where['end_time']) && $where['end_time'] !== '', function ($query) use ($where) {
  41. $query->whereTime('end_time', '>=', intval($where['end_time']));
  42. });
  43. }
  44. /**
  45. * 获取秒杀时间列表
  46. * @param array $where
  47. * @param string $field
  48. * @param int $page
  49. * @param int $limit
  50. * @param string $order
  51. * @return array
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = 'start_time asc,id desc')
  57. {
  58. return $this->search($where)->field($field)
  59. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  60. $query->page($page, $limit);
  61. })->order($order)->select()->toArray();
  62. }
  63. /**
  64. * 开始时间 在别的时间段中
  65. * @param $time
  66. * @param $id
  67. * @return int
  68. * @throws \think\db\exception\DbException
  69. */
  70. public function valStartTime($time, $id)
  71. {
  72. return $this->getModel()
  73. ->when($id, function ($query) use ($id) {
  74. $query->where('id', '<>', $id);
  75. })->where('start_time', '<=', $time)->where('end_time', '>', $time)->count();
  76. }
  77. /**
  78. * 结束时间在别的时间段中
  79. * @param $time
  80. * @param $id
  81. * @return int
  82. * @throws \think\db\exception\DbException
  83. */
  84. public function valEndTime($time, $id)
  85. {
  86. return $this->getModel()
  87. ->when($id, function ($query) use ($id) {
  88. $query->where('id', '<>', $id);
  89. })->where('start_time', '<', $time)->where('end_time', '>=', $time)->count();
  90. }
  91. /**
  92. * 时间段包含了别的时间段
  93. * @param array $data
  94. * @param $id
  95. * @return int
  96. * @throws \think\db\exception\DbException
  97. */
  98. public function valAllTime(array $data, $id)
  99. {
  100. return $this->getModel()
  101. ->when($id, function ($query) use ($id) {
  102. $query->where('id', '<>', $id);
  103. })->where('start_time', '>', $data['start_time'])->where('end_time', '<=', $data['end_time'])->count();
  104. }
  105. }