OrderController.php 29 KB

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