Quellcode durchsuchen

帮别人砍价

lizhen_gitee vor 3 Monaten
Ursprung
Commit
eaff3fa4dd
1 geänderte Dateien mit 104 neuen und 3 gelöschten Zeilen
  1. 104 3
      addons/shopro/controller/activity/Kan.php

+ 104 - 3
addons/shopro/controller/activity/Kan.php

@@ -63,7 +63,10 @@ class Kan extends Common
         $activity_id        = $this->request->param('activity_id');
         $activity_id        = $this->request->param('activity_id');
 
 
         //活动
         //活动
-        $activity = Db::name('shopro_activity')->where('id',$activity_id)->find();
+        $activity = Db::name('shopro_activity')->where('type','kan')->where('deletetime',NULL)->where('id',$activity_id)->find();
+        if(empty($activity)){
+            $this->error('不存在的砍价活动');
+        }
         if($activity['end_time'] < time()){
         if($activity['end_time'] < time()){
             $this->error('该砍价活动已结束');
             $this->error('该砍价活动已结束');
         }
         }
@@ -122,7 +125,99 @@ class Kan extends Common
 
 
     //帮好友砍价
     //帮好友砍价
     public function kan_kan(){
     public function kan_kan(){
+        $kan_id          = $this->request->param('kan_id');
+
+        Db::startTrans();
+        //此次砍价
+        $kan = Db::name('shopro_activity_kan')->where('id',$kan_id)->lock(true)->find();
+        if(empty($kan)){
+            Db::rollback();
+            $this->error('不存在的砍价活动');
+        }
+        if($kan['status'] == 'invalid'){
+            Db::rollback();
+            $this->error('此次砍价已过期,不需要再砍啦!');
+        }
+        if($kan['status'] != 'ing'){
+            Db::rollback();
+            $this->error('此次砍价已完成,不需要再砍啦!');
+        }
+        if($kan['current_num'] >= $kan['num']){
+            Db::rollback();
+            $this->error('已经达到最高砍价人数,不需要再砍啦!');
+        }
+
+        //商品原价
+        $goods_sku_price = Db::name('shopro_goods_sku_price')->where('id',$kan['goods_sku_price_id'])->find();
+
+        //总差价 = 商品原价 - 活动低价
+        $cha_price = bcsub($goods_sku_price['price'],$kan['activity_sku_price'],2); if($cha_price <= 0){$cha_price = 0;}
+
+        //还能砍的剩余价 = 总差价 - 已经砍掉的价
+        $remain_price = bcsub($cha_price,$kan['total_kan_price'],2); if($remain_price <= 0){$remain_price = 0;}
+
+        //本次能砍掉的价格
+        $kan_price = 0;
+        $is_last = 0;
+        if($kan['num'] - $kan['current_num'] == 1){
+            //最后一砍
+            $kan_price = $remain_price;
+            $is_last = 1;
+        }else{
+            //每次砍价的平均数
+            $pingjun_price = bcdiv($cha_price,$kan['num'],2);
+
+            //上下100分钱的浮动
+            $min = $pingjun_price*100 - 100; if($min <= 0){$min = 0;}
+            $max = $pingjun_price*100 + 100;
+            $kan_price = bcdiv(rand($min,$max),100,2);
+
+            //一般最后一次的时候 浮动过大,最后一砍,直接强制结束
+            if($kan_price >= $remain_price){
+                $kan_price = $remain_price;
+                $is_last = 1;
+            }
+        }
+
+        //
+        $user = auth_user();
+        $nowtime = time();
+
+        //日志
+        $log_data = [
+            'user_id' => $user['id'],
+            'nickname' => $user['nickname'],
+            'avatar' => $user['avatar'],
+            'kan_id' => $kan_id,
+            'activity_id' => $kan['activity_id'],
+            'kan_price' => $kan_price,
+            'createtime' => $nowtime,
+            'updatetime' => $nowtime,
+        ];
+        $log_id = Db::name('shopro_activity_kan_log')->insertGetId($log_data);
+        if(!$log_id){
+            Db::rollback();
+            $this->error('砍价失败了');
+        }
+
+        //砍价修改
+        $update = [
+            'current_num' => $kan['current_num'] + 1,
+            'updatetime'  => $nowtime,
+            'total_kan_price' => $kan['total_kan_price'] + $kan_price,
+        ];
+        if($is_last == 1){
+            $update['status'] = 'finish';
+            $update['finish_time'] = $nowtime;
+        };
+        $rs_update = Db::name('shopro_activity_kan')->where('id',$kan_id)->update($update);
+        if(!$rs_update){
+            Db::rollback();
+            $this->error('砍价失败了');
+        }
 
 
+        Db::commit();
+        $this->success('砍价成功');
     }
     }
 
 
     //某个已发起的砍价详情
     //某个已发起的砍价详情
@@ -133,14 +228,20 @@ class Kan extends Common
             ->field(['kan.*',
             ->field(['kan.*',
                 'g.title','g.image',
                 'g.title','g.image',
                 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price',
                 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price',
-                '','','','','','','','','','','','','','','','','','','','','','','','','',''])
+                ])
             ->join('shopro_goods g','kan.goods_id = g.id','LEFT')
             ->join('shopro_goods g','kan.goods_id = g.id','LEFT')
             ->join('shopro_goods_sku_price gsp','kan.goods_sku_price_id = gsp.id','LEFT')
             ->join('shopro_goods_sku_price gsp','kan.goods_sku_price_id = gsp.id','LEFT')
             ->where('kan.id',$kan_id)
             ->where('kan.id',$kan_id)
             ->find();
             ->find();
 
 
         //砍价记录
         //砍价记录
-        Db::name('shopro_');
+        $kan_log = Db::name('shopro_activity_kan_log')->where('kan_id',$kan_id)->order('id asc')->select();
+
+        $result = [
+            'activity' => $kan,
+            'log'      => $kan_log,
+        ];
+        $this->success('获取成功',$result);
     }
     }