You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 lines
16 KiB
PHP

<?php
namespace App\Services;
use App\Http\Controllers\API\AspNetZhuanController;
use App\Http\Controllers\API\H5\OrderController;
use App\Http\Controllers\API\PEISApiController;
use App\Http\Controllers\API\XCXApiController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class OrderService
{
//订单项目按科室分类
public function DepartmentItemCount($id)
{
$order=DB::table('orders')->where('id',$id)->first();
if(!$order) return ['status'=>false,'msg'=>'订单不存在'];
$hospital=DB::table('hospitals')->where('id',$order->hospital_id)->first();
if(!$hospital) return ['status'=>false,'msg'=>'医院不存在'];
$wait_day=false;
$is_checked_sign_in=false;
if(isset($order->tj_number) and !empty($order->tj_number)){
$peis = new PEISApiController();
$wait=$peis::Get($peis::Api('报告时间计算',$order->tj_number),$hospital->code,"报告时间计算");
if(isset($wait['Data'][0]['预计所需天数'])){
$wait_day=$wait['Data'][0]['预计所需天数'];
} else {
$wait_day=-1;
//查询体检报告
$data = [
'电话号码' => "",
'证件号码' => "",
'体检号' => $order->tj_number,
'包含内部信息' => true
];
$res = $peis::Post('体检报告查询', $order->hospital_id, $data);
if(count($res['data']) > 0) {
$res = $res['data'][0];
if (isset($res['收费项目列表'])) {
foreach ($res['收费项目列表'] as $it) {
if($it['项目状态']=='未完成'){
$is_checked_sign_in=false;
break;
}else{
$is_checked_sign_in=true;
}
}
}
}
}
}
$buy_info=json_decode($order->buy_info,true);
$list=[];
$drop_ids = ['519', '603'];
if($buy_info['combo']['id']<>0){
$combo_items=DB::table('combo_items')->where(['combo_id'=>$buy_info['combo']['id'],'status'=>1])->get();
foreach($combo_items as $item){
if(!in_array((string)$item->keshi_id,$drop_ids)){
if($item->keshi_id==711){
if($order->sex==1){
$item->keshi_id=526;
$item->keshi_name='外科';
}
if($order->sex==2){
$item->keshi_id=570;
$item->keshi_name='内科';
}
}
if($item->keshi_id==695 or $item->keshi_id==559){
$item->keshi_id=532;
$item->keshi_name='妇科';
}
$list[]=[
'item_id'=>$item->item_id,
'keshi_name'=>$item->keshi_name,
'keshi_id'=>$item->keshi_id,
];
}
}
}
if(count($buy_info['items'])>0){
$item_ids=[];
foreach($buy_info['items'] as $item){
$item_ids[]=$item['id'];
}
$itemsInfo=DB::table('items')->whereIn('item_id',$item_ids)->get();
foreach($itemsInfo as $item){
if($item->keshi_id==711){
if($order->sex==1){
$item->keshi_id=526;
$item->keshi_name='外科';
}
if($order->sex==2){
$item->keshi_id=570;
$item->keshi_name='内科';
}
}
if($item->keshi_id==695 or $item->keshi_id==559){
$item->keshi_id=532;
$item->keshi_name='妇科';
}
$list[]=[
'item_id'=>$item->item_id,
'keshi_name'=>$item->keshi_name,
'keshi_id'=>$item->keshi_id,
];
}
}
if($buy_info['group']['id']<>''){
$item_ids=[];
foreach($buy_info['group']['items'] as $item){
$item_ids[]=$item['id'];
}
$itemsInfo=DB::table('items')->whereIn('item_id',$item_ids)->get();
foreach($itemsInfo as $item){
if(!in_array((string)$item->keshi_id,$drop_ids)){
if($item->keshi_id==711){
if($order->sex==1){
$item->keshi_id=526;
$item->keshi_name='外科';
}
if($order->sex==2){
$item->keshi_id=570;
$item->keshi_name='内科';
}
}
if($item->keshi_id==695 or $item->keshi_id==559){
$item->keshi_id=532;
$item->keshi_name='妇科';
}
$list[]=[
'item_id'=>$item->item_id,
'keshi_name'=>$item->keshi_name,
'keshi_id'=>$item->keshi_id,
];
}
}
}
$groupedData = [];
foreach ($list as $key => $item) {
$keshiName = $item['keshi_name'];
if (!isset($groupedData[$keshiName])) {
$groupedData[$keshiName] = [];
}
$groupedData[$keshiName][] = $item;
}
$de_list=[];
foreach ($groupedData as $key => $item) {
$de_list[]=[
'name'=>$key,
'keshi_id'=>$item[0]['keshi_id'],
'count'=>count($item),
];
}
return ['department_list'=>$de_list,'wait_day'=>$wait_day,'is_checked_sign_in'=>$is_checked_sign_in];
}
public function Cancel($orderInfo)
{
$person=DB::table('web_user_person')->where(['id' => $orderInfo->person_id])->first();
$now_datetime=date('Y-m-d H:i:s');
//调用接口恢复积分和预存款
$env=config('app.globals.Env');
$AspNet=new AspNetZhuanController();
$jifen_huifu_status=true;
$yucunkuan_huifu_status=true;
$r_yyid=$orderInfo->hospital_id;
if($r_yyid==1){
$yyid=6;
}
if($r_yyid==4){
$yyid=2;
}
if($env=='pro') { //如果是正式环境
//如果有二线取消二线
$AspNet=new AspNetZhuanController();
$erxian_info=json_decode($orderInfo->erxian_appointment_info, true);
if (isset($erxian_info) and !empty($erxian_info)) {
foreach ($erxian_info as $key => $plan_nmr) {
if(isset($plan_nmr['gid'])){
//调用接口校验号源是否可用
$erxian_status = $AspNet::ErXian(['id' =>$plan_nmr['gid'], 'yyid' => $yyid, 'action' => 3,'leixing'=>$plan_nmr['leixing']], uniqid());
$erxian_info[$key]['gid']='';
$ex_u= DB::table('orders')->where(['id' => $orderInfo->id])->update([
'erxian_appointment_info'=>json_encode($erxian_info, JSON_UNESCAPED_UNICODE),
]);
}
}
}
if($orderInfo->jifen>0 and $orderInfo->is_refund_jifen==0) {
$jifen_huifu_status=false;
$jifen_huifu_status= $AspNet::UseJiFen($person->ghzid,$orderInfo->jifen,$yyid,$orderInfo->id,'tj_h5','抵扣体检H5订单',$now_datetime);
if( $jifen_huifu_status===true){
DB::table('orders')->where('id',$orderInfo->id)->update(['is_refund_jifen'=>1]);
}
}
if($orderInfo->yucunkuan>0 and $orderInfo->is_refund_yucunkuan==0) {
$yucunkuan_huifu_status=false;
$yucunkuan_huifu_status= $AspNet::UseYuCunKuan($person->ghzid,$orderInfo->yucunkuan,$yyid,0,$orderInfo->id,'tj_h5','抵扣体检H5订单',$now_datetime);
if($yucunkuan_huifu_status===true){
DB::table('orders')->where('id',$orderInfo->id)->update(['is_refund_yucunkuan'=>1]);
}
}
if(!empty($orderInfo->youhuiquan)){
$youhuiquan=json_decode( $orderInfo->youhuiquan,true);
$data=[
'action'=>4,
'ghzid'=>$person->ghzid,
'dzjid'=>$youhuiquan['id'],
'hxbz'=>"H5撤销核销",
'yyid'=>$yyid
];
$AspNet::YouHuiQuan($data);
}
}
$sanfangcode=json_decode($orderInfo->sanfang_code,true);
if($sanfangcode and !empty($sanfangcode)){
$s=new SanFangCodeService();
$cancel_hexiao=$s->CancelSanFangCode($sanfangcode['qudao_type'],$sanfangcode['code'],$orderInfo->id );
}
DB::table('orders')->where(['id' => $orderInfo->id,'status'=>1])->update([
'status' => 3
]);
//恢复号源
$up_plan = DB::table('plans')->where(['id' => $orderInfo->plan_id, 'status' => 2])->update([
'status' => 1
]);
if($orderInfo->appointment_number<>null and $orderInfo->appointment_number<>'' and strpos($orderInfo->source, '线下体检预约') !== false){
$ap = new OrderController();
$cancel = $ap->cancel_appointment($orderInfo->hospital_id, [
'type' => $orderInfo->type,
'预约Id' => $orderInfo->appointment_number
]);
$orderJson = json_encode($orderInfo, JSON_UNESCAPED_UNICODE);
$insert=DB::table('orders_delete')->insert(['order_info' =>$orderJson]);
if($insert){
DB::table('orders')->where(['id' => $orderInfo->id])->delete();
}
}
return true;
}
public function Refund($id,$yuanyin='')
{
$orderInfo = DB::table('orders')->where(['id' => $id])->first();
if (!$orderInfo) return ['status'=>false,'msg'=>"未找到有效订单"];
if ($orderInfo->status !== 2) return ['status'=>false,'msg'=>"订单状态异常。当前状态:" . $orderInfo->status];
if ($orderInfo->check_status == 2) return ['status'=>false,'msg'=>"已登记体检,禁止退款"];
$person=DB::table('web_user_person')->where(['id' => $orderInfo->person_id])->first();
if(!$person) return ['status'=>false,'msg'=>"用户不存在"];
//调用思信取消,恢复号源
if($orderInfo->appointment_number<>null and $orderInfo->appointment_number<>''){
$ap = new OrderController();
$cancel = $ap->cancel_appointment($orderInfo->hospital_id, [
'type' => $orderInfo->type,
'预约Id' => $orderInfo->appointment_number
]);
if ($cancel['code'] != 0) return ['status'=>false,'msg'=>"取消预约失败," . $cancel['message']];
}
//如果有二线取消二线
$yyid=6;
if($orderInfo->hospital_id == 1){
$yyid=6;
}
if($orderInfo->hospital_id == 4){
$yyid=2;
}
$AspNet=new AspNetZhuanController();
$erxian_info=json_decode($orderInfo->erxian_appointment_info, true);
if (isset($erxian_info) and !empty($erxian_info)) {
foreach ($erxian_info as $key => $plan_nmr) {
if(isset($plan_nmr['gid']) and !empty($plan_nmr['gid'])){
//调用接口取消二线
$erxian_status = $AspNet::ErXian(['id' =>$plan_nmr['gid'], 'yyid' => $yyid, 'action' => 3,'leixing'=>$plan_nmr['leixing']], uniqid());
$erxian_info[$key]['gid']='';
$ex_u= DB::table('orders')->where(['id' => $orderInfo->id])->update([
'erxian_appointment_info'=>json_encode($erxian_info, JSON_UNESCAPED_UNICODE),
]);
}
}
}
$refund_yuanyin="体检H5订单退款";
if(isset($yuanyin) and!empty($yuanyin)){
$refund_yuanyin=$yuanyin;
}
//如果真实支付大于0 则调用小程序退款
if ($orderInfo->true_price > 0) {
$data = [
'orderid' => $orderInfo->order_number,
'refund_order_id' => 'T' . $orderInfo->order_number,
'refund_amount' => (int)($orderInfo->true_price * 100),
'refund_reason' => $refund_yuanyin,
];
$XCX = new XCXApiController();
$res = $XCX::Post('订单退款', $data);
if ($res['data']['refund_state'] != 'SUCCESS') {
return ['status'=>false,'msg'=>"退款失败" . $res['data']['refund_state']];
}
}
$now_datetime=date('Y-m-d H:i:s');
//调用接口恢复积分和预存款
$env=config('app.globals.Env');
$AspNet=new AspNetZhuanController();
$jifen_huifu_status=true;
$yucunkuan_huifu_status=true;
$r_yyid=$orderInfo->hospital_id;
if($r_yyid==1){
$yyid=6;
}
if($r_yyid==4){
$yyid=2;
}
if($env=='pro') { //如果是正式环境
if($orderInfo->jifen>0 and $orderInfo->is_refund_jifen==0) {
$jifen_huifu_status=false;
$jifen_huifu_status= $AspNet::UseJiFen($person->ghzid,$orderInfo->jifen,$yyid,$orderInfo->id,'tj_h5','抵扣体检H5订单',$now_datetime);
if( $jifen_huifu_status===true){
DB::table('orders')->where('id',$orderInfo->id)->update(['is_refund_jifen'=>1]);
}
}
if($orderInfo->yucunkuan>0 and $orderInfo->is_refund_yucunkuan==0) {
$yucunkuan_huifu_status=false;
$yucunkuan_huifu_status= $AspNet::UseYuCunKuan($person->ghzid,$orderInfo->yucunkuan,$yyid,0,$orderInfo->id,'tj_h5','抵扣体检H5订单',$now_datetime);
if($yucunkuan_huifu_status===true){
DB::table('orders')->where('id',$orderInfo->id)->update(['is_refund_yucunkuan'=>1]);
}
}
if(!empty($orderInfo->youhuiquan)){
$youhuiquan=json_decode( $orderInfo->youhuiquan,true);
$data=[
'action'=>4,
'ghzid'=>$person->ghzid,
'dzjid'=>$youhuiquan['id'],
'hxbz'=>"H5撤销核销",
'yyid'=>$yyid
];
$AspNet::YouHuiQuan($data);
}
}
$sanfangcode=json_decode($orderInfo->sanfang_code,true);
if($sanfangcode and !empty($sanfangcode)){
$s=new SanFangCodeService();
$cancel_hexiao=$s->CancelSanFangCode($sanfangcode['qudao_type'],$sanfangcode['code'],$orderInfo->id );
}
if($orderInfo->checkup_type_id==4){ //如果是婚检调用his取消
$his_info=json_decode($orderInfo->his_info,true);
if(!!$his_info and isset($his_info['hunjian_yuyue_gid']) and !empty($his_info['hunjian_yuyue_gid'])){
$his_data=[
"action" => "2",
"yyid" => $yyid,
"ghzid" => $person->ghzid,
"ID"=>$his_info['hunjian_yuyue_gid']
];
$res=$AspNet::HunjianHis($his_data);
if ($res['code'] == "200") {
$his_info['hunjian_yuyue_gid']=null;
$u = DB::table('orders')->where(['id' => $orderInfo->id])->update([
'his_info'=>json_encode($his_info, JSON_UNESCAPED_UNICODE)
]);
}
}
}
DB::table('orders')->where(['id' => $id])->update([
'status' => 5,
'refund_time'=>$now_datetime
]);
//恢复号源
$up_plan = DB::table('plans')->where(['id' => $orderInfo->plan_id, 'status' => 2])->update([
'status' => 1
]);
return ['status'=>true,'msg'=>"退款成功"];
}
//取消转赠
public function CancelZhuanZeng($NewOrderid)
{
$newOrderInfo = DB::table('orders')->where(['id' => $NewOrderid])->first();
if (!$newOrderInfo) return ['status'=>false,'msg'=>"id:".$NewOrderid.",未找到有效订单"];
if ($newOrderInfo->status!== 2) return ['status'=>false,'msg'=>"id:".$NewOrderid."订单状态异常。当前状态:". $newOrderInfo->status];
if (!empty($newOrderInfo->person_id)) return ['status'=>false,'msg'=>"id:".$NewOrderid."已被接收,禁止取消:"];
if(strstr($newOrderInfo->source,"from")){
$from=explode("from",$newOrderInfo->source);
if(count($from)<>2) return ['status'=>false,'msg'=>"id:".$NewOrderid."接收转赠异常,无法取消"];
$oldOrderid=$from[1];
}else{
return ['status'=>false,'msg'=>"id:".$NewOrderid."非转赠订单,禁止取消"];
}
DB::beginTransaction();
$u_new=DB::table('orders')->where(['id' => $NewOrderid])->update([
'status' => 3
]);
$u_order=DB::table('orders')->where(['id' => $oldOrderid])->update([
'status' => 2,
'transfer_type'=>null,
'transfer_link'=>null,
]);
if ($u_new and $u_order) {
DB::commit();
return ['status'=>true,'msg'=>"取消转赠成功"];
}else{
DB::rollBack();
return ['status'=>false,'msg'=>"取消转赠失败"];
}
}
}