UserRechargeDao.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. */
  37. public function getList(array $where, string $filed = "*", int $page, int $limit)
  38. {
  39. return $this->search($where)->field($filed)->with([
  40. 'user' => function ($query) {
  41. $query->field('uid,phone,nickname,avatar');
  42. }])->when($page && $limit, function ($query) use ($page, $limit) {
  43. $query->page($page, $limit);
  44. })->order('id desc')->select()->toArray();
  45. }
  46. /**
  47. * 获取某个字段总和
  48. * @param array $where
  49. * @param string $field
  50. * @return float
  51. */
  52. public function getWhereSumField(array $where, string $field)
  53. {
  54. return $this->search($where, false)
  55. ->when(isset($where['timeKey']), function ($query) use ($where) {
  56. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  57. })
  58. ->sum($field);
  59. }
  60. /**根据某字段分组查询
  61. * @param array $where
  62. * @param string $field
  63. * @param string $group
  64. * @return mixed
  65. */
  66. public function getGroupField(array $where, string $field, string $group)
  67. {
  68. return $this->search($where, false)
  69. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  70. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  71. $timeUinx = "%H";
  72. if ($where['timeKey']['days'] == 1) {
  73. $timeUinx = "%H";
  74. } elseif ($where['timeKey']['days'] == 30) {
  75. $timeUinx = "%Y-%m-%d";
  76. } elseif ($where['timeKey']['days'] == 365) {
  77. $timeUinx = "%Y-%m";
  78. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  79. $timeUinx = "%Y-%m-%d";
  80. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  81. $timeUinx = "%Y-%m";
  82. }
  83. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  84. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  85. })
  86. ->order('add_time ASC')->select()->toArray();
  87. }
  88. public function getTrendData($time, $type, $timeType)
  89. {
  90. return $this->getModel()->when($type != '', function ($query) use ($type) {
  91. $query->where('channel_type', $type);
  92. })->where(function ($query) use ($time) {
  93. if ($time[0] == $time[1]) {
  94. $query->whereDay('pay_time', $time[0]);
  95. } else {
  96. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  97. $query->whereTime('pay_time', 'between', $time);
  98. }
  99. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,count(id) as num")
  100. ->group('days')->select()->toArray();
  101. }
  102. }