factory.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 下午5:30
  7. */
  8. namespace room;
  9. use Exception;
  10. use member_info;
  11. use Log;
  12. use goods_helper;
  13. use QueueClient;
  14. use bonus;
  15. class factory
  16. {
  17. public function __construct()
  18. {
  19. }
  20. public function create($params,&$fnew)
  21. {
  22. $fnew = false;
  23. $type = $params['type'];
  24. if($type == proto_type::sroom_bargain) {
  25. return $this->create_bargain($params);
  26. }
  27. elseif($type == proto_type::sroom_chat) {
  28. return $this->create_chat($params,$fnew);
  29. }
  30. elseif($type == proto_type::sroom_shake_bonus) {
  31. return $this->create_shake_bonus($params);
  32. }
  33. else {
  34. return false;
  35. }
  36. }
  37. public function build($room_id)
  38. {
  39. $mod_room = Model('room');
  40. $room_params = $mod_room->getRoom($room_id);
  41. if(empty($room_params)) return false;
  42. $participants = self::participants($room_id);
  43. $rinfo = new room_info($room_params);
  44. if($rinfo->type() == proto_type::room_bargain) {
  45. return new bargain_room($room_params,$participants);
  46. }
  47. elseif ($rinfo->type() == proto_type::room_chat) {
  48. return new chat_room($room_params,$participants);
  49. }
  50. elseif($rinfo->type() == proto_type::room_shake_bonus) {
  51. return new shake_room($room_params,$participants);
  52. }
  53. else {
  54. return false;
  55. }
  56. }
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. private function create_bargain($params)
  59. {
  60. $creator = intval($params['creator']);
  61. $good_id = intval($params['goods_id']);
  62. $usable_days = intval($params['usable_days']);
  63. $lowest_price = $params['lowest_price'];
  64. $type = $params['random'];
  65. $total_num = $params['total_num'];
  66. if($creator <= 0 || $good_id <= 0) return false;
  67. $mod_bargain = Model('room_bargain');
  68. $ret = $mod_bargain->getBargainByUserGoods($creator,$good_id,true);
  69. if(!empty($ret)) {
  70. $bargain = new bargain($ret);
  71. return ['bargain_id' => $bargain->bargain_id(),'room' => $bargain->room(),'creator' => $bargain->creator()];
  72. }
  73. $minfo = new member_info($creator);
  74. $room_id = $this->create_room(proto_type::room_bargain,"bargain",$minfo);
  75. if($room_id === false) return false;
  76. $helper = new goods_helper(new bonus\account($creator,false));
  77. $summarys = $helper->summary([$good_id],$related_goods);
  78. if(empty($summarys) || empty($summarys['summary'])) return false;
  79. $summary = $summarys['summary'][0];
  80. $bargain_id = $mod_bargain->create($room_id,$creator,$good_id,$summary['goods_price'],$lowest_price,$usable_days,$type,$total_num);
  81. if($bargain_id > 0) {
  82. QueueClient::async_push('onAsyncBargain',['type' => "close", 'bargain_id' => $bargain_id],$usable_days * 86400);
  83. }
  84. return ['bargain_id' => $bargain_id,'room' => $room_id,'creator' => $creator];
  85. }
  86. private function create_chat($params,&$fnew)
  87. {
  88. try
  89. {
  90. $fnew = false;
  91. $user = intval($params['creator']);
  92. if($user <= 0) return false;
  93. $creator = new member_info($user);
  94. $room_type = proto_type::room_chat;
  95. $room_id = $this->create_room($room_type,'',$creator);
  96. $fnew = true;
  97. return ['room' => $room_id,'creator' => $user];
  98. }
  99. catch (Exception $ex) {
  100. Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
  101. return false;
  102. }
  103. }
  104. private function create_shake_bonus($params)
  105. {
  106. try
  107. {
  108. $user = intval($params['creator']);
  109. if($user <= 0) return false;
  110. $mod_room = Model('room');
  111. $room = $mod_room->getRoomByType(proto_type::room_shake_bonus,$user);
  112. if(empty($room))
  113. {
  114. $creator = new member_info($user);
  115. $room_type = proto_type::room_shake_bonus;
  116. $room_id = $this->create_room($room_type,'shake_bonus',$creator);
  117. }
  118. else {
  119. $room_id = intval($room['room_id']);
  120. }
  121. return ['room' => $room_id,'creator' => $user];
  122. }
  123. catch (Exception $ex) {
  124. Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
  125. return false;
  126. }
  127. }
  128. private function create_room($room_type,$name,member_info $creator)
  129. {
  130. $mod_room = Model('room');
  131. $ret = $mod_room->create_room($room_type,$name,$creator->member_id(),$creator->member_id(),$creator->nickname());
  132. return $ret;
  133. }
  134. public static function participants($roomid)
  135. {
  136. $roomid = intval($roomid);
  137. if($roomid <= 0) return false;
  138. $mod_room = Model('room');
  139. $items = $mod_room->participants($roomid);
  140. $uids = [];
  141. $member_nick = [];
  142. foreach ($items as $item) {
  143. $uid = intval($item['member_id']);
  144. $uids[] = $uid;
  145. $member_nick[$uid] = $item['member_nick'];
  146. }
  147. $uids = array_unique($uids,SORT_NUMERIC);
  148. if(!empty($uids)) {
  149. $members = Model('member')->getMemberList(['member_id' => ['in',$uids]]);
  150. } else {
  151. $members = [];
  152. }
  153. $uid_infos = [];
  154. foreach ($members as $member) {
  155. $uinfo = new member_info($member);
  156. $user = $uinfo->member_id();
  157. $nickname = empty($member_nick[$user]) ? $uinfo->nickname() : $member_nick[$user];
  158. $item = ['avatar' => $uinfo->avatar(),'nickname' => $nickname,'userid' => $uinfo->member_id()];
  159. $uid_infos[$user] = $item;
  160. }
  161. $result = [];
  162. foreach ($items as $item)
  163. {
  164. $uid = intval($item['member_id']);
  165. if(array_key_exists($uid,$uid_infos)){
  166. $result[$uid] = $uid_infos[$uid];
  167. }
  168. }
  169. return $result;
  170. }
  171. }