StoreActivitySeckill.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\controller\admin\v1\marketing\seckill;
  12. use app\controller\admin\AuthController;
  13. use app\services\activity\seckill\StoreSeckillServices;
  14. use app\services\activity\StoreActivityServices;
  15. use app\services\product\sku\StoreProductAttrValueServices;
  16. use crmeb\services\CacheService;
  17. use think\facade\App;
  18. /**
  19. * 秒杀活动
  20. * Class StoreActivitySeckill
  21. * @package app\controller\admin\v1\marketing\seckill
  22. */
  23. class StoreActivitySeckill extends AuthController
  24. {
  25. public function __construct(App $app, StoreActivityServices $services)
  26. {
  27. parent::__construct($app);
  28. $this->services = $services;
  29. }
  30. /**
  31. * 显示资源列表
  32. *
  33. * @return \think\Response
  34. */
  35. public function index()
  36. {
  37. $where = $this->request->getMore([
  38. ['time', ''],
  39. [['status', 's'], ''],
  40. [['store_name', 's'], '']
  41. ]);
  42. $where['is_del'] = 0;
  43. $where['type'] = 1;
  44. return $this->success($this->services->systemPage($where));
  45. }
  46. /**
  47. * 详情
  48. * @param $id
  49. * @return mixed
  50. */
  51. public function read($id)
  52. {
  53. $info = $this->services->getSeckillInfo((int)$id);
  54. return $this->success(compact('info'));
  55. }
  56. /**
  57. * 保存秒杀商品
  58. * @param int $id
  59. */
  60. public function save($id = '')
  61. {
  62. $data = $this->request->postMore([
  63. ['name', 's', ''],
  64. ['seckill_ids', []],
  65. ['image', ''],
  66. ['section_data', []],
  67. [['status', 'd'], 0],
  68. [['num', 'd'], 0],
  69. [['once_num', 'd'], 0],
  70. ['time_id', []],
  71. ['applicable_type', 1],//适用门店类型
  72. ['applicable_store_id', []],//适用门店IDS
  73. ]);
  74. $this->validate($data, \app\validate\admin\marketing\StoreActivitySeckillValidate::class, 'save');
  75. if ($data['section_data']) {
  76. [$start_time, $end_time] = $data['section_data'];
  77. if (strtotime($end_time) + 86400 < time()) {
  78. return $this->fail('活动结束时间不能小于当前时间');
  79. }
  80. }
  81. if ($data['num'] < $data['once_num']) {
  82. return $this->fail('限制单次购买数量不能大于总购买数量');
  83. }
  84. if ($data['applicable_type'] == 1) {
  85. $data['applicable_store_id'] = [];
  86. } elseif ($data['applicable_type'] == 2) {
  87. if (!$data['applicable_store_id']) {
  88. return $this->fail('请选择要适用门店');
  89. }
  90. }
  91. $expend = $data['seckill_ids'];
  92. $this->services->saveData((int)$id, $data, $expend);
  93. return $this->success('保存成功');
  94. }
  95. /**
  96. * 删除指定资源
  97. *
  98. * @param int $id
  99. * @return \think\Response
  100. */
  101. public function delete($id)
  102. {
  103. if (!$id) return $this->fail('缺少参数');
  104. $this->services->update($id, ['is_del' => 1]);
  105. /** @var StoreSeckillServices $storeSeckillServices */
  106. $storeSeckillServices = app()->make(StoreSeckillServices::class);
  107. //秒杀商品
  108. $seckillIds = $storeSeckillServices->getActivitySeckillIds(['activity_id' => $id]);
  109. if ($seckillIds) {
  110. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  111. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  112. foreach ($seckillIds as $seckillId) {
  113. $storeSeckillServices->update($seckillId, ['is_del' => 1]);
  114. $unique = $storeProductAttrValueServices->value(['product_id' => $seckillId, 'type' => 1], 'unique');
  115. if ($unique) {
  116. $name = 'seckill_' . $unique . '_1';
  117. /** @var CacheService $cache */
  118. $cache = app()->make(CacheService::class);
  119. $cache->del($name);
  120. }
  121. $this->services->cacheDelById($seckillId);
  122. }
  123. CacheService::redisHandler('product_attr')->clear();
  124. }
  125. return $this->success('删除成功!');
  126. }
  127. /**
  128. * 修改状态
  129. * @param $id
  130. * @param $status
  131. * @return mixed
  132. */
  133. public function set_status($id, $status)
  134. {
  135. $this->services->update($id, ['status' => $status]);
  136. /** @var StoreSeckillServices $storeSeckillServices */
  137. $storeSeckillServices = app()->make(StoreSeckillServices::class);
  138. //秒杀商品
  139. $seckillIds = $storeSeckillServices->getActivitySeckillIds(['activity_id' => $id]);
  140. foreach ($seckillIds as $seckillId) {
  141. $storeSeckillServices->update($seckillId, ['status' => $status]);
  142. //修改状态同步缓存
  143. $this->services->cacheSaveValue($seckillId, 'status', $status);
  144. }
  145. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  146. }
  147. }