LhcOrder.php 10 KB

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