OrderController.php 30 KB

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