2023-03-14 19:48:08 +08:00

394 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @date 2023年03月9日
* @author刘孝全
* @emailq8197264@126.com
*
* @ 商品参数关联表
*/
namespace app\common\model\merchant\system;
use app\common\model\merchant\store\product\Spu;
use app\common\model\merchant\community\Community;
use app\common\model\merchant\store\StoreCategory;
use app\common\model\merchant\system\auth\Menu;
use app\common\model\merchant\system\merchant\Merchant;
use app\common\model\merchant\user\User;
use think\Model;
use think\exception\ValidateException;
class Relevance extends Model
{
protected $connection = 'shop';
protected $table = 'eb_relevance';
protected $pk = 'relevance_id';
//文章关联商品
const TYPE_COMMUNITY_PRODUCT = 'community_product';
//社区关注
const TYPE_COMMUNITY_FANS = 'fans';
//社区文章点赞
const TYPE_COMMUNITY_START = 'community_start';
//社区评论点赞
const TYPE_COMMUNITY_REPLY_START = 'community_reply_start';
//商户权限
const TYPE_MERCHANT_AUTH = 'mer_auth';
//指定范围类型
//0全部商品
const TYPE_ALL = 'scope_type';
//指定商品
const SCOPE_TYPE_PRODUCT = 'scope_type_product';
//指定分类
const SCOPE_TYPE_CATEGORY = 'scope_type_category';
//指定商户
const SCOPE_TYPE_STORE = 'scope_type_store';
//价格说明关联分类
const PRICE_RULE_CATEGORY = 'price_rule_category';
//商品参数关联
const PRODUCT_PARAMES_CATE = 'product_params_cate';
public function fans()
{
return $this->hasOne(User::class,'uid','left_id');
}
public function focus()
{
return $this->hasOne(User::class,'uid','right_id');
}
public function community()
{
return $this->hasOne(Community::class,'community_id','right_id')
->bind(['community_id','title','image','start','uid','create_time','count_start','author','is_type']);
}
public function getIsStartAttr()
{
return self::where('left_id', $this->right_id)
->where('right_id',$this->left_id)
->where('type', 'fans')
->count() > 0;
}
public function spu()
{
return $this->hasOne(Spu::class, 'spu_id','right_id');
}
public function merchant()
{
return $this->hasOne(Merchant::class, 'mer_id','right_id');
}
public function category()
{
return $this->hasOne(StoreCategory::class, 'store_category_id','right_id');
}
public function auth()
{
return $this->hasOne(Menu::class, 'menu_id','right_id');
}
public function searchLeftIdAttr($query, $value)
{
$query->where('left_id', $value);
}
public function searchRightIdAttr($query, $value)
{
$query->where('right_id', $value);
}
public function searchTypeAttr($query, $value)
{
$query->where('type', $value);
}
/**
* @return static
*/
public static function getInstance(): self
{
return new static();
}
/**
* TODO 搜索
* @param $where
* @return BaseModel
* @author Qinii
* @day 2020-10-16
*/
protected function getSearch(array $where)
{
foreach ($where as $key => $item) {
if ($item !== '') {
$keyArray[] = $key;
$whereArr[$key] = $item;
}
}
if(empty($keyArray)){
return self::getInstance()->db();
}else{
return self::withSearch($keyArray, $whereArr);
}
}
/**
* TODO 添加
* @param int $leftId
* @param int $rightId
* @param string $type
* @param $check
* @return bool
* @author Qinii
* @day 10/28/21
*/
public function insert(int $leftId, int $rightId, string $type, bool $check = false)
{
if ($check && $this->checkHas($leftId, $rightId, $type)) {
return false;
}
$data = [
'left_id' => $leftId,
'right_id'=> $rightId,
'type' => $type,
];
try{
Relevance::create($data);
return true;
} catch (\Exception $exception) {
throw new ValidateException('创建失败');
}
}
/**
* TODO 删除
* @param int $leftId
* @param string $type
* @param int $rightId
* @return bool
* @author Qinii
* @day 10/28/21
*/
public function destory(int $leftId, int $rightId, string $type)
{
return $this->getSearch([
'left_id' => $leftId,
'right_id'=> $rightId,
'type' => $type,
])->delete();
}
/**
* TODO 检测是否存在
* @param int $leftId
* @param int $rightId
* @param string $type
* @return int
* @author Qinii
* @day 10/28/21
*/
public function checkHas(int $leftId, int $rightId, string $type)
{
return $this->getSearch([
'left_id' => $leftId,
'right_id'=> $rightId,
'type' => $type,
])->count();
}
/**
* TODO 根据左键批量删除
* @param int $leftId
* @param $type
* @return bool
* @author Qinii
* @day 10/28/21
*/
public function batchDelete(int $leftId, $type)
{
return $this->getSearch([
'left_id' => $leftId,
'type' => $type,
])->delete();
}
/**
* TODO 关注我的人
* @param int $uid
* @return \think\Collection
* @author Qinii
* @day 10/28/21
*/
public function getUserFans(int $uid, int $page, int $limit)
{
$query = $this->getSearch([
'right_id' => $uid,
'type' => self::TYPE_COMMUNITY_FANS,
])->with([
'fans' => function($query) {
$query->field('uid,avatar,nickname,count_fans');
}
]);
$count = $query->count();
$list = $query->page($page, $limit)->select()->append(['is_start']);
return compact('count','list');
}
/**
* TODO 我关注的人
* @param $uid
* @return \think\Collection
* @author Qinii
* @day 10/28/21
*/
public function getUserFocus(int $uid, int $page, int $limit)
{
$query = $this->getSearch([
'left_id' => $uid,
'type' => self::TYPE_COMMUNITY_FANS,
])->with([
'focus' => function($query) {
$query->field('uid,avatar,nickname,count_fans');
}
]);
$count = $query->count();
$list = $query->page($page, $limit)->select()->append(['is_fans']);
return compact('count','list');
}
/**
* TODO 我点赞过的文章
* @param int $uid
* @return \think\Collection
* @author Qinii
* @day 10/28/21
*/
public function getUserStartCommunity(array $where, int $page, int $limit)
{
$query = $this->joinUser($where)->with([
'community'=> function($query) use($where){
$query->with([
'author' => function($query){
$query->field('uid,real_name,status,avatar,nickname,count_start');
},
'is_start' => function($query) use ($where) {
$query->where('left_id',$where['uid']);
},
'topic' => function($query) {
$query->where('status', 1)->where('is_del',0);
$query->field('topic_id,topic_name,status,category_id,pic,is_del');
},
'relevance' => [
'spu' => function($query) {
$query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
}
],
'is_fans' => function($query) use($where){
$query->where('left_id',$where['uid']);
}]);
},
]);
$count = $query->count();
$list = $query->page($page, $limit)->select()->each(function ($item){
$item['time'] = date('m月d日', strtotime($item['create_time']));
return $item;
});
return compact('count','list');
}
/**
* TODO 我点赞过的文章
* @param int $uid
* @return \think\Collection
* @author Qinii
* @day 10/28/21
*/
public function getUserStartCommunityByVideos(array $where, int $page, int $limit)
{
$query = $this->joinUser($where)->with([
'community'=> function($query) {
$query->with(['author'=> function($query) {
$query->field('uid,avatar,nickname');
}]);
},
]);
$count = $query->count();
$list = $query->page($page, $limit)->select()->each(function ($item){
$item['time'] = date('m月d日', strtotime($item['create_time']));
return $item;
});
return compact('count','list');
}
public function getFieldCount(string $field, int $value, string $type)
{
return $this->getSearch([$field => $value, 'type' => $type,])->count();
}
/**
*
*/
public function createMany(int $leftId, array $rightId, string $type)
{
if (!empty($rightId)) {
foreach ($rightId as $value) {
$res[] = [
'left_id' => $leftId,
'right_id' => $value,
'type' => $type,
];
}
self::batchInsert($res);
}
}
/**
* 批量插入
*
* @param array $data
* @return int
* @author xaboy
* @day 2020/6/8
*/
public function batchInsert(array $data)
{
return Relevance::insertAll($data);
}
// ------- 2 ---------------
public function clear(int $id, $type, string $field)
{
if (is_string($type)) $type = [$type];
return Relevance::where($field, $id)->whereIn('type', $type)->delete();
}
public function joinUser($where)
{
$query = Relevance::hasWhere('community',function($query) use($where){
$query->where('status',1)->where('is_show',1)->where('is_del',0);
$query->when(isset($where['is_type']) && $where['is_type'] !== '',function($query) use($where){
$query->where('is_type',$where['is_type']);
});
});
$query->where('left_id',$where['uid'])->where('type',self::TYPE_COMMUNITY_START);
return $query;
}
//--------------------------
}