OrderController.php 28 KB

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