factory.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. class factory
  14. {
  15. public function __construct()
  16. {
  17. }
  18. public function create($params)
  19. {
  20. $type = $params['type'];
  21. if($type == proto_type::act_bargain) {
  22. return $this->create_bargain($params);
  23. }
  24. elseif($type == proto_type::act_group) {
  25. return $this->create_group($params);
  26. }
  27. elseif($type == proto_type::act_shake_bonus) {
  28. return $this->create_shake_bonus($params);
  29. }
  30. else {
  31. return false;
  32. }
  33. }
  34. public function build($room_id)
  35. {
  36. $mod_room = Model('room');
  37. $room_params = $mod_room->getRoom($room_id);
  38. if(empty($room_params)) return false;
  39. $participants = self::participants($room_id);
  40. $rinfo = new base_info($room_params);
  41. if($rinfo->type() == proto_type::room_bargain) {
  42. return new bargain_room($room_params,$participants);
  43. }
  44. elseif ($rinfo->type() == proto_type::room_group) {
  45. return new group_room($room_params,$participants);
  46. }
  47. elseif($rinfo->type() == proto_type::room_shake_bonus) {
  48. return new shake_room($room_params,$participants);
  49. }
  50. elseif ($rinfo->type() == proto_type::room_chatwo) {
  51. }
  52. else {
  53. return false;
  54. }
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. private function create_bargain($params)
  58. {
  59. $creator = intval($params['creator']);
  60. $good_id = intval($params['goods_id']);
  61. $usable_days = intval($params['usable_days']);
  62. $lowest_price = $params['lowest_price'];
  63. $type = $params['random'];
  64. $total_num = $params['total_num'];
  65. if($creator <= 0 || $good_id <= 0) return false;
  66. $mod_bargain = Model('room_bargain');
  67. $ret = $mod_bargain->getBargainByUserGoods($creator,$good_id,true);
  68. if(!empty($ret)) {
  69. $bargain = new bargain($ret);
  70. return ['bargain_id' => $bargain->bargain_id(),'room' => $bargain->room(),'creator' => $bargain->creator()];
  71. }
  72. $minfo = new member_info($creator);
  73. $room_id = $this->create_room(proto_type::room_bargain,"bargain",$minfo);
  74. if($room_id === false) return false;
  75. $helper = new goods_helper();
  76. $summarys = $helper->summary([$good_id],$related_goods);
  77. if(empty($summarys) || empty($summarys['summary'])) return false;
  78. $summary = $summarys['summary'][0];
  79. $bargain_id = $mod_bargain->create($room_id,$creator,$good_id,$summary['goods_price'],$lowest_price,$usable_days,$type,$total_num);
  80. return ['bargain_id' => $bargain_id,'room' => $room_id,'creator' => $creator];
  81. }
  82. private function create_group($params)
  83. {
  84. try
  85. {
  86. $user = intval($params['creator']);
  87. if($user <= 0) return false;
  88. $creator = new member_info($user);
  89. $room_type = proto_type::room_group;
  90. $room_id = $this->create_room($room_type,'group',$creator);
  91. return ['room' => $room_id,'creator' => $user];
  92. }
  93. catch (Exception $ex) {
  94. Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
  95. return false;
  96. }
  97. }
  98. private function create_shake_bonus($params)
  99. {
  100. try
  101. {
  102. $user = intval($params['creator']);
  103. if($user <= 0) return false;
  104. $mod_room = Model('room');
  105. $room = $mod_room->getRoomByType(proto_type::room_shake_bonus,$user);
  106. if(empty($room))
  107. {
  108. $creator = new member_info($user);
  109. $room_type = proto_type::room_shake_bonus;
  110. $room_id = $this->create_room($room_type,'shake_bonus',$creator);
  111. }
  112. else {
  113. $room_id = intval($room['room_id']);
  114. }
  115. return ['room' => $room_id,'creator' => $user];
  116. }
  117. catch (Exception $ex) {
  118. Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
  119. return false;
  120. }
  121. }
  122. private function create_room($room_type,$name,member_info $creator)
  123. {
  124. $mod_room = Model('room');
  125. $ret = $mod_room->create_room($room_type,$name,$creator->member_id(),$creator->member_id(),$creator->nickname());
  126. return $ret;
  127. }
  128. private static function participants($roomid)
  129. {
  130. $roomid = intval($roomid);
  131. if($roomid <= 0) return false;
  132. $mod_room = Model('room');
  133. $items = $mod_room->participants($roomid);
  134. $key_ids = [];
  135. $uids = [];
  136. foreach ($items as $item) {
  137. $uid = intval($item['member_id']);
  138. $room_key = $item['room_key'];
  139. $key_ids[$room_key] = $uid;
  140. $uids[] = $uid;
  141. }
  142. $uids = array_unique($uids,SORT_NUMERIC);
  143. $members = Model('member')->getMemberList(array('member_id' => array('in',$uids)));
  144. $uid_infos = [];
  145. foreach ($members as $member) {
  146. $uinfo = new member_info($member);
  147. $user = $uinfo->member_id();
  148. $item = ['avatar' => $uinfo->avatar(),'nickname' => $uinfo->nickname(),'userid' => $uinfo->member_id(),'active' => false];
  149. $uid_infos[$user] = $item;
  150. }
  151. $result = [];
  152. foreach ($key_ids as $key => $uid) {
  153. $result[$key] = $uid_infos[$uid];
  154. }
  155. return $result;
  156. }
  157. }