StoreOrderRefundDao.php 7.4 KB

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