UserVisitDao.php 7.4 KB

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