RefundController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\validate\order\ReturnValidate;
  4. use app\controller\Curd;
  5. use app\model\GoodsComponent;
  6. use app\model\Order;
  7. use app\model\OrderExpress;
  8. use app\model\OrderReturn;
  9. use app\model\OrderSheet;
  10. use support\exception\BusinessException;
  11. use support\Request;
  12. use support\Response;
  13. class RefundController extends Curd
  14. {
  15. public function __construct()
  16. {
  17. $this->model = new OrderReturn();
  18. $this->validate = true;
  19. $this->validateClass=new ReturnValidate();
  20. }
  21. public function select(Request $request): Response
  22. {
  23. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  24. $order = $request->get('order', 'desc');
  25. $field = $field ?? 'order_return_addtimes';
  26. $where['order_return_category'] = '退款';
  27. $query = $this->doSelect($where, $field, $order);
  28. return $this->doFormat($query, $format, $limit);
  29. }
  30. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  31. {
  32. $model = $this->model->with([
  33. 'member' => function ($query) {
  34. $query->select('member_id', 'member_mobile');
  35. },
  36. 'cert'=>function($query){
  37. $query->select('join_cert_member_id','member_cert_name');
  38. },
  39. 'order' => function ($query) {
  40. $query->select('order_id', 'order_name');
  41. }
  42. ]);
  43. foreach ($where as $column => $value) {
  44. if (is_array($value)) {
  45. if ($value[0] === 'like' || $value[0] === 'not like') {
  46. $model = $model->where($column, $value[0], "%$value[1]%");
  47. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  48. $model = $model->where($column, $value[0], $value[1]);
  49. } elseif ($value[0] == 'in' && !empty($value[1])) {
  50. $valArr = $value[1];
  51. if (is_string($value[1])) {
  52. $valArr = explode(",", trim($value[1]));
  53. }
  54. $model = $model->whereIn($column, $valArr);
  55. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  56. $valArr = $value[1];
  57. if (is_string($value[1])) {
  58. $valArr = explode(",", trim($value[1]));
  59. }
  60. $model = $model->whereNotIn($column, $valArr);
  61. } elseif ($value[0] == 'null') {
  62. $model = $model->whereNull($column);
  63. } elseif ($value[0] == 'not null') {
  64. $model = $model->whereNotNull($column);
  65. } elseif ($value[0] !== '' || $value[1] !== '') {
  66. $model = $model->whereBetween($column, $value);
  67. }
  68. } else {
  69. $model = $model->where($column, $value);
  70. }
  71. }
  72. if ($field) {
  73. $model = $model->orderBy($field, $order);
  74. }
  75. return $model;
  76. }
  77. public function afterQuery($items)
  78. {
  79. foreach ($items as &$item){
  80. if (!empty($item['order_return_apply_json']) && is_json($item['order_return_apply_json'])){
  81. $json = json_decode($item['order_return_apply_json'],true);
  82. $item['order_return_apply_json'] = $json['apply'] ?? '';
  83. }
  84. if (!empty($item['order_return_recharge_json']) && is_json($item['order_return_recharge_json'])){
  85. $json = json_decode($item['order_return_recharge_json'],true);
  86. $item['order_return_recharge_json'] = $json['change'] ?? '';
  87. }
  88. }
  89. return $items;
  90. }
  91. protected function updateInput(Request $request): array
  92. {
  93. $primary_key = $this->model->getKeyName();
  94. $id = $request->post($primary_key);
  95. $data = $this->inputFilter($request->post());
  96. $model = $this->model->find($id);
  97. if (!$model) {
  98. throw new BusinessException('记录不存在', 2);
  99. }
  100. if (empty($model->order_return_accept_datetimes) && $model->order_return_status == 'PENDING' && $data['order_return_status'] == 'DOING'){
  101. $data['order_return_accept_datetimes'] = date('Y-m-d H:i:s');
  102. }
  103. if (!empty($data['order_return_apply_json'])){
  104. $data['order_return_apply_json'] = json_encode(['apply'=>$data['order_return_apply_json']]);
  105. }
  106. if (!empty($data['order_return_recharge_json'])){
  107. $data['order_return_recharge_json'] = json_encode(['change'=>$data['order_return_recharge_json']]);
  108. }
  109. unset($data[$primary_key]);
  110. return [$id, $data];
  111. }
  112. /**
  113. * @Desc 订单商品详情
  114. * @Author Gorden
  115. * @Date 2024/3/29 8:50
  116. *
  117. * @param Request $request
  118. * @return Response
  119. */
  120. public function sheet(Request $request)
  121. {
  122. $orderId = $request->get('order_id');
  123. $orderSheet = OrderSheet::with([
  124. 'member' => function ($query) {
  125. $query->select('member_id', 'member_mobile');
  126. },
  127. 'goods' => function ($query) {
  128. $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify');
  129. },
  130. 'memberInfo',
  131. 'cert',
  132. 'refund'
  133. ])->where('join_sheet_order_id', $orderId)
  134. ->get()
  135. ->toArray();
  136. foreach ($orderSheet as &$item) {
  137. $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
  138. if (!empty($item['goods']) && $item['goods']['goods_classify'] == 'PACKAGE') {
  139. $components = GoodsComponent::with('goods')
  140. ->where('join_component_master_goods_id', $item['goods']['goods_id'])
  141. ->select('join_component_master_goods_id', 'join_component_goods_id', 'goods_component_price',
  142. 'goods_component_price', 'goods_component_config_json')
  143. ->get()
  144. ->toArray();
  145. $goodsArr = [];
  146. foreach ($components as $component) {
  147. $configJson = !empty($component['goods_component_config_json']) ? json_decode($component['goods_component_config_json'], true) : [];
  148. if (!empty($component['goods'])) {
  149. $goodsArr[] = [
  150. 'goods_name' => $component['goods']['goods_name'],
  151. 'goods_cover' => getenv('STORAGE_DOMAIN') . $component['goods']['goods_cover'],
  152. 'nbr' => $configJson['nbr'] ?? 0,
  153. ];
  154. }
  155. }
  156. $item['goods']['components'] = $goodsArr;
  157. }
  158. if (!empty($item['refund'])){
  159. if (!empty($item['refund']['order_return_apply_json']) && is_json($item['refund']['order_return_apply_json'])){
  160. $json = json_decode($item['refund']['order_return_apply_json'],true);
  161. $item['refund']['order_return_apply_json'] = $json['reason'] ?? '';
  162. }
  163. if (!empty($item['refund']['order_return_recharge_json']) && is_json($item['refund']['order_return_recharge_json'])){
  164. $json = json_decode($item['refund']['order_return_recharge_json'],true);
  165. $item['refund']['order_return_recharge_json'] = $json['change'] ?? '';
  166. }
  167. }
  168. }
  169. $order = Order::where('order_id', $orderId)->first();
  170. $express = OrderExpress::where('join_express_order_id', $orderId)->first();
  171. $data = [
  172. 'order' => $order,
  173. 'sheet' => $orderSheet,
  174. 'express' => $express
  175. ];
  176. return json_success('', $data);
  177. }
  178. }