SupplierFlowingWaterDao.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. namespace app\dao\supplier\finance;
  12. use app\dao\BaseDao;
  13. use app\model\supplier\finance\SupplierFlowingWater;
  14. /**
  15. * 供应商流水
  16. * Class SupplierFlowingWaterDao
  17. * @package app\dao\supplier\finance
  18. */
  19. class SupplierFlowingWaterDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return SupplierFlowingWater::class;
  28. }
  29. /**
  30. * 获取供应商流水列表
  31. * @param array $where
  32. * @param string $field
  33. * @param int $page
  34. * @param int $limit
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, array $with = [])
  41. {
  42. return $this->search($where)
  43. ->when($with, function ($query) use ($with) {
  44. $query->with($with);
  45. })->when($page && $limit, function ($query) use ($page, $limit) {
  46. $query->page($page, $limit);
  47. })->field($field)->order('trade_time desc,id desc')->select()->toArray();
  48. }
  49. /**
  50. *供应商流水数量
  51. * @param array $where
  52. * @return \crmeb\basic\BaseModel|int|mixed|\think\Model
  53. */
  54. public function getCount(array $where = [])
  55. {
  56. return $this->search($where)->count();
  57. }
  58. /**
  59. * 搜索
  60. * @param array $where
  61. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  62. */
  63. public function search(array $where = [])
  64. {
  65. return parent::search($where)
  66. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  67. $query->where('type', $where['type']);
  68. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  69. $query->where(function ($que) use ($where) {
  70. $que->whereLike('order_id', '%' . $where['keyword'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  71. $q->name('user')->whereLike('nickname|uid', '%' . $where['keyword'] . '%')->field(['uid'])->select();
  72. });
  73. });
  74. });
  75. }
  76. /**
  77. * 供应商账单
  78. * @param array $where
  79. * @param int $page
  80. * @param int $limit
  81. * @return array
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function getFundRecord(array $where = [], int $page = 0, int $limit = 0)
  87. {
  88. $model = parent::search($where)
  89. ->when(isset($where['timeType']) && $where['timeType'] !== '', function ($query) use ($where) {
  90. $timeUnix = '%Y-%m-%d';
  91. switch ($where['timeType']) {
  92. case "day" :
  93. $timeUnix = "%Y-%m-%d";
  94. break;
  95. case "week" :
  96. $timeUnix = "%Y-%u";
  97. break;
  98. case "month" :
  99. $timeUnix = "%Y-%m";
  100. break;
  101. }
  102. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,sum(if(pm = 1,number,0)) as income_num,sum(if(pm = 0,number,0)) as exp_num,add_time,group_concat(id) as ids");
  103. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  104. });
  105. $count = $model->count();
  106. $list = $model->when($page && $limit, function ($query) use ($page, $limit) {
  107. $query->page($page, $limit);
  108. })->order('add_time desc')->select()->toArray();
  109. return compact('list', 'count');
  110. }
  111. /**
  112. * 获取一段时间订单统计数量、金额
  113. * @param array $where
  114. * @param array $time
  115. * @param string $timeType
  116. * @param string $countField
  117. * @param string $sumField
  118. * @param string $groupField
  119. * @return array
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function orderAddTimeList(array $where, array $time, string $timeType = "week", string $countField = '*', string $sumField = 'number', string $groupField = 'add_time')
  125. {
  126. return parent::search($where)
  127. ->where(isset($where['timekey']) && $where['timekey'] ? $where['timekey'] : 'add_time', 'between time', $time)
  128. ->when($timeType, function ($query) use ($timeType, $countField, $sumField, $groupField) {
  129. switch ($timeType) {
  130. case "hour":
  131. $timeUnix = "%H";
  132. break;
  133. case "day" :
  134. $timeUnix = "%Y-%m-%d";
  135. break;
  136. case "week" :
  137. $timeUnix = "%Y-%w";
  138. break;
  139. case "month" :
  140. $timeUnix = "%Y-%d";
  141. break;
  142. case "weekly" :
  143. $timeUnix = "%W";
  144. break;
  145. case "year" :
  146. $timeUnix = "%Y-%m";
  147. break;
  148. default:
  149. $timeUnix = "%m-%d";
  150. break;
  151. }
  152. $query->field("FROM_UNIXTIME(`" . $groupField . "`,'$timeUnix') as day,count(" . $countField . ") as count,sum(`" . $sumField . "`) as price");
  153. $query->group('day');
  154. })->order('add_time asc')->select()->toArray();
  155. }
  156. }