StoreOrderRefundDao.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. declare (strict_types=1);
  12. namespace app\dao\order;
  13. use app\dao\BaseDao;
  14. use app\model\order\StoreOrderRefund;
  15. /**
  16. * 退款订单
  17. * Class StoreOrderInvoiceDao
  18. * @package app\dao\order
  19. */
  20. class StoreOrderRefundDao extends BaseDao
  21. {
  22. protected function setModel(): string
  23. {
  24. return StoreOrderRefund::class;
  25. }
  26. /**
  27. * 搜索器
  28. * @param array $where
  29. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  30. */
  31. public function search(array $where = [])
  32. {
  33. $realName = $where['real_name'] ?? '';
  34. $fieldKey = $where['field_key'] ?? '';
  35. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  36. return parent::search($where)->when(isset($where['refund_type']) && $where['refund_type'] !== '', function ($query) use ($where) {
  37. if ($where['refund_type'] == 0) {
  38. $query->where('refund_type', '>', 0);
  39. } else {
  40. if (is_array($where['refund_type'])) {
  41. $query->whereIn('refund_type', $where['refund_type']);
  42. } else {
  43. $query->where('refund_type', $where['refund_type']);
  44. }
  45. }
  46. })->when(isset($where['order_id']) && $where['order_id'] != '', function ($query) use ($where) {
  47. $query->where(function ($q) use ($where) {
  48. $q->whereLike('order_id', '%' . $where['order_id'] . '%')->whereOr('store_order_id', 'IN', function ($orderModel) use ($where) {
  49. $orderModel->name('store_order')->field('id')->whereLike('order_id|uid|user_phone|staff_id', '%' . $where['order_id'] . '%');
  50. });
  51. });
  52. })->when($realName && !$fieldKey, function ($query) use ($where) {
  53. $query->where(function ($que) use ($where) {
  54. $que->whereLike('order_id', '%' . $where['real_name'] . '%')->whereOr(function ($q) use ($where) {
  55. $q->whereOr('uid', 'in', function ($q) use ($where) {
  56. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  57. })->whereOr('store_order_id', 'in', function ($que) use ($where) {
  58. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  59. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  60. })->field(['oid'])->select();
  61. })->whereOr('store_order_id', 'in', function ($orderModel) use ($where) {
  62. $orderModel->name('store_order')->field('id')->whereLike('order_id', '%' . $where['real_name'] . '%');
  63. });
  64. });
  65. });
  66. })->when(isset($where['refundTypes']) && $where['refundTypes'] != '', function ($query) use ($where) {
  67. switch ((int)$where['refundTypes']) {
  68. case 1:
  69. $query->where('refund_type', 'in', '1,2');
  70. break;
  71. case 2:
  72. $query->where('refund_type', 4);
  73. break;
  74. case 3:
  75. $query->where('refund_type', 5);
  76. break;
  77. case 4:
  78. $query->where('refund_type', 6);
  79. break;
  80. }
  81. })->when(isset($where['supplier']) && $where['supplier'] !== '', function ($query) use ($where) {
  82. $query->where('supplier_id', '>', 0);
  83. });
  84. }
  85. /**
  86. * 查询退款订单
  87. * @param $where
  88. * @param int $page
  89. * @param int $limit
  90. * @return array
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function getRefundList($where, string $file = '*', array $with = [], int $page = 0, int $limit = 0)
  96. {
  97. return $this->search($where)->field($file)->with(array_merge(['user'], $with))
  98. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  99. $query->page($page, $limit);
  100. })->order('add_time desc')->select()->toArray();
  101. }
  102. /**
  103. * 获取退款金额按照时间分组
  104. * @param array $time
  105. * @param string $timeType
  106. * @param string $field
  107. * @param string $str
  108. * @return mixed
  109. */
  110. public function getUserRefundPriceList(array $time, string $timeType, string $str, string $field = 'add_time')
  111. {
  112. return $this->getModel()->where('is_cancel', 0)->where(function ($query) use ($time, $field) {
  113. if ($time[0] == $time[1]) {
  114. $query->whereDay($field, $time[0]);
  115. } else {
  116. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  117. $query->whereTime($field, 'between', $time);
  118. }
  119. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num,GROUP_CONCAT(store_order_id) as link_ids")->group('days')->select()->toArray();
  120. }
  121. /**
  122. * 根据时间获取
  123. * @param array $where
  124. * @return float|int
  125. */
  126. public function getOrderRefundMoneyByWhere(array $where, string $sum_field, string $selectType, string $group = "")
  127. {
  128. switch ($selectType) {
  129. case "sum" :
  130. return $this->getDayTotalMoney($where, $sum_field);
  131. case "group" :
  132. return $this->getDayGroupMoney($where, $sum_field, $group);
  133. }
  134. }
  135. /**
  136. * 按照支付时间统计支付金额
  137. * @param array $where
  138. * @param string $sumField
  139. * @return mixed
  140. */
  141. public function getDayTotalMoney(array $where, string $sumField)
  142. {
  143. return $this->search($where)
  144. ->when(isset($where['timeKey']), function ($query) use ($where) {
  145. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  146. })
  147. ->sum($sumField);
  148. }
  149. /**
  150. * 时间分组订单付款金额统计
  151. * @param array $where
  152. * @param string $sumField
  153. * @return mixed
  154. */
  155. public function getDayGroupMoney(array $where, string $sumField, string $group)
  156. {
  157. return $this->search($where)
  158. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  159. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  160. if ($where['timeKey']['days'] == 1) {
  161. $timeUinx = "%H";
  162. } elseif ($where['timeKey']['days'] == 30) {
  163. $timeUinx = "%Y-%m-%d";
  164. } elseif ($where['timeKey']['days'] == 365) {
  165. $timeUinx = "%Y-%m";
  166. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  167. $timeUinx = "%Y-%m-%d";
  168. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  169. $timeUinx = "%Y-%m";
  170. } else {
  171. $timeUinx = "%Y-%m";
  172. }
  173. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  174. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  175. })
  176. ->order('add_time ASC')->select()->toArray();
  177. }
  178. }