UserRelationRepository.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\user\UserRelationDao as dao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. use think\facade\Db;
  17. /**
  18. * Class UserRelationRepository
  19. * @package app\common\repositories\user
  20. * @mixin dao
  21. */
  22. class UserRelationRepository extends BaseRepository
  23. {
  24. protected $dao;
  25. /**
  26. * UserRelationRepository constructor.
  27. * @param dao $dao
  28. */
  29. public function __construct(dao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * @param $params
  35. * @return bool
  36. * @author Qinii
  37. */
  38. public function fieldExists($params)
  39. {
  40. switch ($params['type']) {
  41. case 1: //商品
  42. return app()->make(ProductRepository::class)->apiExists(0, $params['type_id']);
  43. break;
  44. case 10: //商铺
  45. return app()->make(MerchantRepository::class)->apiGetOne($params['type_id']);
  46. break;
  47. default:
  48. return false;
  49. break;
  50. }
  51. }
  52. /**
  53. * @param array $params
  54. * @param int $uid
  55. * @return bool
  56. * @author Qinii
  57. */
  58. public function getUserRelation(array $params, int $uid)
  59. {
  60. return ($this->dao->apiFieldExists('type_id', $params['type_id'], $params['type'], $uid)->count()) > 0;
  61. }
  62. /**
  63. * @param array $where
  64. * @param int $page
  65. * @param int $limit
  66. * @return array|bool
  67. * @author Qinii
  68. */
  69. public function search(array $where, int $page, int $limit)
  70. {
  71. $query = $this->dao->search($where);
  72. switch ($where['type']) {
  73. case 1: //商品
  74. $query->with(['product' => function ($query) {
  75. $query->field('product_id,image,store_name,price,is_show,status,care_count');
  76. }]);
  77. break;
  78. case 10: //商铺
  79. $query->with(['merchant' => function ($query) {
  80. $query->field('mer_id,mer_avatar,mer_name,status,care_count,is_trader');
  81. }]);;
  82. break;
  83. default:
  84. break;
  85. }
  86. $count = $query->count();
  87. $list = $query->page($page, $limit)->select();
  88. return compact('count', 'list');
  89. }
  90. /**
  91. * @param int $uid
  92. * @param array $data
  93. * @author Qinii
  94. */
  95. public function batchCreate(int $uid, array $data)
  96. {
  97. Db::transaction(function () use ($data, $uid) {
  98. foreach ($data['type_id'] as $item) {
  99. $param = ['type' => $data['type'], 'type_id' => $item, 'uid' => $uid];
  100. if (!$this->getUserRelation($param, $uid)) {
  101. if ($this->fieldExists($param)) {
  102. $this->create($param);
  103. }
  104. }
  105. }
  106. });
  107. }
  108. /**
  109. * @param array $data
  110. * @return \app\common\dao\BaseDao|\think\Model
  111. * @author Qinii
  112. */
  113. public function create(array $data)
  114. {
  115. if ($data['type'] == 10) {
  116. app()->make(MerchantRepository::class)->incCareCount($data['type_id']);
  117. app()->make(UserMerchantRepository::class)->getInfo($data['uid'], $data['type_id']);
  118. }
  119. if ($data['type'] == 1)
  120. app()->make(ProductRepository::class)->incCareCount($data['type_id']);
  121. return $this->dao->create($data);
  122. }
  123. /**
  124. * @param array $data
  125. * @author Qinii
  126. */
  127. public function destory(array $data)
  128. {
  129. if ($data['type'] == 10)
  130. app()->make(MerchantRepository::class)->decCareCount($data['type_id']);
  131. if ($data['type'] == 1)
  132. app()->make(ProductRepository::class)->decCareCount($data['type_id']);
  133. $this->dao->destory($data);
  134. }
  135. /**
  136. * @param $uid
  137. * @param array $merIds
  138. * @author xaboy
  139. * @day 2020/10/20
  140. */
  141. public function payer($uid, array $merIds)
  142. {
  143. $isset = $this->dao->intersectionPayer($uid, $merIds);
  144. $merIds = array_diff($merIds, $isset);
  145. if (!count($merIds)) return;
  146. $data = [];
  147. foreach ($merIds as $merId) {
  148. $data[] = [
  149. 'type_id' => $merId,
  150. 'type' => 12,
  151. 'uid' => $uid
  152. ];
  153. }
  154. $this->dao->insertAll($data);
  155. }
  156. }