UserRelationRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserRelationDao as dao;
  4. use app\common\repositories\BaseRepository;
  5. use app\common\repositories\store\product\ProductAssistRepository;
  6. use app\common\repositories\store\product\ProductGroupRepository;
  7. use app\common\repositories\store\product\ProductPresellRepository;
  8. use app\common\repositories\store\product\ProductRepository;
  9. use app\common\repositories\store\product\SpuRepository;
  10. use app\common\repositories\system\merchant\MerchantRepository;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. /**
  14. * Class UserRelationRepository
  15. * @package app\common\repositories\user
  16. * @mixin dao
  17. */
  18. class UserRelationRepository extends BaseRepository
  19. {
  20. protected $dao;
  21. /**
  22. * UserRelationRepository constructor.
  23. * @param dao $dao
  24. */
  25. public function __construct(dao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * @param $params
  31. * @return bool
  32. * @author Qinii
  33. */
  34. public function fieldExists($params)
  35. {
  36. switch ($params['type']) {
  37. case 0: //普通商品,
  38. return app()->make(ProductRepository::class)->apiExists(0, $params['type_id']);
  39. break;
  40. case 1: //秒杀商品
  41. return app()->make(ProductRepository::class)->apiExists(0, $params['type_id']);
  42. break;
  43. case 2: //预售商品
  44. return app()->make(ProductPresellRepository::class)->getWhereCount(['product_presell_id' => $params['type_id']]);
  45. break;
  46. case 3: //助力商品
  47. return app()->make(ProductAssistRepository::class)->getWhereCount(['product_assist_id' => $params['type_id']]);
  48. break;
  49. case 4: //拼团商品
  50. return app()->make(ProductGroupRepository::class)->getWhereCount(['product_group_id' => $params['type_id']]);
  51. break;
  52. case 10: //商铺
  53. return app()->make(MerchantRepository::class)->apiGetOne($params['type_id']);
  54. break;
  55. default:
  56. return false;
  57. break;
  58. }
  59. }
  60. /**
  61. * @param array $params
  62. * @param int $uid
  63. * @return bool
  64. * @author Qinii
  65. */
  66. public function getUserRelation(array $params, int $uid)
  67. {
  68. if(in_array($params['type'],[0,1,2,3,4])) {
  69. $spu = $this->getSpu($params);;
  70. $params['type_id'] = $spu['spu_id'];
  71. $params['type'] = 1;
  72. }
  73. return ($this->dao->apiFieldExists('type_id', $params['type_id'], $params['type'], $uid)->count()) > 0;
  74. }
  75. /**
  76. * @param array $where
  77. * @param int $page
  78. * @param int $limit
  79. * @return array|bool
  80. * @author Qinii
  81. */
  82. public function search(array $where, int $page, int $limit)
  83. {
  84. $with = [];
  85. if($where['type'] == 1) $with = ['spu'];
  86. if($where['type'] == 10) $with = ['merchant' => function($query){$query->field('mer_id,type_id,mer_name,mer_avatar,sales,mer_info,care_count');}];
  87. $query = $this->dao->search($where);
  88. $query->with($with)->order('create_time DESC');
  89. $count = $query->count();
  90. $list = $query->page($page, $limit)->select();
  91. if($where['type'] == 1){
  92. $list->each(function ($item){
  93. if(isset($item['spu']['product_type']) && $item['spu']['product_type'] == 1){
  94. $item['spu']['stop_time'] = $item->stop_time;
  95. unset($item['spu']['seckillActive']);
  96. }
  97. if($item['type'] === 1 && $item['spu'] === null){
  98. $item->delete();
  99. }
  100. return $item;
  101. });
  102. }
  103. return compact('count', 'list');
  104. }
  105. /**
  106. * @param int $uid
  107. * @param array $data
  108. * @author Qinii
  109. */
  110. public function batchCreate(int $uid, array $data)
  111. {
  112. Db::transaction(function () use ($data, $uid) {
  113. foreach ($data['type_id'] as $item) {
  114. $param = ['type' => $data['type'], 'type_id' => $item, 'uid' => $uid];
  115. if (!$this->getUserRelation($param, $uid)) {
  116. if ($this->fieldExists($param)) {
  117. $this->create($param);
  118. }
  119. }
  120. }
  121. });
  122. }
  123. /**
  124. * @param array $data
  125. * @return \app\common\dao\BaseDao|\think\Model
  126. * @author Qinii
  127. */
  128. public function create(array $params)
  129. {
  130. if($params['type'] == 10) {
  131. $id = $params['type_id'];
  132. app()->make(UserMerchantRepository::class)->getInfo($params['uid'], $params['type_id']);
  133. $make = app()->make(MerchantRepository::class);
  134. }else{
  135. $spu = $this->getSpu($params);
  136. $params['type_id'] = $spu->spu_id;
  137. $params['type'] = 1;
  138. $make = app()->make(ProductRepository::class);
  139. $id = $spu->product_id;
  140. }
  141. return Db::transaction(function()use($params,$make,$id){
  142. $make->incCareCount($id);
  143. $this->dao->create($params);
  144. });
  145. }
  146. /**
  147. * @param array $data
  148. * @author Qinii
  149. */
  150. public function destory(array $data,$lst = 0)
  151. {
  152. if($lst){
  153. $id = $data['type_id'];
  154. $make = app()->make(ProductRepository::class);
  155. }else{
  156. if(in_array($data['type'],[0,1,2,3,4])) {
  157. $spu = $this->getSpu($data);
  158. $data['type_id'] = $spu->spu_id;
  159. $id = $spu['product_id'];
  160. $data['type'] = 1;
  161. $make = app()->make(ProductRepository::class);
  162. }
  163. if($data['type'] == 10){
  164. $id = $data['type_id'];
  165. $make = app()->make(MerchantRepository::class);
  166. }
  167. }
  168. return Db::transaction(function()use($data,$make,$id){
  169. $make->decCareCount($id);
  170. $this->dao->destory($data);
  171. });
  172. }
  173. /**
  174. * @param $uid
  175. * @param array $merIds
  176. * @author zfy
  177. * @day 2020/10/20
  178. */
  179. public function payer($uid, array $merIds)
  180. {
  181. $isset = $this->dao->intersectionPayer($uid, $merIds);
  182. $merIds = array_diff($merIds, $isset);
  183. if (!count($merIds)) return;
  184. $data = [];
  185. foreach ($merIds as $merId) {
  186. $data[] = [
  187. 'type_id' => $merId,
  188. 'type' => 12,
  189. 'uid' => $uid
  190. ];
  191. }
  192. $this->dao->insertAll($data);
  193. }
  194. public function getSpu(array $data)
  195. {
  196. $make = app()->make(SpuRepository::class);
  197. $where['product_type'] = $data['type'];
  198. switch ($data['type']) {
  199. case 0:
  200. $where['product_id'] = $data['type_id'];
  201. break;
  202. case 1:
  203. $where['product_id'] = $data['type_id'];
  204. break;
  205. default:
  206. $where['activity_id'] = $data['type_id'];
  207. break;
  208. }
  209. $ret = $make->getSearch($where)->find();
  210. if(!$ret) throw new ValidateException('SPU不存在');
  211. return $ret;
  212. }
  213. }