WechatQrcodeRecordServices.php 4.0 KB

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