WechatQrcodeRecordServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. declare (strict_types=1);
  12. namespace app\services\wechat;
  13. use app\dao\wechat\WechatQrcodeRecordDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. *
  18. * Class WechatQrcodeRecordServices
  19. * @package app\services\wechat
  20. * @mixin WechatQrcodeRecordDao
  21. */
  22. class WechatQrcodeRecordServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * WechatReplyKeyServices constructor.
  27. * @param WechatQrcodeRecordDao $dao
  28. */
  29. public function __construct(WechatQrcodeRecordDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取用户列表
  35. * @param $qid
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function userList($qid)
  42. {
  43. [$page, $limit] = $this->getPageValue();
  44. $where['qid'] = $qid;
  45. $list = $this->dao->getList($where, $page, $limit, 1);
  46. $count = $this->dao->getDistinctCount($where, 'uid');
  47. return compact('list', 'count');
  48. }
  49. /**
  50. * 渠道码统计
  51. * @param $where
  52. * @param $time
  53. * @return mixed
  54. */
  55. public function qrcodeStatistic($where, $time)
  56. {
  57. $data['all_follow'] = $this->dao->count($where + ['is_follow' => 1]);
  58. $data['all_scan'] = $this->dao->count($where);
  59. $data['y_follow'] = $this->dao->count($where + ['is_follow' => 1, 'time' => 'yesterday']);
  60. $data['y_scan'] = $this->dao->count($where + ['time' => 'yesterday']);
  61. $data['trend'] = $this->getTrend($where['qid'], explode('-', $time));
  62. return $data;
  63. }
  64. /**
  65. * 余额趋势
  66. * @param $qid
  67. * @param $time
  68. * @return array
  69. */
  70. public function getTrend($qid, $time)
  71. {
  72. if (count($time) != 2) throw new AdminException('参数错误');
  73. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  74. $data = [];
  75. if ($dayCount == 1) {
  76. $data = $this->trend($qid, $time, 0);
  77. } elseif ($dayCount > 1 && $dayCount <= 31) {
  78. $data = $this->trend($qid, $time, 1);
  79. } elseif ($dayCount > 31 && $dayCount <= 92) {
  80. $data = $this->trend($qid, $time, 3);
  81. } elseif ($dayCount > 92) {
  82. $data = $this->trend($qid, $time, 30);
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 余额趋势
  88. * @param $qid
  89. * @param $time
  90. * @param $num
  91. * @param false $excel
  92. * @return array
  93. */
  94. public function trend($qid, $time, $num)
  95. {
  96. if ($num == 0) {
  97. $xAxis = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
  98. $timeType = '%H';
  99. } elseif ($num != 0) {
  100. $dt_start = strtotime($time[0]);
  101. $dt_end = strtotime($time[1]);
  102. while ($dt_start <= $dt_end) {
  103. if ($num == 30) {
  104. $xAxis[] = date('Y-m', $dt_start);
  105. $dt_start = strtotime("+1 month", $dt_start);
  106. $timeType = '%Y-%m';
  107. } else {
  108. $xAxis[] = date('m-d', $dt_start);
  109. $dt_start = strtotime("+$num day", $dt_start);
  110. $timeType = '%m-%d';
  111. }
  112. }
  113. }
  114. $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
  115. $follow = array_column($this->dao->getRecordTrend($qid, $time, $timeType, 'add_time', 'count(uid)', 'yes'), 'num', 'days');
  116. $scan = array_column($this->dao->getRecordTrend($qid, $time, $timeType, 'add_time', 'count(uid)', 'no'), 'num', 'days');
  117. $data = $series = [];
  118. foreach ($xAxis as $item) {
  119. $data['新增关注'][] = isset($follow[$item]) ? floatval($follow[$item]) : 0;
  120. $data['新增参与'][] = isset($scan[$item]) ? floatval($scan[$item]) : 0;
  121. }
  122. foreach ($data as $key => $item) {
  123. $series[] = [
  124. 'name' => $key,
  125. 'data' => $item,
  126. 'type' => 'line',
  127. ];
  128. }
  129. return compact('xAxis', 'series');
  130. }
  131. }