StoreSeckillTimeRepository.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. /**
  31. * 根据条件获取列表数据
  32. *
  33. * 本函数用于根据给定的条件数组 $where,从数据库中检索数据列表。
  34. * 它支持分页查询,每页的数据数量由 $limit 参数指定,查询的页码由 $page 参数指定。
  35. * 函数返回一个包含两个元素的数组,第一个元素是数据总数 $count,第二个元素是当前页的数据列表 $list。
  36. *
  37. * @param array $where 查询条件数组
  38. * @param int $page 查询的页码
  39. * @param int $limit 每页的数据数量
  40. * @return array 返回包含 'count' 和 'list' 两个元素的数组
  41. */
  42. public function getList(array $where, int $page, int $limit)
  43. {
  44. // 根据条件查询数据
  45. $query = $this->dao->search($where);
  46. // 统计满足条件的数据总数
  47. $count = $query->count();
  48. // 获取当前页的数据列表
  49. $list = $query->page($page, $limit)->select();
  50. // 返回数据总数和当前页的数据列表
  51. return compact('count', 'list');
  52. }
  53. /**
  54. * 根据活动ID选择秒杀活动商品列表
  55. *
  56. * @param int $activeId 秒杀活动的ID
  57. * @return array 秒杀活动商品列表
  58. *
  59. * 此方法用于根据提供的活动ID来获取相应的秒杀活动商品列表。
  60. * 如果未提供活动ID,则获取所有处于激活状态的秒杀活动商品。
  61. * 如果提供了活动ID且该活动有效,那么将获取该活动关联的所有商品。
  62. */
  63. public function select($activeId)
  64. {
  65. $list = [];
  66. // 当未指定活动ID时,查询所有处于激活状态的秒杀活动商品
  67. if(!$activeId){
  68. $query = $this->dao->search(['status' => 1]);
  69. $list = $query->select();
  70. }else{
  71. // 指定了活动ID时,获取该秒杀活动的信息
  72. $seckillActive = app()->make(StoreSeckillActiveRepository::class)->get($activeId);
  73. // 如果秒杀活动存在且设置了秒杀时间ID,查询该秒杀活动的商品列表
  74. if($seckillActive && isset($seckillActive['seckill_time_ids'])){
  75. $list = $this->dao->getSearch([])->whereIn('seckill_time_id',$seckillActive['seckill_time_ids'])->select();
  76. }
  77. }
  78. return $list;
  79. }
  80. /**
  81. * 创建或编辑秒杀配置表单
  82. *
  83. * 本函数用于生成一个包含各种输入字段的表单,用于创建或编辑秒杀活动的配置信息。
  84. * 表单字段包括标题、开始时间、结束时间、状态和图片等。根据传入的$id$参数决定是创建新记录还是编辑已有的记录。
  85. *
  86. * @param int|null $id 秒杀配置的ID,如果为null,则表示创建新记录;否则,表示编辑已有的记录。
  87. * @param array $formData 表单的初始数据,用于填充表单字段的值。
  88. * @return \FormBuilder\Form|\think\form\Form
  89. */
  90. public function form(?int $id = null ,array $formData = [])
  91. {
  92. // 根据$id$的值决定表单的提交URL,如果是新建,则提交到创建URL;如果是编辑,则提交到更新URL。
  93. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemSeckillConfigCreate')->build() : Route::buildUrl('systemSeckillConfigUpdate', ['id' => $id])->build());
  94. // 设置表单的验证规则,包括各种输入字段的类型、必填项、占位符等。
  95. $form->setRule([
  96. // 设置标题输入字段的规则。
  97. Elm::input('title','标题:')->placeholder('请输入标题')->required(),
  98. // 设置开始时间选择字段的规则。
  99. Elm::select('start_time','开始时间:')->placeholder('请选择')->options($this->dao->getTime(1))->requiredNum(),
  100. // 设置结束时间选择字段的规则。
  101. Elm::select('end_time','结束时间:')->placeholder('请选择')->options($this->dao->getTime(0))->requiredNum(),
  102. // 设置状态开关字段的规则。
  103. Elm::switches('status','是否启用:')->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  104. // 设置图片上传字段的规则,包括图片预览、上传配置等。
  105. Elm::frameImage('pic', '图片:', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=pic&type=1')->width('1000px')->height('600px')->spin(0)->icon('el-icon-camera')->modal(['modal' => false])->props(['footer' => false])->appendRule('suffix', [
  106. 'type' => 'guidancePop',
  107. 'props' => [
  108. 'info' => '此图片将展示在移动端秒杀商品列表上方,(建议尺寸:710*300px)',
  109. ]
  110. ]),
  111. ]);
  112. // 设置表单的标题和初始数据,然后返回表单对象。
  113. return $form->setTitle(is_null($id) ? '添加' : '编辑')->formData($formData);
  114. }
  115. /**
  116. * 更新表单数据
  117. * 该方法用于根据给定的ID获取数据库中的记录,并使用这些数据来构建一个表单。这在更新现有记录的信息时非常有用。
  118. * @param int $id 表单记录的唯一标识符。这个ID用于从数据库中检索特定的记录。
  119. * @return mixed 返回一个表单实例,该实例使用从数据库中检索到的数据进行了填充。这个表单可以用于显示当前数据或进行数据更新操作。
  120. */
  121. public function updateForm($id)
  122. {
  123. // 根据$id获取数据库中的记录,并转换为数组格式,然后使用这个数据来创建一个新的表单实例
  124. return $this->form($id,$this->dao->get($id)->toArray());
  125. }
  126. /**
  127. * 所选时间段是否重叠
  128. * @param $where
  129. * @return bool
  130. * @author Qinii
  131. * @day 2020-07-31
  132. */
  133. public function checkTime(array $where,?int $id)
  134. {
  135. if(!$this->dao->valStartTime($where['start_time'],$id) && !$this->dao->valEndTime($where['end_time'],$id) && !$this->dao->valAllTime($where,$id)) return true;
  136. return false;
  137. }
  138. /**
  139. * APi秒杀时间列表
  140. * @return array
  141. * @author Qinii
  142. * @day 2020-08-11
  143. */
  144. public function selectTime()
  145. {
  146. $seckillTimeIndex = 0;
  147. $_h = date('H',time());
  148. $query = $this->dao->search(['status' => 1]);
  149. $list = $query->select();
  150. $seckillEndTime = time();
  151. $seckillTime = [];
  152. foreach($list as $k => $item){
  153. $item['stop'] = strtotime((date('Y-m-d ',time()).$item['end_time'].':00:00'));
  154. if($item['end_time'] <= $_h) {
  155. $item['pc_status'] = 0;
  156. $item['state'] = '已结束';
  157. }
  158. if($item['start_time'] > $_h ) {
  159. $item['pc_status'] = 2;
  160. $item['state'] = '待开始';
  161. }
  162. if($item['start_time'] <= $_h && $_h < $item['end_time']){
  163. $item['pc_status'] = 1;
  164. $item['state'] = '抢购中';
  165. $seckillTimeIndex = $k;
  166. $seckillEndTime = strtotime((date('Y-m-d ',time()).$item['end_time'].':00:00'));
  167. $item['stop_time'] = date('Y-m-d H:i:s', $seckillEndTime);
  168. }
  169. $seckillTime[$k] = $item;
  170. }
  171. return compact('seckillTime','seckillTimeIndex','seckillEndTime');
  172. }
  173. /**
  174. * 获取某个时间是否有开启秒杀活动
  175. * @param array $where
  176. * @return mixed
  177. * @author Qinii
  178. * @day 2020-08-19
  179. */
  180. public function getBginTime(array $where)
  181. {
  182. if(empty($where) || ($where['start_time'] == '' || $where['end_time'] == '')){
  183. $where['start_time'] = date('H',time());
  184. $where['end_time'] = date('H',time()) + 1;
  185. }
  186. $where['status'] = 1;
  187. return $this->dao->search($where)->find();
  188. }
  189. }