OrderController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. Order::payCompletedHandle($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()->getOriginal();
  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. $orderPatient = OrderPatient::create($addPatient);
  220. //保存订单疫苗信息
  221. if ($req['product_type'] == 4) {
  222. $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();
  223. $addVaccine['order_id'] = $order['id'];
  224. $addVaccine['order_patient_id'] = $orderPatient['id'];
  225. OrderVaccine::create($addVaccine);
  226. }
  227. //保存儿保订单信息
  228. if ($req['product_type'] == 5) {
  229. $nurse_ids = json_decode($req['nurse_ids'], true);
  230. foreach ($nurse_ids as $k => $v) {
  231. $addNurse = Nurse::select(['id as nurse_id', 'price as nurse_price', 'name as nurse_name', 'remark as nurse_remark'])->where('id', $v)->first()->getOriginal();
  232. $addNurse['order_id'] = $order['id'];
  233. $addNurse['order_patient_id'] = $orderPatient['id'];
  234. OrderNurse::create($addNurse);
  235. }
  236. }
  237. //判断是微信支付
  238. if ($req['payment_type'] == 1) {
  239. //生成支付交易单
  240. if ($payment_amount > 0) {
  241. $trade_sn = build_sn($order['id'], 3, 'T');
  242. $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
  243. Payment::create([
  244. 'user_id' => $user['id'],
  245. 'order_id' => $order['id'],
  246. 'trade_sn' => $trade_sn,
  247. 'amount' => $payment_amount,
  248. 'remark' => $payBody,
  249. ]);
  250. //请求支付
  251. $payment = Factory::payment(config('config.wechat_pay'));
  252. $result = $payment->order->unify([
  253. 'body' => $payBody,
  254. 'out_trade_no' => $trade_sn,
  255. 'total_fee' => $payment_amount,
  256. 'trade_type' => 'JSAPI',
  257. 'openid' => $user['openid'],
  258. ]);
  259. if (empty($result['prepay_id'])) {
  260. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  261. return out(null, 702, $errorMsg);
  262. }
  263. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  264. }
  265. }
  266. //判断是余额支付
  267. elseif ($req['payment_type'] == 2) {
  268. if ($payment_amount > 0) {
  269. //改变用户余额
  270. $change_amount = 0 - $payment_amount;
  271. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
  272. Order::payCompletedHandle($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()->getOriginal();
  339. $addPatient['order_id'] = $order['id'];
  340. $addPatient['patient_id'] = $req['patient_id'];
  341. OrderPatient::create($addPatient);
  342. //保存订单服务包表
  343. $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();
  344. $addPack['order_id'] = $order['id'];
  345. $addPack['is_security'] = $req['is_security'];
  346. $addPack['guardian_name'] = $req['guardian_name'];
  347. $addPack['relationship_type'] = $req['relationship_type'];
  348. $addPack['start_time'] = time();
  349. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  350. OrderPack::create($addPack);
  351. //判断是微信支付
  352. if ($req['payment_type'] == 1) {
  353. //生成支付交易单
  354. if ($payment_amount > 0) {
  355. $trade_sn = build_sn($order['id'], 3, 'T');
  356. $payBody = '超级宝妈-'.config('config.product_type_map')[6];
  357. Payment::create([
  358. 'user_id' => $user['id'],
  359. 'order_id' => $order['id'],
  360. 'trade_sn' => $trade_sn,
  361. 'amount' => $payment_amount,
  362. 'remark' => $payBody,
  363. ]);
  364. //请求支付
  365. $payment = Factory::payment(config('config.wechat_pay'));
  366. $result = $payment->order->unify([
  367. 'body' => $payBody,
  368. 'out_trade_no' => $trade_sn,
  369. 'total_fee' => $payment_amount,
  370. 'trade_type' => 'JSAPI',
  371. 'openid' => $user['openid'],
  372. ]);
  373. if (empty($result['prepay_id'])) {
  374. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  375. return out(null, 702, $errorMsg);
  376. }
  377. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  378. }
  379. }
  380. //判断是余额支付
  381. elseif ($req['payment_type'] == 2) {
  382. if ($payment_amount > 0) {
  383. //改变用户余额
  384. $change_amount = 0 - $payment_amount;
  385. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
  386. Order::payCompletedHandle($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,3',
  401. 'product_type' => 'integer',
  402. 'order_status' => 'integer',
  403. 'time_sort' => 'in:0,1',
  404. 'is_pack_expire' => 'in:0,1,2',
  405. ]);
  406. $user = $this->user;
  407. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter'])->where('user_id', $user['id']);
  408. if (!empty($req['product_type'])) {
  409. $builder->where('product_type', $req['product_type']);
  410. }
  411. else {
  412. if (!empty($req['list_type'])) {
  413. if ($req['list_type'] == 1) {
  414. $builder->whereIn('product_type', [1,2]);
  415. }
  416. elseif ($req['list_type'] == 2) {
  417. $builder->whereIn('product_type', [3,4,5]);
  418. }
  419. elseif ($req['list_type'] == 3) {
  420. $builder->where('product_type', '<', 7);
  421. }
  422. }
  423. }
  424. if (!empty($req['order_status'])) {
  425. $builder->where('order_status', $req['order_status']);
  426. }
  427. if (!empty($req['is_pack_expire'])) {
  428. $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
  429. if ($req['is_pack_expire'] == 1) {
  430. $tmpBuilder->where('order_packs.end_time', '<', time());
  431. }
  432. else {
  433. $tmpBuilder->where('order_packs.end_time', '>=', time());
  434. }
  435. $order_ids = $tmpBuilder->pluck('o.id')->toArray();
  436. $builder->whereIn('id', $order_ids);
  437. }
  438. if (!empty($req['time_sort'])) {
  439. $builder->orderBy('id', 'desc');
  440. }
  441. else {
  442. $builder->orderBy('id', 'asc');
  443. }
  444. $data = $builder->paginate();
  445. return out($data);
  446. }
  447. public function orderDetail()
  448. {
  449. $req = request()->post();
  450. $this->validate(request(), [
  451. 'order_id' => 'required|integer'
  452. ]);
  453. $user = $this->user;
  454. $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first();
  455. return out($data);
  456. }
  457. public function topup()
  458. {
  459. $req = request()->post();
  460. $this->validate(request(), [
  461. 'amount' => 'required|integer',
  462. ]);
  463. $user = $this->user;
  464. DB::beginTransaction();
  465. try {
  466. //保存订单数据
  467. $order = Order::create([
  468. 'user_id' => $user['id'],
  469. 'product_type' => 7,
  470. 'total_amount' => $req['amount'],
  471. 'payment_amount' => $req['amount'],
  472. ]);
  473. $order_sn = build_sn($order['id']);
  474. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  475. //生成支付交易单
  476. $trade_sn = build_sn($order['id'], 3, 'T');
  477. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  478. Payment::create([
  479. 'user_id' => $user['id'],
  480. 'order_id' => $order['id'],
  481. 'trade_sn' => $trade_sn,
  482. 'amount' => $req['amount'],
  483. 'remark' => $payBody,
  484. ]);
  485. //请求支付
  486. $payment = Factory::payment(config('config.wechat_pay'));
  487. $result = $payment->order->unify([
  488. 'body' => $payBody,
  489. 'out_trade_no' => $trade_sn,
  490. 'total_fee' => $req['amount'],
  491. 'trade_type' => 'JSAPI',
  492. 'openid' => $user['openid'],
  493. ]);
  494. if (empty($result['prepay_id'])) {
  495. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  496. return out(null, 702, $errorMsg);
  497. }
  498. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  499. DB::commit();
  500. } catch (\Exception $e) {
  501. DB::rollBack();
  502. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  503. }
  504. return out($config);
  505. }
  506. }