Kan.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace addons\shopro\library\activity\provider;
  3. use addons\shopro\service\StockSale;
  4. use addons\shopro\exception\ShoproException;
  5. use think\Db;
  6. /**
  7. * 砍价
  8. */
  9. class Kan extends Base
  10. {
  11. protected $rules = [
  12. "is_commission" => "require|boolean",
  13. "is_free_shipping" => "require|boolean",
  14. "sales_show_type" => "require",
  15. "limit_num" => "number|egt:0",
  16. "order_auto_close" => "float|egt:0",
  17. "team_num" => "require|number|egt:0",
  18. ];
  19. protected $message = [
  20. 'team_num.require' => '请填写最多砍价人数',
  21. ];
  22. protected $default = [
  23. "is_commission" => 0, // 是否参与分销
  24. "is_free_shipping" => 0, // 是否包邮
  25. "sales_show_type" => "real", // real=真实活动销量|goods=商品总销量(包含虚拟销量)
  26. "limit_num" => 0, // 每人限购数量 0:不限购
  27. "order_auto_close" => 0, // 订单自动关闭时间,如果为 0 将使用系统级订单自动关闭时间
  28. "team_num" => 2, // 砍价人数,最少两人
  29. ];
  30. public function check($params, $activity_id = 0)
  31. {
  32. // 数据验证
  33. $params = parent::check($params);
  34. // 验证添加的活动商品是否至少设置了一个活动规格
  35. $this->checkActivitySkuPrice($params['goods_list']);
  36. // 检测活动之间是否存在冲突
  37. $this->checkActivityConflict($params, $params['goods_list'], $activity_id);
  38. return $params;
  39. }
  40. public function save($activity, $params = [])
  41. {
  42. $goodsList = $params['goods_list'];
  43. $this->saveSkuPrice($goodsList, $activity);
  44. }
  45. public function recoverSkuPrices($goods, $activity)
  46. {
  47. $activitySkuPrices = $activity['activity_sku_prices'];
  48. $skuPrices = $goods->sku_prices;
  49. foreach ($skuPrices as $key => &$skuPrice) {
  50. $stock = $skuPrice->stock; // 下面要用
  51. $skuPrice->stock = 0;
  52. $skuPrice->sales = 0;
  53. foreach ($activitySkuPrices as $activitySkuPrice) {
  54. if ($skuPrice['id'] == $activitySkuPrice['goods_sku_price_id']) {
  55. // 采用活动的 规格内容
  56. $skuPrice->old_price = $skuPrice->price; // 保存原始普通商品规格的价格(计算活动的优惠)
  57. $skuPrice->stock = ($activitySkuPrice['stock'] > $stock) ? $stock : $activitySkuPrice['stock']; // 活动库存不能超过商品库存
  58. $skuPrice->sales = $activitySkuPrice['sales'];
  59. $skuPrice->price = $activitySkuPrice['price']; //砍完才是活动价,没砍应该是原价
  60. $skuPrice->status = $activitySkuPrice['status']; // 采用活动的上下架
  61. $skuPrice->min_price = $activitySkuPrice['price']; // 当前活动规格最小价格,这里是秒杀价
  62. $skuPrice->max_price = $activitySkuPrice['price']; // 用作计算活动中最大价格
  63. // 记录相关活动类型
  64. $skuPrice->activity_type = $activity['type'];
  65. $skuPrice->activity_id = $activity['id'];
  66. $skuPrice->activity_endtime = $activity['end_time'];
  67. $skuPrice->activity_seconds = $activity['end_time'] - time();
  68. // 下单的时候需要存活动 的 sku_price_id)
  69. $skuPrice->item_goods_sku_price = $activitySkuPrice;
  70. break;
  71. }
  72. }
  73. }
  74. return $skuPrices;
  75. }
  76. /**
  77. * 这里要使用 shoproException 抛出异常
  78. *
  79. * @param array $buyInfo
  80. * @param array $activity
  81. * @return array
  82. */
  83. public function buyCheck($buyInfo, $activity)
  84. {
  85. $buy_type = 'kan';
  86. $kan_id = request()->param('kan_id', 0);
  87. $user = auth_user();
  88. // 砍价
  89. $rules = $activity['rules'];
  90. $currentSkuPrice = $buyInfo['current_sku_price'];
  91. //dump($currentSkuPrice);
  92. //砍价情况
  93. $map_kan = [
  94. 'id' => $kan_id,
  95. 'user_id' => $user['id'],
  96. 'goods_id' => $currentSkuPrice['goods_id'],
  97. 'goods_sku_price_id' => $currentSkuPrice['id'],
  98. 'activity_id' => $currentSkuPrice['activity_id'],
  99. 'status' => ['IN',['ing','finish']],
  100. 'expire_time' => ['gt',time()],
  101. ];
  102. $kan_info = Db::name('shopro_activity_kan')->where($map_kan)->find();
  103. if(empty($kan_info)){
  104. throw new ShoproException('本次砍价下过单了或已过期');
  105. }
  106. $total_kan_price = $kan_info ? $kan_info['total_kan_price'] : 0;
  107. //砍价情况
  108. $buyInfo['current_sku_price']['price'] = $buyInfo['current_sku_price']['old_price'] - $total_kan_price;
  109. // 当前库存,小于要购买的数量
  110. $need_num = $buyInfo['goods_num'] + ($need_add_num ?? 0);
  111. if ($currentSkuPrice['stock'] < $need_num) {
  112. throw new ShoproException('商品库存不足');
  113. }
  114. $buyInfo['is_commission'] = $rules['is_commission'] ?? 0; // 是否参与分销
  115. return $buyInfo;
  116. }
  117. public function buy($buyInfo, $activity)
  118. {
  119. $user = auth_user();
  120. $buy_type = 'kan';
  121. $kan_id = request()->param('kan_id', 0);
  122. // 判断 并 增加 redis 销量
  123. $stockSale = new StockSale();
  124. $stockSale->cacheForwardSale($buyInfo);
  125. return $buyInfo;
  126. }
  127. public function buyOk($order, $user)
  128. {
  129. // 不需要处理
  130. }
  131. /**
  132. * 拼团购买失败
  133. *
  134. * @param \think\Model $order
  135. * @param string $type
  136. * @return void
  137. */
  138. public function buyFail($order, $type)
  139. {
  140. // 判断扣除预销量 (活动信息还在 redis)
  141. $stockSale = new StockSale();
  142. $stockSale->cacheBackSale($order);
  143. }
  144. }