UserMoneyDao.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. namespace app\dao\user;
  12. use app\dao\BaseDao;
  13. use app\model\user\UserMoney;
  14. class UserMoneyDao extends BaseDao
  15. {
  16. /**
  17. * 设置模型
  18. * @return string
  19. */
  20. protected function setModel(): string
  21. {
  22. return UserMoney::class;
  23. }
  24. /**
  25. * 获取列表
  26. * @param array $where
  27. * @param string $field
  28. * @param int $page
  29. * @param int $limit
  30. * @param array $typeWhere
  31. * @return array
  32. */
  33. public function getList(array $where, int $page = 0, int $limit = 0)
  34. {
  35. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  36. $query->page($page, $limit);
  37. })->order('id desc')->select()->toArray();
  38. }
  39. /**
  40. * 余额趋势
  41. * @param $time
  42. * @param $timeType
  43. * @param $field
  44. * @param $str
  45. * @return mixed
  46. */
  47. public function getBalanceTrend($time, $timeType, $field, $str, $orderStatus = '')
  48. {
  49. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  50. if ($orderStatus == 'add') {
  51. $query->where('pm', 1);
  52. } elseif ($orderStatus == 'sub') {
  53. $query->where('pm', 0);
  54. }
  55. })->where(function ($query) use ($time, $field) {
  56. if ($time[0] == $time[1]) {
  57. $query->whereDay($field, $time[0]);
  58. } else {
  59. $query->whereTime($field, 'between', $time);
  60. }
  61. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  62. }
  63. /**
  64. * 获取某个字段总和
  65. * @param array $where
  66. * @param string $field
  67. * @return float
  68. */
  69. public function getWhereSumField(array $where, string $field)
  70. {
  71. return $this->search($where, false)
  72. ->when(isset($where['timeKey']), function ($query) use ($where) {
  73. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  74. })
  75. ->sum($field);
  76. }
  77. /**
  78. * 根据某字段分组查询
  79. * @param array $where
  80. * @param string $field
  81. * @param string $group
  82. * @return array
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function getGroupField(array $where, string $field, string $group)
  88. {
  89. return $this->search($where, false)
  90. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  91. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  92. if ($where['timeKey']['days'] == 1) {
  93. $timeUinx = "%H";
  94. } elseif ($where['timeKey']['days'] == 30) {
  95. $timeUinx = "%Y-%m-%d";
  96. } elseif ($where['timeKey']['days'] == 365) {
  97. $timeUinx = "%Y-%m";
  98. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  99. $timeUinx = "%Y-%m-%d";
  100. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  101. $timeUinx = "%Y-%m";
  102. }
  103. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  104. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  105. })
  106. ->order('add_time ASC')->select()->toArray();
  107. }
  108. }