StoreFinanceFlowDao.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\store\finance;
  12. use app\dao\BaseDao;
  13. use app\model\store\finance\StoreFinanceFlow;
  14. /**
  15. * 门店流水
  16. * Class StoreExtractDao
  17. * @package app\dao\store\finance
  18. */
  19. class StoreFinanceFlowDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return StoreFinanceFlow::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('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['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  67. $query->where(function ($que) use ($where) {
  68. $que->whereLike('order_id', '%' . $where['keyword'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  69. $q->name('user')->whereLike('nickname|uid', '%' . $where['keyword'] . '%')->field(['uid'])->select();
  70. });
  71. });
  72. });
  73. }
  74. /**
  75. * 门店账单
  76. * @param array $where
  77. * @param int $page
  78. * @param int $limit
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function getFundRecord(array $where = [], int $page = 0, int $limit = 0)
  85. {
  86. $model = parent::search($where)
  87. ->when(isset($where['timeType']) && $where['timeType'] !== '', function ($query) use ($where) {
  88. $timeUnix = '%Y-%m-%d';
  89. switch ($where['timeType']) {
  90. case "day" :
  91. $timeUnix = "%Y-%m-%d";
  92. break;
  93. case "week" :
  94. $timeUnix = "%Y-%u";
  95. break;
  96. case "month" :
  97. $timeUnix = "%Y-%m";
  98. break;
  99. }
  100. $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");
  101. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  102. });
  103. $count = $model->count();
  104. $list = $model->when($page && $limit, function ($query) use ($page, $limit) {
  105. $query->page($page, $limit);
  106. })->order('add_time desc')->select()->toArray();
  107. return compact('list', 'count');
  108. }
  109. /**
  110. * 店员交易统计头部数据
  111. * @param array $where
  112. * @param string $group
  113. * @param string $field
  114. * @return mixed
  115. */
  116. public function getStatisticsHeader(array $where = [], $group = 'staff_id', string $field = 'number')
  117. {
  118. return parent::search($where)->with(['systemStoreStaff'])
  119. ->field("*,sum(`" . $field . "`) as total_number,count(*) as order_count")
  120. ->group($group)
  121. ->order('total_number desc')
  122. ->select()->toArray();
  123. }
  124. /**
  125. * 获取一段时间订单统计数量、金额
  126. * @param array $where
  127. * @param array $time
  128. * @param string $timeType
  129. * @param string $countField
  130. * @param string $sumField
  131. * @param string $groupField
  132. * @return array
  133. * @throws \think\db\exception\DataNotFoundException
  134. * @throws \think\db\exception\DbException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. */
  137. public function orderAddTimeList(array $where, array $time, string $timeType = "week", string $countField = '*', string $sumField = 'number', string $groupField = 'add_time')
  138. {
  139. return parent::search($where)
  140. ->where(isset($where['timekey']) && $where['timekey'] ? $where['timekey'] : 'add_time', 'between time', $time)
  141. ->when($timeType, function ($query) use ($timeType, $countField, $sumField, $groupField) {
  142. switch ($timeType) {
  143. case "hour":
  144. $timeUnix = "%H";
  145. break;
  146. case "day" :
  147. $timeUnix = "%Y-%m-%d";
  148. break;
  149. case "week" :
  150. $timeUnix = "%Y-%w";
  151. break;
  152. case "month" :
  153. $timeUnix = "%Y-%d";
  154. break;
  155. case "weekly" :
  156. $timeUnix = "%W";
  157. break;
  158. case "year" :
  159. $timeUnix = "%Y-%m";
  160. break;
  161. default:
  162. $timeUnix = "%m-%d";
  163. break;
  164. }
  165. $query->field("FROM_UNIXTIME(`" . $groupField . "`,'$timeUnix') as day,count(" . $countField . ") as count,sum(`" . $sumField . "`) as price");
  166. $query->group('day');
  167. })->order('add_time asc')->select()->toArray();
  168. }
  169. }