123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?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;
- use QueueClient;
- use bonus;
- class factory
- {
- public function __construct()
- {
- }
- public function create($params,&$fnew)
- {
- $fnew = false;
- $type = $params['type'];
- if($type == proto_type::sroom_bargain) {
- return $this->create_bargain($params);
- }
- elseif($type == proto_type::sroom_chat) {
- return $this->create_chat($params,$fnew);
- }
- elseif($type == proto_type::sroom_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 room_info($room_params);
- if($rinfo->type() == proto_type::room_bargain) {
- return new bargain_room($room_params,$participants);
- }
- elseif ($rinfo->type() == proto_type::room_chat) {
- return new chat_room($room_params,$participants);
- }
- elseif($rinfo->type() == proto_type::room_shake_bonus) {
- return new shake_room($room_params,$participants);
- }
- else {
- return false;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private function create_bargain($params)
- {
- $creator = intval($params['creator']);
- $good_id = intval($params['goods_id']);
- $usable_days = intval($params['usable_days']);
- $lowest_price = $params['lowest_price'];
- $type = $params['random'];
- $total_num = $params['total_num'];
- 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(new bonus\account($creator,false));
- $summarys = $helper->summary([$good_id],$related_goods);
- if(empty($summarys) || empty($summarys['summary'])) return false;
- $summary = $summarys['summary'][0];
- $bargain_id = $mod_bargain->create($room_id,$creator,$good_id,$summary['goods_price'],$lowest_price,$usable_days,$type,$total_num);
- if($bargain_id > 0) {
- QueueClient::async_push('onAsyncBargain',['type' => "close", 'bargain_id' => $bargain_id],$usable_days * 86400);
- }
- return ['bargain_id' => $bargain_id,'room' => $room_id,'creator' => $creator];
- }
- private function create_chat($params,&$fnew)
- {
- try
- {
- $fnew = false;
- $user = intval($params['creator']);
- if($user <= 0) return false;
- $creator = new member_info($user);
- $room_type = proto_type::room_chat;
- $room_id = $this->create_room($room_type,'',$creator);
- $fnew = true;
- 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;
- }
- public static function participants($roomid)
- {
- $roomid = intval($roomid);
- if($roomid <= 0) return false;
- $mod_room = Model('room');
- $items = $mod_room->participants($roomid);
- $uids = [];
- $member_nick = [];
- foreach ($items as $item) {
- $uid = intval($item['member_id']);
- $uids[] = $uid;
- $member_nick[$uid] = $item['member_nick'];
- }
- $uids = array_unique($uids,SORT_NUMERIC);
- if(!empty($uids)) {
- $members = Model('member')->getMemberList(['member_id' => ['in',$uids]]);
- } else {
- $members = [];
- }
- $uid_infos = [];
- foreach ($members as $member) {
- $uinfo = new member_info($member);
- $user = $uinfo->member_id();
- $nickname = empty($member_nick[$user]) ? $uinfo->nickname() : $member_nick[$user];
- $item = ['avatar' => $uinfo->avatar(),'nickname' => $nickname,'userid' => $uinfo->member_id()];
- $uid_infos[$user] = $item;
- }
- $result = [];
- foreach ($items as $item)
- {
- $uid = intval($item['member_id']);
- if(array_key_exists($uid,$uid_infos)){
- $result[$uid] = $uid_infos[$uid];
- }
- }
- return $result;
- }
- }
|