| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020/5/27
- *
- *
- */
- namespace app\common\repositories\user;
- use app\common\dao\user\UserRelationDao as dao;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\store\product\ProductRepository;
- use app\common\repositories\system\merchant\MerchantRepository;
- use think\facade\Db;
- /**
- * Class UserRelationRepository
- * @package app\common\repositories\user
- * @mixin dao
- */
- class UserRelationRepository extends BaseRepository
- {
- protected $dao;
- private $source;
- private $merId;
- /**
- * UserRelationRepository constructor.
- * @param dao $dao
- */
- public function __construct(dao $dao)
- {
- $this->dao = $dao;
- $this->source = request()->header('source');
- $this->merId = request()->header('merId');
- }
- /**
- * @param $params
- * @return bool
- * @author Qinii
- */
- public function fieldExists($params)
- {
- switch ($params['type'])
- {
- case 1: //商品
- return app()->make(ProductRepository::class)->apiExists(0,$params['type_id']);
- break;
- case 10: //商铺
- return app()->make(MerchantRepository::class)->apiGetOne($params['type_id']);
- break;
- case 11: //商铺
- $count=Db::name('business_school')->where(['is_del'=>0,'state' => 1,'id' => $params['type_id']])->count();
- return $count>0?true:false;
- break;
- default:return false;break;
- }
- }
- /**
- * @param array $params
- * @param int $uid
- * @return bool
- * @author Qinii
- */
- public function getUserRelation(array $params,int $uid)
- {
- return ($this->dao->apiFieldExists('type_id',$params['type_id'],$params['type'],$uid)->count()) > 0;
- }
- /**
- * @param array $where
- * @param int $page
- * @param int $limit
- * @return array|bool
- * @author Qinii
- */
- public function search(array $where, int $page,int $limit)
- {
- $where2=['a.type'=>1,'a.uid'=>$where['uid']];
- if($this->source=='yhc' && $this->merId){
- $where2['b.mer_id']=$this->merId;
- }
- $query =Db::name("user_relation")->alias('a')
- ->join(['rrx_store_product'=>'b'],'a.type_id= b.product_id')
- ->field('a.type_id,a.type,b.product_id,b.image,b.store_name,b.price,b.is_show,b.status,b.care_count,b.ot_price')
- ->where($where2)
- ->order("a.create_time",'desc');
- if(isset($where['keyword']) && $where['keyword'] != ''){
- $query = $query->whereLike('b.store_name','%'.$where['keyword'].'%');
- }
- $count = $query->count();
- $ProductRepository=app()->make(ProductRepository::class);
- $list = $query->page($page, $limit)->select()->each(function ($item) use ($ProductRepository) {
- $item['yanglao']=$ProductRepository->yanglaojin($item['product_id']);
- return $item;
- });
- return compact('count', 'list');
- }
- /**
- * @param array $where
- * @param int $page
- * @param int $limit
- * @return array|bool
- * @author Qinii
- */
- public function searchMer(array $where, int $page,int $limit)
- {
- $query =Db::name("user_relation")->alias('a')
- ->join(['rrx_merchant'=>'c'],'a.type_id = c.mer_id')
- ->field('a.type_id,a.type,c.mer_id,c.mer_avatar,c.mer_name,c.product_score,c.service_score,c.postage_score,c.status,c.care_count')
- ->where(['a.type'=>10,'a.uid'=>$where['uid']])
- ->order("a.create_time",'desc');
- if(isset($where['keyword']) && $where['keyword'] != ''){
- $query = $query->whereLike('c.mer_name','%'.$where['keyword'].'%');
- }
- $count = $query->count();
- $list = $query->page($page, $limit)->select();
- return compact('count', 'list');
- }
- /**
- * @param int $uid
- * @param array $data
- * @author Qinii
- */
- public function batchCreate(int $uid,array $data)
- {
- Db::transaction(function ()use ($data,$uid){
- foreach($data['type_id'] as $item){
- $param = ['type' => $data['type'],'type_id' => $item,'uid' => $uid];
- if(!$this->getUserRelation($param,$uid)){
- if($this->fieldExists($param)){
- $this->create($param);
- }
- }
- }
- });
- }
- /**
- * @param array $data
- * @return \app\common\dao\BaseDao|\think\Model
- * @author Qinii
- */
- public function create(array $data)
- {
- if($data['type'] == 10)
- app()->make(MerchantRepository::class)->incCareCount($data['type_id']);
- if($data['type'] == 1)
- app()->make(ProductRepository::class)->incCareCount($data['type_id']);
- return $this->dao->create($data);
- }
- /**
- * @param array $data
- * @author Qinii
- */
- public function destory(array $data)
- {
- if($data['type'] == 10)
- app()->make(MerchantRepository::class)->decCareCount($data['type_id']);
- if($data['type'] == 1)
- app()->make(ProductRepository::class)->decCareCount($data['type_id']);
- $this->dao->destory($data);
- }
- }
|