room.model.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/18
  6. * Time: 下午9:18
  7. */
  8. require_once(BASE_ROOT_PATH . '/helper/room/proto_type.php');
  9. class roomModel extends Model
  10. {
  11. public function __construct() {
  12. parent::__construct();
  13. }
  14. ///群聊接口//////////////////////////////////////////////////////////////////////////////////////////////////////////
  15. public function participants($room_id)
  16. {
  17. return $this->table('room_participant')->field('*')->where(['room_id' => $room_id,'state' => 0])->order('invite_time asc')->limit(false)->select();
  18. }
  19. public function getRoomParts($cond,$field = '*',$per_page = false,$order='invite_time asc') {
  20. return $this->table('room_participant')->field($field)->where($cond)->page($per_page)->order($order)->limit($per_page)->select();
  21. }
  22. public function editRoomParts($cond,$updata) {
  23. $ret = $this->table('room_participant')->where($cond)->update($updata);
  24. return $ret;
  25. }
  26. public function countParts($cond) {
  27. $ret = $this->table('room_participant')->where($cond)->count();
  28. return $ret;
  29. }
  30. public function invite($room_id,$user,$inviter) {
  31. $ret = $this->table('room_participant')->insert(['room_id' => $room_id,'member_id' => $user,'inviter' => $inviter,'invite_time' => time(),'join_time' => 0,'jointimes' => 0,'state' => 0],true);
  32. return $ret;
  33. }
  34. public function leave($room_id,$user) {
  35. $ret = $this->table('room_participant')->where(['room_id' => $room_id,'member_id' => $user])->update(['leave_time' => time(),'state' => 1]);
  36. return $ret;
  37. }
  38. public function kickout($room_id,$user) {
  39. $ret = $this->table('room_participant')->where(['room_id' => $room_id,'member_id' => $user])->update(['leave_time' => time(),'state' => 2]);
  40. return $ret;
  41. }
  42. ///群聊接口//////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. public function create_room($type,$name,$creator,$owner,$owner_name,$max_user = 0) {
  44. $params = ['type' => $type,'room_name' => $name,'room_creator' => $creator,'room_owner' => $owner,'owner_name' => $owner_name,'max_user' => $max_user,'add_time' => time()];
  45. return $this->table('room')->insert($params);
  46. }
  47. public function editRoom($cond,$updata)
  48. {
  49. return $this->table('room')->where($cond)->update($updata);
  50. }
  51. public function edit($condition,$data) {
  52. return $this->table('room')->where($condition)->update($data);
  53. }
  54. public function getRoom($roomid) {
  55. return $this->table('room')->field('*')->where(['room_id' => $roomid])->limit(1)->find();
  56. }
  57. public function getRoomByType($room_type,$user) {
  58. $room = $this->table('room')->field('*')->where(['type' => $room_type, 'room_creator' => $user])->limit(1)->find();
  59. return $room;
  60. }
  61. public function getRooms($cond,$field = '*',$order = 'room_id asc',$limit = 0,$page = false) {
  62. return $this->table('room')->field($field)->where($cond)->page($page)->order($order)->limit($limit)->select();
  63. }
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. public function findChatid($left,$right,$lock=false)
  66. {
  67. if($left > $right) {
  68. $tmp = $left;
  69. $left = $right;
  70. $right = $tmp;
  71. }
  72. $item = $this->table('room_chatwo')->field('*')->where(['user_left' => $left,'user_right' => $right])->master($lock)->find();
  73. if(empty($item)) {
  74. return false;
  75. } else {
  76. return intval($item['chat_id']);
  77. }
  78. }
  79. public function addChatwo($left,$right)
  80. {
  81. if($left > $right) {
  82. $tmp = $left;
  83. $left = $right;
  84. $right = $tmp;
  85. }
  86. return $this->table('room_chatwo')->insert(['user_left' => $left,'user_right' => $right,'add_time' => time()]);
  87. }
  88. public function getChatwoRooms($cond,$field = '*') {
  89. return $this->table('room_chatwo')->field($field)->where($cond)->select();
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. public function getRoomMsg($room_id,$msg_type) {
  93. return $this->table('room_msg')->field('*')->where(['room_id' => $room_id, 'type' => $msg_type])->order('add_time desc')->select();
  94. }
  95. public function getRoomsgList($condition, $limit = false, $field = '*', $order = 'msg_id desc',$master = false) {
  96. return $this->table('room_msg')->field($field)->where($condition)->order($order)->limit($limit)->master($master)->select();
  97. }
  98. public function getMssage($cond)
  99. {
  100. return $this->table('room_msg')->field('*')->where($cond)->select();
  101. }
  102. public function getLastRoomMsgs($cond,$order='msg_id desc')
  103. {
  104. $items = $this->table('room_msg')->field("MAX(msg_id) as msg_id,room_id")->where($cond)->group('room_id')->order($order)->select();
  105. if(empty($items)) return [];
  106. $msg_ids = [];
  107. foreach ($items as $item) {
  108. $msg_ids[] = intval($item['msg_id']);
  109. }
  110. return $this->table('room_msg')->field("*")->where(['msg_id' => ['in',$msg_ids]])->limit(count($msg_ids))->limit(false)->select();
  111. }
  112. public function addRoomMsg($datas)
  113. {
  114. return $this->table('room_msg')->insert($datas);
  115. }
  116. public function getRoomMsgsCount($cond){
  117. return $this->table('room_msg')->where($cond)->count();
  118. }
  119. public function getUserRoomMsg($room_id,$userid,$msg_type)
  120. {
  121. return $this->table('room_msg')->field('*')->where(['room_id' => $room_id,'member_id' => $userid,'type' => $msg_type])->order('add_time desc')->select();
  122. }
  123. public function updateMsg($msgid,$data) {
  124. if($msgid <= 0) return false;
  125. return $this->table('room_msg')->where(['msg_id' => $msgid])->update($data);
  126. }
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. public function getRoomExtend($userid,$lock=false)
  129. {
  130. $item = $this->table('room_extend')->field('*')->where(['userid' => $userid])->master($lock)->find();
  131. return $item;
  132. }
  133. public function addExtend($userid,$datas)
  134. {
  135. $datas['userid'] = $userid;
  136. return $this->table('room_extend')->insert($datas);
  137. }
  138. public function editExtend($userid,$datas)
  139. {
  140. return $this->table('room_extend')->where(['userid' => $userid])->update($datas);
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  143. public function findFace($cond) {
  144. return $this->table('room_face')->where($cond)->master(true)->find();
  145. }
  146. public function createFace($lochash,$code)
  147. {
  148. return $this->table('room_face')->insert(['lochash' => $lochash,'check_code' => $code,'add_time' => time()]);
  149. }
  150. public function editFace($cond,$data)
  151. {
  152. return $this->table('room_face')->where($cond)->update($data);
  153. }
  154. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. public function roomSteps($cond,$field="*",$page=20,$order='steps_id desc') {
  156. return $this->table('room_steps')->field($field)->where($cond)->page($page)->order($order)->limit($page)->select();
  157. }
  158. public function findSteps($cond,$field="*"){
  159. return $this->table('room_steps')->field($field)->where($cond)->find();
  160. }
  161. public function addSteps($room,$member,$steps,$datetime){
  162. return $this->table('room_steps')->insert(['room_id' => $room,'member_id' => $member,'steps' => $steps,"date_stamp"=>$datetime]);
  163. }
  164. public function editSteps($cond,$updata){
  165. return $this->table('room_steps')->where($cond)->update($updata);
  166. }
  167. public function totalSteps($room,$member){
  168. return $this->table('room_steps')->where(['room_id' => $room,'member_id' => $member])->sum('steps');
  169. }
  170. public function totalRoomSteps($room){
  171. return $this->table('room_participant')->where(['room_id' => $room])->sum('steps');
  172. }
  173. /////////////////
  174. public function roomCerts($cond,$field='*',$page=20,$order='cert_id desc')
  175. {
  176. return $this->table('room_certs')->field($field)->where($cond)->page($page)->order($order)->limit($page)->select();
  177. }
  178. public function findCert($cond,$field='*')
  179. {
  180. return $this->table('room_certs')->field($field)->where($cond)->find();
  181. }
  182. public function addCert($ctype,$room_id,$short_name,$full_name,$cname,$cmobile,$cmail,$cinfo,$cimage)
  183. {
  184. $insertData = [
  185. "ctype" =>$ctype,
  186. "room_id" => $room_id,
  187. "short_name" => $short_name,
  188. "full_name" => $full_name,
  189. "cname" => $cname,
  190. "cmobile" => $cmobile,
  191. "cmail" => $cmail,
  192. "cinfo" => $cinfo,
  193. "cimage" => $cimage,
  194. "cstatus" => 0,
  195. "add_time" => time()
  196. ];
  197. return $this->table('room_certs')->insert($insertData);
  198. }
  199. public function editCerts($cond,$updata)
  200. {
  201. return $this->table('room_certs')->where($cond)->update($updata);
  202. }
  203. }