LhcOrder.php 11 KB

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