OrderController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-30
  6. * Time: 下午10:58
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\Nurse;
  10. use App\Models\Order;
  11. use App\Models\OrderNurse;
  12. use App\Models\OrderPack;
  13. use App\Models\OrderPatient;
  14. use App\Models\OrderVaccine;
  15. use App\Models\Patient;
  16. use App\Models\Payment;
  17. use App\Models\ServicePack;
  18. use App\Models\TimePeriod;
  19. use App\Models\User;
  20. use App\Models\UserCoupon;
  21. use App\Models\Vaccine;
  22. use EasyWeChat\Factory;
  23. use DB;
  24. class OrderController extends AuthController
  25. {
  26. public function consultPlaceOrder()
  27. {
  28. $req = request()->post();
  29. $this->validate(request(), [
  30. 'payment_type' => 'required|in:1,2',
  31. 'product_type' => 'required|in:1,2',
  32. 'docter_id' => 'required|integer',
  33. 'patient_id' => 'required|integer',
  34. 'total_amount' => 'required|integer',
  35. 'user_coupon_id' => 'integer',
  36. 'phone' => 'required_if:product_type,1',
  37. 'phone_minutes' => 'required_if:product_type,1|integer',
  38. 'symptoms' => 'required_if:product_type,2|max:2000',
  39. 'medical_imgs' => 'required_if:product_type,2|json|max:3000',
  40. ]);
  41. $user = $this->user;
  42. $discount_amount = 0;
  43. if (!empty($req['user_coupon_id'])) {
  44. //计算优惠金额
  45. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
  46. }
  47. $payment_amount = $req['total_amount'] - $discount_amount;
  48. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  49. if ($req['payment_type'] == 2) {
  50. if ($user['balance'] < $payment_amount) {
  51. return out(null, 601, '余额不足');
  52. }
  53. }
  54. $order_status = $payment_status = 1;
  55. $payment_time = 0;
  56. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  57. $order_status = $payment_status = 2;
  58. $payment_time = time();
  59. }
  60. $config = null;
  61. DB::beginTransaction();
  62. try {
  63. //保存订单数据
  64. $order = Order::create([
  65. 'user_id' => $user['id'],
  66. 'payment_type' => $req['payment_type'],
  67. 'product_type' => $req['product_type'],
  68. 'docter_id' => $req['docter_id'],
  69. 'total_amount' => $req['total_amount'],
  70. 'payment_amount' => $payment_amount,
  71. 'discount_amount' => $discount_amount,
  72. 'patient_id' => $req['patient_id'],
  73. 'order_status' => $order_status,
  74. 'payment_status' => $payment_status,
  75. 'payment_time' => $payment_time,
  76. ]);
  77. $order_sn = build_sn($order['id']);
  78. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  79. //保存订单患者信息
  80. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship', 'info', 'card_type', 'card_number'])->where('id', $req['patient_id'])->first()->toArray();
  81. $addPatient['order_id'] = $order['id'];
  82. $addPatient['patient_id'] = $req['patient_id'];
  83. if ($req['product_type'] == 1) {
  84. $addPatient['phone'] = $req['phone'];
  85. $addPatient['phone_minutes'] = $req['phone_minutes'];
  86. }
  87. elseif ($req['product_type'] == 2) {
  88. $addPatient['symptoms'] = $req['symptoms'];
  89. $addPatient['medical_imgs'] = $req['medical_imgs'];
  90. }
  91. OrderPatient::create($addPatient);
  92. //判断是微信支付
  93. if ($req['payment_type'] == 1) {
  94. //生成支付交易单
  95. if ($payment_amount > 0) {
  96. $trade_sn = build_sn($order['id'], 3, 'T');
  97. $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
  98. Payment::create([
  99. 'user_id' => $user['id'],
  100. 'order_id' => $order['id'],
  101. 'trade_sn' => $trade_sn,
  102. 'amount' => $payment_amount,
  103. 'remark' => $payBody,
  104. ]);
  105. //请求支付
  106. $payment = Factory::payment(config('config.wechat_pay'));
  107. $result = $payment->order->unify([
  108. 'body' => $payBody,
  109. 'out_trade_no' => $trade_sn,
  110. 'total_fee' => $payment_amount,
  111. 'trade_type' => 'JSAPI',
  112. 'openid' => $user['openid'],
  113. ]);
  114. if (empty($result['prepay_id'])) {
  115. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  116. return out(null, 702, $errorMsg);
  117. }
  118. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  119. }
  120. }
  121. //判断是余额支付
  122. elseif ($req['payment_type'] == 2) {
  123. if ($payment_amount > 0) {
  124. //改变用户余额
  125. $change_amount = 0 - $payment_amount;
  126. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '咨询订单消费');
  127. }
  128. }
  129. DB::commit();
  130. } catch (\Exception $e) {
  131. DB::rollBack();
  132. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  133. }
  134. return out($config);
  135. }
  136. public function appointPlaceOrder()
  137. {
  138. $req = request()->post();
  139. $this->validate(request(), [
  140. 'payment_type' => 'required|in:1,2',
  141. 'product_type' => 'required|in:3,4,5',
  142. 'patient_id' => 'required|integer',
  143. 'organization_id' => 'required|integer',
  144. 'schedule_date' => 'required|date',
  145. 'time_period_id' => 'required|integer',
  146. 'total_amount' => 'required|integer',
  147. 'user_coupon_id' => 'integer',
  148. 'docter_id' => 'required_if:product_type,3|integer',
  149. 'vaccine_id' => 'required_if:product_type,4|integer',
  150. 'nurse_ids' => 'required_if:product_type,5|json',
  151. ]);
  152. $user = $this->user;
  153. $product_type = $req['product_type'];
  154. if ($req['product_type'] == 4) {
  155. $vaccine = Vaccine::select(['type'])->where('id', $req['vaccine_id'])->first();
  156. if ($vaccine['type'] == 2) {
  157. if (empty($req['total_amount'])) {
  158. return out(null, 10001, '总价不能为0');
  159. }
  160. }
  161. }
  162. $discount_amount = 0;
  163. if (!empty($req['user_coupon_id'])) {
  164. //计算优惠金额
  165. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $product_type);
  166. }
  167. $payment_amount = $req['total_amount'] - $discount_amount;
  168. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  169. if ($req['payment_type'] == 2) {
  170. if ($user['balance'] < $payment_amount) {
  171. return out(null, 601, '余额不足');
  172. }
  173. }
  174. $order_status = $payment_status = 1;
  175. $payment_time = 0;
  176. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  177. $order_status = 3;
  178. $payment_status = 2;
  179. $payment_time = time();
  180. }
  181. $config = null;
  182. DB::beginTransaction();
  183. try {
  184. //保存订单数据
  185. $order = Order::create([
  186. 'user_id' => $user['id'],
  187. 'docter_id' => $req['docter_id'] ?? 0,
  188. 'patient_id' => $req['patient_id'],
  189. 'organization_id' => $req['organization_id'],
  190. 'payment_type' => $req['payment_type'],
  191. 'product_type' => $product_type,
  192. 'order_status' => $order_status,
  193. 'payment_status' => $payment_status,
  194. 'total_amount' => $req['total_amount'],
  195. 'payment_amount' => $payment_amount,
  196. 'discount_amount' => $discount_amount,
  197. 'payment_time' => $payment_time,
  198. ]);
  199. $order_sn = build_sn($order['id']);
  200. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  201. //保存订单患者信息
  202. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship', 'info', 'card_type', 'card_number'])->where('id', $req['patient_id'])->first()->toArray();
  203. $addPatient['order_id'] = $order['id'];
  204. $addPatient['patient_id'] = $req['patient_id'];
  205. $addPatient['organization_id'] = $req['organization_id'];
  206. $addPatient['time_period_id'] = $req['time_period_id'];
  207. $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
  208. $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
  209. $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
  210. $orderPatient = OrderPatient::create($addPatient);
  211. //保存订单疫苗信息
  212. if ($req['product_type'] == 4) {
  213. $addVaccine = Vaccine::select(['id vaccine_id', 'type vaccine_type', 'price vaccine_price', 'name vaccine_name', 'remark vaccine_remark', 'supplier vaccine_supplier'])->where('id', $req['vaccine_id'])->first()->toArray();
  214. $addVaccine['order_id'] = $order['id'];
  215. $addVaccine['order_patient_id'] = $orderPatient['id'];
  216. OrderVaccine::create($addVaccine);
  217. }
  218. //保存儿保订单信息
  219. if ($req['product_type'] == 5) {
  220. $nurse_ids = json_decode($req['nurse_ids'], true);
  221. foreach ($nurse_ids as $k => $v) {
  222. $addNurse = Nurse::select(['id nurse_id', 'price nurse_price', 'name nurse_name', 'remark nurse_remark'])->where('id', $v)->first()->toArray();
  223. $addNurse['order_id'] = $order['id'];
  224. $addNurse['order_patient_id'] = $orderPatient['id'];
  225. OrderNurse::create($addNurse);
  226. }
  227. }
  228. //判断是微信支付
  229. if ($req['payment_type'] == 1) {
  230. //生成支付交易单
  231. if ($payment_amount > 0) {
  232. $trade_sn = build_sn($order['id'], 3, 'T');
  233. $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
  234. Payment::create([
  235. 'user_id' => $user['id'],
  236. 'order_id' => $order['id'],
  237. 'trade_sn' => $trade_sn,
  238. 'amount' => $payment_amount,
  239. 'remark' => $payBody,
  240. ]);
  241. //请求支付
  242. $payment = Factory::payment(config('config.wechat_pay'));
  243. $result = $payment->order->unify([
  244. 'body' => $payBody,
  245. 'out_trade_no' => $trade_sn,
  246. 'total_fee' => $payment_amount,
  247. 'trade_type' => 'JSAPI',
  248. 'openid' => $user['openid'],
  249. ]);
  250. if (empty($result['prepay_id'])) {
  251. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  252. return out(null, 702, $errorMsg);
  253. }
  254. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  255. }
  256. }
  257. //判断是余额支付
  258. elseif ($req['payment_type'] == 2) {
  259. if ($payment_amount > 0) {
  260. //改变用户余额
  261. $change_amount = 0 - $payment_amount;
  262. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
  263. }
  264. }
  265. DB::commit();
  266. } catch (\Exception $e) {
  267. DB::rollBack();
  268. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  269. }
  270. return out($config);
  271. }
  272. public function packPlaceOrder()
  273. {
  274. $req = request()->post();
  275. $this->validate(request(), [
  276. 'payment_type' => 'required|in:1,2',
  277. 'patient_id' => 'required|integer',
  278. 'total_amount' => 'required|integer',
  279. 'user_coupon_id' => 'integer',
  280. 'service_pack_id' => 'required|integer',
  281. 'is_security' => 'required|in:0,1',
  282. 'guardian_name' => 'required|max:50',
  283. 'relationship_type' => 'required|integer',
  284. ]);
  285. $user = $this->user;
  286. $discount_amount = 0;
  287. if (!empty($req['user_coupon_id'])) {
  288. //计算优惠金额
  289. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
  290. }
  291. $payment_amount = $req['total_amount'] - $discount_amount;
  292. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  293. if ($req['payment_type'] == 2) {
  294. if ($user['balance'] < $payment_amount) {
  295. return out(null, 601, '余额不足');
  296. }
  297. }
  298. $order_status = $payment_status = 1;
  299. $payment_time = 0;
  300. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  301. $order_status = 3;
  302. $payment_status = 2;
  303. $payment_time = time();
  304. }
  305. $config = null;
  306. DB::beginTransaction();
  307. try {
  308. //保存订单数据
  309. $order = Order::create([
  310. 'user_id' => $user['id'],
  311. 'patient_id' => $req['patient_id'],
  312. 'payment_type' => $req['payment_type'],
  313. 'product_type' => 6,
  314. 'docter_id' => $req['docter_id'],
  315. 'total_amount' => $req['total_amount'],
  316. 'payment_amount' => $payment_amount,
  317. 'discount_amount' => $discount_amount,
  318. 'order_status' => $order_status,
  319. 'payment_status' => $payment_status,
  320. 'payment_time' => $payment_time,
  321. ]);
  322. $order_sn = build_sn($order['id']);
  323. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  324. //保存订单患者信息
  325. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship', 'info', 'card_type', 'card_number'])->where('id', $req['patient_id'])->first()->toArray();
  326. $addPatient['order_id'] = $order['id'];
  327. $addPatient['patient_id'] = $req['patient_id'];
  328. OrderPatient::create($addPatient);
  329. //保存订单服务包表
  330. $addPack = ServicePack::select(['id service_pack_id', 'name pack_name', 'intro pack_intro', 'price pack_price', 'team_id', 'phone_minutes', 'chat_num', 'appoint_num', 'vaccine_limit_amount', 'nurses_limit_amount', 'effective_days'])->where('id', $req['service_pack_id'])->first()->toArray();
  331. $addPack['order_id'] = $order['id'];
  332. $addPack['is_security'] = $req['is_security'];
  333. $addPack['guardian_name'] = $req['guardian_name'];
  334. $addPack['relationship_type'] = $req['relationship_type'];
  335. $addPack['start_time'] = time();
  336. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  337. OrderPack::create($addPack);
  338. //判断是微信支付
  339. if ($req['payment_type'] == 1) {
  340. //生成支付交易单
  341. if ($payment_amount > 0) {
  342. $trade_sn = build_sn($order['id'], 3, 'T');
  343. $payBody = '超级宝妈-'.config('config.product_type_map')[6];
  344. Payment::create([
  345. 'user_id' => $user['id'],
  346. 'order_id' => $order['id'],
  347. 'trade_sn' => $trade_sn,
  348. 'amount' => $payment_amount,
  349. 'remark' => $payBody,
  350. ]);
  351. //请求支付
  352. $payment = Factory::payment(config('config.wechat_pay'));
  353. $result = $payment->order->unify([
  354. 'body' => $payBody,
  355. 'out_trade_no' => $trade_sn,
  356. 'total_fee' => $payment_amount,
  357. 'trade_type' => 'JSAPI',
  358. 'openid' => $user['openid'],
  359. ]);
  360. if (empty($result['prepay_id'])) {
  361. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  362. return out(null, 702, $errorMsg);
  363. }
  364. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  365. }
  366. }
  367. //判断是余额支付
  368. elseif ($req['payment_type'] == 2) {
  369. if ($payment_amount > 0) {
  370. //改变用户余额
  371. $change_amount = 0 - $payment_amount;
  372. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
  373. }
  374. }
  375. DB::commit();
  376. } catch (\Exception $e) {
  377. DB::rollBack();
  378. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  379. }
  380. return out($config);
  381. }
  382. public function orderList()
  383. {
  384. $req = request()->post();
  385. $this->validate(request(), [
  386. 'list_type' => 'required|in:1,2,3',
  387. 'product_type' => 'integer',
  388. 'order_status' => 'integer',
  389. 'time_sort' => 'in:0,1'
  390. ]);
  391. $user = $this->user;
  392. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine'])->where('user_id', $user['id']);
  393. if (!empty($req['product_type'])) {
  394. $builder->where('product_type', $req['product_type']);
  395. }
  396. else {
  397. if ($req['list_type'] == 1) {
  398. $builder->whereIn('product_type', [1,2]);
  399. }
  400. elseif ($req['list_type'] == 2) {
  401. $builder->whereIn('product_type', [3,4,5]);
  402. }
  403. elseif ($req['list_type'] == 3) {
  404. $builder->where('product_type', 6);
  405. }
  406. }
  407. if (!empty($req['order_status'])) {
  408. $builder->where('order_status', $req['order_status']);
  409. }
  410. if (!empty($req['time_sort'])) {
  411. $builder->orderBy('id', 'desc');
  412. }
  413. else {
  414. $builder->orderBy('id', 'asc');
  415. }
  416. $data = $builder->paginate();
  417. return out($data);
  418. }
  419. public function orderDetail()
  420. {
  421. $req = request()->post();
  422. $this->validate(request(), [
  423. 'order_id' => 'required|integer'
  424. ]);
  425. $user = $this->user;
  426. $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first();
  427. return out($data);
  428. }
  429. public function topup()
  430. {
  431. $req = request()->post();
  432. $this->validate(request(), [
  433. 'amount' => 'required|integer',
  434. ]);
  435. $user = $this->user;
  436. DB::beginTransaction();
  437. try {
  438. //保存订单数据
  439. $order = Order::create([
  440. 'user_id' => $user['id'],
  441. 'product_type' => 7,
  442. 'total_amount' => $req['amount'],
  443. 'payment_amount' => $req['amount'],
  444. ]);
  445. $order_sn = build_sn($order['id']);
  446. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  447. //生成支付交易单
  448. $trade_sn = build_sn($order['id'], 3, 'T');
  449. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  450. Payment::create([
  451. 'user_id' => $user['id'],
  452. 'order_id' => $order['id'],
  453. 'trade_sn' => $trade_sn,
  454. 'amount' => $req['amount'],
  455. 'remark' => $payBody,
  456. ]);
  457. //请求支付
  458. $payment = Factory::payment(config('config.wechat_pay'));
  459. $result = $payment->order->unify([
  460. 'body' => $payBody,
  461. 'out_trade_no' => $trade_sn,
  462. 'total_fee' => $req['amount'],
  463. 'trade_type' => 'JSAPI',
  464. 'openid' => $user['openid'],
  465. ]);
  466. if (empty($result['prepay_id'])) {
  467. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  468. return out(null, 702, $errorMsg);
  469. }
  470. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  471. DB::commit();
  472. } catch (\Exception $e) {
  473. DB::rollBack();
  474. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  475. }
  476. return out($config);
  477. }
  478. }