StoreOrderRefundDao.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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\order;
  12. use app\dao\BaseDao;
  13. use app\model\order\StoreOrderRefund;
  14. class StoreOrderRefundDao extends BaseDao
  15. {
  16. /**
  17. * 设置模型
  18. * @return string
  19. */
  20. protected function setModel(): string
  21. {
  22. return StoreOrderRefund::class;
  23. }
  24. /**
  25. * 搜索器
  26. * @param array $where
  27. * @param bool $search
  28. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  29. * @throws \ReflectionException
  30. */
  31. public function search(array $where = [], bool $search = false)
  32. {
  33. $realName = $where['real_name'] ?? '';
  34. $fieldKey = $where['field_key'] ?? '';
  35. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  36. return parent::search($where, $search)->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->where('order_id|refund_express', 'like', '%' . $where['order_id'] . '%')->whereOr('store_order_id', 'IN', function ($orderModel) use ($where) {
  49. $orderModel->name('store_order')->field('id')->whereLike('order_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|user_phone', '%' . $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. });
  82. }
  83. /**
  84. * 退款订单列表
  85. * @param $where
  86. * @param int $page
  87. * @param int $limit
  88. * @param string $field
  89. * @param array $with
  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 getList($where, $page = 0, $limit = 0, $field = '*', $with = [])
  96. {
  97. return $this->search($where)->field($field)->with(array_merge(['user'], $with))->when($page && $limit, function ($query) use ($page, $limit) {
  98. $query->page($page, $limit);
  99. })->order('id DESC')->select()->toArray();
  100. }
  101. /**
  102. * 退款订单数量
  103. * @param array $where
  104. * @param bool $search
  105. * @return int
  106. * @throws \ReflectionException
  107. * @author 吴汐
  108. * @email 442384644@qq.com
  109. * @date 2023/06/19
  110. */
  111. public function count(array $where = [], bool $search = false)
  112. {
  113. return $this->search($where, $search)->count();
  114. }
  115. /**
  116. * 根据时间获取
  117. * @param array $where
  118. * @param string $sum_field
  119. * @param string $selectType
  120. * @param string $group
  121. * @return float|int
  122. */
  123. public function getOrderRefundMoneyByWhere(array $where, string $sum_field, string $selectType, string $group = "")
  124. {
  125. switch ($selectType) {
  126. case "sum" :
  127. return $this->getDayTotalMoney($where, $sum_field);
  128. case "group" :
  129. return $this->getDayGroupMoney($where, $sum_field, $group);
  130. }
  131. }
  132. /**
  133. * 按照支付时间统计支付金额
  134. * @param array $where
  135. * @param string $sumField
  136. * @return mixed
  137. */
  138. public function getDayTotalMoney(array $where, string $sumField)
  139. {
  140. return $this->search($where)
  141. ->when(isset($where['timeKey']), function ($query) use ($where) {
  142. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  143. })
  144. ->sum($sumField);
  145. }
  146. /**
  147. * 时间分组订单付款金额统计
  148. * @param array $where
  149. * @param string $sumField
  150. * @return mixed
  151. */
  152. public function getDayGroupMoney(array $where, string $sumField, string $group)
  153. {
  154. return $this->search($where)
  155. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  156. $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  157. if ($where['timeKey']['days'] == 1) {
  158. $timeUinx = "%H";
  159. } elseif ($where['timeKey']['days'] == 30) {
  160. $timeUinx = "%Y-%m-%d";
  161. } elseif ($where['timeKey']['days'] == 365) {
  162. $timeUinx = "%Y-%m";
  163. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  164. $timeUinx = "%Y-%m-%d";
  165. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  166. $timeUinx = "%Y-%m";
  167. }
  168. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  169. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  170. })
  171. ->order('add_time ASC')->select()->toArray();
  172. }
  173. /**
  174. * @param $time
  175. * @param $timeType
  176. * @param $field
  177. * @param $str
  178. * @return mixed
  179. * @author 吴汐
  180. * @email 442384644@qq.com
  181. * @date 2023/03/06
  182. */
  183. public function getProductTrend($time, $timeType, $field, $str)
  184. {
  185. return $this->getModel()->where(function ($query) use ($time, $field) {
  186. if ($time[0] == $time[1]) {
  187. $query->whereDay($field, $time[0]);
  188. } else {
  189. $query->whereTime($field, 'between', $time);
  190. }
  191. })->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  192. }
  193. public function orderIsRefund($store_order_id)
  194. {
  195. return boolval($this->getModel()->where('store_order_id', $store_order_id)->whereIn('refund_type', [1, 2, 4, 5])->where('is_cancel', 0)->count());
  196. }
  197. }