OrderController.php 19 KB

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