OrderController.php 21 KB

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