stanley-king 7 éve
szülő
commit
c3fa7ae68a

+ 35 - 0
data/model/room_bargain.model.php

@@ -0,0 +1,35 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2018/1/5
+ * Time: 下午8:00
+ */
+
+class room_bargainModel extends Model
+{
+    public function __construct() {
+        parent::__construct('room_bargain');
+    }
+
+    public function create($roomid,$userid,$goods_id,$goods_price,$cost_price,$lowest_price,$days)
+    {
+        $params = ['room_id' => $roomid,'user_id' => $userid,'goods_id' => $goods_id,
+            'goods_price' => $goods_price,'cost_price' => $cost_price,'lowest_price' => $lowest_price,
+            'add_time' => time(),'over_time' => time() + 86400 * $days];
+
+        return $this->insert($params);
+    }
+
+    public function getBargainById($bargain_id,$lock=false) {
+        return $this->where(['bargain_id' => $bargain_id])->lock($lock)->find();
+    }
+
+    public function getBargainByRoom($room_id,$lock=false) {
+        return $this->where(['room_id' => $room_id])->lock($lock)->find();
+    }
+
+    public function getBargainByUserGoods($user_id,$goods_id,$lock=false) {
+        return $this->where(['user_id' => $user_id,'goods_id' => $goods_id])->lock($lock)->find();
+    }
+}

+ 80 - 0
helper/room/bargain_room.php

@@ -9,11 +9,91 @@
 namespace room;
 
 
+class bargain
+{
+    private $mParams;
+
+    public function __construct($params) {
+        $this->mParams = $params;
+    }
+    public function bargain_id() {
+        return intval($this->mParams['bargain_id']);
+    }
+    public function room() {
+        return intval($this->mParams['room_id']);
+    }
+    public function creator() {
+        return intval($this->mParams['user_id']);
+    }
+    public function goods_price() {
+        return floatval($this->mParams['goods_price']);
+    }
+    public function cost_price() {
+        return floatval($this->mParams['cost_price']);
+    }
+    public function lowest_price() {
+        return floatval($this->mParams['lowest_price']);
+    }
+    public function discount() {
+        return floatval($this->mParams['discount']);
+    }
+    public function add_time() {
+        return intval($this->mParams['add_time']);
+    }
+    public function over_time() {
+        return intval($this->mParams['over_time']);
+    }
+    public function user_num() {
+        return intval($this->mParams['user_num']);
+    }
+    public function has_over() {
+        return $this->over_time() <= time();
+    }
+}
+
 class bargain_room extends base_room
 {
+    private $mBargain;
+
     public function __construct($cinfos, $roomkeys = [])
     {
         parent::__construct($cinfos,$roomkeys);
         $this->mRoomType = 'bargain';
+        $this->init();
+    }
+
+    private function init()
+    {
+        $mod_bargain = Model('room_bargain');
+        $info = $mod_bargain->getBargainByRoom($this->room_id(),true);
+        $this->mBargain = new bargain($info);
+    }
+
+    public function bargainOp($input)
+    {
+        $this->clear();
+
+        $room_key = $input['room_key'];
+        if(empty($room_key)) {
+            return false;
+        }
+
+        $this->mAccReq = true;
+        $ret = $this->bargain($room_key);
+        if($ret !== false)
+        {
+            $this->add_return([$room_key],'ret_bargain',['value' => $ret]);
+            $this->add_broad('bargain',['value' => $ret]);
+            return true;
+        }
+        else {
+            return false;
+        }
     }
+
+    private function bargain($room_key)
+    {
+        return 3;
+    }
+
 }

+ 1 - 1
helper/room/base_room.php

@@ -80,7 +80,7 @@ abstract class base_room extends base_info
         return true;
     }
 
-    private function room_info($roomkey)
+    protected function room_info($roomkey)
     {
         $result = [];
         $result['room'] = $this->mRoomid;

+ 31 - 3
helper/room/factory.php

@@ -11,6 +11,7 @@ namespace room;
 use Exception;
 use member_info;
 use Log;
+use goods_helper;
 
 class factory
 {
@@ -44,7 +45,7 @@ class factory
 
         $rinfo = new base_info($room_params);
         if($rinfo->type() == proto_type::room_bargain) {
-
+            return new bargain_room($room_params,$participants);
         }
         elseif ($rinfo->type() == proto_type::room_group) {
             return new group_room($room_params,$participants);
@@ -60,10 +61,38 @@ class factory
     }
 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
     private function create_bargain($params)
     {
-        return false;
+        global $config;
+        $usable_days = intval($config['bargain']['usable_days']);
+        $usable_days = $usable_days > 0 ? $usable_days : 3;
+
+        $creator = intval($params['creator']);
+        $good_id = intval($params['goods_id']);
+
+        $cost_price = $params['cost_price'];
+        $lowest_price = $params['lowest_price'];
+        if($creator <= 0 || $good_id <= 0) return false;
+
+        $mod_bargain = Model('room_bargain');
+        $ret = $mod_bargain->getBargainByUserGoods($creator,$good_id,true);
+        if(!empty($ret)) {
+            $bargain = new bargain($ret);
+            return ['bargain_id' => $bargain->bargain_id(),'room' => $bargain->room(),'creator' => $bargain->creator()];
+        }
+
+        $minfo = new member_info($creator);
+        $room_id = $this->create_room(proto_type::room_bargain,"bargain",$minfo);
+        if($room_id === false) return false;
+
+        $helper = new goods_helper();
+        $summary = $helper->summary([$good_id],$related_goods);
+        $bargain_id = $mod_bargain->create($room_id,$creator,$good_id,$summary['goods_price'],$cost_price,$lowest_price,$usable_days);
+
+        return ['bargain_id' => $bargain_id,'room' => $room_id,'creator' => $creator];
     }
+
     private function create_group($params)
     {
         try
@@ -115,7 +144,6 @@ class factory
         return $ret;
     }
 
-
     private static function participants($roomid)
     {
         $roomid = intval($roomid);

+ 22 - 4
test/TestRoomFactory.php

@@ -32,9 +32,6 @@ class TestRoomFactory extends PHPUnit_Framework_TestCase
     {
         Base::run_util();
     }
-    public function testProcessorCreateBargain()
-    {
-    }
     public function testProcessorCreateChatwo()
     {
     }
@@ -45,6 +42,7 @@ class TestRoomFactory extends PHPUnit_Framework_TestCase
         $creator = 36429;
         $ret = $processor->onRequest(0,json_encode(["act" => 'factory','op' => 'create', "type" => 'group','creator' => $creator]));
     }
+
     public function testProcessorInvite()
     {
         $processor = new room\factory_processor();
@@ -60,8 +58,28 @@ class TestRoomFactory extends PHPUnit_Framework_TestCase
         $creator = 36429;
         $ret = $processor->onRequest(0,json_encode(["act" => 'factory','op' => 'create', "type" => 'shake_bonus','creator' => $creator]));
     }
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+    public function testFactoryCreateBargain()
+    {
+        $factory = new room\factory();
+        $creator = 36429;
+        $parms = ["act" => 'factory','op' => 'create', "type" => 'bargain','creator' => $creator,'goods_id' => 4831,'cost_price' => 20, 'lowest_price' => 10];
+        $factory->create($parms);
+    }
+    public function testFactoryBuildBargain()
+    {
+        $factory = new room\factory();
+        $factory->build(43);
+
+    }
+    public function testProcessorCreateBargain()
+    {
+        $processor = new room\factory_processor();
+        $creator = 36429;
+        $ret = $processor->onRequest(0,json_encode(["act" => 'factory','op' => 'create', "type" => 'bargain','creator' => $creator,'goods_id' => 4831,'cost_price' => 20, 'lowest_price' => 10]));
+    }
+
+    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     public function testSocketInvite()
     {
         $user = 36429;

+ 1 - 1
test/TestRoomSrv.php

@@ -30,7 +30,7 @@ class TestRoomSrv extends PHPUnit_Framework_TestCase
 
     public function testBuildRoom()
     {
-        $roomid=30;
+        $roomid=43;
         $processor = new room\room_processor();
         $ret = $processor->onRequest(0,json_encode(["act" => 'factory','room' => $roomid,'op' => 'build']));
     }