LhcOrder.php 11 KB

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