StoreRefundOrder.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\controller\api\store\order;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\store\order\StoreOrderProductRepository;
  5. use app\common\repositories\store\order\StoreOrderRepository;
  6. use app\common\repositories\store\order\StoreRefundOrderRepository;
  7. use app\common\repositories\store\shipping\ExpressRepository;
  8. use app\validate\api\BackGoodsValidate;
  9. use app\validate\api\StoreRefundOrderValidate;
  10. use ln\services\SwooleTaskService;
  11. use think\App;
  12. use function GuzzleHttp\Psr7\str;
  13. /**
  14. * Class StoreRefundOrder
  15. * @package app\controller\api\store\order
  16. * @author zfy
  17. * @day 2020/6/12
  18. */
  19. class StoreRefundOrder extends BaseController
  20. {
  21. /**
  22. * @var StoreRefundOrderRepository
  23. */
  24. protected $repository;
  25. /**
  26. * StoreRefundOrder constructor.
  27. * @param App $app
  28. * @param StoreRefundOrderRepository $repository
  29. */
  30. public function __construct(App $app, StoreRefundOrderRepository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * @param $id
  37. * @param StoreOrderRepository $orderRepository
  38. * @return mixed
  39. * @author zfy
  40. * @day 2020/6/12
  41. */
  42. public function batchProduct($id, StoreOrderRepository $orderRepository)
  43. {
  44. return app('json')->success($orderRepository->refundProduct($id, $this->request->uid()));
  45. }
  46. /**
  47. * @param $id
  48. * @param StoreOrderProductRepository $orderProductRepository
  49. * @param StoreOrderRepository $storeOrderRepository
  50. * @return \think\response\Json
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @author zfy
  55. * @day 2020/9/2
  56. */
  57. public function product($id, StoreOrderProductRepository $orderProductRepository, StoreOrderRepository $storeOrderRepository)
  58. {
  59. $_id = (string)$this->request->param('ids', '');
  60. $ids = explode(',', $_id);
  61. if (!$_id || !count($ids))
  62. return app('json')->fail('请选择退款商品');
  63. $uid = $this->request->uid();
  64. $order = $storeOrderRepository->userOrder(intval($id), $uid);
  65. if (!$order)
  66. return app('json')->fail('订单状态有误');
  67. if (!$order->refund_status)
  68. return app('json')->fail('订单已过退款/退货期限');
  69. if ($order->status < 0) return app('json')->fail('订单已退款');
  70. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  71. $product = $orderProductRepository->userRefundProducts($ids, $uid, intval($id));
  72. if (!$product)
  73. return app('json')->fail('商品不存在或已退款');
  74. if (count($product) != count($ids))
  75. return app('json')->fail('请选择正确的退款商品');
  76. $total_refund_price = $this->repository->getRefundTotalPrice($product);
  77. $postage_price = !$order->status ? floatval($order->pay_postage) : 0;
  78. $status = $order->status;
  79. $activity_type = $order->activity_type;
  80. return app('json')->success(compact('activity_type', 'total_refund_price', 'product', 'postage_price', 'status'));
  81. }
  82. /**
  83. * @param $id
  84. * @param StoreRefundOrderValidate $validate
  85. * @param StoreOrderRepository $orderRepository
  86. * @return mixed
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @author zfy
  91. * @day 2020/6/12
  92. */
  93. public function refund($id, StoreRefundOrderValidate $validate, StoreOrderRepository $orderRepository)
  94. {
  95. $data = $this->request->params(['type', 'refund_type', 'num', 'ids', 'refund_message', 'mark', 'refund_price', 'pics']);
  96. $validate->check($data);
  97. $ids = explode(',', $data['ids']);
  98. $type = $data['type'];
  99. $num = $data['num'];
  100. unset($data['num'], $data['ids'], $data['type']);
  101. if ($type == 1 && count($ids) > 1)
  102. return app('json')->fail('请选择正确的退款商品');
  103. $uid = $this->request->uid();
  104. $order = $orderRepository->userOrder($id, $uid);
  105. if (!$order) return app('json')->fail('订单状态错误');
  106. if (!$order->refund_status)
  107. return app('json')->fail('订单已过退款/退货期限');
  108. if ($order->status < 0) return app('json')->fail('订单已退款');
  109. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  110. if ($type == 1) {
  111. $refund = $this->repository->refund($order, (int)$ids[0], $num, $uid, $data);
  112. } else {
  113. $refund = $this->repository->refunds($order, $ids, $uid, $data);
  114. }
  115. return app('json')->success('申请退款成功', ['refund_order_id' => $refund->refund_order_id]);
  116. }
  117. /**
  118. * @return mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @author zfy
  123. * @day 2020/6/12
  124. */
  125. public function lst()
  126. {
  127. $type = $this->request->param('type');
  128. [$page, $limit] = $this->getPage();
  129. return app('json')->success($this->repository->userList([
  130. 'type' => $type,
  131. 'uid' => $this->request->uid(),
  132. 'is_del' => 0,
  133. ], $page, $limit));
  134. }
  135. /**
  136. * @param $id
  137. * @return mixed
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. * @author zfy
  142. * @day 2020/6/12
  143. */
  144. public function detail($id)
  145. {
  146. $refund = $this->repository->userDetail(intval($id), $this->request->uid());
  147. if (!$refund)
  148. return app('json')->fail('退款单不存在');
  149. return app('json')->success($refund->toArray());
  150. }
  151. /**
  152. * @param $id
  153. * @return mixed
  154. * @throws \think\db\exception\DbException
  155. * @author zfy
  156. * @day 2020/6/12
  157. */
  158. public function del($id)
  159. {
  160. $this->repository->userDel(intval($id), $this->request->uid());
  161. return app('json')->success('删除成功');
  162. }
  163. public function back_goods($id, BackGoodsValidate $validate, ExpressRepository $expressRepository)
  164. {
  165. $data = $this->request->params(['delivery_type', 'delivery_id', 'delivery_phone', 'delivery_mark', 'delivery_pics']);
  166. $validate->check($data);
  167. if (!$expressRepository->merFieldExists('name', $data['delivery_type'], null, null, true))
  168. return app('json')->fail('不支持该快递公司');
  169. $this->repository->backGoods($this->request->uid(), $id, $data);
  170. return app('json')->success('提交成功');
  171. }
  172. public function express($id)
  173. {
  174. if (!$refund = $this->repository->getWhere(['status' => 2, 'refund_order_id' => $id, 'uid' => $this->request->uid()]))
  175. return app('json')->fail('退款单不存在');
  176. $express = $this->repository->express($id);
  177. return app('json')->success(compact('refund', 'express'));
  178. }
  179. }