StoreSeckillTimeServices.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\services\activity\seckill;
  13. use app\dao\activity\seckill\StoreSeckillTimeDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use FormBuilder\Factory\Iview;
  18. use think\facade\Route as Url;
  19. /**
  20. * 秒杀时间
  21. * Class StoreSeckillTimeServices
  22. * @package app\services\activity\seckill
  23. * @mixin StoreSeckillTimeDao
  24. */
  25. class StoreSeckillTimeServices extends BaseServices
  26. {
  27. /**
  28. * StoreSeckillTimeServices constructor.
  29. * @param StoreSeckillTimeDao $dao
  30. */
  31. public function __construct(StoreSeckillTimeDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取当前的秒杀时间time
  37. * @return int
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getSeckillTime()
  43. {
  44. $seckillTime = $this->time_list();
  45. $currentHour = date('Hi');
  46. $time = 0;
  47. foreach ($seckillTime as $value) {
  48. $start = str_replace(':', '', $value['start_time']);
  49. $end = str_replace(':', '', $value['end_time']);
  50. if ($currentHour >= $start && $currentHour < $end) {
  51. $time = $value['id'];
  52. }
  53. }
  54. return (int)$time;
  55. }
  56. /**
  57. * 获取列表
  58. * @param array $where
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function systemPage(array $where)
  65. {
  66. [$page, $limit] = $this->getPageValue();
  67. $list = $this->dao->getList($where, '*', $page, $limit);
  68. $count = $this->dao->count($where);
  69. foreach ($list as &$item) {
  70. $item['start_time'] = substr_replace($item['start_time'], ':', 2, 0);
  71. $item['end_time'] = substr_replace($item['end_time'], ':', 2, 0);
  72. }
  73. return compact('list', 'count');
  74. }
  75. /**
  76. * 可用秒杀时间段
  77. * @return array
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public function time_list()
  83. {
  84. $list = $this->dao->getList(['status' => 1], 'id,title,start_time,end_time,pic');
  85. foreach ($list as &$item) {
  86. $item['start_time'] = substr_replace($item['start_time'], ':', 2, 0);
  87. $item['end_time'] = substr_replace($item['end_time'], ':', 2, 0);
  88. $item['slide'] = $item['pic'];
  89. };
  90. return $list;
  91. }
  92. /**
  93. * 验证时间是否重复
  94. * @param array $where
  95. * @param int $id
  96. * @return bool
  97. * @throws \think\db\exception\DbException
  98. */
  99. public function checkTime(array $where, int $id)
  100. {
  101. if (!$this->dao->valStartTime($where['start_time'], $id) && !$this->dao->valEndTime($where['end_time'], $id) && !$this->dao->valAllTime($where, $id)) return true;
  102. return false;
  103. }
  104. /**
  105. * 获取秒杀时间
  106. * @param int $id
  107. * @return array
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function getInfo(int $id)
  113. {
  114. $info = $this->dao->get($id);
  115. if (!$info) {
  116. throw new AdminException('数据不存在');
  117. }
  118. $info = $info->toArray();
  119. $info['time'] = [
  120. substr_replace($info['start_time'], ':', 2, 0) . ':00',
  121. substr_replace($info['end_time'], ':', 2, 0) . ':00'
  122. ];
  123. return $info;
  124. }
  125. /**
  126. * 添加、编辑表单
  127. * @param int $id
  128. * @return mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function createForm(int $id)
  134. {
  135. $info = [];
  136. if ($id) {
  137. $info = $this->getInfo($id);
  138. }
  139. $f[] = Form::input('title', '标题', $info['title'] ?? '')->required();
  140. $f[] = Form::timeRange('time', '时间选择', $info['time'][0] ?? '', $info['time'][1] ?? '')->prop('picker-options', ['format' => 'HH:mm'])->appendValidate(Iview::validateArr()->required()->message('请选择时间'));
  141. $f[] = Form::frameImage('pic', '图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'pic')), $info['pic'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择图片'));
  142. $f[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]]);
  143. $f[] = Form::input('describe', '描述',$Info['describe'] ?? '')->required();
  144. return create_form($id ? '编辑' : '添加秒杀时间', $f, $this->url('/marketing/seckill/time/' . $id), 'POST');
  145. }
  146. /**
  147. * 组合4位秒杀时间
  148. * @param $time
  149. * @return array|string|string[]
  150. */
  151. public function changeTime($time)
  152. {
  153. $str_time = '';
  154. switch (strlen($time)) {
  155. case 1:
  156. $str_time = '0' . $time . '00';
  157. break;
  158. case 2:
  159. $str_time = $time . '00';
  160. break;
  161. case 3:
  162. $str_time = '0' . $time;
  163. break;
  164. case 4:
  165. $str_time = $time;
  166. break;
  167. }
  168. return substr_replace($str_time, ':', 2, 0);
  169. }
  170. }