OrderController.php 22 KB

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