SearchHistoryService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace addons\TinyShop\services\common;
  3. use Yii;
  4. use common\components\Service;
  5. use common\helpers\EchantsHelper;
  6. use addons\TinyShop\common\models\common\SearchHistory;
  7. /**
  8. * Class SearchHistoryService
  9. * @package addons\TinyShop\services\common
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class SearchHistoryService extends Service
  13. {
  14. /**
  15. * @param $keyword
  16. */
  17. public function create($keyword, $member_id)
  18. {
  19. $model = SearchHistory::find()
  20. ->where([
  21. 'keyword' => $keyword,
  22. 'member_id' => $member_id,
  23. 'search_date' => date('Y-m-d')
  24. ])
  25. ->one();
  26. if (!$model) {
  27. $model = new SearchHistory();
  28. $model = $model->loadDefaultValues();
  29. $model->search_date = date('Y-m-d');
  30. $model->keyword = $keyword;
  31. $model->member_id = $member_id;
  32. $model->ip = Yii::$app->request->userIP;
  33. }
  34. $model->num += 1;
  35. $model->save();
  36. }
  37. /**
  38. * @param $type
  39. * @return array
  40. */
  41. public function getBetweenCountStat($type)
  42. {
  43. // 获取时间和格式化
  44. list($time, $format) = EchantsHelper::getFormatTime($type);
  45. // 获取数据
  46. return EchantsHelper::wordCloud(function ($start_time, $end_time) {
  47. return SearchHistory::find()
  48. ->select([
  49. 'sum(num) as value',
  50. 'keyword as name',
  51. ])
  52. ->andWhere(['between', 'created_at', $start_time, $end_time])
  53. ->groupBy(['keyword'])
  54. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  55. ->asArray()
  56. ->all();
  57. }, $time);
  58. }
  59. }
粤ICP备19079148号