OrderController.php 23 KB

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