UserRelationRepository.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/5/27
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\user;
  11. use app\common\dao\user\UserRelationDao as dao;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\repositories\store\product\ProductRepository;
  14. use app\common\repositories\system\merchant\MerchantRepository;
  15. use think\facade\Db;
  16. /**
  17. * Class UserRelationRepository
  18. * @package app\common\repositories\user
  19. * @mixin dao
  20. */
  21. class UserRelationRepository extends BaseRepository
  22. {
  23. protected $dao;
  24. private $source;
  25. private $merId;
  26. /**
  27. * UserRelationRepository constructor.
  28. * @param dao $dao
  29. */
  30. public function __construct(dao $dao)
  31. {
  32. $this->dao = $dao;
  33. $this->source = request()->header('source');
  34. $this->merId = request()->header('merId');
  35. }
  36. /**
  37. * @param $params
  38. * @return bool
  39. * @author Qinii
  40. */
  41. public function fieldExists($params)
  42. {
  43. switch ($params['type'])
  44. {
  45. case 1: //商品
  46. return app()->make(ProductRepository::class)->apiExists(0,$params['type_id']);
  47. break;
  48. case 10: //商铺
  49. return app()->make(MerchantRepository::class)->apiGetOne($params['type_id']);
  50. break;
  51. case 11: //商铺
  52. $count=Db::name('business_school')->where(['is_del'=>0,'state' => 1,'id' => $params['type_id']])->count();
  53. return $count>0?true:false;
  54. break;
  55. default:return false;break;
  56. }
  57. }
  58. /**
  59. * @param array $params
  60. * @param int $uid
  61. * @return bool
  62. * @author Qinii
  63. */
  64. public function getUserRelation(array $params,int $uid)
  65. {
  66. return ($this->dao->apiFieldExists('type_id',$params['type_id'],$params['type'],$uid)->count()) > 0;
  67. }
  68. /**
  69. * @param array $where
  70. * @param int $page
  71. * @param int $limit
  72. * @return array|bool
  73. * @author Qinii
  74. */
  75. public function search(array $where, int $page,int $limit)
  76. {
  77. $where2=['a.type'=>1,'a.uid'=>$where['uid']];
  78. if($this->source=='yhc' && $this->merId){
  79. $where2['b.mer_id']=$this->merId;
  80. }
  81. $query =Db::name("user_relation")->alias('a')
  82. ->join(['rrx_store_product'=>'b'],'a.type_id= b.product_id')
  83. ->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')
  84. ->where($where2)
  85. ->order("a.create_time",'desc');
  86. if(isset($where['keyword']) && $where['keyword'] != ''){
  87. $query = $query->whereLike('b.store_name','%'.$where['keyword'].'%');
  88. }
  89. $count = $query->count();
  90. $ProductRepository=app()->make(ProductRepository::class);
  91. $list = $query->page($page, $limit)->select()->each(function ($item) use ($ProductRepository) {
  92. $item['yanglao']=$ProductRepository->yanglaojin($item['product_id']);
  93. return $item;
  94. });
  95. return compact('count', 'list');
  96. }
  97. /**
  98. * @param array $where
  99. * @param int $page
  100. * @param int $limit
  101. * @return array|bool
  102. * @author Qinii
  103. */
  104. public function searchMer(array $where, int $page,int $limit)
  105. {
  106. $query =Db::name("user_relation")->alias('a')
  107. ->join(['rrx_merchant'=>'c'],'a.type_id = c.mer_id')
  108. ->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')
  109. ->where(['a.type'=>10,'a.uid'=>$where['uid']])
  110. ->order("a.create_time",'desc');
  111. if(isset($where['keyword']) && $where['keyword'] != ''){
  112. $query = $query->whereLike('c.mer_name','%'.$where['keyword'].'%');
  113. }
  114. $count = $query->count();
  115. $list = $query->page($page, $limit)->select();
  116. return compact('count', 'list');
  117. }
  118. /**
  119. * @param int $uid
  120. * @param array $data
  121. * @author Qinii
  122. */
  123. public function batchCreate(int $uid,array $data)
  124. {
  125. Db::transaction(function ()use ($data,$uid){
  126. foreach($data['type_id'] as $item){
  127. $param = ['type' => $data['type'],'type_id' => $item,'uid' => $uid];
  128. if(!$this->getUserRelation($param,$uid)){
  129. if($this->fieldExists($param)){
  130. $this->create($param);
  131. }
  132. }
  133. }
  134. });
  135. }
  136. /**
  137. * @param array $data
  138. * @return \app\common\dao\BaseDao|\think\Model
  139. * @author Qinii
  140. */
  141. public function create(array $data)
  142. {
  143. if($data['type'] == 10)
  144. app()->make(MerchantRepository::class)->incCareCount($data['type_id']);
  145. if($data['type'] == 1)
  146. app()->make(ProductRepository::class)->incCareCount($data['type_id']);
  147. return $this->dao->create($data);
  148. }
  149. /**
  150. * @param array $data
  151. * @author Qinii
  152. */
  153. public function destory(array $data)
  154. {
  155. if($data['type'] == 10)
  156. app()->make(MerchantRepository::class)->decCareCount($data['type_id']);
  157. if($data['type'] == 1)
  158. app()->make(ProductRepository::class)->decCareCount($data['type_id']);
  159. $this->dao->destory($data);
  160. }
  161. }