Comment.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model;
  13. /**
  14. * 商品评价模型
  15. * Class Comment
  16. * @package app\common\model
  17. */
  18. class Comment extends BaseModel
  19. {
  20. // 定义表名
  21. protected $name = 'comment';
  22. // 定义主键
  23. protected $pk = 'comment_id';
  24. /**
  25. * 所属订单
  26. * @return \think\model\relation\BelongsTo
  27. */
  28. public function orderData()
  29. {
  30. return $this->belongsTo('Order');
  31. }
  32. /**
  33. * 订单商品
  34. * @return \think\model\relation\BelongsTo
  35. */
  36. public function orderGoods()
  37. {
  38. return $this->belongsTo('OrderGoods')
  39. ->field(['order_goods_id', 'goods_id', 'goods_name', 'image_id', 'goods_props', 'order_id']);
  40. }
  41. /**
  42. * 关联用户表
  43. * @return \think\model\relation\BelongsTo
  44. */
  45. public function user()
  46. {
  47. return $this->belongsTo('User')->field(['user_id', 'nick_name', 'avatar_id']);
  48. }
  49. /**
  50. * 关联评价图片表
  51. * @return \think\model\relation\HasMany
  52. */
  53. public function images()
  54. {
  55. return $this->hasMany('CommentImage')->order(['id']);
  56. }
  57. public function getStatusTextAttr($value, $data) {
  58. $text = str_replace([0,1,2], ['待审核', '已通过', '不通过'], $data['status']);
  59. return $text;
  60. }
  61. /**
  62. * 详情记录
  63. * @param int $commentId
  64. * @param array $with
  65. * @return static|array|null
  66. */
  67. public static function detail(int $commentId, array $with = [])
  68. {
  69. return static::get($commentId, $with);
  70. }
  71. /**
  72. * 添加评论图片
  73. * @param array $images
  74. * @return array|false
  75. */
  76. protected function addCommentImages(array $images)
  77. {
  78. $data = array_map(function ($imageId) {
  79. return [
  80. 'image_id' => $imageId,
  81. 'store_id' => self::$storeId
  82. ];
  83. }, $images);
  84. return $this->image()->saveAll($data) !== false;
  85. }
  86. /**
  87. * 累积点赞数
  88. * @param int $commentId ID
  89. * @param int $num 递增的数量
  90. * @return mixed
  91. */
  92. public static function setIncByField(int $commentId, $field = '', int $num = 1)
  93. {
  94. return (new static)->setInc([['comment_id', '=', $commentId]], $field, $num);
  95. }
  96. /**
  97. * 累积收藏数
  98. * @param int $commentId ID
  99. * @param int $num 递减的数量
  100. * @return mixed
  101. */
  102. public static function setDecByField(int $commentId, $field = '', int $num = 1)
  103. {
  104. return (new static)->setDec([['comment_id', '=', $commentId]], $field, $num);
  105. }
  106. }