123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-9-30
- * Time: 下午10:58
- */
- namespace App\Http\Controllers\Api\V1;
- use App\Models\Nurse;
- use App\Models\Order;
- use App\Models\OrderNurse;
- use App\Models\OrderPack;
- use App\Models\OrderPatient;
- use App\Models\OrderVaccine;
- use App\Models\Patient;
- use App\Models\Payment;
- use App\Models\ServicePack;
- use App\Models\TimePeriod;
- use App\Models\User;
- use App\Models\UserCoupon;
- use App\Models\Vaccine;
- use EasyWeChat\Factory;
- use DB;
- class OrderController extends AuthController
- {
- public function consultPlaceOrder()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'payment_type' => 'required|in:1,2',
- 'product_type' => 'required|in:1,2',
- 'docter_id' => 'required|integer',
- 'patient_id' => 'required|integer',
- 'total_amount' => 'required|integer',
- 'user_coupon_id' => 'integer',
- 'phone' => 'required_if:product_type,1',
- 'phone_minutes' => 'required_if:product_type,1|integer',
- 'symptoms' => 'required_if:product_type,2|max:2000',
- 'medical_imgs' => 'required_if:product_type,2|json|max:3000',
- 'pay_password|支付密码' => 'required_if:payment_type,2|integer',
- ]);
- $user = $this->user;
- if (!empty($req['pay_password'])) {
- if (empty($user['pay_password'])) {
- return out(null, 60010, '未设置支付密码');
- }
- if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
- return out(null, 10001, '密码错误');
- }
- }
- $discount_amount = 0;
- if (!empty($req['user_coupon_id'])) {
- //计算优惠金额
- $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
- }
- $payment_amount = $req['total_amount'] - $discount_amount;
- $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
- if ($req['payment_type'] == 2) {
- if ($user['balance'] < $payment_amount) {
- return out(null, 601, '余额不足');
- }
- }
- $order_status = $payment_status = 1;
- $payment_time = 0;
- if ($payment_amount == 0 || $req['payment_type'] == 2) {
- $order_status = $payment_status = 2;
- $payment_time = time();
- }
- $config = null;
- DB::beginTransaction();
- try {
- //保存订单数据
- $order = Order::create([
- 'user_id' => $user['id'],
- 'payment_type' => $req['payment_type'],
- 'product_type' => $req['product_type'],
- 'docter_id' => $req['docter_id'],
- 'total_amount' => $req['total_amount'],
- 'payment_amount' => $payment_amount,
- 'discount_amount' => $discount_amount,
- 'patient_id' => $req['patient_id'],
- 'order_status' => $order_status,
- 'payment_status' => $payment_status,
- 'payment_time' => $payment_time,
- ]);
- $order_sn = build_sn($order['id']);
- Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
- //保存订单患者信息
- $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url'])->where('id', $req['patient_id'])->first()->getOriginal();
- $addPatient['order_id'] = $order['id'];
- $addPatient['patient_id'] = $req['patient_id'];
- if ($req['product_type'] == 1) {
- $addPatient['phone'] = $req['phone'];
- $addPatient['phone_minutes'] = $req['phone_minutes'];
- }
- elseif ($req['product_type'] == 2) {
- $addPatient['symptoms'] = $req['symptoms'];
- $addPatient['medical_imgs'] = $req['medical_imgs'];
- }
- OrderPatient::create($addPatient);
- //判断是微信支付
- if ($req['payment_type'] == 1) {
- //生成支付交易单
- if ($payment_amount > 0) {
- $trade_sn = build_sn($order['id'], 3, 'T');
- $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
- Payment::create([
- 'user_id' => $user['id'],
- 'order_id' => $order['id'],
- 'trade_sn' => $trade_sn,
- 'amount' => $payment_amount,
- 'remark' => $payBody,
- ]);
- //请求支付
- $payment = Factory::payment(config('config.wechat_pay'));
- $result = $payment->order->unify([
- 'body' => $payBody,
- 'out_trade_no' => $trade_sn,
- 'total_fee' => $payment_amount,
- 'trade_type' => 'JSAPI',
- 'openid' => $user['openid'],
- ]);
- if (empty($result['prepay_id'])) {
- $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
- return out(null, 702, $errorMsg);
- }
- $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
- }
- }
- //判断是余额支付
- elseif ($req['payment_type'] == 2) {
- if ($payment_amount > 0) {
- //改变用户余额
- $change_amount = 0 - $payment_amount;
- User::changeBalance($user['id'], $change_amount, 1, $order['id'], '咨询订单消费');
- Order::payCompletedHandle($order['id']);
- }
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
- }
- return out($config);
- }
- public function appointPlaceOrder()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'payment_type' => 'required|in:1,2',
- 'product_type' => 'required|in:3,4,5',
- 'patient_id' => 'required|integer',
- 'organization_id' => 'required|integer',
- 'schedule_date' => 'required|date',
- 'time_period_id' => 'required|integer',
- 'total_amount' => 'required|integer',
- 'user_coupon_id' => 'integer',
- 'docter_id' => 'required_if:product_type,3|integer',
- 'vaccine_id' => 'required_if:product_type,4|integer',
- 'nurse_ids' => 'required_if:product_type,5|json',
- 'pay_password|支付密码' => 'required_if:payment_type,2|integer',
- ]);
- $user = $this->user;
- if (!empty($req['pay_password'])) {
- if (empty($user['pay_password'])) {
- return out(null, 60010, '未设置支付密码');
- }
- if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
- return out(null, 10001, '密码错误');
- }
- }
- $product_type = $req['product_type'];
- if ($req['product_type'] == 4) {
- $vaccine = Vaccine::select(['type'])->where('id', $req['vaccine_id'])->first();
- if ($vaccine['type'] == 2) {
- if (empty($req['total_amount'])) {
- return out(null, 10001, '总价不能为0');
- }
- }
- }
- $discount_amount = 0;
- if (!empty($req['user_coupon_id'])) {
- //计算优惠金额
- $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $product_type);
- }
- $payment_amount = $req['total_amount'] - $discount_amount;
- $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
- if ($req['payment_type'] == 2) {
- if ($user['balance'] < $payment_amount) {
- return out(null, 601, '余额不足');
- }
- }
- $order_status = $payment_status = 1;
- $payment_time = 0;
- if ($payment_amount == 0 || $req['payment_type'] == 2) {
- $order_status = 3;
- $payment_status = 2;
- $payment_time = time();
- }
- $config = null;
- DB::beginTransaction();
- try {
- //保存订单数据
- $order = Order::create([
- 'user_id' => $user['id'],
- 'docter_id' => $req['docter_id'] ?? 0,
- 'patient_id' => $req['patient_id'],
- 'organization_id' => $req['organization_id'],
- 'payment_type' => $req['payment_type'],
- 'product_type' => $product_type,
- 'order_status' => $order_status,
- 'payment_status' => $payment_status,
- 'total_amount' => $req['total_amount'],
- 'payment_amount' => $payment_amount,
- 'discount_amount' => $discount_amount,
- 'payment_time' => $payment_time,
- ]);
- $order_sn = build_sn($order['id']);
- Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
- //保存订单患者信息
- $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url'])->where('id', $req['patient_id'])->first()->getOriginal();
- $addPatient['order_id'] = $order['id'];
- $addPatient['patient_id'] = $req['patient_id'];
- $addPatient['organization_id'] = $req['organization_id'];
- $addPatient['time_period_id'] = $req['time_period_id'];
- $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
- $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
- $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
- $orderPatient = OrderPatient::create($addPatient);
- //保存订单疫苗信息
- if ($req['product_type'] == 4) {
- $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();
- $addVaccine['order_id'] = $order['id'];
- $addVaccine['order_patient_id'] = $orderPatient['id'];
- OrderVaccine::create($addVaccine);
- }
- //保存儿保订单信息
- if ($req['product_type'] == 5) {
- $nurse_ids = json_decode($req['nurse_ids'], true);
- foreach ($nurse_ids as $k => $v) {
- $addNurse = Nurse::select(['id as nurse_id', 'price as nurse_price', 'name as nurse_name', 'remark as nurse_remark'])->where('id', $v)->first()->getOriginal();
- $addNurse['order_id'] = $order['id'];
- $addNurse['order_patient_id'] = $orderPatient['id'];
- OrderNurse::create($addNurse);
- }
- }
- //判断是微信支付
- if ($req['payment_type'] == 1) {
- //生成支付交易单
- if ($payment_amount > 0) {
- $trade_sn = build_sn($order['id'], 3, 'T');
- $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
- Payment::create([
- 'user_id' => $user['id'],
- 'order_id' => $order['id'],
- 'trade_sn' => $trade_sn,
- 'amount' => $payment_amount,
- 'remark' => $payBody,
- ]);
- //请求支付
- $payment = Factory::payment(config('config.wechat_pay'));
- $result = $payment->order->unify([
- 'body' => $payBody,
- 'out_trade_no' => $trade_sn,
- 'total_fee' => $payment_amount,
- 'trade_type' => 'JSAPI',
- 'openid' => $user['openid'],
- ]);
- if (empty($result['prepay_id'])) {
- $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
- return out(null, 702, $errorMsg);
- }
- $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
- }
- }
- //判断是余额支付
- elseif ($req['payment_type'] == 2) {
- if ($payment_amount > 0) {
- //改变用户余额
- $change_amount = 0 - $payment_amount;
- User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
- Order::payCompletedHandle($order['id']);
- }
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
- }
- return out($config);
- }
- public function packPlaceOrder()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'payment_type' => 'required|in:1,2',
- 'patient_id' => 'required|integer',
- 'total_amount' => 'required|integer',
- 'user_coupon_id' => 'integer',
- 'service_pack_id' => 'required|integer',
- 'is_security' => 'required|in:0,1',
- 'guardian_name' => 'required|max:50',
- 'relationship_type' => 'required|integer',
- 'pay_password|支付密码' => 'required_if:payment_type,2|integer',
- ]);
- $user = $this->user;
- if (!empty($req['pay_password'])) {
- if (empty($user['pay_password'])) {
- return out(null, 60010, '未设置支付密码');
- }
- if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
- return out(null, 10001, '密码错误');
- }
- }
- $discount_amount = 0;
- if (!empty($req['user_coupon_id'])) {
- //计算优惠金额
- $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
- }
- $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url'])->where('id', $req['patient_id'])->first()->getOriginal();
- if (empty($addPatient['card_img_url'])) {
- return out(null, 70011, '该患者未上传身份证');
- }
- $payment_amount = $req['total_amount'] - $discount_amount;
- $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
- if ($req['payment_type'] == 2) {
- if ($user['balance'] < $payment_amount) {
- return out(null, 601, '余额不足');
- }
- }
- $order_status = $payment_status = 1;
- $payment_time = 0;
- if ($payment_amount == 0 || $req['payment_type'] == 2) {
- $order_status = 3;
- $payment_status = 2;
- $payment_time = time();
- }
- $config = null;
- DB::beginTransaction();
- try {
- //保存订单数据
- $order = Order::create([
- 'user_id' => $user['id'],
- 'patient_id' => $req['patient_id'],
- 'payment_type' => $req['payment_type'],
- 'product_type' => 6,
- 'total_amount' => $req['total_amount'],
- 'payment_amount' => $payment_amount,
- 'discount_amount' => $discount_amount,
- 'order_status' => $order_status,
- 'payment_status' => $payment_status,
- 'payment_time' => $payment_time,
- ]);
- $order_sn = build_sn($order['id']);
- Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
- //保存订单患者信息
- $addPatient['order_id'] = $order['id'];
- $addPatient['patient_id'] = $req['patient_id'];
- OrderPatient::create($addPatient);
- //保存订单服务包表
- $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'])->where('id', $req['service_pack_id'])->first()->getOriginal();
- $addPack['order_id'] = $order['id'];
- $addPack['is_security'] = $req['is_security'];
- $addPack['guardian_name'] = $req['guardian_name'];
- $addPack['relationship_type'] = $req['relationship_type'];
- $addPack['start_time'] = time();
- $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
- OrderPack::create($addPack);
- //判断是微信支付
- if ($req['payment_type'] == 1) {
- //生成支付交易单
- if ($payment_amount > 0) {
- $trade_sn = build_sn($order['id'], 3, 'T');
- $payBody = '超级宝妈-'.config('config.product_type_map')[6];
- Payment::create([
- 'user_id' => $user['id'],
- 'order_id' => $order['id'],
- 'trade_sn' => $trade_sn,
- 'amount' => $payment_amount,
- 'remark' => $payBody,
- ]);
- //请求支付
- $payment = Factory::payment(config('config.wechat_pay'));
- $result = $payment->order->unify([
- 'body' => $payBody,
- 'out_trade_no' => $trade_sn,
- 'total_fee' => $payment_amount,
- 'trade_type' => 'JSAPI',
- 'openid' => $user['openid'],
- ]);
- if (empty($result['prepay_id'])) {
- $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
- return out(null, 702, $errorMsg);
- }
- $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
- }
- }
- //判断是余额支付
- elseif ($req['payment_type'] == 2) {
- if ($payment_amount > 0) {
- //改变用户余额
- $change_amount = 0 - $payment_amount;
- User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
- Order::payCompletedHandle($order['id']);
- }
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
- }
- return out($config);
- }
- public function orderList()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'list_type' => 'required|in:0,1,2',
- 'product_type' => 'integer',
- 'order_status' => 'integer',
- 'time_sort' => 'in:0,1',
- 'is_pack_expire' => 'in:0,1,2',
- ]);
- $user = $this->user;
- $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter'])->where('user_id', $user['id']);
- if (!empty($req['product_type'])) {
- $builder->where('product_type', $req['product_type']);
- }
- else {
- if (!empty($req['list_type'])) {
- if ($req['list_type'] == 1) {
- $builder->whereIn('product_type', [1,2]);
- }
- elseif ($req['list_type'] == 2) {
- $builder->whereIn('product_type', [3,4,5]);
- }
- }
- else {
- $builder->where('product_type', '<', 7);
- }
- }
- if (!empty($req['order_status'])) {
- $builder->where('order_status', $req['order_status']);
- }
- if (!empty($req['is_pack_expire'])) {
- $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
- if ($req['is_pack_expire'] == 1) {
- $tmpBuilder->where('order_packs.end_time', '<', time());
- }
- else {
- $tmpBuilder->where('order_packs.end_time', '>=', time());
- }
- $order_ids = $tmpBuilder->pluck('o.id')->toArray();
- $builder->whereIn('id', $order_ids);
- }
- if (!empty($req['time_sort'])) {
- $builder->orderBy('id', 'desc');
- }
- else {
- $builder->orderBy('id', 'asc');
- }
- $data = $builder->paginate();
- return out($data);
- }
- public function orderDetail()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'order_id' => 'required|integer'
- ]);
- $user = $this->user;
- $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack.team.docter', 'orderNurse', 'orderVaccine', 'organization.docter'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first();
- return out($data);
- }
- public function topup()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'amount' => 'required|integer',
- ]);
- $user = $this->user;
- DB::beginTransaction();
- try {
- //保存订单数据
- $order = Order::create([
- 'user_id' => $user['id'],
- 'product_type' => 7,
- 'total_amount' => $req['amount'],
- 'payment_amount' => $req['amount'],
- ]);
- $order_sn = build_sn($order['id']);
- Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
- //生成支付交易单
- $trade_sn = build_sn($order['id'], 3, 'T');
- $payBody = '超级宝妈-'.config('config.product_type_map')[7];
- Payment::create([
- 'user_id' => $user['id'],
- 'order_id' => $order['id'],
- 'trade_sn' => $trade_sn,
- 'amount' => $req['amount'],
- 'remark' => $payBody,
- ]);
- //请求支付
- $payment = Factory::payment(config('config.wechat_pay'));
- $result = $payment->order->unify([
- 'body' => $payBody,
- 'out_trade_no' => $trade_sn,
- 'total_fee' => $req['amount'],
- 'trade_type' => 'JSAPI',
- 'openid' => $user['openid'],
- ]);
- if (empty($result['prepay_id'])) {
- $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
- return out(null, 702, $errorMsg);
- }
- $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
- }
- return out($config);
- }
- }
|