123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
-
-
- namespace app\common\model\community;
-
-
- use app\common\basics\Models;
- use app\common\enum\CommunityArticleEnum;
- use app\common\model\goods\Goods;
- use app\common\model\shop\Shop;
- use app\common\model\user\User;
-
-
- class CommunityArticle extends Models
- {
-
-
-
-
- public function user()
- {
- return $this->hasOne(User::class, 'id', 'user_id');
- }
-
-
-
-
- public function images()
- {
- return $this->hasMany(CommunityArticleImage::class, 'article_id', 'id');
- }
-
-
-
-
- public function topic()
- {
- return $this->hasOne(CommunityTopic::class, 'id', 'topic_id');
- }
-
-
-
-
- public function getGoodsDataAttr($value, $data)
- {
- if (empty($data['goods'])) {
- return [];
- }
-
- $result = Goods::field(['id', 'name', 'image'])
- ->where(['status' => 1, 'del' => 0])
- ->whereIn('id', $data['goods'])
- ->select();
-
- return $result;
- }
-
-
-
-
- public function getShopDataAttr($value, $data)
- {
- if (empty($data['shop'])) {
- return [];
- }
-
- $result = Shop::field(['id', 'name', 'logo'])
- ->where(['is_freeze' => 0, 'del' => 0, 'is_run' => 1])
- ->whereIn('id', $data['shop'])
- ->select();
-
- return $result;
- }
-
-
-
-
-
- public function getStatusDescAttr($value, $data)
- {
- return CommunityArticleEnum::getStatusDesc($data['status']);
- }
-
-
-
-
- public function getShopAttr($value, $data)
- {
- return $this->formattingData($value, 'explode');
- }
-
-
-
- public function setShopAttr($value, $data)
- {
- return $this->formattingData($value, 'implode');
- }
-
-
-
-
- public function getGoodsAttr($value, $data)
- {
- return $this->formattingData($value, 'explode');
- }
-
-
-
-
- public function setGoodsAttr($value, $data)
- {
- return $this->formattingData($value, 'implode');
- }
-
-
-
-
- protected function formattingData($params, $operation)
- {
- if (empty($params) || !in_array($operation, ['explode', 'implode'])) {
- return $operation == 'explode' ? [] : $params;
- }
-
- if ('explode' == $operation) {
- return array_map('intval', explode(',', $params));
- }
- return implode(',', $params);
- }
-
-
-
-
- public static function incLike($id)
- {
- return self::where(['id' => $id])->inc('like')->update();
- }
-
-
-
-
- public static function decLike($id)
- {
- $where = [
- ['id', '=', $id],
- ['like', '>=', 1]
- ];
- return self::where($where)->dec('like')->update();
- }
-
- }
|