StoreSeckill.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\product\sku\StoreProductAttrValueServices;
  15. use crmeb\services\CacheService;
  16. use think\facade\App;
  17. /**
  18. * 限时秒杀商品 控制器
  19. * Class StoreSeckill
  20. * @package app\controller\admin\v1\marketing\seckill
  21. */
  22. class StoreSeckill extends AuthController
  23. {
  24. public function __construct(App $app, StoreSeckillServices $services)
  25. {
  26. parent::__construct($app);
  27. $this->services = $services;
  28. }
  29. /**
  30. * 显示资源列表
  31. *
  32. * @return \think\Response
  33. */
  34. public function index()
  35. {
  36. $where = $this->request->getMore([
  37. ['start_status', ''],
  38. [['status', 's'], ''],
  39. [['store_name', 's'], '']
  40. ]);
  41. $where['is_del'] = 0;
  42. return $this->success($this->services->systemPage($where));
  43. }
  44. /**
  45. * 详情
  46. * @param $id
  47. * @return mixed
  48. */
  49. public function read($id)
  50. {
  51. $info = $this->services->getInfo($id);
  52. return $this->success(compact('info'));
  53. }
  54. /**
  55. * 保存秒杀商品
  56. * @param int $id
  57. */
  58. public function save($id)
  59. {
  60. $data = $this->request->postMore([
  61. [['product_id', 'd'], 0],
  62. [['title', 's'], ''],
  63. [['info', 's'], ''],
  64. [['unit_name', 's'], ''],
  65. ['images', []],
  66. [['give_integral', 'd'], 0],
  67. ['section_time', []],
  68. [['is_hot', 'd'], 0],
  69. [['status', 'd'], 0],
  70. [['num', 'd'], 0],
  71. [['once_num', 'd'], 0],
  72. [['time_id', 'd'], 0],
  73. [['temp_id', 'd'], 0],
  74. [['sort', 'd'], 0],
  75. [['description', 's'], ''],
  76. ['attrs', []],
  77. ['items', []],
  78. ['copy', 0],
  79. ['is_support_refund', 1],//是否支持退款
  80. ['delivery_type', []],//物流方式
  81. ['freight', 1],//运费设置
  82. ['postage', 0],//邮费
  83. ['custom_form', ''],//自定义表单
  84. ['system_form_id', 0],//系统表单ID
  85. ['product_type', 0],//商品类型
  86. ['applicable_type', 1],//适用门店类型
  87. ['applicable_store_id', []],//适用门店IDS
  88. ]);
  89. $this->validate($data, \app\validate\admin\marketing\StoreSeckillValidate::class, 'save');
  90. if ($data['section_time']) {
  91. [$start_time, $end_time] = $data['section_time'];
  92. if (strtotime($end_time) + 86400 < time()) {
  93. return $this->fail('活动结束时间不能小于当前时间');
  94. }
  95. }
  96. $seckill = [];
  97. if ($id) {
  98. $seckill = $this->services->get((int)$id);
  99. if (!$seckill) {
  100. return $this->fail('数据不存在');
  101. }
  102. }
  103. //限制编辑
  104. if ($data['copy'] == 0 && $seckill) {
  105. if (($seckill['stop_time'] + 86400) < time()) {
  106. return $this->fail('活动已结束,请重新添加或复制');
  107. }
  108. }
  109. if ($data['num'] < $data['once_num']) {
  110. return $this->fail('限制单次购买数量不能大于总购买数量');
  111. }
  112. if ($data['applicable_type'] == 1) {
  113. $data['applicable_store_id'] = [];
  114. } elseif ($data['applicable_type'] == 2) {
  115. if (!$data['applicable_store_id']) {
  116. return $this->fail('请选择要适用门店');
  117. }
  118. }
  119. if ($data['copy'] == 1) {
  120. $id = 0;
  121. unset($data['copy']);
  122. }
  123. $this->services->saveData($id, $data);
  124. return $this->success('保存成功');
  125. }
  126. /**
  127. * 删除指定资源
  128. *
  129. * @param int $id
  130. * @return \think\Response
  131. */
  132. public function delete($id)
  133. {
  134. if (!$id) return $this->fail('缺少参数');
  135. $this->services->update($id, ['is_del' => 1]);
  136. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  137. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  138. $unique = $storeProductAttrValueServices->value(['product_id' => $id, 'type' => 1], 'unique');
  139. if ($unique) {
  140. $name = 'seckill_' . $unique . '_1';
  141. /** @var CacheService $cache */
  142. $cache = app()->make(CacheService::class);
  143. $cache->del($name);
  144. }
  145. $this->services->cacheDelById($id);
  146. CacheService::redisHandler('product_attr')->clear();
  147. return $this->success('删除成功!');
  148. }
  149. /**
  150. * 修改状态
  151. * @param $id
  152. * @param $status
  153. * @return mixed
  154. */
  155. public function set_status($id, $status)
  156. {
  157. $this->services->update($id, ['status' => $status]);
  158. //修改状态同步缓存
  159. $this->services->cacheSaveValue($id, 'status', $status);
  160. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  161. }
  162. /**
  163. * 秒杀统计
  164. * @return mixed
  165. */
  166. public function seckillStatistics($id)
  167. {
  168. $data = $this->services->seckillStatistics($id);
  169. return app('json')->success($data);
  170. }
  171. /**
  172. * 秒杀参与人统计
  173. * @param $id
  174. * @return mixed
  175. */
  176. public function seckillPeople($id)
  177. {
  178. [$real_name] = $this->request->getMore([
  179. ['real_name', '']
  180. ], true);
  181. return app('json')->success($this->services->seckillPeople($id, $real_name));
  182. }
  183. /**
  184. * 秒杀订单统计
  185. * @param $id
  186. * @return mixed
  187. */
  188. public function seckillOrder($id)
  189. {
  190. $where = $this->request->getMore([
  191. ['real_name', ''],
  192. ['status', '']
  193. ]);
  194. return app('json')->success($this->services->seckillOrder((int)$id, $where));
  195. }
  196. }