Comment.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model;
  10. use think\Db;
  11. /**
  12. * 商品评价模型
  13. * Class Comment
  14. * @package app\common\model
  15. */
  16. class Comment extends BaseModel
  17. {
  18. protected $name = 'comment';
  19. /**
  20. * 所属订单
  21. * @return \think\model\relation\BelongsTo
  22. */
  23. public function orderM()
  24. {
  25. return $this->belongsTo('Order');
  26. }
  27. /**
  28. * 订单商品
  29. * @return \think\model\relation\BelongsTo
  30. */
  31. public function OrderGoods()
  32. {
  33. return $this->belongsTo('OrderGoods');
  34. }
  35. /**
  36. * 关联用户表
  37. * @return \think\model\relation\BelongsTo
  38. */
  39. public function user()
  40. {
  41. return $this->belongsTo('User');
  42. }
  43. /**
  44. * 关联评价图片表
  45. * @return \think\model\relation\HasMany
  46. */
  47. public function image()
  48. {
  49. return $this->hasMany('CommentImage')->order(['id' => 'asc']);
  50. }
  51. /**
  52. * 评价详情
  53. * @param $comment_id
  54. * @return Comment|null
  55. * @throws \think\exception\DbException
  56. */
  57. public static function detail($comment_id)
  58. {
  59. return self::get($comment_id, ['user', 'orderM', 'OrderGoods', 'image.file']);
  60. }
  61. /**
  62. * 更新记录
  63. * @param $data
  64. * @return bool
  65. */
  66. public function edit($data)
  67. {
  68. return $this->transaction(function () use ($data) {
  69. // 删除评价图片
  70. $this->image()->delete();
  71. // 添加评论图片
  72. isset($data['images']) && $this->addCommentImages($data['images']);
  73. // 是否为图片评价
  74. $data['is_picture'] = !$this->image()->select()->isEmpty();
  75. // 更新评论记录
  76. return $this->allowField(true)->save($data);
  77. });
  78. }
  79. /**
  80. * 添加评论图片
  81. * @param $images
  82. * @return int
  83. */
  84. private function addCommentImages($images)
  85. {
  86. $data = array_map(function ($image_id) {
  87. return [
  88. 'image_id' => $image_id,
  89. 'wxapp_id' => self::$wxapp_id
  90. ];
  91. }, $images);
  92. return $this->image()->saveAll($data);
  93. }
  94. /**
  95. * 获取评价列表
  96. * @return \think\Paginator
  97. * @throws \think\exception\DbException
  98. */
  99. public function getList()
  100. {
  101. return $this->with(['user', 'orderM', 'OrderGoods'])
  102. ->where('is_delete', '=', 0)
  103. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  104. ->paginate(15, false, [
  105. 'query' => request()->request()
  106. ]);
  107. }
  108. }