ugc_comment.model.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/30
  6. * Time: 下午3:47
  7. */
  8. defined('InShopNC') or exit('Access Invalid!');
  9. class ugc_commentModel extends Model
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct('ugc_comment');
  14. }
  15. public function top_counts($special_id)
  16. {
  17. return $this->table('ugc_comment')->where("comment_id=top_id and special_id={$special_id}")->count();
  18. }
  19. public function comment_counts($special_id,$comment_id)
  20. {
  21. return $this->table('ugc_comment')->where("top_id={$comment_id} and special_id={$special_id}")->count();
  22. }
  23. public function comments_counts($special_id,$comment_ids)
  24. {
  25. return $this->table('ugc_comment')->field('count(*) - 1 as nc_count,comment_id')->where(array('special_id' => $special_id,'top_id' => ['in',$comment_ids]))->group('top_id')->select();
  26. }
  27. public function getSubCommentList($special_id,$comment_id,$field='*',$page_size = 0,$total_count = 0,$lock = false)
  28. {
  29. return $this->table('ugc_comment')->field($field)->where("top_id={$comment_id} and special_id={$special_id}")->order('top_id desc')->page($page_size, $total_count)->lock($lock)->select();
  30. }
  31. public function getTopCommentList($special_id,$field='*',$page_size = 0,$total_count = 0,$lock = false)
  32. {
  33. return $this->table('ugc_comment')->field($field)->where("comment_id=top_id and special_id={$special_id}")->order('top_id desc')->page($page_size, $total_count)->lock($lock)->select();
  34. }
  35. public function getCommentList($cond,$field='*',$limit=false,$lock = false)
  36. {
  37. return $this->table('ugc_comment')->field($field)->where($cond)->order('top_id desc,comment_id asc')->limit($limit)->lock($lock)->select();
  38. }
  39. public function getCommentByID($comment_id,$lock=false)
  40. {
  41. return $this->table('ugc_comment')->field('*')->lock($lock)->find($comment_id);
  42. }
  43. public function comment($special_id,$user_id,$content)
  44. {
  45. $data['special_id'] = $special_id;
  46. $data['likes'] = 0;
  47. $data['user_id'] = $user_id;
  48. $data['response_id'] = 0;
  49. $data['content'] = $content;
  50. $data['addtime'] = time();
  51. $ret = $this->insert($data);
  52. if($ret != false) {
  53. $this->table('ugc_comment')->where(['comment_id' => $ret])->update(['top_id' => $ret]);
  54. $this->table('mb_special')->where(['special_id' => $special_id])->update(array('comments' => ['exp', 'comments + 1']));
  55. }
  56. return $ret;
  57. }
  58. private function top_id($comment_id)
  59. {
  60. $item = $this->getCommentByID($comment_id);
  61. if(empty($item)) {
  62. $item = $this->getCommentByID($comment_id,true);
  63. }
  64. if(empty($item)) {
  65. return 0;
  66. } else {
  67. return intval($item['top_id']);
  68. }
  69. }
  70. public function respond_comment($special_id,$comment_id,$user_id,$content)
  71. {
  72. $data['special_id'] = $special_id;
  73. $data['likes'] = 0;
  74. $data['user_id'] = $user_id;
  75. $data['response_id'] = $comment_id;
  76. $data['content'] = $content;
  77. $data['addtime'] = time();
  78. $data['top_id'] = $this->top_id($comment_id);
  79. $ret = $this->table('ugc_comment')->insert($data);
  80. if($ret != false) {
  81. $this->table('ugc_comment')->where(['special_id' => $special_id,'comment_id' => $comment_id])->update(['sub_comments' => ['exp', 'sub_comments + 1'] ]);
  82. }
  83. return $ret;
  84. }
  85. }