截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommunityArticle.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\common\model\community;
  3. use app\common\basics\Models;
  4. use app\common\enum\CommunityArticleEnum;
  5. use app\common\model\goods\Goods;
  6. use app\common\model\shop\Shop;
  7. use app\common\model\user\User;
  8. /**
  9. * 种草社区文章
  10. * Class CommunityCategory
  11. * @package app\common\model\content
  12. */
  13. class CommunityArticle extends Models
  14. {
  15. /**
  16. * @notes 关联作者信息
  17. * @return \think\model\relation\HasOne
  18. * @author 段誉
  19. * @date 2022/4/29 17:59
  20. */
  21. public function user()
  22. {
  23. return $this->hasOne(User::class, 'id', 'user_id');
  24. }
  25. /**
  26. * @notes 关联文章图片
  27. * @return \think\model\relation\HasMany
  28. * @author 段誉
  29. * @date 2022/4/29 11:29
  30. */
  31. public function images()
  32. {
  33. return $this->hasMany(CommunityArticleImage::class, 'article_id', 'id');
  34. }
  35. /**
  36. * @notes 关联话题
  37. * @return \think\model\relation\HasOne
  38. * @author 段誉
  39. * @date 2022/5/6 14:37
  40. */
  41. public function topic()
  42. {
  43. return $this->hasOne(CommunityTopic::class, 'id', 'topic_id');
  44. }
  45. /**
  46. * @notes 关联商品相关信息
  47. * @param $value
  48. * @param $data
  49. * @return array|\think\Collection
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author 段誉
  54. * @date 2022/5/6 10:05
  55. */
  56. public function getGoodsDataAttr($value, $data)
  57. {
  58. if (empty($data['goods'])) {
  59. return [];
  60. }
  61. $result = Goods::field(['id', 'name', 'image'])
  62. ->where(['status' => 1, 'del' => 0])
  63. ->whereIn('id', $data['goods'])
  64. ->select();
  65. return $result;
  66. }
  67. /**
  68. * @notes 关联店铺信息
  69. * @param $value
  70. * @param $data
  71. * @return array|\think\Collection
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @author 段誉
  76. * @date 2022/5/6 17:55
  77. */
  78. public function getShopDataAttr($value, $data)
  79. {
  80. if (empty($data['shop'])) {
  81. return [];
  82. }
  83. $result = Shop::field(['id', 'name', 'logo'])
  84. ->where(['is_freeze' => 0, 'del' => 0, 'is_run' => 1])
  85. ->whereIn('id', $data['shop'])
  86. ->select();
  87. return $result;
  88. }
  89. /**
  90. * @notes 状态属性
  91. * @param $value
  92. * @param $data
  93. * @return string|string[]
  94. * @author 段誉
  95. * @date 2022/5/9 18:13
  96. */
  97. public function getStatusDescAttr($value, $data)
  98. {
  99. return CommunityArticleEnum::getStatusDesc($data['status']);
  100. }
  101. /**
  102. * @notes 文章所选店铺格式化
  103. * @param $value
  104. * @param $data
  105. * @return false|string|string[]
  106. * @author 段誉
  107. * @date 2022/4/29 10:55
  108. */
  109. public function getShopAttr($value, $data)
  110. {
  111. return $this->formattingData($value, 'explode');
  112. }
  113. /**
  114. * @notes 文章所选店铺格式化
  115. * @param $value
  116. * @param $data
  117. * @return false|string|string[]
  118. * @author 段誉
  119. * @date 2022/4/29 10:55
  120. */
  121. public function setShopAttr($value, $data)
  122. {
  123. return $this->formattingData($value, 'implode');
  124. }
  125. /**
  126. * @notes 文章所选商品格式化
  127. * @param $value
  128. * @param $data
  129. * @return false|string|string[]
  130. * @author 段誉
  131. * @date 2022/4/29 10:55
  132. */
  133. public function getGoodsAttr($value, $data)
  134. {
  135. return $this->formattingData($value, 'explode');
  136. }
  137. /**
  138. * @notes 文章所选商品格式化
  139. * @param $value
  140. * @param $data
  141. * @return false|string|string[]
  142. * @author 段誉
  143. * @date 2022/4/29 10:55
  144. */
  145. public function setGoodsAttr($value, $data)
  146. {
  147. return $this->formattingData($value, 'implode');
  148. }
  149. /**
  150. * @notes 格式化数据
  151. * @param $params
  152. * @param $operation
  153. * @return array|string
  154. * @author 段誉
  155. * @date 2022/5/6 9:50
  156. */
  157. protected function formattingData($params, $operation)
  158. {
  159. if (empty($params) || !in_array($operation, ['explode', 'implode'])) {
  160. return $operation == 'explode' ? [] : $params;
  161. }
  162. if ('explode' == $operation) {
  163. return array_map('intval', explode(',', $params));
  164. }
  165. return implode(',', $params);
  166. }
  167. /**
  168. * @notes 增加点赞数量
  169. * @param $id
  170. * @return mixed
  171. * @author 段誉
  172. * @date 2022/5/9 15:34
  173. */
  174. public static function incLike($id)
  175. {
  176. return self::where(['id' => $id])->inc('like')->update();
  177. }
  178. /**
  179. * @notes 减少点赞数量
  180. * @param $id
  181. * @return mixed
  182. * @author 段誉
  183. * @date 2022/5/9 15:37
  184. */
  185. public static function decLike($id)
  186. {
  187. $where = [
  188. ['id', '=', $id],
  189. ['like', '>=', 1]
  190. ];
  191. return self::where($where)->dec('like')->update();
  192. }
  193. }