OrderController.php 30 KB

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