LhcOrder.php 11 KB

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