StoreSeckillTimeRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\common\repositories\store;
  3. use app\common\dao\store\StoreSeckillTimeDao;
  4. use app\common\repositories\BaseRepository;
  5. use FormBuilder\Factory\Elm;
  6. use think\facade\Route;
  7. class StoreSeckillTimeRepository extends BaseRepository
  8. {
  9. /**
  10. * @var StoreSeckillDao
  11. */
  12. protected $dao;
  13. /**
  14. * StoreSeckillTimeRepository constructor.
  15. * @param StoreSeckillDao $dao
  16. */
  17. public function __construct(StoreSeckillTimeDao $dao)
  18. {
  19. $this->dao = $dao;
  20. }
  21. public function getList(array $where,int $page, int$limit)
  22. {
  23. $query = $this->dao->search($where);
  24. $count = $query->count();
  25. $list = $query->page($page,$limit)->select();
  26. return compact('count','list');
  27. }
  28. public function select()
  29. {
  30. $query = $this->dao->search(['status' => 1]);
  31. $list = $query->select();
  32. return $list;
  33. }
  34. public function form(?int $id = null ,array $formData = [])
  35. {
  36. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemSeckillConfigCreate')->build() : Route::buildUrl('systemSeckillConfigUpdate', ['id' => $id])->build());
  37. $form->setRule([
  38. Elm::input('title','标题'),
  39. Elm::select('start_time','开始时间')->options($this->dao->getTime(1)),
  40. Elm::select('end_time','结束时间')->options($this->dao->getTime(0)),
  41. Elm::switches('status','是否启用')->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  42. Elm::frameImage('pic', '图片', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=pic&type=1')->width('896px')->height('480px')->spin(0)->modal(['modal' => false])->props(['footer' => false]),
  43. ]);
  44. return $form->setTitle(is_null($id) ? '添加' : '编辑')->formData($formData);
  45. }
  46. public function updateForm($id)
  47. {
  48. return $this->form($id,$this->dao->get($id)->toArray());
  49. }
  50. /**
  51. * TODO 所选时间段是否重叠
  52. * @param $where
  53. * @return bool
  54. * @author Qinii
  55. * @day 2020-07-31
  56. */
  57. public function checkTime(array $where,?int $id)
  58. {
  59. if(!$this->dao->valStartTime($where['start_time'],$id) && !$this->dao->valEndTime($where['end_time'],$id) && !$this->dao->valAllTime($where,$id)) return true;
  60. return false;
  61. }
  62. /**
  63. * TODO APi秒杀时间列表
  64. * @return array
  65. * @author Qinii
  66. * @day 2020-08-11
  67. */
  68. public function selectTime()
  69. {
  70. $seckillTimeIndex = 0;
  71. $_h = date('H',time());
  72. $query = $this->dao->search(['status' => 1]);
  73. $list = $query->select();
  74. $seckillEndTime = time();
  75. $seckillTime = [];
  76. foreach($list as $k => $item){
  77. $item['stop'] = strtotime((date('Y-m-d ',time()).$item['end_time'].':00:00'));
  78. if($item['end_time'] <= $_h) {
  79. $item['pc_status'] = 0;
  80. $item['state'] = '已结束';
  81. }
  82. if($item['start_time'] > $_h ) {
  83. $item['pc_status'] = 2;
  84. $item['state'] = '待开始';
  85. }
  86. if($item['start_time'] <= $_h && $_h < $item['end_time']){
  87. $item['pc_status'] = 1;
  88. $item['state'] = '抢购中';
  89. $seckillTimeIndex = $k;
  90. $seckillEndTime = strtotime((date('Y-m-d ',time()).$item['end_time'].':00:00'));
  91. }
  92. $seckillTime[$k] = $item;
  93. }
  94. return compact('seckillTime','seckillTimeIndex','seckillEndTime');
  95. }
  96. /**
  97. * TODO 获取某个时间是否有开启秒杀活动
  98. * @param array $where
  99. * @return mixed
  100. * @author Qinii
  101. * @day 2020-08-19
  102. */
  103. public function getBginTime(array $where)
  104. {
  105. if($where['start_time'] == '' || $where['end_time'] == ''){
  106. $where['start_time'] = date('H',time());
  107. $where['end_time'] = date('H',time()) + 1;
  108. }
  109. $where['status'] = 1;
  110. return $this->dao->search($where)->find();
  111. }
  112. }