UserRechargeDao.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\dao\user;
  13. use app\dao\BaseDao;
  14. use app\model\user\UserRecharge;
  15. /**
  16. *
  17. * Class UserRechargeDao
  18. * @package app\dao\user
  19. */
  20. class UserRechargeDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return UserRecharge::class;
  29. }
  30. /**
  31. * 获取充值记录
  32. * @param array $where
  33. * @param string $filed
  34. * @param int $page
  35. * @param int $limit
  36. * @param array $with
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getList(array $where, string $filed = "*", int $page = 0, int $limit = 0, array $with = [])
  43. {
  44. return $this->search($where)->field($filed)->with(array_merge([
  45. 'user' => function ($query) {
  46. $query->field('uid,phone,nickname,avatar,delete_time');
  47. }], $with))->when($page && $limit, function ($query) use ($page, $limit) {
  48. $query->page($page, $limit);
  49. })->order('id desc')->select()->toArray();
  50. }
  51. /**
  52. * 获取某个字段总和
  53. * @param array $where
  54. * @param string $field
  55. * @return float
  56. */
  57. public function getWhereSumField(array $where, string $field)
  58. {
  59. return $this->search($where)
  60. ->when(isset($where['timeKey']), function ($query) use ($where) {
  61. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  62. })
  63. ->sum($field);
  64. }
  65. /**
  66. * 根据某字段分组查询
  67. * @param array $where
  68. * @param string $field
  69. * @param string $group
  70. * @return mixed
  71. */
  72. public function getGroupField(array $where, string $field, string $group)
  73. {
  74. return $this->search($where)
  75. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  76. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  77. if ($where['timeKey']['days'] == 1) {
  78. $timeUinx = "%H";
  79. } elseif ($where['timeKey']['days'] == 30) {
  80. $timeUinx = "%Y-%m-%d";
  81. } elseif ($where['timeKey']['days'] == 365) {
  82. $timeUinx = "%Y-%m";
  83. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  84. $timeUinx = "%Y-%m-%d";
  85. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  86. $timeUinx = "%Y-%m";
  87. } else {
  88. $timeUinx = "%Y-%m";
  89. }
  90. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  91. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  92. })
  93. ->order('add_time ASC')->select()->toArray();
  94. }
  95. /**
  96. * 获取充值统计曲线
  97. * @param $time
  98. * @param $type
  99. * @param $timeType
  100. * @param string $str
  101. * @return mixed
  102. */
  103. public function getTrendData($time, $type, $timeType, $str = 'count(id)')
  104. {
  105. return $this->getModel()->when($type != '', function ($query) use ($type) {
  106. $query->where('channel_type', $type);
  107. })->where('paid', 1)->where(function ($query) use ($time) {
  108. if ($time[0] == $time[1]) {
  109. $query->whereDay('pay_time', $time[0]);
  110. } else {
  111. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  112. $query->whereTime('pay_time', 'between', $time);
  113. }
  114. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days, " . $str . "as num")
  115. ->group('days')->select()->toArray();
  116. }
  117. /**
  118. * 每月统计数据
  119. * @param int $page
  120. * @param int $limit
  121. * @return array
  122. */
  123. public function getDataPriceCount(array $where, array $field, int $page = 0, int $limit = 0)
  124. {
  125. return $this->search($where)
  126. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  127. ->order('add_time DESC')->when($page && $limit, function ($query) use ($page, $limit) {
  128. $query->page($page, $limit);
  129. })->select()->toArray();
  130. }
  131. }