Order.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\FundsRecord;
  5. use app\admin\model\User;
  6. use app\admin\model\Admin;
  7. use Exception;
  8. use app\admin\model\Order as OrderModel;
  9. use think\facade\Db;
  10. class Order extends BaseController
  11. {
  12. /**
  13. * 订单手动退款
  14. */
  15. public function adminRefund()
  16. {
  17. $errors = [];
  18. Db::startTrans();
  19. try {
  20. $params = $this->request->param();
  21. $admin = Admin::where('id', $this->admin_id)->findOrFail();
  22. if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误');
  23. $orderList = OrderModel::whereIn('id', $params['order_id'])->get();
  24. foreach ($orderList as $order) {
  25. if ($order->return_status != 0 || $order->pay_status != 1) {
  26. continue;
  27. }
  28. $order->status = 3;
  29. $order->return_status = 2;
  30. $order->return_operation_time = time();
  31. $order->save();
  32. $user = User::where('user_id', $order->user_id)->findOrFail();
  33. if (!$user) continue;
  34. if ($user->type == 1) {
  35. FundsRecord::addData([
  36. 'transaction_type' => 'return_order',
  37. 'amount_change' => $order->amount,
  38. 'balance_before' => $user->money,
  39. 'balance_after' => bcsub($user->money, $order->amount, 2),
  40. 'user_id' => $user->user_id,
  41. ]);
  42. $user->money = bcsub($user->money, $order->amount, 2);
  43. $user->save();
  44. }
  45. }
  46. DB::commit();
  47. } catch (Exception $e) {
  48. DB::rollBack();
  49. return $this->error($e->getMessage(), $errors);
  50. }
  51. return $this->success();
  52. }
  53. /**
  54. * @api {post} /order/refund 同意退款
  55. * @apiGroup 订单管理
  56. */
  57. public function refund()
  58. {
  59. $errors = [];
  60. Db::startTrans();
  61. try {
  62. $params = $this->request->param();
  63. $order_id = $params['order_id'];
  64. $safeWord = $params['safe_word'];
  65. $admin = Admin::where('id', $this->admin_id)->findOrFail();
  66. if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误');
  67. $order = OrderModel::where('id', $order_id)->findOrFail();
  68. if (!$order) throw new Exception('订单不存在');
  69. if ($order->return_status != 1 || $order->pay_status != 1) {
  70. $errors = ['id' => $order_id];
  71. throw new Exception("该订单状态无法操作");
  72. }
  73. $order->status = 3;
  74. $order->return_status = 2;
  75. $order->return_operation_time = time();
  76. $order->save();
  77. $user = User::where('user_id', $order->user_id)->findOrFail();
  78. $balanceAfter = bcadd($user->money, $order->amount, 2);
  79. FundsRecord::addData([
  80. 'transaction_type' => 'return_order',
  81. 'amount_change' => $order->amount,
  82. 'balance_before' => $user->money,
  83. 'balance_after' => $balanceAfter,
  84. 'user_id' => $user->user_id,
  85. ]);
  86. $user->money = $balanceAfter;
  87. $user->save();
  88. Db::commit();
  89. } catch (Exception $e) {
  90. Db::rollBack();
  91. return $this->error($e->getMessage(), $errors);
  92. }
  93. return $this->success();
  94. }
  95. /**
  96. * 驳回退款申请
  97. * @apiGroup 订单管理
  98. */
  99. public function rejection()
  100. {
  101. $errors = [];
  102. Db::startTrans();
  103. try {
  104. $params = $this->request->param();
  105. if (empty($params['failure_msg'])) $params['failure_msg'] = '';
  106. $order = OrderModel::where('id', $params['order_id'])->findOrFail();
  107. if (!$order) throw new Exception('订单不存在');
  108. if ($order->status != 1 || $order->return_status != 1) {
  109. $errors = ['id' => $params['order_id']];
  110. throw new Exception("该订单状态无法操作");
  111. }
  112. $order->return_operation_time = time();
  113. $order->failure_msg = $params['failure_msg'];
  114. $order->save();
  115. Db::commit();
  116. } catch (Exception $e) {
  117. Db::rollBack();
  118. return $this->error($e->getMessage(), $errors);
  119. }
  120. return $this->success();
  121. }
  122. /**
  123. * 订单列表
  124. */
  125. public function list()
  126. {
  127. try {
  128. $params = $this->request->param();
  129. $page = $this->request->param('page', 1);
  130. $limit = $this->request->param('limit', 15);
  131. $query = new OrderModel();
  132. if (!empty($params['start_time'])) {
  133. $startTime = strtotime($params['start_time'] . " 00:00:00");
  134. $query = $query->where('create_time', '>=', $startTime);
  135. }
  136. if (!empty($params['end_time'])) {
  137. $endTime = strtotime($params['end_time'] . " 23:59:59");
  138. $query = $query->where('create_time', '<=', $endTime);
  139. }
  140. if (!empty($params['id'])) {
  141. $query = $query->where('id', $params['id']);
  142. }
  143. if (!empty($params['order_id'])) {
  144. $query = $query->where('order_id', $params['order_id']);
  145. }
  146. if (!empty($params['user_id'])) {
  147. $query = $query->where('user_id', $params['user_id']);
  148. }
  149. if (isset($params['status'])) {
  150. $query = $query->where('status', $params['status']);
  151. }
  152. if (isset($params['return_status'])) {
  153. $query = $query->where('return_status', $params['return_status']);
  154. }
  155. if (isset($params['pay_status'])) {
  156. $query = $query->where('pay_status', $params['pay_status']);
  157. }
  158. if (isset($params['is_win'])) {
  159. $query = $query->where('is_win', $params['is_win']);
  160. }
  161. if (isset($params['settlement_status'])) {
  162. $query = $query->where('settlement_status', $params['settlement_status']);
  163. }
  164. if (isset($params['is_roll'])) {
  165. $query = $query->where('is_roll', $params['is_roll']);
  166. }
  167. $count = $query->count();
  168. $list = $query
  169. ->limit($limit)
  170. ->page($page)
  171. ->order('create_time', 'desc')
  172. ->select();
  173. } catch (Exception $e) {
  174. return $this->error($e->getMessage());
  175. }
  176. return $this->success(['count' => $count, 'list' => $list]);
  177. }
  178. //订单详情
  179. function info()
  180. {
  181. try {
  182. $order_id = $this->request->param('order_id');
  183. $order = OrderModel::where('order_id', $order_id)->findOrFail();
  184. if (!$order) throw new Exception('订单不存在');
  185. $order = $order->toArray();
  186. $order['detail'] = json_decode($order['detail'],true);
  187. $order['game_result'] = $order['game_result'] ? json_decode($order['game_result'],true) : null;
  188. ksort($order);
  189. } catch (Exception $e) {
  190. return $this->error($e->getMessage());
  191. }
  192. return $this->success($order);
  193. }
  194. }