OrderController.php 22 KB

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