StoreRefundOrder.php 7.5 KB

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