123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 下午5:30
- */
- namespace room;
- use Exception;
- use member_info;
- use Log;
- use goods_helper;
- class factory
- {
- public function __construct()
- {
- }
- public function create($params)
- {
- $type = $params['type'];
- if($type == proto_type::act_bargain) {
- return $this->create_bargain($params);
- }
- elseif($type == proto_type::act_group) {
- return $this->create_group($params);
- }
- elseif($type == proto_type::act_shake_bonus) {
- return $this->create_shake_bonus($params);
- }
- else {
- return false;
- }
- }
- public function build($room_id)
- {
- $mod_room = Model('room');
- $room_params = $mod_room->getRoom($room_id);
- if(empty($room_params)) return false;
- $participants = self::participants($room_id);
- $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);
- }
- elseif($rinfo->type() == proto_type::room_shake_bonus) {
- return new shake_room($room_params,$participants);
- }
- elseif ($rinfo->type() == proto_type::room_chatwo) {
- }
- else {
- return false;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private function create_bargain($params)
- {
- 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
- {
- $user = intval($params['creator']);
- if($user <= 0) return false;
- $creator = new member_info($user);
- $room_type = proto_type::room_group;
- $room_id = $this->create_room($room_type,'group',$creator);
- return ['room' => $room_id,'creator' => $user];
- }
- catch (Exception $ex) {
- Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
- return false;
- }
- }
- private function create_shake_bonus($params)
- {
- try
- {
- $user = intval($params['creator']);
- if($user <= 0) return false;
- $mod_room = Model('room');
- $room = $mod_room->getRoomByType(proto_type::room_shake_bonus,$user);
- if(empty($room))
- {
- $creator = new member_info($user);
- $room_type = proto_type::room_shake_bonus;
- $room_id = $this->create_room($room_type,'shake_bonus',$creator);
- }
- else {
- $room_id = intval($room['room_id']);
- }
- return ['room' => $room_id,'creator' => $user];
- }
- catch (Exception $ex) {
- Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
- return false;
- }
- }
- private function create_room($room_type,$name,member_info $creator)
- {
- $mod_room = Model('room');
- $ret = $mod_room->create_room($room_type,$name,$creator->member_id(),$creator->member_id(),$creator->nickname());
- return $ret;
- }
- private static function participants($roomid)
- {
- $roomid = intval($roomid);
- if($roomid <= 0) return false;
- $mod_room = Model('room');
- $items = $mod_room->participants($roomid);
- $key_ids = [];
- $uids = [];
- foreach ($items as $item) {
- $uid = intval($item['member_id']);
- $room_key = $item['room_key'];
- $key_ids[$room_key] = $uid;
- $uids[] = $uid;
- }
- $uids = array_unique($uids,SORT_NUMERIC);
- $members = Model('member')->getMemberList(array('member_id' => array('in',$uids)));
- $uid_infos = [];
- foreach ($members as $member) {
- $uinfo = new member_info($member);
- $user = $uinfo->member_id();
- $item = ['avatar' => $uinfo->avatar(),'nickname' => $uinfo->nickname(),'userid' => $uinfo->member_id(),'active' => false];
- $uid_infos[$user] = $item;
- }
- $result = [];
- foreach ($key_ids as $key => $uid) {
- $result[$key] = $uid_infos[$uid];
- }
- return $result;
- }
- }
|