UserSearchServices.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\services\product\product\StoreProductServices;
  15. use app\dao\user\UserSearchDao;
  16. /**
  17. *
  18. * Class UserLabelServices
  19. * @package app\services\user
  20. * @mixin UserSearchDao
  21. */
  22. class UserSearchServices extends BaseServices
  23. {
  24. /**
  25. * UserSearchServices constructor.
  26. * @param UserSearchDao $dao
  27. */
  28. public function __construct(UserSearchDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 分词搜索得到ids
  34. * @param int $uid
  35. * @param string $keyword
  36. * @param array $where
  37. * @return array
  38. */
  39. public function vicSearch(int $uid, string $keyword, array $where)
  40. {
  41. //分词
  42. try {
  43. $vicWordArr = \crmeb\services\VicWordService::instance()->getWord($keyword);
  44. } catch (\Throwable $e) {
  45. $vicWordArr = [];
  46. }
  47. if ($vicWordArr) {
  48. $vicword = $vicWordArr;
  49. unset($where['store_name']);
  50. $where['keyword'] = $vicWordArr;
  51. }
  52. $result = $this->dao->getKeywordResult(0, $keyword);
  53. $ids = [];
  54. if ($result && isset($result['result']) && $result['result']) {//之前查询结果记录
  55. $ids = $result['result'];
  56. } else {//分词查询
  57. }
  58. //搜索没有记录
  59. if (!$ids && $where) {
  60. //查出所有结果ids存搜索记录表
  61. /** @var StoreProductServices $services */
  62. $services = app()->make(StoreProductServices::class);
  63. $idsArr = $services->getSearchList($where, 0, 0, ['id'], '', []);
  64. if ($idsArr) {
  65. $ids = array_column($idsArr, 'id');
  66. }
  67. }
  68. $vicword = is_string($vicword) ? [$vicword] : $vicword;
  69. $this->saveUserSearch($uid, $keyword, $vicword, $ids);
  70. return $ids;
  71. }
  72. /**
  73. * 获取用户搜索关键词列表
  74. * @param int $uid
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getUserList(int $uid)
  81. {
  82. if (!$uid) {
  83. return [];
  84. }
  85. [$page, $limit] = $this->getPageValue();
  86. return $this->dao->getList(['uid' => $uid, 'is_del' => 0], 'add_time desc,num desc', $page, $limit);
  87. }
  88. /**
  89. * 用户增加搜索记录
  90. * @param int $uid
  91. * @param string $key
  92. * @param array $result
  93. */
  94. public function saveUserSearch(int $uid, string $keyword, array $vicword, array $result)
  95. {
  96. $result = json_encode($result);
  97. $vicword = json_encode($vicword, JSON_UNESCAPED_UNICODE);
  98. $userkeyword = $this->dao->getKeywordResult($uid, $keyword, 0);
  99. $data = [];
  100. $data['result'] = $result;
  101. $data['vicword'] = $vicword;
  102. $data['add_time'] = time();
  103. if ($userkeyword) {
  104. $data['num'] = $userkeyword['num'] + 1;
  105. $this->dao->update(['id' => $userkeyword['id']], $data);
  106. } else {
  107. $data['uid'] = $uid;
  108. $data['keyword'] = $keyword;
  109. $this->dao->save($data);
  110. }
  111. return true;
  112. }
  113. }