UserVisitDao.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\common\dao\user;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\user\UserVisit;
  5. use think\facade\Db;
  6. use think\Model;
  7. /**
  8. * Class UserVisitDao
  9. * @package app\common\dao\user
  10. * @author zfy
  11. * @day 2020/5/27
  12. */
  13. class UserVisitDao extends BaseDao
  14. {
  15. /**
  16. * @return string
  17. * @author zfy
  18. * @day 2020/5/27
  19. */
  20. protected function getModel(): string
  21. {
  22. return UserVisit::class;
  23. }
  24. /**
  25. * @param int $uid
  26. * @param string $type
  27. * @param int $type_id
  28. * @param string|null $content
  29. * @return BaseDao|Model
  30. * @author zfy
  31. * @day 2020/5/27
  32. */
  33. public function addVisit(int $uid, string $type, int $type_id, ?string $content = '')
  34. {
  35. return $this->create(compact('uid', 'type', 'type_id', 'content'));
  36. }
  37. /**
  38. * @param int $uid
  39. * @param int $productId
  40. * @return BaseDao|Model
  41. * @author zfy
  42. * @day 2020/5/27
  43. */
  44. public function visitProduct(int $uid, int $productId)
  45. {
  46. return $this->addVisit($uid, 'product', $productId);
  47. }
  48. /**
  49. * @param int $uid
  50. * @param string $page
  51. * @return BaseDao|Model
  52. * @author zfy
  53. * @day 2020/5/27
  54. */
  55. public function visitPage(int $uid, string $page)
  56. {
  57. return $this->addVisit($uid, 'page', 0, $page);
  58. }
  59. /**
  60. * @param int $uid
  61. * @param string $page
  62. * @return BaseDao|Model
  63. * @author zfy
  64. * @day 2020/5/27
  65. */
  66. public function visitSmallProgram(int $uid, string $page)
  67. {
  68. return $this->addVisit($uid, 'smallProgram', 0, $page);
  69. }
  70. /**
  71. * @param int $uid
  72. * @param string $keyword
  73. * @return BaseDao|Model
  74. * @author zfy
  75. * @day 2020/5/27
  76. */
  77. public function searchProduct(int $uid, string $keyword, int $merId = 0)
  78. {
  79. return $this->addVisit($uid, 'searchProduct', $merId, $keyword);
  80. }
  81. /**
  82. * @param int $uid
  83. * @param string $keyword
  84. * @return BaseDao|Model
  85. * @author zfy
  86. * @day 2020/5/27
  87. */
  88. public function searchMerchant(int $uid, string $keyword)
  89. {
  90. return $this->addVisit($uid, 'searchMerchant', 0, $keyword);
  91. }
  92. /**
  93. * @param $uid
  94. * @return int
  95. * @author zfy
  96. * @day 2020/6/19
  97. */
  98. public function userTotalVisit($uid)
  99. {
  100. return UserVisit::getDB()->where('uid', $uid)->where('type', 'product')->count();
  101. }
  102. public function search(array $where)
  103. {
  104. if ((isset($where['nickname']) && $where['nickname'] !== '') || (isset($where['user_type']) && $where['user_type'] !== '')) {
  105. $query = UserVisit::hasWhere('user', function ($query) use ($where) {
  106. $query->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  107. $query->whereLike('User.nickname', "%{$where['nickname']}%");
  108. })->when(isset($where['user_type']) && $where['user_type'] !== '', function ($query) use ($where) {
  109. $query->where('User.user_type', $where['user_type']);
  110. });
  111. });
  112. } else {
  113. $query = UserVisit::getDB()->alias('UserVisit');
  114. }
  115. $query = $query->when(isset($where['uid']), function ($query) use ($where) {
  116. $query->where('UserVisit.uid', $where['uid']);
  117. })->when(isset($where['mer_id']), function ($query) use ($where) {
  118. $query->where('UserVisit.type_id', $where['mer_id']);
  119. })->when(isset($where['type']), function ($query) use ($where) {
  120. if(is_array($where['type'])){
  121. $query->where('UserVisit.type','in', $where['type']);
  122. }else{
  123. $query->where('UserVisit.type', $where['type']);
  124. }
  125. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  126. getModelTime($query, $where['date'], 'UserVisit.create_time');
  127. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  128. $query->whereLike('UserVisit.content', "%{$where['keyword']}%");
  129. });
  130. return $query->order('UserVisit.create_time DESC');
  131. }
  132. public function dateVisitUserNum($date, $merId = null)
  133. {
  134. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')->when($date, function ($query, $date) {
  135. getModelTime($query, $date, 'A.create_time');
  136. })->when($merId, function ($query, $merId) {
  137. $query->where('B.mer_id', $merId);
  138. })->where('A.type', 'product')->group('uid')->count();
  139. }
  140. public function dateVisitMerchantNum($date, $limit = 7)
  141. {
  142. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  143. ->join('Merchant C', 'C.mer_id = B.mer_id')
  144. ->field(Db::raw('count(A.type) as total,B.mer_id,C.mer_name'))
  145. ->when($date, function ($query, $date) {
  146. getModelTime($query, $date, 'A.create_time');
  147. })->where('A.type', 'product')->limit($limit)->group('B.mer_id')->order('total DESC')->select();
  148. }
  149. public function dateVisitProductNum($date, $merId, $limit = 7)
  150. {
  151. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  152. ->join('Merchant C', 'C.mer_id = B.mer_id')
  153. ->field(Db::raw('count(A.type_id) as total,B.image,B.store_name'))
  154. ->when($date, function ($query, $date) {
  155. getModelTime($query, $date, 'A.create_time');
  156. })->where('A.type', 'product')->where('B.mer_id', $merId)->group('A.type_id')->order('total DESC')
  157. ->limit($limit)->select();
  158. }
  159. public function dateVisitMerchantTotal($date)
  160. {
  161. return UserVisit::getDB()->when($date, function ($query, $date) {
  162. getModelTime($query, $date, 'create_time');
  163. })->whereIn('type', 'product')->count();
  164. }
  165. public function dateVisitNum($date)
  166. {
  167. return UserVisit::getDB()->when($date, function ($query, $date) {
  168. getModelTime($query, $date, 'create_time');
  169. })->whereIn('type', ['page', 'smallProgram'])->count();
  170. }
  171. public function dateVisitNumGroup($date)
  172. {
  173. return UserVisit::getDB()->when($date, function ($query, $date) {
  174. getModelTime($query, $date, 'create_time');
  175. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(DISTINCT uid) as total'))
  176. ->group('time')
  177. ->order('time ASC')->select();
  178. }
  179. public function batchDelete(? array $ids,?int $uid)
  180. {
  181. if($ids) return UserVisit::getDB()->where($this->getPk(),'in',$ids)->delete();
  182. if($uid) return UserVisit::getDB()->where('uid',$uid)->where('type','product')->delete();
  183. }
  184. }