StoreSeckillTimeRepository.php 4.6 KB

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