RelevanceRepository.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 SCOPE_TYPE_PRODUCT_LABEL = 'scope_type_product_label';
  42. //价格说明关联分类
  43. const PRICE_RULE_CATEGORY = 'price_rule_category';
  44. //商品参数关联
  45. const PRODUCT_PARAMES_CATE = 'product_params_cate';
  46. //DIY默认模板适用范围 0. 指定店铺、1. 指定商户分类、2. 指定店铺类型、3. 指定商户类别
  47. const MER_DIY_SCOPE = [
  48. 'mer_diy_scope_0',
  49. 'mer_diy_scope_1',
  50. 'mer_diy_scope_2',
  51. 'mer_diy_scope_3',
  52. ];
  53. protected $dao;
  54. /**
  55. * RelevanceRepository constructor.
  56. * @param RelevanceDao $dao
  57. */
  58. public function __construct(RelevanceDao $dao)
  59. {
  60. $this->dao = $dao;
  61. }
  62. /**
  63. * 添加
  64. * @param int $leftId
  65. * @param int $rightId
  66. * @param string $type
  67. * @param $check
  68. * @return bool
  69. * @author Qinii
  70. * @day 10/28/21
  71. */
  72. public function create(int $leftId, int $rightId, string $type, bool $check = false)
  73. {
  74. if ($check && $this->checkHas($leftId, $rightId, $type)) {
  75. return false;
  76. }
  77. $data = [
  78. 'left_id' => $leftId,
  79. 'right_id'=> $rightId,
  80. 'type' => $type,
  81. ];
  82. try{
  83. $this->dao->create($data);
  84. return true;
  85. } catch (\Exception $exception) {
  86. throw new ValidateException('创建失败');
  87. }
  88. }
  89. /**
  90. * 删除
  91. * @param int $leftId
  92. * @param string $type
  93. * @param int $rightId
  94. * @return bool
  95. * @author Qinii
  96. * @day 10/28/21
  97. */
  98. public function destory(int $leftId, int $rightId, string $type)
  99. {
  100. return $this->dao->getSearch([
  101. 'left_id' => $leftId,
  102. 'right_id'=> $rightId,
  103. 'type' => $type,
  104. ])->delete();
  105. }
  106. /**
  107. * 检测是否存在
  108. * @param int $leftId
  109. * @param int $rightId
  110. * @param string $type
  111. * @return int
  112. * @author Qinii
  113. * @day 10/28/21
  114. */
  115. public function checkHas(int $leftId, $rightId, string $type)
  116. {
  117. return $this->dao->getSearch([
  118. 'left_id' => $leftId,
  119. 'right_id'=> $rightId,
  120. 'type' => $type,
  121. ])->count();
  122. }
  123. /**
  124. * 根据左键批量删除
  125. * @param int $leftId
  126. * @param $type
  127. * @return bool
  128. * @author Qinii
  129. * @day 10/28/21
  130. */
  131. public function batchDelete(int $leftId, $type)
  132. {
  133. return $this->dao->getSearch([
  134. 'left_id' => $leftId,
  135. 'type' => $type,
  136. ])->delete();
  137. }
  138. /**
  139. * 关注我的人
  140. * @param int $uid
  141. * @return array
  142. * @author Qinii
  143. * @day 10/28/21
  144. */
  145. public function getUserFans(int $uid, int $page, int $limit)
  146. {
  147. $query = $this->dao->getSearch([
  148. 'right_id' => $uid,
  149. 'type' => self::TYPE_COMMUNITY_FANS,
  150. ])->with([
  151. 'fans' => function($query) {
  152. $query->field('uid,avatar,nickname,count_fans,count_content');
  153. }
  154. ]);
  155. $count = $query->count();
  156. $list = $query->page($page, $limit)->select()->append(['is_start']);
  157. $data = [];
  158. foreach ($list as $item) {
  159. if (!$item['fans']) {
  160. $item->delete();
  161. $count -= 1;
  162. }
  163. $data[] = $item;
  164. }
  165. $list = $data;
  166. return compact('count','list');
  167. }
  168. /**
  169. * 我关注的人
  170. * @param $uid
  171. * @return array
  172. * @author Qinii
  173. * @day 10/28/21
  174. */
  175. public function getUserFocus(int $uid, int $page, int $limit)
  176. {
  177. $query = $this->dao->getSearch([
  178. 'left_id' => $uid,
  179. 'type' => self::TYPE_COMMUNITY_FANS,
  180. ])->with([
  181. 'focus' => function($query) {
  182. $query->field('uid,avatar,nickname,count_fans,count_content');
  183. }
  184. ]);
  185. $count = $query->count();
  186. $list = $query->page($page, $limit)->select()->append(['is_fans']);
  187. $data = [];
  188. foreach ($list as $item) {
  189. if (!$item['focus']) {
  190. $item->delete();
  191. $count -= 1;
  192. }
  193. $data[] = $item;
  194. }
  195. $list = $data;
  196. return compact('count','list');
  197. }
  198. /**
  199. * 我点赞过的文章
  200. * @param int $uid
  201. * @return \think\Collection
  202. * @author Qinii
  203. * @day 10/28/21
  204. */
  205. public function getUserStartCommunity(array $where, int $page, int $limit)
  206. {
  207. $query = $this->dao->joinUser($where)->with([
  208. 'community'=> function($query) use($where){
  209. $query->with([
  210. 'author' => function($query){
  211. $query->field('uid,real_name,status,avatar,nickname,count_start');
  212. },
  213. 'is_start' => function($query) use ($where) {
  214. $query->where('left_id',$where['uid']);
  215. },
  216. 'topic' => function($query) {
  217. $query->where('status', 1)->where('is_del',0);
  218. $query->field('topic_id,topic_name,status,category_id,pic,is_del');
  219. },
  220. 'relevance' => [
  221. 'spu' => function($query) {
  222. $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id');
  223. }
  224. ],
  225. 'isFanss' => function($query) use($where){
  226. $query->where('left_id',$where['uid']);
  227. }]);
  228. },
  229. ]);
  230. $count = $query->count();
  231. $list = $query->page($page, $limit)->select()->each(function ($item){
  232. $item['time'] = date('m月d日', strtotime($item['create_time']));
  233. $item['isFans'] = $item['isFanss'];
  234. return $item;
  235. });
  236. return compact('count','list');
  237. }
  238. /**
  239. * 我点赞过的文章
  240. * @param int $uid
  241. * @return \think\Collection
  242. * @author Qinii
  243. * @day 10/28/21
  244. */
  245. public function getUserStartCommunityByVideos(array $where, int $page, int $limit)
  246. {
  247. $query = $this->dao->joinUser($where)->with([
  248. 'community'=> function($query) {
  249. $query->with(['author'=> function($query) {
  250. $query->field('uid,avatar,nickname');
  251. }]);
  252. },
  253. ]);
  254. $count = $query->count();
  255. $list = $query->page($page, $limit)->select()->each(function ($item){
  256. $item['time'] = date('m月d日', strtotime($item['create_time']));
  257. return $item;
  258. });
  259. return compact('count','list');
  260. }
  261. /**
  262. * 获取指定字段在特定类型中符合条件的记录数量
  263. *
  264. * 此方法通过调用DAO层的搜索功能,统计符合指定字段值和类型条件的记录数。
  265. * 主要用于在数据检索和分析场景中,快速获取特定字段在特定类型中的数据量。
  266. *
  267. * @param string $field 需要查询的字段名
  268. * @param int $value 字段的指定值
  269. * @param string $type 数据的类型
  270. * @return int 符合条件的记录数量
  271. */
  272. public function getFieldCount(string $field, int $value, string $type)
  273. {
  274. // 通过DAO层的搜索方法查询符合条件的记录,并返回其数量
  275. return $this->dao->getSearch([$field => $value, 'type' => $type,])->count();
  276. }
  277. /**
  278. * 批量创建关联数据。
  279. * 该方法用于通过给定的左ID、一系列右ID和类型,创建多个关联数据记录。
  280. * 主要用于在具有多对多关系的数据库表中插入关联数据。
  281. *
  282. * @param int $leftId 左侧实体的ID,用于建立关联。
  283. * @param array $rightId 一个包含多个右侧实体ID的数组,这些ID将与左ID建立关联。
  284. * @param string $type 关联的类型或标签,用于区分不同类型的关联。
  285. */
  286. public function createMany(int $leftId, array $rightId, string $type)
  287. {
  288. // 当右ID不为空时,才进行关联数据的创建
  289. if (!empty($rightId)) {
  290. // 遍历右ID数组,为每个右ID创建关联数据数组
  291. foreach ($rightId as $value) {
  292. $res[] = [
  293. 'left_id' => $leftId,
  294. 'right_id' => $value,
  295. 'type' => $type,
  296. ];
  297. }
  298. // 将所有关联数据一次性插入到数据库中
  299. $this->dao->insertAll($res);
  300. }
  301. }
  302. }