CapitalFlowDao.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\dao\system;
  3. use app\dao\BaseDao;
  4. use app\model\system\CapitalFlow;
  5. class CapitalFlowDao extends BaseDao
  6. {
  7. /**
  8. * 设置模型
  9. * @return string
  10. */
  11. protected function setModel(): string
  12. {
  13. return CapitalFlow::class;
  14. }
  15. /**
  16. * 资金流水
  17. * @param array $where
  18. * @param string $field
  19. * @param int $page
  20. * @param int $limit
  21. * @return array
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  27. {
  28. return $this->search($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  29. $query->page($page, $limit);
  30. })->order('id desc')->select()->toArray();
  31. }
  32. /**
  33. * 账单记录
  34. * @param $where
  35. * @param int $page
  36. * @param int $limit
  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 getRecordList($where, $page = 0, $limit = 0)
  43. {
  44. $model = $this->search($where)
  45. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  46. $timeUnix = '%Y-%m-%d';
  47. switch ($where['type']) {
  48. case "day" :
  49. $timeUnix = "%Y-%m-%d";
  50. break;
  51. case "week" :
  52. $timeUnix = "%Y-%u";
  53. break;
  54. case "month" :
  55. $timeUnix = "%Y-%m";
  56. break;
  57. }
  58. $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,group_concat(id) as ids");
  59. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  60. });
  61. $count = $model->count();
  62. $list = $model->when($page && $limit, function ($query) use ($page, $limit) {
  63. $query->page($page, $limit);
  64. })->order('add_time desc')->select()->toArray();
  65. return compact('list', 'count');
  66. }
  67. }