WechatQrcodeRecordServices.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\wechat;
  12. use app\dao\wechat\WechatQrcodeRecordDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. class WechatQrcodeRecordServices extends BaseServices
  16. {
  17. /**
  18. * WechatQrcodeRecordServices constructor.
  19. * @param WechatQrcodeRecordDao $dao
  20. */
  21. public function __construct(WechatQrcodeRecordDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. /**
  26. * 获取用户列表
  27. * @param $qid
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function userList($qid)
  34. {
  35. [$page, $limit] = $this->getPageValue();
  36. $where['qid'] = $qid;
  37. $list = $this->dao->getList($where, $page, $limit, 1);
  38. $count = $this->dao->getDistinctCount($where, 'uid');
  39. return compact('list', 'count');
  40. }
  41. /**
  42. * 渠道码统计
  43. * @param $where
  44. * @param $time
  45. * @return mixed
  46. */
  47. public function qrcodeStatistic($where, $time)
  48. {
  49. $data['all_follow'] = $this->dao->count($where + ['is_follow' => 1]);
  50. $data['all_scan'] = $this->dao->count($where);
  51. $data['y_follow'] = $this->dao->count($where + ['is_follow' => 1, 'time' => 'yesterday']);
  52. $data['y_scan'] = $this->dao->count($where + ['time' => 'yesterday']);
  53. $data['trend'] = $this->getTrend($where['qid'], explode('-', $time));
  54. return $data;
  55. }
  56. /**
  57. * 余额趋势
  58. * @param $qid
  59. * @param $time
  60. * @return array
  61. */
  62. public function getTrend($qid, $time)
  63. {
  64. if (count($time) != 2) throw new AdminException('参数错误');
  65. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  66. $data = [];
  67. if ($dayCount == 1) {
  68. $data = $this->trend($qid, $time, 0);
  69. } elseif ($dayCount > 1 && $dayCount <= 31) {
  70. $data = $this->trend($qid, $time, 1);
  71. } elseif ($dayCount > 31 && $dayCount <= 92) {
  72. $data = $this->trend($qid, $time, 3);
  73. } elseif ($dayCount > 92) {
  74. $data = $this->trend($qid, $time, 30);
  75. }
  76. return $data;
  77. }
  78. /**
  79. * 余额趋势
  80. * @param $qid
  81. * @param $time
  82. * @param $num
  83. * @param false $excel
  84. * @return array
  85. */
  86. public function trend($qid, $time, $num)
  87. {
  88. if ($num == 0) {
  89. $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'];
  90. $timeType = '%H';
  91. } elseif ($num != 0) {
  92. $dt_start = strtotime($time[0]);
  93. $dt_end = strtotime($time[1]);
  94. while ($dt_start <= $dt_end) {
  95. if ($num == 30) {
  96. $xAxis[] = date('Y-m', $dt_start);
  97. $dt_start = strtotime("+1 month", $dt_start);
  98. $timeType = '%Y-%m';
  99. } else {
  100. $xAxis[] = date('m-d', $dt_start);
  101. $dt_start = strtotime("+$num day", $dt_start);
  102. $timeType = '%m-%d';
  103. }
  104. }
  105. }
  106. $time[1] = date("Y-m-d", strtotime("+1 day", strtotime($time[1])));
  107. $follow = array_column($this->dao->getRecordTrend($qid, $time, $timeType, 'add_time', 'count(uid)', 'yes'), 'num', 'days');
  108. $scan = array_column($this->dao->getRecordTrend($qid, $time, $timeType, 'add_time', 'count(uid)', 'no'), 'num', 'days');
  109. $data = $series = [];
  110. foreach ($xAxis as $item) {
  111. $data['新增关注'][] = isset($follow[$item]) ? floatval($follow[$item]) : 0;
  112. $data['新增参与'][] = isset($scan[$item]) ? floatval($scan[$item]) : 0;
  113. }
  114. foreach ($data as $key => $item) {
  115. $series[] = [
  116. 'name' => $key,
  117. 'data' => $item,
  118. 'type' => 'line',
  119. ];
  120. }
  121. return compact('xAxis', 'series');
  122. }
  123. }