RelevanceRepository.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\system;
  12. use app\common\dao\system\RelevanceDao;
  13. use app\common\repositories\BaseRepository;
  14. use think\exception\ValidateException;
  15. use think\facade\Db;
  16. /**
  17. * @mixin RelevanceDao
  18. */
  19. class RelevanceRepository extends BaseRepository
  20. {
  21. //文章关联商品
  22. const TYPE_COMMUNITY_PRODUCT = 'community_product';
  23. //社区关注
  24. const TYPE_COMMUNITY_FANS = 'fans';
  25. //社区文章点赞
  26. const TYPE_COMMUNITY_START = 'community_start';
  27. //社区评论点赞
  28. const TYPE_COMMUNITY_REPLY_START = 'community_reply_start';
  29. //商户权限
  30. const TYPE_MERCHANT_AUTH = 'mer_auth';
  31. //指定范围类型
  32. //0全部商品
  33. const TYPE_ALL = 'scope_type';
  34. //指定商品
  35. const SCOPE_TYPE_PRODUCT = 'scope_type_product';
  36. //指定分类
  37. const SCOPE_TYPE_CATEGORY = 'scope_type_category';
  38. //指定商户
  39. const SCOPE_TYPE_STORE = 'scope_type_store';
  40. //价格说明关联分类
  41. const PRICE_RULE_CATEGORY = 'price_rule_category';
  42. //商品参数关联
  43. const PRODUCT_PARAMES_CATE = 'product_params_cate';
  44. protected $dao;
  45. /**
  46. * RelevanceRepository constructor.
  47. * @param RelevanceDao $dao
  48. */
  49. public function __construct(RelevanceDao $dao)
  50. {
  51. $this->dao = $dao;
  52. }
  53. /**
  54. * TODO 添加
  55. * @param int $leftId
  56. * @param int $rightId
  57. * @param string $type
  58. * @param $check
  59. * @return bool
  60. * @author Qinii
  61. * @day 10/28/21
  62. */
  63. public function create(int $leftId, int $rightId, string $type, bool $check = false)
  64. {
  65. if ($check && $this->checkHas($leftId, $rightId, $type)) {
  66. return false;
  67. }
  68. $data = [
  69. 'left_id' => $leftId,
  70. 'right_id'=> $rightId,
  71. 'type' => $type,
  72. ];
  73. try{
  74. $this->dao->create($data);
  75. return true;
  76. } catch (\Exception $exception) {
  77. throw new ValidateException('创建失败');
  78. }
  79. }
  80. /**
  81. * TODO 删除
  82. * @param int $leftId
  83. * @param string $type
  84. * @param int $rightId
  85. * @return bool
  86. * @author Qinii
  87. * @day 10/28/21
  88. */
  89. public function destory(int $leftId, int $rightId, string $type)
  90. {
  91. return $this->dao->getSearch([
  92. 'left_id' => $leftId,
  93. 'right_id'=> $rightId,
  94. 'type' => $type,
  95. ])->delete();
  96. }
  97. /**
  98. * TODO 检测是否存在
  99. * @param int $leftId
  100. * @param int $rightId
  101. * @param string $type
  102. * @return int
  103. * @author Qinii
  104. * @day 10/28/21
  105. */
  106. public function checkHas(int $leftId, int $rightId, string $type)
  107. {
  108. return $this->dao->getSearch([
  109. 'left_id' => $leftId,
  110. 'right_id'=> $rightId,
  111. 'type' => $type,
  112. ])->count();
  113. }
  114. /**
  115. * TODO 根据左键批量删除
  116. * @param int $leftId
  117. * @param $type
  118. * @return bool
  119. * @author Qinii
  120. * @day 10/28/21
  121. */
  122. public function batchDelete(int $leftId, $type)
  123. {
  124. return $this->dao->getSearch([
  125. 'left_id' => $leftId,
  126. 'type' => $type,
  127. ])->delete();
  128. }
  129. /**
  130. * TODO 关注我的人
  131. * @param int $uid
  132. * @return \think\Collection
  133. * @author Qinii
  134. * @day 10/28/21
  135. */
  136. public function getUserFans(int $uid, int $page, int $limit)
  137. {
  138. $query = $this->dao->getSearch([
  139. 'right_id' => $uid,
  140. 'type' => self::TYPE_COMMUNITY_FANS,
  141. ])->with([
  142. 'fans' => function($query) {
  143. $query->field('uid,avatar,nickname,count_fans');
  144. }
  145. ]);
  146. $count = $query->count();
  147. $list = $query->page($page, $limit)->select()->append(['is_start']);
  148. return compact('count','list');
  149. }
  150. /**
  151. * TODO 我关注的人
  152. * @param $uid
  153. * @return \think\Collection
  154. * @author Qinii
  155. * @day 10/28/21
  156. */
  157. public function getUserFocus(int $uid, int $page, int $limit)
  158. {
  159. $query = $this->dao->getSearch([
  160. 'left_id' => $uid,
  161. 'type' => self::TYPE_COMMUNITY_FANS,
  162. ])->with([
  163. 'focus' => function($query) {
  164. $query->field('uid,avatar,nickname,count_fans');
  165. }
  166. ]);
  167. $count = $query->count();
  168. $list = $query->page($page, $limit)->select()->append(['is_fans']);
  169. return compact('count','list');
  170. }
  171. /**
  172. * TODO 我点赞过的文章
  173. * @param int $uid
  174. * @return \think\Collection
  175. * @author Qinii
  176. * @day 10/28/21
  177. */
  178. public function getUserStartCommunity(array $where, int $page, int $limit)
  179. {
  180. $query = $this->dao->joinUser($where)->with([
  181. 'community'=> function($query) use($where){
  182. $query->with([
  183. 'author' => function($query){
  184. $query->field('uid,real_name,status,avatar,nickname,count_start');
  185. },
  186. 'is_start' => function($query) use ($where) {
  187. $query->where('left_id',$where['uid']);
  188. },
  189. 'topic' => function($query) {
  190. $query->where('status', 1)->where('is_del',0);
  191. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  192. },
  193. 'relevance' => [
  194. 'spu' => function($query) {
  195. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  196. }
  197. ],
  198. 'is_fans' => function($query) use($where){
  199. $query->where('left_id',$where['uid']);
  200. }]);
  201. },
  202. ]);
  203. $count = $query->count();
  204. $list = $query->page($page, $limit)->select()->each(function ($item){
  205. $item['time'] = date('m月d日', strtotime($item['create_time']));
  206. return $item;
  207. });
  208. return compact('count','list');
  209. }
  210. /**
  211. * TODO 我点赞过的文章
  212. * @param int $uid
  213. * @return \think\Collection
  214. * @author Qinii
  215. * @day 10/28/21
  216. */
  217. public function getUserStartCommunityByVideos(array $where, int $page, int $limit)
  218. {
  219. $query = $this->dao->joinUser($where)->with([
  220. 'community'=> function($query) {
  221. $query->with(['author'=> function($query) {
  222. $query->field('uid,avatar,nickname');
  223. }]);
  224. },
  225. ]);
  226. $count = $query->count();
  227. $list = $query->page($page, $limit)->select()->each(function ($item){
  228. $item['time'] = date('m月d日', strtotime($item['create_time']));
  229. return $item;
  230. });
  231. return compact('count','list');
  232. }
  233. public function getFieldCount(string $field, int $value, string $type)
  234. {
  235. return $this->dao->getSearch([$field => $value, 'type' => $type,])->count();
  236. }
  237. public function createMany(int $leftId, array $rightId, string $type)
  238. {
  239. if (!empty($rightId)) {
  240. foreach ($rightId as $value) {
  241. $res[] = [
  242. 'left_id' => $leftId,
  243. 'right_id' => $value,
  244. 'type' => $type,
  245. ];
  246. }
  247. $this->dao->insertAll($res);
  248. }
  249. }
  250. }