OrderController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. ]);
  336. $user = $this->user;
  337. if (!empty($req['pay_password'])) {
  338. if (empty($user['pay_password'])) {
  339. return out(null, 60010, '未设置支付密码');
  340. }
  341. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  342. return out(null, 10001, '密码错误');
  343. }
  344. }
  345. $discount_amount = 0;
  346. if (!empty($req['user_coupon_id'])) {
  347. //计算优惠金额
  348. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
  349. }
  350. $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();
  351. if (empty($addPatient['card_img_url'])) {
  352. return out(null, 70011, '该患者未上传身份证');
  353. }
  354. $payment_amount = $req['total_amount'] - $discount_amount;
  355. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  356. if ($req['payment_type'] == 2 && $payment_amount > 0 && empty($req['pay_password'])) {
  357. return out(null, 10011, '请输入支付密码');
  358. }
  359. if ($req['payment_type'] == 2) {
  360. if ($user['balance'] < $payment_amount) {
  361. return out(null, 601, '余额不足');
  362. }
  363. }
  364. $order_status = $payment_status = 1;
  365. $payment_time = 0;
  366. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  367. $order_status = 3;
  368. $payment_status = 2;
  369. $payment_time = time();
  370. }
  371. $config = null;
  372. DB::beginTransaction();
  373. try {
  374. //保存订单数据
  375. $order = Order::create([
  376. 'user_id' => $user['id'],
  377. 'patient_id' => $req['patient_id'],
  378. 'payment_type' => $req['payment_type'],
  379. 'product_type' => 6,
  380. 'total_amount' => $req['total_amount'],
  381. 'payment_amount' => $payment_amount,
  382. 'discount_amount' => $discount_amount,
  383. 'order_status' => $order_status,
  384. 'payment_status' => $payment_status,
  385. 'payment_time' => $payment_time,
  386. ]);
  387. $order_sn = build_sn($order['id']);
  388. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  389. //保存订单患者信息
  390. $addPatient['order_id'] = $order['id'];
  391. $addPatient['patient_id'] = $req['patient_id'];
  392. OrderPatient::create($addPatient);
  393. //保存订单服务包表
  394. $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();
  395. $addPack['user_id'] = $user['id'];
  396. $addPack['order_id'] = $order['id'];
  397. $addPack['is_security'] = $req['is_security'];
  398. $addPack['guardian_name'] = $req['guardian_name'];
  399. $addPack['relationship_type'] = $req['relationship_type'];
  400. $addPack['start_time'] = time();
  401. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  402. $addPack['total_phone_minutes'] = $addPack['phone_minutes'];
  403. $addPack['total_chat_num'] = $addPack['chat_num'];
  404. $addPack['total_appoint_num'] = $addPack['appoint_num'];
  405. $addPack['total_vaccine_limit_amount'] = $addPack['vaccine_limit_amount'];
  406. $addPack['total_nurses_limit_amount'] = $addPack['nurses_limit_amount'];
  407. $addPack['label'] = json_decode($addPack['label'], true);
  408. OrderPack::create($addPack);
  409. //判断是微信支付
  410. if ($req['payment_type'] == 1) {
  411. //生成支付交易单
  412. if ($payment_amount > 0) {
  413. $trade_sn = build_sn($order['id'], 3, 'T');
  414. $payBody = '超级宝妈-'.config('config.product_type_map')[6];
  415. Payment::create([
  416. 'user_id' => $user['id'],
  417. 'order_id' => $order['id'],
  418. 'trade_sn' => $trade_sn,
  419. 'amount' => $payment_amount,
  420. 'remark' => $payBody,
  421. ]);
  422. //请求支付
  423. $payment = Factory::payment(config('config.wechat_pay'));
  424. $result = $payment->order->unify([
  425. 'body' => $payBody,
  426. 'out_trade_no' => $trade_sn,
  427. 'total_fee' => $payment_amount,
  428. 'trade_type' => 'JSAPI',
  429. 'openid' => $user['openid'],
  430. ]);
  431. if (empty($result['prepay_id'])) {
  432. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  433. return out(null, 702, $errorMsg);
  434. }
  435. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  436. }
  437. }
  438. //判断是余额支付
  439. elseif ($req['payment_type'] == 2) {
  440. if ($payment_amount > 0) {
  441. //改变用户余额
  442. $change_amount = 0 - $payment_amount;
  443. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
  444. Order::payCompletedHandle($order['id']);
  445. }
  446. }
  447. DB::commit();
  448. } catch (Exception $e) {
  449. DB::rollBack();
  450. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  451. }
  452. return out($config);
  453. }
  454. public function orderList()
  455. {
  456. $req = request()->post();
  457. $this->validate(request(), [
  458. 'list_type' => 'required|in:0,1,2',
  459. 'product_type' => 'integer',
  460. 'order_status' => 'integer',
  461. 'time_sort' => 'in:0,1',
  462. 'is_pack_expire' => 'in:0,1,2',
  463. ]);
  464. $user = $this->user;
  465. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('user_id', $user['id']);
  466. if (!empty($req['product_type'])) {
  467. $builder->where('product_type', $req['product_type']);
  468. }
  469. else {
  470. if (!empty($req['list_type'])) {
  471. if ($req['list_type'] == 1) {
  472. $builder->whereIn('product_type', [1,2]);
  473. }
  474. elseif ($req['list_type'] == 2) {
  475. $builder->whereIn('product_type', [3,4,5]);
  476. }
  477. }
  478. else {
  479. $builder->where('product_type', '<', 7);
  480. }
  481. }
  482. if (!empty($req['order_status'])) {
  483. $builder->where('order_status', $req['order_status']);
  484. }
  485. if (!empty($req['is_pack_expire'])) {
  486. $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
  487. if ($req['is_pack_expire'] == 1) {
  488. $tmpBuilder->where('order_packs.end_time', '<', time());
  489. }
  490. else {
  491. $tmpBuilder->where('order_packs.end_time', '>=', time());
  492. }
  493. $order_ids = $tmpBuilder->pluck('o.id')->toArray();
  494. $builder->whereIn('id', $order_ids);
  495. }
  496. if (!empty($req['time_sort'])) {
  497. $builder->orderBy('id', 'desc');
  498. }
  499. else {
  500. $builder->orderBy('id', 'asc');
  501. }
  502. $data = $builder->paginate();
  503. return out($data);
  504. }
  505. public function orderDetail()
  506. {
  507. $req = request()->post();
  508. $this->validate(request(), [
  509. 'order_id' => 'required|integer'
  510. ]);
  511. $user = $this->user;
  512. $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();
  513. if (!empty($data['order_pack'])) {
  514. $data['order_pack']['team'] = [];
  515. if (!empty($data['order_pack']['team_id'])) {
  516. $data['order_pack']['team'] = Team::with(['docter.office', 'docter.qualification'])->whereIn('id', $data['order_pack']['team_id'])->get()->toArray();
  517. }
  518. }
  519. return out($data);
  520. }
  521. public function topup()
  522. {
  523. $req = request()->post();
  524. $this->validate(request(), [
  525. 'amount' => 'required|integer',
  526. ]);
  527. $user = $this->user;
  528. DB::beginTransaction();
  529. try {
  530. //保存订单数据
  531. $order = Order::create([
  532. 'user_id' => $user['id'],
  533. 'product_type' => 7,
  534. 'total_amount' => $req['amount'],
  535. 'payment_amount' => $req['amount'],
  536. ]);
  537. $order_sn = build_sn($order['id']);
  538. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  539. //生成支付交易单
  540. $trade_sn = build_sn($order['id'], 3, 'T');
  541. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  542. Payment::create([
  543. 'user_id' => $user['id'],
  544. 'order_id' => $order['id'],
  545. 'trade_sn' => $trade_sn,
  546. 'amount' => $req['amount'],
  547. 'remark' => $payBody,
  548. ]);
  549. //请求支付
  550. $payment = Factory::payment(config('config.wechat_pay'));
  551. $result = $payment->order->unify([
  552. 'body' => $payBody,
  553. 'out_trade_no' => $trade_sn,
  554. 'total_fee' => $req['amount'],
  555. 'trade_type' => 'JSAPI',
  556. 'openid' => $user['openid'],
  557. ]);
  558. if (empty($result['prepay_id'])) {
  559. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  560. return out(null, 702, $errorMsg);
  561. }
  562. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  563. DB::commit();
  564. } catch (Exception $e) {
  565. DB::rollBack();
  566. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  567. }
  568. return out($config);
  569. }
  570. public function orderCancel()
  571. {
  572. $req = request()->post();
  573. $this->validate(request(), [
  574. 'order_id' => 'required|integer'
  575. ]);
  576. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
  577. if ($order['order_status'] == 4) {
  578. return out(null, 10001, '订单已完成,不能取消了');
  579. }
  580. if ($order['order_status'] == 5) {
  581. return out(null, 10002, '订单已取消了,请勿重复操作');
  582. }
  583. if ($order['product_type'] == 3) {
  584. if ($order['order_patient']['appoint_start_time'] + 1800 > time()) {
  585. return out(null, 10003, '预约时间临近,不能取消订单了');
  586. }
  587. DB::beginTransaction();
  588. try {
  589. //改变订单状态
  590. Order::where('id', $req['order_id'])->update(['order_status' => 5]);
  591. //退钱到余额
  592. User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '取消订单退款');
  593. //预约时间段的订单数减1
  594. $orderPatient = OrderPatient::select(['time_period_id', 'appoint_start_time'])->where('order_id', $req['order_id'])->first();
  595. $schedule_date = date('Y-m-d', $orderPatient['appoint_start_time']);
  596. SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $orderPatient['time_period_id'])->where('schedule_date', $schedule_date)->decrement('order_num');
  597. //更新医生的服务人数
  598. Docter::where('id', $order['docter_id'])->decrement('service_persons');
  599. DB::commit();
  600. } catch (Exception $e) {
  601. DB::rollBack();
  602. return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
  603. }
  604. }
  605. else {
  606. return out(null, 10001, '暂时只支持取消门诊预约的订单');
  607. }
  608. return out();
  609. }
  610. }