UserMoneyDao.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\UserMoney;
  15. /**
  16. * 用户余额
  17. * Class UserMoneyDao
  18. * @package app\dao\user
  19. */
  20. class UserMoneyDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return UserMoney::class;
  29. }
  30. /**
  31. * 获取列表
  32. * @param array $where
  33. * @param string $field
  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 $field = '*', int $page = 0, int $limit = 0, array $with = [])
  43. {
  44. return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
  45. $query->with($with);
  46. })->when($page && $limit, function ($query) use ($page, $limit) {
  47. $query->page($page, $limit);
  48. })->order('id desc')->select()->toArray();
  49. }
  50. /**
  51. * 获取某一个月数量
  52. * @param array $where
  53. * @param string $month
  54. * @return int
  55. */
  56. public function getMonthCount(array $where, string $month)
  57. {
  58. return $this->search($where)->whereMonth('add_time', $month)->count();
  59. }
  60. /**
  61. * 获取余额记录类型
  62. * @param array $where
  63. * @param string $filed
  64. * @return mixed
  65. */
  66. public function getMoneyType(array $where, string $filed = 'title,type')
  67. {
  68. return $this->search($where)->distinct(true)->field($filed)->group('type')->select();
  69. }
  70. /**
  71. * 余额趋势
  72. * @param $time
  73. * @param $timeType
  74. * @param $field
  75. * @param $str
  76. * @return mixed
  77. */
  78. public function getBalanceTrend($time, $timeType, $field, $str, $orderStatus = '')
  79. {
  80. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  81. if ($orderStatus == 'add') {
  82. $query->where('pm', 1);
  83. } elseif ($orderStatus == 'sub') {
  84. $query->where('pm', 0);
  85. }
  86. })->where(function ($query) use ($time, $field) {
  87. if ($time[0] == $time[1]) {
  88. $query->whereDay($field, $time[0]);
  89. } else {
  90. $query->whereTime($field, 'between', $time);
  91. }
  92. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  93. }
  94. /**
  95. * 获取某个字段总和
  96. * @param array $where
  97. * @param string $field
  98. * @return float
  99. */
  100. public function getWhereSumField(array $where, string $field)
  101. {
  102. return $this->search($where)
  103. ->when(isset($where['timeKey']), function ($query) use ($where) {
  104. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  105. })
  106. ->sum($field);
  107. }
  108. /**
  109. * 根据某字段分组查询
  110. * @param array $where
  111. * @param string $field
  112. * @param string $group
  113. * @return array
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  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. }