OrderController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. OrderVaccine::create($addVaccine);
  279. }
  280. //保存儿保订单信息
  281. if ($req['product_type'] == 5) {
  282. $nurse_ids = json_decode($req['nurse_ids'], true);
  283. foreach ($nurse_ids as $k => $v) {
  284. $addNurse = Nurse::select(['id as nurse_id', 'price as nurse_price', 'name as nurse_name', 'remark as nurse_remark'])->where('id', $v)->first()->getOriginal();
  285. $addNurse['order_id'] = $order['id'];
  286. $addNurse['order_patient_id'] = $orderPatient['id'];
  287. OrderNurse::create($addNurse);
  288. }
  289. }
  290. //判断是微信支付
  291. if ($req['payment_type'] == 1) {
  292. //生成支付交易单
  293. if ($payment_amount > 0) {
  294. $trade_sn = build_sn($order['id'], 3, 'T');
  295. $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
  296. Payment::create([
  297. 'user_id' => $user['id'],
  298. 'order_id' => $order['id'],
  299. 'trade_sn' => $trade_sn,
  300. 'amount' => $payment_amount,
  301. 'remark' => $payBody,
  302. ]);
  303. //请求支付
  304. $payment = Factory::payment(config('config.wechat_pay'));
  305. $result = $payment->order->unify([
  306. 'body' => $payBody,
  307. 'out_trade_no' => $trade_sn,
  308. 'total_fee' => $payment_amount,
  309. 'trade_type' => 'JSAPI',
  310. 'openid' => $user['openid'],
  311. ]);
  312. if (empty($result['prepay_id'])) {
  313. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  314. return out(null, 702, $errorMsg);
  315. }
  316. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  317. }
  318. }
  319. //判断是余额支付
  320. elseif ($req['payment_type'] == 2) {
  321. if ($payment_amount > 0) {
  322. //改变用户余额
  323. $change_amount = 0 - $payment_amount;
  324. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
  325. }
  326. }
  327. if ($payment_amount == 0 || in_array($req['payment_type'], [2, 3])) {
  328. Order::payCompletedHandle($order['id']);
  329. }
  330. //如果有优惠券就标记优惠券为已使用
  331. if (!empty($req['user_coupon_id'])) {
  332. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  333. }
  334. DB::commit();
  335. } catch (Exception $e) {
  336. DB::rollBack();
  337. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  338. }
  339. return out($config);
  340. }
  341. public function packPlaceOrder()
  342. {
  343. $req = request()->post();
  344. $this->validate(request(), [
  345. 'payment_type' => 'required|in:1,2',
  346. 'patient_id' => 'required|integer',
  347. 'total_amount' => 'required|integer',
  348. 'user_coupon_id' => 'integer',
  349. 'service_pack_id' => 'required|integer',
  350. 'is_security' => 'required|in:0,1',
  351. 'guardian_name' => 'required|max:50',
  352. 'relationship_type' => 'required|integer',
  353. 'pay_password|支付密码' => 'integer',
  354. 'is_need_insurance' => 'in:0,1',
  355. ]);
  356. $user = $this->user;
  357. if (!empty($req['pay_password'])) {
  358. if (empty($user['pay_password'])) {
  359. return out(null, 60010, '未设置支付密码');
  360. }
  361. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  362. return out(null, 10001, '密码错误');
  363. }
  364. }
  365. $discount_amount = 0;
  366. if (!empty($req['user_coupon_id'])) {
  367. //计算优惠金额
  368. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
  369. }
  370. $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();
  371. $payment_amount = $req['total_amount'] - $discount_amount;
  372. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  373. if ($req['payment_type'] == 2 && $payment_amount > 0 && empty($req['pay_password'])) {
  374. return out(null, 10011, '请输入支付密码');
  375. }
  376. if ($req['payment_type'] == 2) {
  377. if ($user['balance'] < $payment_amount) {
  378. return out(null, 601, '余额不足');
  379. }
  380. }
  381. $order_status = $payment_status = 1;
  382. $payment_time = 0;
  383. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  384. $order_status = 3;
  385. $payment_status = 2;
  386. $payment_time = time();
  387. }
  388. $config = null;
  389. DB::beginTransaction();
  390. try {
  391. //保存订单数据
  392. $order = Order::create([
  393. 'user_id' => $user['id'],
  394. 'patient_id' => $req['patient_id'],
  395. 'payment_type' => $req['payment_type'],
  396. 'product_type' => 6,
  397. 'total_amount' => $req['total_amount'],
  398. 'payment_amount' => $payment_amount,
  399. 'discount_amount' => $discount_amount,
  400. 'order_status' => $order_status,
  401. 'payment_status' => $payment_status,
  402. 'payment_time' => $payment_time,
  403. ]);
  404. $order_sn = build_sn($order['id']);
  405. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  406. //保存订单患者信息
  407. $addPatient['order_id'] = $order['id'];
  408. $addPatient['patient_id'] = $req['patient_id'];
  409. OrderPatient::create($addPatient);
  410. //保存订单服务包表
  411. $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();
  412. $addPack['user_id'] = $user['id'];
  413. $addPack['order_id'] = $order['id'];
  414. $addPack['is_security'] = $req['is_security'];
  415. $addPack['guardian_name'] = $req['guardian_name'];
  416. $addPack['relationship_type'] = $req['relationship_type'];
  417. $addPack['start_time'] = time();
  418. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  419. $addPack['total_phone_minutes'] = $addPack['phone_minutes'];
  420. $addPack['total_chat_num'] = $addPack['chat_num'];
  421. $addPack['total_appoint_num'] = $addPack['appoint_num'];
  422. $addPack['total_vaccine_limit_amount'] = $addPack['vaccine_limit_amount'];
  423. $addPack['total_nurses_limit_amount'] = $addPack['nurses_limit_amount'];
  424. $addPack['label'] = json_decode($addPack['label'], true);
  425. $addPack['is_need_insurance'] = !empty($req['is_need_insurance']) ? $req['is_need_insurance'] : 0;
  426. OrderPack::create($addPack);
  427. //判断是微信支付
  428. if ($req['payment_type'] == 1) {
  429. //生成支付交易单
  430. if ($payment_amount > 0) {
  431. $trade_sn = build_sn($order['id'], 3, 'T');
  432. $payBody = '超级宝妈-'.config('config.product_type_map')[6];
  433. Payment::create([
  434. 'user_id' => $user['id'],
  435. 'order_id' => $order['id'],
  436. 'trade_sn' => $trade_sn,
  437. 'amount' => $payment_amount,
  438. 'remark' => $payBody,
  439. ]);
  440. //请求支付
  441. $payment = Factory::payment(config('config.wechat_pay'));
  442. $result = $payment->order->unify([
  443. 'body' => $payBody,
  444. 'out_trade_no' => $trade_sn,
  445. 'total_fee' => $payment_amount,
  446. 'trade_type' => 'JSAPI',
  447. 'openid' => $user['openid'],
  448. ]);
  449. if (empty($result['prepay_id'])) {
  450. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  451. return out(null, 702, $errorMsg);
  452. }
  453. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  454. }
  455. }
  456. //判断是余额支付
  457. elseif ($req['payment_type'] == 2) {
  458. if ($payment_amount > 0) {
  459. //改变用户余额
  460. $change_amount = 0 - $payment_amount;
  461. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
  462. }
  463. }
  464. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  465. Order::payCompletedHandle($order['id']);
  466. }
  467. //如果有优惠券就标记优惠券为已使用
  468. if (!empty($req['user_coupon_id'])) {
  469. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  470. }
  471. DB::commit();
  472. } catch (Exception $e) {
  473. DB::rollBack();
  474. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  475. }
  476. return out($config);
  477. }
  478. public function orderList()
  479. {
  480. $req = request()->post();
  481. $this->validate(request(), [
  482. 'list_type' => 'required|in:0,1,2',
  483. 'product_type' => 'integer',
  484. 'order_status' => 'integer',
  485. 'time_sort' => 'in:0,1',
  486. 'is_pack_expire' => 'in:0,1,2',
  487. ]);
  488. $user = $this->user;
  489. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('user_id', $user['id']);
  490. if (!empty($req['product_type'])) {
  491. $builder->where('product_type', $req['product_type']);
  492. }
  493. else {
  494. if (!empty($req['list_type'])) {
  495. if ($req['list_type'] == 1) {
  496. $builder->whereIn('product_type', [1,2]);
  497. }
  498. elseif ($req['list_type'] == 2) {
  499. $builder->whereIn('product_type', [3,4,5]);
  500. }
  501. }
  502. else {
  503. $builder->where('product_type', '<', 7);
  504. }
  505. }
  506. if (!empty($req['order_status'])) {
  507. $builder->where('order_status', $req['order_status']);
  508. }
  509. if (!empty($req['is_pack_expire'])) {
  510. $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
  511. if ($req['is_pack_expire'] == 1) {
  512. $tmpBuilder->where('order_packs.end_time', '<', time());
  513. }
  514. else {
  515. $tmpBuilder->where('order_packs.end_time', '>=', time());
  516. }
  517. $order_ids = $tmpBuilder->pluck('o.id')->toArray();
  518. $builder->whereIn('id', $order_ids);
  519. }
  520. if (!empty($req['time_sort'])) {
  521. $builder->orderBy('id', 'desc');
  522. }
  523. else {
  524. $builder->orderBy('id', 'asc');
  525. }
  526. $data = $builder->paginate();
  527. return out($data);
  528. }
  529. public function orderDetail()
  530. {
  531. $req = request()->post();
  532. $this->validate(request(), [
  533. 'order_id' => 'required|integer'
  534. ]);
  535. $user = $this->user;
  536. $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();
  537. if (!empty($data['order_pack'])) {
  538. $data['order_pack']['team'] = [];
  539. if (!empty($data['order_pack']['team_id'])) {
  540. $data['order_pack']['team'] = Team::with(['docter.office', 'docter.qualification'])->whereIn('id', $data['order_pack']['team_id'])->get()->toArray();
  541. }
  542. }
  543. return out($data);
  544. }
  545. public function topup()
  546. {
  547. $req = request()->post();
  548. $this->validate(request(), [
  549. 'amount' => 'required|integer',
  550. ]);
  551. $user = $this->user;
  552. DB::beginTransaction();
  553. try {
  554. //保存订单数据
  555. $order = Order::create([
  556. 'user_id' => $user['id'],
  557. 'product_type' => 7,
  558. 'total_amount' => $req['amount'],
  559. 'payment_amount' => $req['amount'],
  560. ]);
  561. $order_sn = build_sn($order['id']);
  562. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  563. //生成支付交易单
  564. $trade_sn = build_sn($order['id'], 3, 'T');
  565. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  566. Payment::create([
  567. 'user_id' => $user['id'],
  568. 'order_id' => $order['id'],
  569. 'trade_sn' => $trade_sn,
  570. 'amount' => $req['amount'],
  571. 'remark' => $payBody,
  572. ]);
  573. //请求支付
  574. $payment = Factory::payment(config('config.wechat_pay'));
  575. $result = $payment->order->unify([
  576. 'body' => $payBody,
  577. 'out_trade_no' => $trade_sn,
  578. 'total_fee' => $req['amount'],
  579. 'trade_type' => 'JSAPI',
  580. 'openid' => $user['openid'],
  581. ]);
  582. if (empty($result['prepay_id'])) {
  583. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  584. return out(null, 702, $errorMsg);
  585. }
  586. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  587. DB::commit();
  588. } catch (Exception $e) {
  589. DB::rollBack();
  590. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  591. }
  592. return out($config);
  593. }
  594. public function orderCancel()
  595. {
  596. $req = request()->post();
  597. $this->validate(request(), [
  598. 'order_id' => 'required|integer'
  599. ]);
  600. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
  601. if ($order['order_status'] == 4) {
  602. return out(null, 10001, '订单已完成,不能取消了');
  603. }
  604. if ($order['order_status'] == 5) {
  605. return out(null, 10002, '订单已取消了,请勿重复操作');
  606. }
  607. if ($order['product_type'] == 3) {
  608. if ($order['order_patient']['appoint_start_time'] - 1800 < time()) {
  609. return out(null, 10003, '预约时间临近,不能取消订单了');
  610. }
  611. DB::beginTransaction();
  612. try {
  613. Order::orderCancel($req['order_id']);
  614. DB::commit();
  615. } catch (Exception $e) {
  616. DB::rollBack();
  617. return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
  618. }
  619. }
  620. else {
  621. return out(null, 10001, '暂时只支持取消门诊预约的订单');
  622. }
  623. return out();
  624. }
  625. public function orderPackPayList()
  626. {
  627. $req = request()->post();
  628. $this->validate(request(), [
  629. 'docter_id' => 'required|integer',
  630. ]);
  631. $user = $this->user;
  632. $orderPacks = OrderPack::where('user_id', $user['id'])->where('end_time', '>', time())->get()->toArray();
  633. if (!empty($orderPacks)) {
  634. foreach ($orderPacks as $k => $v) {
  635. if ($v['usable_status'] == 0) {
  636. unset($orderPacks[$k]);
  637. }
  638. if (!empty($v['team_id'])) {
  639. if (!TeamDocter::whereIn('team_id', $v['team_id'])->where('docter_id', $req['docter_id'])->exists()) {
  640. unset($orderPacks[$k]);
  641. }
  642. }
  643. }
  644. $orderPacks = array_values($orderPacks);
  645. }
  646. return out($orderPacks);
  647. }
  648. }