StoreVisit.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\store;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use app\admin\model\user\User;
  10. /**
  11. * TODO 产品浏览记录
  12. * Class StoreVisit
  13. * @package app\admin\model\store
  14. */
  15. class StoreVisit extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'store_visit';
  27. use ModelTrait;
  28. /**
  29. * @param $date
  30. * @param array $class
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. */
  36. public static function getVisit($date,$class=[]){
  37. $model=new self();
  38. switch ($date){
  39. case null:case 'today':case 'week':case 'year':
  40. if($date==null) $date='month';
  41. $model=$model->whereTime('add_time',$date);
  42. break;
  43. case 'quarter':
  44. list($startTime,$endTime)=User::getMonth('n');
  45. $model = $model->where('add_time','>',$startTime);
  46. $model = $model->where('add_time','<',$endTime);
  47. break;
  48. default:
  49. list($startTime,$endTime)=explode('-',$date);
  50. $model = $model->where('add_time','>',strtotime($startTime));
  51. $model = $model->where('add_time','<',strtotime($endTime));
  52. break;
  53. }
  54. $list=$model->group('type')->field('sum(count) as sum,product_id,cate_id,type,content')->order('sum desc')->limit(0,10)->select()->toArray();
  55. $view=[];
  56. foreach ($list as $key=>$val){
  57. $now_list['name']=$val['type']=='viwe'?'浏览量':'搜索';
  58. $now_list['value']=$val['sum'];
  59. $now_list['class']=isset($class[$key])?$class[$key]:'';
  60. $view[]=$now_list;
  61. }
  62. if(empty($list)){
  63. $view=[['name'=>'暂无数据', 'value'=>100, 'class'=>'']];
  64. }
  65. return $view;
  66. }
  67. }