OrderController.php 22 KB

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