UserVisitDao.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/5/27
  7. *
  8. *
  9. */
  10. namespace app\common\dao\user;
  11. use app\common\dao\BaseDao;
  12. use app\common\model\user\UserVisit;
  13. use think\facade\Db;
  14. use think\Model;
  15. /**
  16. * Class UserVisitDao
  17. * @package app\common\dao\user
  18. * @author xaboy
  19. * @day 2020/5/27
  20. */
  21. class UserVisitDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020/5/27
  27. */
  28. protected function getModel(): string
  29. {
  30. return UserVisit::class;
  31. }
  32. /**
  33. * @param int $uid
  34. * @param string $type
  35. * @param int $type_id
  36. * @param string|null $content
  37. * @param string|null $ip
  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 = '', ?string $ip = '')
  43. {
  44. return $this->create(compact('uid', 'type', 'type_id', 'content', 'ip'));
  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 int $page
  60. * @return BaseDao|Model
  61. * @author xaboy
  62. * @day 2020/5/27
  63. */
  64. public function visitPage(int $uid, int $page)
  65. {
  66. return $this->addVisit($uid, 'page', 0, $page);
  67. }
  68. /**
  69. * @param int $uid
  70. * @param int $page
  71. * @return BaseDao|Model
  72. * @author xaboy
  73. * @day 2020/5/27
  74. */
  75. public function visitSmallProgram(int $uid, int $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 dateVisitUserNumAdmin($date)
  129. {
  130. return UserVisit::getDB()->when($date, function ($query, $date) {
  131. getModelTime($query, $date, 'create_time');
  132. })->whereIn('type', ['count'])->group('ip')->count();
  133. }
  134. public function dateVisitMerchantNum($date, $limit = 7)
  135. {
  136. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  137. ->join('Merchant C', 'C.mer_id = B.mer_id')
  138. ->field(Db::raw('count(A.type) as total,B.mer_id,C.mer_name'))
  139. ->when($date, function ($query, $date) {
  140. getModelTime($query, $date, 'A.create_time');
  141. })->where('A.type', 'product')->limit($limit)->group('B.mer_id')->select();
  142. }
  143. public function dateVisitProductNum($date, $merId, $limit = 7)
  144. {
  145. return UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
  146. ->join('Merchant C', 'C.mer_id = B.mer_id')
  147. ->field(Db::raw('count(A.type_id) as total,B.image,B.store_name'))
  148. ->when($date, function ($query, $date) {
  149. getModelTime($query, $date, 'A.create_time');
  150. })->where('A.type', 'product')->where('B.mer_id', $merId)->group('A.type_id')->order('total DESC')
  151. ->limit($limit)->select();
  152. }
  153. public function dateVisitMerchantTotal($date)
  154. {
  155. return UserVisit::getDB()->when($date, function ($query, $date) {
  156. getModelTime($query, $date, 'create_time');
  157. })->whereIn('type', 'product')->count();
  158. }
  159. public function dateVisitNum($date)
  160. {
  161. return UserVisit::getDB()->when($date, function ($query, $date) {
  162. getModelTime($query, $date, 'create_time');
  163. })->whereIn('type', ['page', 'smallProgram'])->count();
  164. }
  165. public function dateVisitNumGroup($date)
  166. {
  167. return UserVisit::getDB()->when($date, function ($query, $date) {
  168. getModelTime($query, $date, 'create_time');
  169. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%Y-%m-%d\') as time, count(DISTINCT ip) as total'))
  170. ->where('type', 'count')
  171. ->group('time')
  172. ->order('time ASC')->select();
  173. }
  174. public function dateVisitNumGroupHour($date)
  175. {
  176. return UserVisit::getDB()->when($date, function ($query, $date) {
  177. getModelTime($query, $date, 'create_time');
  178. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%h\') as time, count(DISTINCT ip) as total'))
  179. ->where('type', 'count')
  180. ->group('time')
  181. ->order('time ASC')->select();
  182. }
  183. public function dateVisitNumGroupYear($date)
  184. {
  185. return UserVisit::getDB()->when($date, function ($query, $date) {
  186. getModelTime($query, $date, 'create_time');
  187. })->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m\') as time, count(DISTINCT ip) as total'))
  188. ->where('type', 'count')
  189. ->group('time')
  190. ->order('time ASC')->select();
  191. }
  192. }