QuanModel.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 圈子模型
  6. * @author fish
  7. *
  8. */
  9. class QuanModel {
  10. public $table = 'pin';
  11. public function getSiteUrl()
  12. {
  13. $config_info = M('config')->where( array('name' => 'SITE_URL') )->find();
  14. $url = $config_info['value'];
  15. return $url;
  16. }
  17. /**
  18. 检测圈子是否存在
  19. **/
  20. public function check_quan($seller_id)
  21. {
  22. $group_info = M('group')->where( array('seller_id' => $seller_id) )->find();
  23. if(empty($group_info))
  24. {
  25. $seller_info = M('seller')->field("s_true_name")->where( array('s_id' => $seller_id) )->find();
  26. //s_true_name
  27. $ins_data = array();
  28. $ins_data['seller_id'] = $seller_id;
  29. $ins_data['title'] = $seller_info['s_true_name'];
  30. $ins_data['post_count'] = 0;
  31. $ins_data['status'] = 1;
  32. $ins_data['member_count'] = 0;
  33. $ins_data['create_time'] = time();
  34. $group_id = M('group')->add($ins_data);
  35. }else {
  36. $group_id = $group_info['id'];
  37. }
  38. return $group_id;
  39. }
  40. /**
  41. 用户加入圈子
  42. **/
  43. public function member_add_group($member_id, $group_id)
  44. {
  45. $group_member_data = array();
  46. $group_member_data['group_id'] = $group_id;
  47. $group_member_data['member_id'] = $member_id;
  48. $group_member_data['status'] = 1;
  49. $group_member_data['last_view'] = time();
  50. $group_member_data['position'] = 1;
  51. $group_member_data['create_time'] = time();
  52. M('group_member')->add($group_member_data);
  53. }
  54. /**
  55. 点赞/取消赞
  56. **/
  57. public function member_fav_post($member_id,$post_id)
  58. {
  59. $post_fav = M('group_post_fav')->where( array('member_id' => $member_id,'post_id' => $post_id) )->find();
  60. if(empty($post_fav))
  61. {
  62. $fav_data = array();
  63. $fav_data['member_id'] = $member_id;
  64. $fav_data['post_id'] = $post_id;
  65. $fav_data['fav_time'] = time();
  66. $rs = M('group_post_fav')->add($fav_data);
  67. if($rs)
  68. {
  69. M('group_post')->where( array('id' => $post_id) )->setInc('fav_count', 1);
  70. }
  71. return array('code' => 1,'fav_id' => $rs);
  72. } else{
  73. $fav_info = M('group_post_fav')->where( array('member_id' => $member_id,'post_id' => $post_id) )->find();
  74. $fav_id = $fav_info['id'];
  75. M('group_post_fav')->where( array('member_id' => $member_id,'post_id' => $post_id) )->delete();
  76. M('group_post')->where( array('id' => $post_id) )->setDec('fav_count', 1);
  77. return array('code' => 2, 'fav_id' => $fav_id);
  78. }
  79. }
  80. /**
  81. 评论
  82. **/
  83. public function comment_group_post($post_id,$content,$member_id,$to_member_id)
  84. {
  85. //group_lzl_reply
  86. $reply_data = array();
  87. $reply_data['post_id'] = $post_id;
  88. $reply_data['content'] = htmlspecialchars($content);
  89. $reply_data['member_id'] = $member_id;
  90. $reply_data['to_member_id'] = $to_member_id;
  91. $reply_data['status'] = 1;
  92. $reply_data['create_time'] = time();
  93. $rs = M('group_lzl_reply')->add($reply_data);
  94. if($rs)
  95. {
  96. M('group_post')->where( array('id' => $post_id) )->setInc('reply_count', 1);
  97. M('group_post')->where( array('id' => $post_id) )->save( array('last_reply_time' => time()) );
  98. }
  99. return $rs;
  100. }
  101. /**
  102. 发布
  103. **/
  104. public function send_group_post($data)
  105. {
  106. //group_post
  107. $ins_data = array();
  108. $ins_data['member_id'] = $data['member_id'];
  109. $ins_data['group_id'] = $data['group_id'];
  110. $ins_data['goods_id'] = $data['goods_id'];
  111. $ins_data['title'] = $data['title'];
  112. $ins_data['content'] = $data['content'];
  113. $ins_data['link'] = isset($data['link']) ? $data['link'] : '' ;
  114. $ins_data['is_share'] = isset($data['is_share']) ? $data['is_share'] : 0;
  115. $ins_data['status'] = 1;
  116. $ins_data['is_vir'] = isset($data['is_vir']) ? $data['is_vir'] : 0;
  117. $ins_data['avatar'] = isset($data['avatar']) ? $data['avatar'] : '';
  118. $ins_data['user_name'] = isset($data['user_name']) ? $data['user_name'] : '';
  119. $ins_data['last_reply_time'] = time();
  120. $ins_data['fav_count'] = 0;
  121. $ins_data['reply_count'] = 0;
  122. $ins_data['create_time'] = time();
  123. $res = M('group_post')->add($ins_data);
  124. return $res;
  125. }
  126. /**
  127. 加载圈子内容
  128. **/
  129. public function load_group_post($group_id,$post_id,$up_down,$limit=10)
  130. {
  131. //up_down 1 底部加载, 2顶部加载。
  132. $where = " and p.group_id = {$group_id} ";
  133. $order_by = "";
  134. if($up_down == 1)
  135. {
  136. if($post_id>0)
  137. {
  138. $where .= " and p.id <{$post_id} ";
  139. }
  140. $order_by = " p.id desc ";
  141. }else {
  142. $where .= " and p.id >{$post_id} ";
  143. $order_by = " p.id desc ";
  144. }
  145. $sql = "select p.*,m.uname,m.avatar as avatar2 from ".C('DB_PREFIX')."group_post p,".C('DB_PREFIX')."member as m
  146. where p.member_id = m.member_id {$where} order by {$order_by} limit {$limit} ";
  147. $list = M()->query($sql);
  148. //htmlspecialchars_decode
  149. foreach($list as $key => $val)
  150. {
  151. $has_fav = M('group_post_fav')->where( array('post_id' => $val['id'],'member_id' => is_login()) )->find(); if( $val['is_vir'] == 1) { //,m.uname,m.avatar $val['uname'] = $val['user_name']; }else{ $val['avatar'] = $val['avatar2']; }
  152. $val['has_fav'] = empty($has_fav) ? false : true;
  153. $val['title'] = htmlspecialchars_decode($val['title']); $val['content'] = unserialize($val['content']); $image_list = $val['content']; foreach($image_list as $kkk => $vvv) { $vvv = C('SITE_URL').'Uploads/image/'.$vvv; $image_list[$kkk] = $vvv; } $val['content'] = $image_list;
  154. $val['fav_list'] = $this->get_post_fav_info($val['id']);
  155. //获取评论信息
  156. $val['comment_list'] = $this->get_post_comment_info($val['id']);
  157. if($val['goods_id'] != 0){
  158. $val['goods_info'] = $this->get_goods_info(array('goods_id'=>$val['goods_id']));
  159. $val['goods_info']['buy_user'] = $this->get_goods_pin_avatar($val['goods_id'],5);
  160. }
  161. $val['last_reply_date'] = date('Y-m-d H:i:s', $val['last_reply_time']);
  162. $list[$key] = $val;
  163. }
  164. return $list;
  165. }
  166. public function get_goods_pin_avatar($goods_id,$limit =5)
  167. {
  168. $sql = "select distinct(m.member_id), m.avatar from ".C('DB_PREFIX')."order_goods as og ,".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."member as m
  169. where og.order_id=o.order_id and o.member_id=m.member_id and og.goods_id={$goods_id} order by o.order_id desc limit {$limit}";
  170. $avatar_list = M()->query($sql);
  171. if( empty($avatar_list) || count($avatar_list) < $limit )
  172. {
  173. $del = $limit - count($avatar_list);
  174. //look for jiaorder
  175. $list = M('jiauser')->order(' rand() ')->limit($del)->select();
  176. foreach($list as $val)
  177. {
  178. $tmp = array();
  179. $tmp['avatar'] = $val['avatar'];
  180. $avatar_list[] = $tmp;
  181. }
  182. }
  183. return $avatar_list;
  184. }
  185. /**
  186. 获取评论信息
  187. **/
  188. public function get_post_comment_info($post_id)
  189. {
  190. //group_lzl_reply
  191. $sql = "select p.*,m.uname,m.avatar from ".C('DB_PREFIX')."group_lzl_reply p,".C('DB_PREFIX')."member as m
  192. where p.member_id = m.member_id and p.post_id = {$post_id} order by id asc ";
  193. $list = M()->query($sql);
  194. foreach($list as $key => $val)
  195. {
  196. $val['content'] = htmlspecialchars_decode($val['content']);
  197. //to_member_id
  198. if($val['to_member_id'] >0)
  199. {
  200. $to_member_info = M('member')->field('uname')->where( array('member_id' => $val['to_member_id']) )->find();
  201. $val['to_member_name'] = $to_member_info['uname'];
  202. }
  203. $list[$key] = $val;
  204. }
  205. return $list;
  206. }
  207. /**
  208. 获取点赞信息
  209. **/
  210. public function get_post_fav_info($post_id)
  211. {
  212. $sql = "select p.*,m.uname,m.avatar from ".C('DB_PREFIX')."group_post_fav p,".C('DB_PREFIX')."member as m
  213. where p.member_id = m.member_id and p.post_id = {$post_id} order by id asc ";
  214. $list = M()->query($sql);
  215. return $list;
  216. }
  217. /**
  218. 获取圈子信息
  219. **/
  220. public function get_quan_info($where = array())
  221. {
  222. $group_info = M('group')->where( $where )->find();
  223. return $group_info;
  224. }
  225. /**
  226. 获取圈子信息
  227. **/
  228. public function get_goods_info($where = array())
  229. {
  230. $goods_info = M('goods')->field('seller_count,virtual_count,goods_id,name,image,fan_image')->where( $where )->find();
  231. $goods_model = D('Home/goods');
  232. $price_arr = $goods_model->get_goods_price($goods_info['goods_id']);
  233. $goods_info['pin_price'] = $price_arr['pin_price'];
  234. $goods_info['danprice'] = $price_arr['danprice'];
  235. $goods_info['price'] = $price_arr['price'];
  236. $goods_info['seller_count'] += $goods_info['virtual_count'];
  237. unset($goods_info['virtual_count']);
  238. //pinprice
  239. $goods_info['image']= C('SITE_URL'). resize($goods_info['image'], 80, 80);
  240. if(!empty($goods_info['fan_image'])){
  241. $goods_info['image']= C('SITE_URL'). resize($goods_info['fan_image'], 80, 80);
  242. }
  243. return $goods_info;
  244. }
  245. /**
  246. 加载圈子内容
  247. **/
  248. public function load_dynamic_post($goods_id,$post_id,$up_down,$limit=10,$is_goods_info=1)
  249. {
  250. //up_down 1 底部加载, 2顶部加载。
  251. $where = " and p.goods_id = {$goods_id} ";
  252. $order_by = "";
  253. if($up_down == 1)
  254. {
  255. if($post_id>0)
  256. {
  257. $where .= " and p.id <{$post_id} ";
  258. }
  259. $order_by = " p.id desc ";
  260. }else {
  261. $where .= " and p.id >{$post_id} ";
  262. $order_by = " p.id desc ";
  263. }
  264. $sql = "select p.*,m.uname,m.avatar from ".C('DB_PREFIX')."group_post p,".C('DB_PREFIX')."member as m
  265. where p.member_id = m.member_id {$where} order by {$order_by} limit {$limit} ";
  266. $list = M()->query($sql);
  267. //htmlspecialchars_decode
  268. foreach($list as $key => $val)
  269. {
  270. $has_fav = M('group_post_fav')->where( array('post_id' => $val['id'],'member_id' => is_login()) )->find();
  271. $val['has_fav'] = empty($has_fav) ? false : true;
  272. $val['title'] = htmlspecialchars_decode($val['title']);
  273. $val['fav_list'] = $this->get_post_fav_info($val['id']);
  274. if($is_goods_info==1){
  275. //获取评论信息
  276. $val['comment_list'] = $this->get_post_comment_info($val['id']);
  277. if($val['goods_id'] != 0){
  278. $val['goods_info'] = $this->get_goods_info(array('goods_id'=>$val['goods_id']));
  279. }
  280. }
  281. $list[$key] = $val;
  282. }
  283. return $list;
  284. }
  285. /**
  286. 加载圈子内容
  287. **/
  288. public function load_dynamic_view($id,$post_id)
  289. {
  290. $where = " and p.id = {$id} ";
  291. $order_by = "";
  292. $sql = "select p.*,m.uname,m.avatar from ".C('DB_PREFIX')."group_post p,".C('DB_PREFIX')."member as m
  293. where p.member_id = m.member_id {$where} limit 1";
  294. $list = M()->query($sql);
  295. //htmlspecialchars_decode
  296. foreach($list as $key => $val)
  297. {
  298. $has_fav = M('group_post_fav')->where( array('post_id' => $val['id'],'member_id' => is_login()) )->find();
  299. $val['has_fav'] = empty($has_fav) ? false : true;
  300. $val['title'] = htmlspecialchars_decode($val['title']);
  301. $val['fav_list'] = $this->get_post_fav_info($val['id']);
  302. //获取评论信息
  303. $val['comment_list'] = $this->get_post_comment_info($val['id']);
  304. if($val['goods_id'] != 0){
  305. $val['goods_info'] = $this->get_goods_info(array('goods_id'=>$val['goods_id']));
  306. }
  307. $list[$key] = $val;
  308. }
  309. return $list;
  310. }
  311. }