UserExchangeDao.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\UserExchange;
  15. use app\model\user\UserExtract;
  16. /**
  17. *
  18. * Class UserExtractDao
  19. * @package app\dao\user
  20. */
  21. class UserExchangeDao extends BaseDao
  22. {
  23. /**
  24. * 设置模型
  25. * @return string
  26. */
  27. protected function setModel(): string
  28. {
  29. return UserExchange::class;
  30. }
  31. /**
  32. * 获取列表
  33. * @param array $where
  34. * @param string $field
  35. * @param int $page
  36. * @param int $limit
  37. * @param array $typeWhere
  38. * @return array
  39. */
  40. public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0)
  41. {
  42. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  43. $query->with($with);
  44. })->when($page && $limit, function ($query) use ($page, $limit) {
  45. $query->page($page, $limit);
  46. })->order('id desc')->select()->toArray();
  47. }
  48. /**
  49. * 获取某个条件的提现总和
  50. * @param array $where
  51. * @return float
  52. */
  53. public function getWhereSum(array $where)
  54. {
  55. return $this->search($where)->field('(extract_num + extract_fee) as extract_num')->sum('extract_num');
  56. }
  57. /**
  58. * 获取某些条件总数组合列表
  59. * @param array $where
  60. * @param string $field
  61. * @param string $key
  62. * @return mixed
  63. */
  64. public function getWhereSumList(array $where, string $field = 'extract_num', string $key = 'uid')
  65. {
  66. return $this->search($where)->group($key)->column('(sum(extract_num) + sum(extract_num)) as extract_num', $key);
  67. }
  68. /**
  69. * 获取提现列表
  70. * @param array $where
  71. * @param string $field
  72. * @param int $page
  73. * @param int $limit
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function getExtractList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  80. {
  81. return $this->search($where)->field($field)->with([
  82. 'user' => function ($query) {
  83. $query->field('uid,nickname');
  84. }])->page($page, $limit)->order('id desc')->select()->toArray();
  85. }
  86. public function getExtractAll(array $where, string $field = '*')
  87. {
  88. return $this->search($where)->field($field)->with([
  89. 'user' => function ($query) {
  90. $query->field('uid,nickname,real_name,card_id');
  91. }])->order('id desc')->select()->toArray();
  92. }
  93. /**
  94. * 获取某个字段总和
  95. * @param array $where
  96. * @param string $field
  97. * @return float
  98. */
  99. public function getWhereSumField(array $where, string $field)
  100. {
  101. return $this->search($where)
  102. ->when(isset($where['timeKey']), function ($query) use ($where) {
  103. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  104. })
  105. ->sum($field);
  106. }
  107. /**
  108. * 根据某字段分组查询
  109. * @param array $where
  110. * @param string $field
  111. * @param string $group
  112. * @return mixed
  113. */
  114. public function getGroupField(array $where, string $field, string $group)
  115. {
  116. return $this->search($where)
  117. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  118. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  119. if ($where['timeKey']['days'] == 1) {
  120. $timeUinx = "%H";
  121. } elseif ($where['timeKey']['days'] == 30) {
  122. $timeUinx = "%Y-%m-%d";
  123. } elseif ($where['timeKey']['days'] == 365) {
  124. $timeUinx = "%Y-%m";
  125. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  126. $timeUinx = "%Y-%m-%d";
  127. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  128. $timeUinx = "%Y-%m";
  129. } else {
  130. $timeUinx = "%Y-%m";
  131. }
  132. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  133. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  134. })
  135. ->order('add_time ASC')->select()->toArray();
  136. }
  137. /**
  138. * @param array $where
  139. * @param string $field
  140. * @return float
  141. */
  142. public function getExtractMoneyByWhere(array $where, string $field)
  143. {
  144. return $this->search($where)->sum($field);
  145. }
  146. }