OrderController.php 21 KB

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