OrderController.php 32 KB

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