CapitalFlowDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\system\statistics;
  12. use app\dao\BaseDao;
  13. use app\model\system\statistics\CapitalFlow;
  14. class CapitalFlowDao extends BaseDao
  15. {
  16. /**
  17. * 设置模型
  18. * @return string
  19. */
  20. protected function setModel(): string
  21. {
  22. return CapitalFlow::class;
  23. }
  24. /**
  25. * 资金流水
  26. * @param $where
  27. * @param int $page
  28. * @param int $limit
  29. * @return array
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function getList($where, $page = 0, $limit = 0)
  35. {
  36. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  37. $query->page($page, $limit);
  38. })->order('id desc')->select()->toArray();
  39. }
  40. /**
  41. * 账单记录
  42. * @param $where
  43. * @param int $page
  44. * @param int $limit
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getRecordList($where, int $page = 0, int $limit = 0)
  51. {
  52. $timeUnix = "%Y-%m-%d";
  53. switch ($where['type']) {
  54. case "day" :
  55. $timeUnix = "%Y-%m-%d";
  56. break;
  57. case "week" :
  58. $timeUnix = "%u";
  59. break;
  60. case "month" :
  61. $timeUnix = "%Y-%m";
  62. break;
  63. }
  64. $model = $this->search($where, false)
  65. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where, $timeUnix) {
  66. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,sum(if(price >= 0,price,0)) as income_price,sum(if(price < 0,price,0)) as exp_price,add_time");
  67. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  68. });
  69. $count = $model->count();
  70. $list = $model->when($page && $limit, function ($query) use ($page, $limit) {
  71. $query->page($page, $limit);
  72. })->order('add_time desc')->select()->toArray();
  73. foreach ($list as &$item) {
  74. if ($where['type'] == 'day') {
  75. $item['ids'] = array_merge($this->getModel()->whereDay('add_time', $item['day'])->column('id'));
  76. } elseif ($where['type'] == 'week') {
  77. $day = $this->weekDayTime(date('Y'), $item['day']);
  78. $item['ids'] = array_merge($this->getModel()->whereWeek('add_time', $day)->column('id'));
  79. } elseif ($where['type'] == 'month') {
  80. $item['ids'] = array_merge($this->getModel()->whereMonth('add_time', $item['day'])->column('id'));
  81. }
  82. }
  83. return compact('list', 'count');
  84. }
  85. /**
  86. * 获取某年第几周的开始日期
  87. * @param int $year
  88. * @param int $week
  89. * @return array|false|string
  90. */
  91. public function weekDayTime(int $year, int $week = 1)
  92. {
  93. $year_start = mktime(0, 0, 0, 1, 1, $year);
  94. // 判断第一天是否为第一周的开始
  95. if (intval(date('W', $year_start)) === 1) {
  96. $start = $year_start;//把第一天做为第一周的开始
  97. } else {
  98. $start = strtotime('+1 monday', $year_start);//把第一个周一作为开始
  99. }
  100. // 第几周的开始时间
  101. if ($week === 1) {
  102. $weekday = $start;
  103. } else {
  104. $weekday = strtotime('+' . ($week - 0) . ' monday', $start);
  105. }
  106. return date('Y-m-d', $weekday);
  107. }
  108. }