UserVisitDao.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. 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)
  87. {
  88. return $this->addVisit($uid, 'searchProduct', 0, $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(?int $uid, string $type)
  112. {
  113. $query = ($this->getModel()::getDB())->when($uid, function ($query) use ($uid) {
  114. $query->where('uid', $uid);
  115. })->when($type, function ($query) use ($type) {
  116. $query->where('type', $type);
  117. });
  118. return $query->order('create_time DESC');
  119. }
  120. public function dateVisitUserNum($date, $merId = null)
  121. {
  122. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')->when($date, function ($query, $date) {
  123. getModelTime($query, $date, 'A.create_time');
  124. })->when($merId, function ($query, $merId) {
  125. $query->where('B.mer_id', $merId);
  126. })->where('A.type', 'product')->group('uid')->count();
  127. }
  128. public function dateVisitMerchantNum($date, $limit = 7)
  129. {
  130. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  131. ->join('Merchant C', 'C.mer_id = B.mer_id')
  132. ->field(Db::raw('count(A.type) as total,B.mer_id,C.mer_name'))
  133. ->when($date, function ($query, $date) {
  134. getModelTime($query, $date, 'A.create_time');
  135. })->where('A.type', 'product')->limit($limit)->group('B.mer_id')->order('total DESC')->select();
  136. }
  137. public function dateVisitProductNum($date, $merId, $limit = 7)
  138. {
  139. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  140. ->join('Merchant C', 'C.mer_id = B.mer_id')
  141. ->field(Db::raw('count(A.type_id) as total,B.image,B.store_name'))
  142. ->when($date, function ($query, $date) {
  143. getModelTime($query, $date, 'A.create_time');
  144. })->where('A.type', 'product')->where('B.mer_id', $merId)->group('A.type_id')->order('total DESC')
  145. ->limit($limit)->select();
  146. }
  147. public function dateVisitMerchantTotal($date)
  148. {
  149. return UserVisit::getDB()->when($date, function ($query, $date) {
  150. getModelTime($query, $date, 'create_time');
  151. })->whereIn('type', 'product')->count();
  152. }
  153. public function dateVisitNum($date)
  154. {
  155. return UserVisit::getDB()->when($date, function ($query, $date) {
  156. getModelTime($query, $date, 'create_time');
  157. })->whereIn('type', ['page', 'smallProgram'])->count();
  158. }
  159. public function dateVisitNumGroup($date)
  160. {
  161. return UserVisit::getDB()->when($date, function ($query, $date) {
  162. getModelTime($query, $date, 'create_time');
  163. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(DISTINCT uid) as total'))
  164. ->group('time')
  165. ->order('time ASC')->select();
  166. }
  167. public function batchDelete(? array $ids,?int $uid)
  168. {
  169. if($ids) return UserVisit::getDB()->where($this->getPk(),'in',$ids)->delete();
  170. if($uid) return UserVisit::getDB()->where('uid',$uid)->where('type','product')->delete();
  171. }
  172. }