Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\api\model\system\Message;
  4. use app\api\service\ThirdPayService;
  5. use app\api\service\WalletService;
  6. use app\common\controller\Backend;
  7. use redis\RedLock;
  8. use think\Db;
  9. use think\Exception;
  10. /**
  11. * 订单管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Order extends Backend
  16. {
  17. /**
  18. * Order模型对象
  19. * @var \app\admin\model\order\Order
  20. */
  21. protected $model = null;
  22. protected $noNeedRight = ["*"];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\order\Order;
  27. $this->view->assign("tripTypeList", $this->model->getTripTypeList());
  28. $this->view->assign("paymentTypeList", $this->model->getPaymentTypeList());
  29. $this->view->assign("statusList", $this->model->getStatusList());
  30. }
  31. private function fetchWhere()
  32. {
  33. $admin = $this->auth->getUserInfo();
  34. if (!$admin)
  35. $this->error("error");
  36. $p_where = [];
  37. if (\E_ADMIN_TYPE::Store === $admin["type"]) {
  38. array_push($p_where, ["order.store_id", "=", $admin["store_id"]]);
  39. }
  40. if (\E_ADMIN_TYPE::Agency === $admin["type"]) {
  41. array_push($p_where, ["order.city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]);
  42. }
  43. return $p_where;
  44. }
  45. /**
  46. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  47. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  48. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  49. */
  50. /**
  51. * 查看
  52. */
  53. public function index()
  54. {
  55. //当前是否为关联查询
  56. $this->relationSearch = true;
  57. //设置过滤方法
  58. $this->request->filter(['strip_tags', 'trim']);
  59. $c_where = $this->fetchWhere();
  60. if ($this->request->isAjax()) {
  61. //如果发送的来源是Selectpage,则转发到Selectpage
  62. if ($this->request->request('keyField')) {
  63. return $this->c_selectpage($c_where);
  64. }
  65. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  66. $query = $this->model
  67. ->with(['user', 'store', 'massager'])
  68. ->where($where);
  69. foreach ($c_where as $item) {
  70. $query->where($item[0], $item[1], $item[2]);
  71. }
  72. $list = $query
  73. ->order($sort, $order)
  74. ->paginate($limit);
  75. foreach ($list as $row) {
  76. $row->visible([
  77. 'id', 'no', "reorder", 'store_id',
  78. 'reason', 'total_amount', 'total_real_amount', 'total_service_amount',
  79. 'trip_type', 'trip_amount', 'voucher_amount', 'distance', 'payment_type',
  80. 'status', 'massager_address', 'user_address', 'service_start_date',
  81. 'service_end_date', 'pay_time', 'cancel_time', 'parent_id', 'is_appeal',
  82. 'appeal_reason', 'createtime', 'updatetime', 'balance_deduction',
  83. 'is_refund_trip', 'refund_amount'
  84. ]);
  85. $row->visible(['user']);
  86. $row->getRelation('user')->visible(['nickname', 'mobile']);
  87. $row->visible(['store']);
  88. $row->getRelation('store')->visible(['name']);
  89. $row->visible(['massager']);
  90. $row->getRelation('massager')->visible(['name']);
  91. }
  92. $result = array("total" => $list->total(), "rows" => $list->items());
  93. return json($result);
  94. }
  95. return $this->view->fetch();
  96. }
  97. public function serviceDetail($id = null)
  98. {
  99. $this->assign("rows", (new \app\admin\model\order\Service())->where([
  100. "order_id" => $id,
  101. "status" => \E_BASE_STATUS::Normal
  102. ])->select());
  103. return $this->view->fetch();
  104. }
  105. public function progressDetail($id = null)
  106. {
  107. $this->assign("rows", (new \app\admin\model\order\Progress())->where("order_id", $id)->select());
  108. return $this->view->fetch();
  109. }
  110. // 处理订单
  111. public function recharge($ids = null)
  112. {
  113. if (!$ids)
  114. $this->error("Id不存在!");
  115. $order = $this->model->get($ids);
  116. if (is_null($order))
  117. $this->error("订单不存在!");
  118. if ($this->request->isPost()) {
  119. $params = $this->request->param();
  120. $back_trip = $params["row"]["back_trip"];
  121. $refund_amount = $params["row"]["refund"];
  122. $refund_trip_amount = 0;
  123. if (1 == $back_trip)
  124. $refund_trip_amount = $order["trip_amount"];
  125. if (!(($refund_amount + $refund_amount) > 0)) {
  126. $this->error("退款必须大于0");
  127. }
  128. $max_refund_amount = $order["total_real_amount"] - $order["trip_amount"] - $order["balance_deduction"];
  129. if ($refund_amount > $max_refund_amount) {
  130. $this->error("最多可退{$max_refund_amount}");
  131. }
  132. $orderLock = new RedLock();
  133. $oLock = $orderLock->lock(\app\api\model\order\Order::OKey($ids));
  134. if (!is_array($oLock))
  135. $this->error("请稍后再试");
  136. try {
  137. $thirdPayService = new ThirdPayService();
  138. \db("cancel_order_log")->insert([
  139. "order_no" => $order["no"],
  140. "order_status_enum" => \E_ORDER_STATUS::AdminCancel,
  141. "createtime" => time()
  142. ]);
  143. switch ($order["payment_type"]) {
  144. case \E_ORDER_PAY_TYPE::Wechat:
  145. $res = $thirdPayService->refundByWx(
  146. $order["pay_platform"],
  147. $order["no"],
  148. "平台处理订单退款",
  149. $order["total_real_amount"] + $order["membership_amount"],
  150. $refund_amount + $refund_trip_amount
  151. );
  152. break;
  153. case \E_ORDER_PAY_TYPE::ALi:
  154. $res = $thirdPayService->refundByAli($order);
  155. break;
  156. default:
  157. $res = (new WalletService())->refundByBalance($order, \E_ORDER_STATUS::AdminCancel);
  158. }
  159. if (0 == $res->code())
  160. throw new Exception($res->msg());
  161. $this->model->update([
  162. "refund_amount" => $refund_amount + $refund_trip_amount,
  163. "is_refund_trip" => $back_trip
  164. ], ["id" => $ids]);
  165. Message::sendSystemMessages([
  166. [
  167. "identity_type" => \E_IDENTITY_TYPE::Massager,
  168. "to_massager_id" => $order->massager_id,
  169. "title" => "管理员处理订单",
  170. "content" => "您的订单【{$order->no}】已被管理员处理!",
  171. "is_read" => 0,
  172. "createtime" => time(),
  173. "updatetime" => time()
  174. ],
  175. [
  176. "identity_type" => \E_IDENTITY_TYPE::User,
  177. "to_user_id" => $order->user_id,
  178. "title" => "管理员处理订单",
  179. "content" => "您的订单【{$order->no}】已被管理员处理!",
  180. "is_read" => 0,
  181. "createtime" => time(),
  182. "updatetime" => time()
  183. ]
  184. ]);
  185. } catch (Exception $e) {
  186. $this->error($e->getMessage());
  187. } finally {
  188. $orderLock->unlock($oLock);
  189. }
  190. $this->success("处理成功");
  191. }
  192. $this->view->assign("row", $order);
  193. $this->view->assign("max_refund_amount", $order["total_real_amount"] - $order["trip_amount"] - $order["balance_deduction"]);
  194. return $this->view->fetch();
  195. }
  196. // 开始球房服务
  197. public function startStoreOrder($ids = null)
  198. {
  199. if (!$ids)
  200. $this->error("Id不存在!");
  201. $orderLock = new RedLock();
  202. $oLock = $orderLock->lock(\app\api\model\order\Order::OKey($ids));
  203. if (!is_array($oLock))
  204. $this->error("请稍后再试");
  205. $order = $this->model->get($ids);
  206. if (!$order)
  207. $this->error("订单不存在!");
  208. if ($order["status"] != \E_ORDER_STATUS::Purchase)
  209. $this->error("订单状态异常,无法核销订单");
  210. $progressModel = new \app\admin\model\order\Progress();
  211. Db::startTrans();
  212. try {
  213. $this->model->update([
  214. "service_start_date" => date("Y-m-d H:i:s"),
  215. "status" => \E_ORDER_STATUS::Proceed,
  216. "updatetime" => time()
  217. ], ["id" => $order->id]);
  218. $progressModel->update(
  219. [
  220. "clock_in" => 1,
  221. "clock_in_time" => time(),
  222. ],
  223. [
  224. "order_id" => $order["id"],
  225. "index" => 1,
  226. ]
  227. );
  228. Db::commit();
  229. $this->success();
  230. } catch (Exception $e) {
  231. Db::rollback();
  232. $this->error($e->getMessage());
  233. } finally {
  234. $orderLock->unlock($oLock);
  235. }
  236. $this->success();
  237. }
  238. // 结束球房服务
  239. public function overStoreOrder($ids = null)
  240. {
  241. if (null === $ids)
  242. $this->error("Id不存在!");
  243. $orderLock = new RedLock();
  244. $oLock = $orderLock->lock(\app\api\model\order\Order::OKey($ids));
  245. if (!is_array($oLock))
  246. $this->error("请稍后再试");
  247. $order = $this->model->get($ids);
  248. if (!$order)
  249. $this->error("订单不存在!");
  250. if ($order["status"] != \E_ORDER_STATUS::Proceed)
  251. $this->error("订单状态异常,无法核销订单");
  252. $progressModel = new \app\admin\model\order\Progress();
  253. Db::startTrans();
  254. try {
  255. $this->model->update([
  256. "service_end_date" => date("Y-m-d H:i:s"),
  257. "status" => \E_ORDER_STATUS::WaitFeedback,
  258. "updatetime" => time()
  259. ], ["id" => $order->id]);
  260. $progressModel->update(
  261. [
  262. "clock_in" => 1,
  263. "clock_in_time" => time(),
  264. ],
  265. [
  266. "order_id" => $order["id"],
  267. "index" => 2,
  268. ]
  269. );
  270. Db::commit();
  271. $this->success();
  272. } catch (Exception $e) {
  273. Db::rollback();
  274. $this->error($e->getMessage());
  275. } finally {
  276. $orderLock->unlock($oLock);
  277. }
  278. $this->success();
  279. }
  280. }