UserMoneyDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $a=$this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  36. $query->page($page, $limit);
  37. })->order('id desc')->select()->toArray();
  38. // @file_put_contents('quanju.txt', $this->getModel()->getLastSql(). "-余额账单sql\r\n", 8);
  39. return $a;
  40. }
  41. /**
  42. * 余额趋势
  43. * @param $time
  44. * @param $timeType
  45. * @param $field
  46. * @param $str
  47. * @return mixed
  48. */
  49. public function getBalanceTrend($time, $timeType, $field, $str, $orderStatus = '')
  50. {
  51. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  52. if ($orderStatus == 'add') {
  53. $query->where('pm', 1);
  54. } elseif ($orderStatus == 'sub') {
  55. $query->where('pm', 0);
  56. }
  57. })->where(function ($query) use ($time, $field) {
  58. if ($time[0] == $time[1]) {
  59. $query->whereDay($field, $time[0]);
  60. } else {
  61. $query->whereTime($field, 'between', $time);
  62. }
  63. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  64. }
  65. /**
  66. * 获取某个字段总和
  67. * @param array $where
  68. * @param string $field
  69. * @return float
  70. */
  71. public function getWhereSumField(array $where, string $field)
  72. {
  73. return $this->search($where, false)
  74. ->when(isset($where['timeKey']), function ($query) use ($where) {
  75. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  76. })
  77. ->sum($field);
  78. }
  79. /**
  80. * 根据某字段分组查询
  81. * @param array $where
  82. * @param string $field
  83. * @param string $group
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getGroupField(array $where, string $field, string $group)
  90. {
  91. return $this->search($where, false)
  92. ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
  93. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  94. if ($where['timeKey']['days'] == 1) {
  95. $timeUinx = "%H";
  96. } elseif ($where['timeKey']['days'] == 30) {
  97. $timeUinx = "%Y-%m-%d";
  98. } elseif ($where['timeKey']['days'] == 365) {
  99. $timeUinx = "%Y-%m";
  100. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  101. $timeUinx = "%Y-%m-%d";
  102. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  103. $timeUinx = "%Y-%m";
  104. }
  105. $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  106. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  107. })
  108. ->order('add_time ASC')->select()->toArray();
  109. }
  110. }