factory.php 5.5 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. global $config;
  60. $usable_days = intval($config['bargain']['usable_days']);
  61. $usable_days = $usable_days > 0 ? $usable_days : 3;
  62. $creator = intval($params['creator']);
  63. $good_id = intval($params['goods_id']);
  64. $cost_price = $params['cost_price'];
  65. $lowest_price = $params['lowest_price'];
  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();
  77. $summary = $helper->summary([$good_id],$related_goods);
  78. $bargain_id = $mod_bargain->create($room_id,$creator,$good_id,$summary['goods_price'],$cost_price,$lowest_price,$usable_days);
  79. return ['bargain_id' => $bargain_id,'room' => $room_id,'creator' => $creator];
  80. }
  81. private function create_group($params)
  82. {
  83. try
  84. {
  85. $user = intval($params['creator']);
  86. if($user <= 0) return false;
  87. $creator = new member_info($user);
  88. $room_type = proto_type::room_group;
  89. $room_id = $this->create_room($room_type,'group',$creator);
  90. return ['room' => $room_id,'creator' => $user];
  91. }
  92. catch (Exception $ex) {
  93. Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
  94. return false;
  95. }
  96. }
  97. private function create_shake_bonus($params)
  98. {
  99. try
  100. {
  101. $user = intval($params['creator']);
  102. if($user <= 0) return false;
  103. $mod_room = Model('room');
  104. $room = $mod_room->getRoomByType(proto_type::room_shake_bonus,$user);
  105. if(empty($room))
  106. {
  107. $creator = new member_info($user);
  108. $room_type = proto_type::room_shake_bonus;
  109. $room_id = $this->create_room($room_type,'shake_bonus',$creator);
  110. }
  111. else {
  112. $room_id = intval($room['room_id']);
  113. }
  114. return ['room' => $room_id,'creator' => $user];
  115. }
  116. catch (Exception $ex) {
  117. Log::record(__METHOD__ . " {$ex->getMessage()}",Log::ERR);
  118. return false;
  119. }
  120. }
  121. private function create_room($room_type,$name,member_info $creator)
  122. {
  123. $mod_room = Model('room');
  124. $ret = $mod_room->create_room($room_type,$name,$creator->member_id(),$creator->member_id(),$creator->nickname());
  125. return $ret;
  126. }
  127. private static function participants($roomid)
  128. {
  129. $roomid = intval($roomid);
  130. if($roomid <= 0) return false;
  131. $mod_room = Model('room');
  132. $items = $mod_room->participants($roomid);
  133. $key_ids = [];
  134. $uids = [];
  135. foreach ($items as $item) {
  136. $uid = intval($item['member_id']);
  137. $room_key = $item['room_key'];
  138. $key_ids[$room_key] = $uid;
  139. $uids[] = $uid;
  140. }
  141. $uids = array_unique($uids,SORT_NUMERIC);
  142. $members = Model('member')->getMemberList(array('member_id' => array('in',$uids)));
  143. $uid_infos = [];
  144. foreach ($members as $member) {
  145. $uinfo = new member_info($member);
  146. $user = $uinfo->member_id();
  147. $item = ['avatar' => $uinfo->avatar(),'nickname' => $uinfo->nickname(),'userid' => $uinfo->member_id(),'active' => false];
  148. $uid_infos[$user] = $item;
  149. }
  150. $result = [];
  151. foreach ($key_ids as $key => $uid) {
  152. $result[$key] = $uid_infos[$uid];
  153. }
  154. return $result;
  155. }
  156. }