LhcOrder.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\LhcOrder as LhcOrderModel;
  5. use App\Models\FundsRecord;
  6. use App\Models\Wallet;
  7. use App\Models\LhcLottery;
  8. use App\Models\PcIssue;
  9. use App\Models\Issue;
  10. use App\Models\JisuLottery;
  11. use Exception;
  12. use App\Constants\HttpStatus;
  13. use Illuminate\Support\Facades\DB;
  14. class LhcOrder extends Controller
  15. {
  16. /**
  17. * 订单列表
  18. */
  19. public function list()
  20. {
  21. try {
  22. $params = request()->validate([
  23. 'page' => ['nullable', 'integer', 'min:1'],
  24. 'limit' => ['nullable', 'integer', 'min:1'],
  25. 'issue' => ['nullable', 'string'],
  26. 'ordernum' => ['nullable', 'string'],
  27. 'member_id' => ['nullable', 'integer'],
  28. 'first_name' => ['nullable'],
  29. 'game' => ['nullable', 'string'],
  30. 'gameplay' => ['nullable', 'string'],
  31. 'number' => ['nullable'],
  32. 'lottery_status' => ['nullable', 'integer'],
  33. 'is_faker' => ['nullable', 'integer'],
  34. 'id' => ['nullable', 'integer'],
  35. 'type' => ['nullable', 'integer'],
  36. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  37. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time', 'after_or_equal:start_time'],
  38. ]);
  39. $page = request()->input('page', 1);
  40. $limit = request()->input('limit', 15);
  41. $query = new LhcOrderModel();
  42. $query = LhcOrderModel::with(['items'])->join('users', 'users.member_id', '=', 'lhc_order.member_id');
  43. if (!empty($params['id'])) {
  44. $query = $query->where('lhc_order.id', $params['id']);
  45. }
  46. if (!empty($params['issue'])) {
  47. $query = $query->where('lhc_order.issue', $params['issue']);
  48. }
  49. if (!empty($params['ordernum'])) {
  50. $query = $query->where('lhc_order.ordernum', $params['ordernum']);
  51. }
  52. if (!empty($params['member_id'])) {
  53. $query = $query->where('lhc_order.member_id', $params['member_id']);
  54. }
  55. if (isset($params['lottery_status']) && $params['lottery_status'] !== null) {
  56. $query = $query->where('lhc_order.lottery_status', $params['lottery_status']);
  57. }
  58. if (isset($params['is_faker']) && $params['is_faker'] !== null) {
  59. $query = $query->where('lhc_order.is_faker', $params['is_faker']);
  60. }
  61. if (!empty($params['game'])) {
  62. $query = $query->where('lhc_order.game', $params['game']);
  63. }
  64. if (!empty($params['gameplay'])) {
  65. $query = $query->where('lhc_order.gameplay', $params['gameplay']);
  66. }
  67. if (!empty($params['number'])) {
  68. $query = $query->where('lhc_order.number', $params['number']);
  69. }
  70. if (!empty($params['type'])) {
  71. $query = $query->where('lhc_order.type', $params['type']);
  72. }
  73. if (!empty($params['first_name'])) {
  74. $query = $query->where('users.first_name', 'like', "%{$params['first_name']}%");
  75. }
  76. // created_at 为 Unix 时间戳(模型 dateFormat=U)
  77. if (!empty($params['start_time'])) {
  78. $query = $query->where('lhc_order.created_at', '>=', strtotime($params['start_time'] . ' 00:00:00'));
  79. }
  80. if (!empty($params['end_time'])) {
  81. $query = $query->where('lhc_order.created_at', '<=', strtotime($params['end_time'] . ' 23:59:59'));
  82. }
  83. $count = $query->count();
  84. $list = $query->select('lhc_order.*','users.first_name','users.member_id')
  85. ->forPage($page, $limit)
  86. ->orderByDesc('lhc_order.created_at')
  87. ->get();
  88. foreach($list as &$info) {
  89. //六合彩彩票
  90. if ($info['type'] >= 1 and $info['type'] <= 4) {
  91. $lottery = LhcLottery::where('type', $info['type'])->where('issue', $info['issue'])->first();
  92. if ($lottery) {
  93. $info['lottery'] = [
  94. 'open_code' => $lottery->open_code,
  95. 'open_code_other' => ''
  96. ];
  97. }
  98. }
  99. //28
  100. if ($info['type'] == 5 || $info['type'] == 6) {
  101. if ($info['type'] == 6) {
  102. $issue = PcIssue::where('issue_no', $info['issue'])->first();
  103. } elseif ($info['type'] == 5) {
  104. $issue = Issue::where('issue_no', $info['issue'])->first();
  105. }
  106. $info['lottery'] = [
  107. 'open_code' => $issue->winning_numbers,
  108. 'open_code_other' => $issue->combo
  109. ];
  110. }
  111. //极速彩票
  112. if ($info['type'] >= 7 and $info['type'] <= 13) {
  113. $lottery = JisuLottery::where('type', $info['type'] - 6)->where('issue', $info['issue'])->first();
  114. if ($lottery) {
  115. $info['lottery'] = [
  116. 'open_code' => $lottery->open_code,
  117. 'open_code_other' => $lottery->open_code_other
  118. ];
  119. }
  120. }
  121. }
  122. } catch (Exception $e) {
  123. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  124. }
  125. return $this->success(['total' => $count, 'data' => $list]);
  126. }
  127. //订单详情
  128. function info()
  129. {
  130. try {
  131. request()->validate([
  132. 'id' => ['required', 'integer'],
  133. ]);
  134. $id = request()->input('id');
  135. $info = LhcOrderModel::with(['items'])
  136. ->leftJoin('users', 'users.member_id', '=', 'lhc_order.member_id')
  137. ->where('lhc_order.id', $id)
  138. ->select('lhc_order.*', 'users.first_name')
  139. ->first();
  140. if (!$info) throw new Exception('订单不存在');
  141. $info = $info->toArray();
  142. //六合彩彩票
  143. if ($info['type'] >= 1 and $info['type'] <= 4) {
  144. $lottery = LhcLottery::where('type', $info['type'])->where('issue', $info['issue'])->first();
  145. if ($lottery) {
  146. $info['lottery'] = [
  147. 'open_code' => $lottery->open_code,
  148. 'open_code_other' => ''
  149. ];
  150. }
  151. }
  152. //28
  153. if ($info['type'] == 5 || $info['type'] == 6) {
  154. if ($info['type'] == 6) {
  155. $issue = PcIssue::where('issue_no', $info['issue'])->first();
  156. } elseif ($info['type'] == 5) {
  157. $issue = Issue::where('issue_no', $info['issue'])->first();
  158. }
  159. $info['lottery'] = [
  160. 'open_code' => $issue->winning_numbers,
  161. 'open_code_other' => $issue->combo
  162. ];
  163. }
  164. //极速彩票
  165. if ($info['type'] >= 7 and $info['type'] <= 13) {
  166. $lottery = JisuLottery::where('type', $info['type'] - 6)->where('issue', $info['issue'])->first();
  167. if ($lottery) {
  168. $info['lottery'] = [
  169. 'open_code' => $lottery->open_code,
  170. 'open_code_other' => $lottery->open_code_other
  171. ];
  172. }
  173. }
  174. } catch (Exception $e) {
  175. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  176. }
  177. return $this->success($info);
  178. }
  179. /**
  180. * @api {post} /lhcorder/refund 同意退款
  181. * @apiGroup 订单管理
  182. */
  183. public function refund()
  184. {
  185. $errors = [];
  186. try {
  187. DB::beginTransaction();
  188. $params = request()->validate([
  189. 'id' => ['required', 'array', 'min:1'],
  190. ]);
  191. $orderList = LhcOrderModel::whereIn('id', $params['id'])->get();
  192. foreach ($orderList as $order) {
  193. if ($order->lottery_status != 0) {
  194. continue;
  195. }
  196. $order->lottery_status = 4;
  197. $order->updated_at = time();
  198. $order->save();
  199. // 获取用户余额
  200. $walletInfo = Wallet::where(['member_id' => $order->member_id])->first();
  201. if (!$walletInfo) continue;
  202. $before = $walletInfo->available_balance;
  203. $after = bcsub($walletInfo->available_balance, $order->amount, 2);
  204. $walletInfo->available_balance = $after;
  205. $walletInfo->save();
  206. $remark = LhcOrderModel::getRemark($order->type);
  207. FundsRecord::addData([
  208. 'change_type' => $remark.'退款',
  209. 'amount' => $order->amount,
  210. 'before_balance' => $before,
  211. 'after_balance' => $after,
  212. 'member_id' => $order->member_id,
  213. 'related_id' => $order->id,
  214. 'remark' => $remark.'订单退款',
  215. ]);
  216. }
  217. DB::commit();
  218. } catch (Exception $e) {
  219. DB::rollBack();
  220. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage(), $errors);
  221. }
  222. return $this->success();
  223. }
  224. }