截流自动化的商城平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SeckillGoodsValidate.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\shop\validate;
  3. use app\common\model\bargain\Bargain;
  4. use app\common\model\team\TeamActivity;
  5. use think\Validate;
  6. use app\common\model\seckill\SeckillTime;
  7. use app\common\model\seckill\SeckillGoods;
  8. use app\common\model\goods\Goods;
  9. use app\common\model\goods\GoodsItem;
  10. class SeckillGoodsValidate extends Validate
  11. {
  12. protected $rule = [
  13. 'start_end' => 'require',
  14. 'seckill_id' => 'require|checkSeckill',
  15. 'item' => 'require|checkActivity'
  16. ];
  17. protected $message = [
  18. 'start_end.require' => '请选择日期',
  19. 'seckill_id.require' => '请选择秒杀时段',
  20. 'item.require' => '请选择秒杀商品',
  21. ];
  22. public function checkSeckill($value,$rule,$data){
  23. $seckill = SeckillTime::where(['del'=>0,'id'=>$value])->findOrEmpty();
  24. if($seckill->isEmpty()){
  25. return '秒杀时间段已被调整,请重新选择时间段';
  26. }
  27. return true;
  28. }
  29. public function sceneAdd(){
  30. $this->append('item','checkAddGoods');
  31. }
  32. public function sceneEdit(){
  33. $this->append('item','checkEditGoods');
  34. }
  35. public function checkAddGoods($value,$rule,$data){
  36. $goods_ids = array_unique(array_column($value,'goods_id'));
  37. $goods = Goods::where(['del'=>0, 'shop_id'=>$data['shop_id']])->column('id');
  38. $goods_item = GoodsItem::where(['goods_id'=>$goods_ids])->column('price,spec_value_str','id');
  39. $seckill_goods = SeckillGoods::where(['seckill_id'=>$data['seckill_id'],'del'=>0])->column('item_id');
  40. // 参与时期
  41. $start_end_arr = explode('~', $data['start_end']);
  42. $start_date = strtotime(trim($start_end_arr[0]));
  43. $end_date = strtotime(trim($start_end_arr[1]));
  44. // 同一日期同一时间段内不允许重复添加活动商品
  45. foreach($value as $item) {
  46. if(!in_array($item['goods_id'],$goods)){
  47. return '商品ID:'.$item['goods_id'].'已下架';
  48. }
  49. $goods_price = $goods_item[$item['item_id']]['price'] ?? 0;
  50. //验证商品价格
  51. if($item['price'] > $goods_price){
  52. return '商品规格:'.$goods_item[$item['item_id']]['spec_value_str'] .'的秒杀价格高于原价';
  53. }
  54. // 获取当前商品参与过哪些日期及哪些时段的秒杀
  55. $joinDateTime = SeckillGoods::where(['del'=>0, 'goods_id'=>$item['goods_id']])
  56. ->field('seckill_id,goods_id,start_date,end_date')
  57. ->group('seckill_id,goods_id,start_date,end_date')
  58. ->select()
  59. ->toArray();
  60. foreach($joinDateTime as $subItem) {
  61. if($data['seckill_id'] == $subItem['seckill_id'] && $start_date < strtotime($subItem['start_date']) && $end_date < strtotime($subItem['start_date'])) {
  62. // 时间段相同,新增日期不在已存在的日期范围,允许添加
  63. continue;
  64. }else if($data['seckill_id'] == $subItem['seckill_id'] && $start_date > strtotime($subItem['end_date']) && $end_date > strtotime($subItem['end_date'])) {
  65. // 时间段相同,新增日期不在已存在的日期范围,允许添加
  66. continue;
  67. }else if($data['seckill_id'] != $subItem['seckill_id']) {
  68. // 时间段不同,允许新增
  69. continue;
  70. }else{
  71. return '商品已在活动中,请勿重新添加';
  72. }
  73. }
  74. }
  75. return true;
  76. }
  77. public function checkEditGoods($value,$rule,$data){
  78. $goods_ids = array_unique(array_column($value,'goods_id'));
  79. $seckill_ids = array_column($value,'id');
  80. $seckill_goods = SeckillGoods::where(['goods_id'=>$goods_ids,'seckill_id'=>$data['seckill_id']])
  81. ->where('id','not in',$seckill_ids)
  82. ->find();
  83. $goods = Goods::where(['del'=>0])->column('id');
  84. $goods_item = GoodsItem::where(['goods_id'=>$goods_ids])->column('price,spec_value_str','id');
  85. // 参与时期
  86. $start_end_arr = explode('~', $data['start_end']);
  87. $start_date = strtotime(trim($start_end_arr[0]));
  88. $end_date = strtotime(trim($start_end_arr[1]));
  89. // 同一日期同一时间段内不允许重复添加活动商品
  90. foreach ($value as $item){
  91. if(!in_array($item['goods_id'],$goods)){
  92. return '商品ID:'.$item['goods_id'].'已下架';
  93. }
  94. $goods_price = $goods_item[$item['item_id']]['price'] ?? 0;
  95. //验证商品价格
  96. if($item['price'] > $goods_price){
  97. return '商品规格:'.$goods_item[$item['item_id']]['spec_value_str'] .'的秒杀价格高于原价';
  98. }
  99. // 获取当前商品参与过哪些日期及哪些时段的秒杀
  100. $joinDateTime = SeckillGoods::where([
  101. ['del', '=', 0],
  102. ['goods_id', '=', $item['goods_id']],
  103. ['id', 'not in', $seckill_ids]
  104. ])
  105. ->field('seckill_id,goods_id,start_date,end_date')
  106. ->group('seckill_id,goods_id,start_date,end_date')
  107. ->select()
  108. ->toArray();
  109. foreach($joinDateTime as $subItem) {
  110. if($data['seckill_id'] == $subItem['seckill_id'] && $start_date < strtotime($subItem['start_date']) && $end_date < strtotime($subItem['start_date'])) {
  111. // 时间段相同,新增日期不在已存在的日期范围,允许添加
  112. continue;
  113. }else if($data['seckill_id'] == $subItem['seckill_id'] && $start_date > strtotime($subItem['end_date']) && $end_date > strtotime($subItem['end_date'])) {
  114. // 时间段相同,新增日期不在已存在的日期范围,允许添加
  115. continue;
  116. }else if($data['seckill_id'] != $subItem['seckill_id']) {
  117. // 时间段不同,允许新增
  118. continue;
  119. }else{
  120. return '商品已在活动中,请勿重新添加';
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126. public function checkActivity($item, $rule, $data)
  127. {
  128. foreach($item as $v) {
  129. $team = TeamActivity::where(['del' => 0, 'goods_id' => $v['goods_id']])->select()->toArray();
  130. if($team) {
  131. return '商品正在参加拼团活动,不能再参与限时秒杀!';
  132. }
  133. $bargain = Bargain::where(['del' => 0, 'goods_id' => $v['goods_id']])->select()->toArray();
  134. if($bargain) {
  135. return '商品正在参加砍价活动,不能再参与限时秒杀!';
  136. }
  137. }
  138. return true;
  139. }
  140. }