Comment.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\backend\modules\goods\models;
  3. use app\common\models\Order;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * Created by PhpStorm.
  7. * Author: 芸众商城 www.yunzshop.com
  8. * Date: 2017/2/27
  9. * Time: 下午5:10
  10. */
  11. class Comment extends \app\common\models\Comment
  12. {
  13. static protected $needLog = true;
  14. /**
  15. * @param $search
  16. * @param $comment_type
  17. * @return mixed
  18. */
  19. public static function getComments($search,$comment_type='')
  20. {
  21. $commentModdel = self::uniacid();
  22. if ($search['keyword']) {
  23. $commentModdel->whereHas('goods', function ($query) use ($search) {
  24. return $query->searchLike($search['keyword']);
  25. });
  26. }
  27. if ($search['order_sn']){
  28. $commentModdel->whereHas('hasOneOrder', function ($query) use ($search) {
  29. return $query->where('order_sn',$search['order_sn']);
  30. });
  31. }
  32. $commentModdel->with([
  33. 'goods' => function ($query) {
  34. return $query->select(['id', 'title', 'thumb','plugin_id']);
  35. },
  36. 'hasOneOrder' => function ($query) use ($search) {
  37. return $query->select('id','order_sn');
  38. }
  39. ]);
  40. if (!$comment_type) {
  41. $commentModdel->where('comment_id', '0');
  42. }
  43. if ($search['fade'] == 1) {
  44. $commentModdel->where('uid', '>', '0');
  45. } elseif ($search['fade'] == 2) {
  46. $commentModdel->where('uid', '=', '0');
  47. }
  48. if ($search['searchtime']) {
  49. if ($search['starttime'] != '请选择' && $search['endtime'] != '请选择') {
  50. $range = [$search['starttime'], $search['endtime']];
  51. $commentModdel->whereBetween('created_at', $range);
  52. }
  53. }
  54. $commentModdel->orderBy('created_at', 'desc');
  55. return $commentModdel;
  56. }
  57. /**
  58. * @param $id
  59. * @return mixed
  60. */
  61. public static function getComment($id)
  62. {
  63. return self::with(['hasManyReply'=>function ($query) {
  64. return $query->where('type', 2)
  65. ->orderBy('created_at', 'asc');
  66. }])
  67. ->with(['hasManyAppend' => function ($query) {
  68. return $query->where('type', 3)
  69. ->orderBy('created_at', 'asc');
  70. }])
  71. ->where('id', $id);
  72. }
  73. /**
  74. * @param $comment
  75. * @return bool
  76. */
  77. public static function saveComment($comment)
  78. {
  79. return self::insert($comment);
  80. }
  81. /**
  82. * @param $comment_id
  83. * @return mixed
  84. */
  85. public static function getReplysByCommentId($comment_id)
  86. {
  87. return self::where('comment_id', $comment_id)
  88. ->orderBy('created_at', 'asc')
  89. ->get();
  90. }
  91. /**
  92. * @param $id
  93. * @return mixed
  94. */
  95. public static function daletedComment($id)
  96. {
  97. return self::where('id', $id)
  98. ->delete();
  99. }
  100. /**
  101. *
  102. * @return mixed
  103. */
  104. public function goods()
  105. {
  106. return $this->belongsTo(\app\common\models\Goods::class);
  107. }
  108. /**
  109. * 定义字段名
  110. * 可使
  111. * @return array
  112. */
  113. public function atributeNames()
  114. {
  115. return [
  116. 'goods_id' => '评论商品',
  117. 'content' => '评论内容',
  118. ];
  119. }
  120. /**
  121. * 字段规则
  122. * @return array
  123. */
  124. public function rules()
  125. {
  126. return [
  127. 'goods_id' => 'required',
  128. 'content' => 'required'
  129. ];
  130. }
  131. public function hasOneOrder()
  132. {
  133. return $this->hasOne(\app\common\models\Order::class,'id','order_id');
  134. }
  135. }