OrderController.php 30 KB

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