UserExchangeDao.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. public function getLastOne(int $uid)
  49. {
  50. return $this->search(['uid' => $uid])->order('id desc')->find();
  51. }
  52. /**
  53. * 获取某个条件的提现总和
  54. * @param array $where
  55. * @return float
  56. */
  57. public function getWhereSum(array $where)
  58. {
  59. return $this->search($where)->field('(extract_num + extract_fee) as extract_num')->sum('extract_num');
  60. }
  61. /**
  62. * 获取某些条件总数组合列表
  63. * @param array $where
  64. * @param string $field
  65. * @param string $key
  66. * @return mixed
  67. */
  68. public function getWhereSumList(array $where, string $field = 'extract_num', string $key = 'uid')
  69. {
  70. return $this->search($where)->group($key)->column('(sum(extract_num) + sum(extract_num)) as extract_num', $key);
  71. }
  72. /**
  73. * 获取提现列表
  74. * @param array $where
  75. * @param string $field
  76. * @param int $page
  77. * @param int $limit
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function getExtractList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  84. {
  85. return $this->search($where)->field($field)->with([
  86. 'user' => function ($query) {
  87. $query->field('uid,nickname,real_name,card_id,phone');
  88. }])->page($page, $limit)->order('id desc')->select()->toArray();
  89. }
  90. public function getExtractAll(array $where, string $field = '*')
  91. {
  92. return $this->search($where)->field($field)->with([
  93. 'user' => function ($query) {
  94. $query->field('uid,nickname,real_name,card_id,phone');
  95. }])->order('id desc')->select()->toArray();
  96. }
  97. /**
  98. * 获取某个字段总和
  99. * @param array $where
  100. * @param string $field
  101. * @return float
  102. */
  103. public function getWhereSumField(array $where, string $field)
  104. {
  105. return $this->search($where)
  106. ->when(isset($where['timeKey']), function ($query) use ($where) {
  107. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  108. })
  109. ->sum($field);
  110. }
  111. /**
  112. * 根据某字段分组查询
  113. * @param array $where
  114. * @param string $field
  115. * @param string $group
  116. * @return mixed
  117. */
  118. public function getGroupField(array $where, string $field, string $group)
  119. {
  120. return $this->search($where)
  121. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  122. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  123. if ($where['timeKey']['days'] == 1) {
  124. $timeUinx = "%H";
  125. } elseif ($where['timeKey']['days'] == 30) {
  126. $timeUinx = "%Y-%m-%d";
  127. } elseif ($where['timeKey']['days'] == 365) {
  128. $timeUinx = "%Y-%m";
  129. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  130. $timeUinx = "%Y-%m-%d";
  131. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  132. $timeUinx = "%Y-%m";
  133. } else {
  134. $timeUinx = "%Y-%m";
  135. }
  136. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  137. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  138. })
  139. ->order('add_time ASC')->select()->toArray();
  140. }
  141. /**
  142. * @param array $where
  143. * @param string $field
  144. * @return float
  145. */
  146. public function getExtractMoneyByWhere(array $where, string $field)
  147. {
  148. return $this->search($where)->sum($field);
  149. }
  150. }