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.

900 lines
35 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\API\H5;
use App\Http\Controllers\API\AspNetZhuanController;
use App\Http\Controllers\API\PEISApiController;
use App\Http\Controllers\Controller;
use App\Lib\Tools;
use App\Services\OrderService;
use App\Services\SanFangCodeService;
use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class OrderNewController extends Controller
{
public function CreateNewOrder()
{
$hospital_id = request('hospital');
$openid = request('openid');
$person_id = request('person_id');
$combo_id = request('combo_id');
$type = request('type');//1个检2团检
$group_id = request('group_id');//团检id
$item_ids = request('item_ids');//自选item的ids
$peiou_info = request('peiou_info');//配偶信息
$sanfang_code=request('sanfang_code'); //第三方code
$configs_youxiaoqi=DB::table('configs')->where(['label'=>"订单有效期"])->first();
$date = new DateTime();
$date->modify('+'.$configs_youxiaoqi->value.' days');
$youxiaoqi_end=$date->format('Y-m-d');
if (count($item_ids) != 0 and $item_ids[0] == "") {
$item_ids = [];
}
$duo_xuan_yi = request('duo_xuan_yi'); //多选1
$plan_nmr_info = request('plan_nmr_info');//核磁号源信息
$doctor = request('doctor');//预约的医生名字
$coupon_id = request('coupon_id');//优惠券
$jifen = request('jifen', 0); //使用的积分数量
$yucunkuan = request('yucunkuan', 0);//使用的预存款数量
$erxian_info = request('erxian_info');//使用的预存款数量
$wj_flag = request('wj'); //问卷标记
if (isset($wj_flag) and $wj_flag == 1) {
$wj_zhekou = config('app.globals.Wj_ZheKou');//问卷过来的折扣率
}
if (!isset($hospital_id)) return \Yz::echoError1('医院id不能为空');
if ($hospital_id == 1) {
$yyid = 6;
}
if ($hospital_id == 4) {
$yyid = 2;
}
if (!isset($openid)) return \Yz::echoError1('openid不能为空');
if (!isset($person_id)) return \Yz::echoError1('person_id不能为空');
if (!isset($type)) return \Yz::echoError1('type体检类型不能为空');
if ($type != 1 && $type != 2) {
return \Yz::echoError1('type参数体检类型错误');
}
if ($type == 2 and !isset($group_id)) return \Yz::echoError1('团检group_id不能为空');
if ($type == 1 and isset($group_id)) return \Yz::echoError1('体检类型:个检 与group_id冲突');
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
$person = DB::table('web_user_person')->where(['id' => $person_id, 'is_del' => 0])->first();
if (!$person) return \Yz::echoError1('体检人不存在');
$title = "自选项目";
$price = 0;
$quanInfo = false;
$true_price = 0;//订单真实支付金额
$buy_info = [
'combo' => [
'id' => 0,
'name' => $title,
'price' => 0,
],
'items' => [],
'group' => [
'id' => '',
],
'nmr_list' => [],
'peiou_info' => [],
];
if (!empty($peiou_info)) {
$buy_info['peiou_info'] = $peiou_info;
}
//如果是套餐
$Nx1_arrInfo = [];
$TJ_Leixing_id = 1;//存储用体检类型
$checkup_type_id = false; //体检类型id
$cha_sanfang_code=false;
if (isset($combo_id) and $combo_id <> 0) {
$combo_info = DB::table('combos')->where(['combo_id' => $combo_id, 'status' => 1])->first();
if (!$combo_info) return \Yz::echoError1("套餐不存在");
if (!isset($combo_info->checkup_type_id)) return \Yz::echoError1("套餐未关联体检类型");
//医院要求购买套餐时,不限制性别,预约和转赠时再限制,所以这段注释掉了
//2025-04-30 18:45又要求限制性别所以又取消注释了
//2025-05-7 16:28 购买不限制性别,所以又注释掉了
// if($combo_info->sex<>0 and $combo_info->sex<>$person->sex) {
// return \Yz::echoError1("套餐性别与体检人性别不匹配");
// }
$checkup_type_id = $combo_info->checkup_type_id;
//构建多选一数据
if (isset($duo_xuan_yi) and !empty($duo_xuan_yi)) {
$combo_Nx1 = json_decode($combo_info->duo_xuan_yi, true);
foreach ($duo_xuan_yi as $r_k => $r_v) {
foreach ($combo_Nx1 as $k => $n1v) {
if ($r_v['zu_name'] == $n1v['组名称']) {
foreach ($n1v['包含项目'] as $k2 => $v2) {
if ($v2['Id'] == $r_v['item_id']) {
if ($v2['科室名称'] == '影像科') {
$buy_info['nmr_list'][] = [
'item_id' => $v2['Id'],
'name' => $v2['名称'],
];
}
$Nx1_arrInfo[] = [
'id' => $v2['Id'],
'name' => $v2['名称'],
'price' => 0
];
}
}
}
}
}
}
$TJ_Leixing_id = $checkup_type_id;
$price += $combo_info->price;
//如果使用了第三方券
if(isset($sanfang_code) and isset($sanfang_code['code_num'])){
if($sanfang_code['combo_id']<>$combo_id) return \Yz::echoError1("转赠码与套餐不匹配");
//校验有效性,临时抵消套餐价格
$sanfang_service = new SanFangCodeService();
$cha_sanfang_code = $sanfang_service->GetSanFangCode($sanfang_code['code_num'], $person_id, $sanfang_code['qudao_type']);
if ($cha_sanfang_code['status'] == true) {
$price=$price-$cha_sanfang_code['data']['price'];
if($price<0){
$price=0;
}
}else{
return \Yz::echoError1($cha_sanfang_code['msg']);
}
// if($sanfang_code['qudao_type']==1){
// $cha_sanfang_code = DB::table('transfer_code')->where(['code' => $sanfang_code['code_num'], 'status' => 1, 'is_del' => 0])->first();
// if (!$cha_sanfang_code) return \Yz::echoError1("此转赠码不可用");
// $price=$price-$cha_sanfang_code->price;
// if($price<0){
// $price=0;
// }
// }
}
$title = $combo_info->name;
$buy_info['combo'] = [
'id' => $combo_info->combo_id,
'name' => $combo_info->name,
'price' => $combo_info->price,
];
//如果有影像科则存储在nmr_list字段
$comboItem = DB::table('combo_items')->where(['combo_id' => $combo_id, 'status' => 1, 'keshi_name' => '影像科'])->get();
if (count($comboItem) > 0) {
foreach ($comboItem as $item) {
$buy_info['nmr_list'][] = [
'item_id' => $item->item_id,
'name' => $item->name,
];
}
}
}
//如果有自选项目
$items_list = [];
if (count($item_ids) != 0) {
$items_list = DB::table('items')->whereIn('item_id', $item_ids)->where(['status' => 1])->get();
$existingIds = [];
$item_price = 0;
foreach ($items_list as $item) {
$item_price += $item->price;
$existingIds[] = $item->item_id;
$buy_info['items'][] = [
'id' => $item->item_id,
'name' => $item->name,
'price' => $item->price
];
//如果有影像科则存储在nmr_list字段
if ($item->keshi_name == '影像科') {
$buy_info['nmr_list'][] = [
'item_id' => $item->item_id,
'name' => $item->name,
];
}
}
// if (isset($wj_flag) and $wj_flag == 1) {
// $item_price = $item_price * $wj_zhekou;
// }
$item_price = $item_price * config('app.globals.ZiXuan_ZheKou'); //所有自选项目打8折
$price += $item_price;
$missingIds = array_diff($item_ids, $existingIds);
if (count($missingIds) > 0) return \Yz::echoError1("部分自选项目不可用Id:" . implode(', ', $missingIds));
}
//如果有 多选一项目
if (!empty($Nx1_arrInfo)) {
$buy_info['items'] = array_merge($buy_info['items'], $Nx1_arrInfo);
}
$true_price = $price;
//如果是团检
$group_info = false;
if ($type == 2) {
$P = new PersonController();
$data = [
'电话号码' => null,
'证件号码' => null,
'预约Id' => $group_id
];
$group_info = $P->group_info($hospital_id, $data);
$group_info = $group_info[0];
//如果有多选一项目
//构建多选一数据
$Nx1_arrInfo = [];
if (isset($duo_xuan_yi) and !empty($duo_xuan_yi)) {
foreach ($duo_xuan_yi as $r_k => $r_v) {
$Nx1_arrInfo[] = [
'id' => $r_v['item_id'],
'name' => $r_v['item_name'],
];
}
}
$group_info['items'] = array_merge($group_info['items'], $Nx1_arrInfo); //合并多选一
$buy_info['group'] = [
'id' => $group_id,
'combo_name' => $group_info['combo_name'],
'combo_id' => $group_info['combo_id'],
'group_name' => $group_info['group_name'],
'items' => $group_info['items'],
'group_id' => $group_info['group_id'],
'have_Nx1' => !empty($Nx1_arrInfo)
];
$TJ_Leixing_id = $group_info['checkup_type_id'];
$title = "单位团检" . $group_info['combo_name'];
$price = $price + $group_info['sixi_zong_ji_jin_e'];
$need_pay = ($price - $group_info['tongshou_xiane']) > 0 ? $price - $group_info['tongshou_xiane'] : 0;
//团检订单金额为减去统收后的金的
$price = $need_pay;
$true_price = $need_pay;
//如果有影像科则存储在nmr_list字段
$all_items = DB::table('items')->where(['status' => 1, 'keshi_name' => '影像科'])->get();
foreach ($group_info['items'] as $item) {
foreach ($all_items as $it) {
if ($it->item_id == $item['id']) {
$buy_info['nmr_list'][] = [
'item_id' => $it->item_id,
'name' => $it->name,
];
}
}
}
}
//调用思信接口判断各个项目是否可用,单纯购买,不预约,不调用思信接口,所以注释掉了------------------------⬇⬇⬇⬇⬇⬇
// $check_items = [];
// foreach ($item_ids as $item_id) {
// $check_items[] = ['Id' => $item_id];
// }
// if ((isset($combo_id) and $combo_id <> 0) || count($check_items) != 0) {
// $item_check = self::item_check($hospital_id, [
// '人员信息列表' => [[
// "序号" => 0,
// "性别" => $person->sex == 1 ? '男' : '女',
// "年龄" => floor((time() - strtotime($person->birthday)) / 86400 / 360),
// "婚姻状态" => $person->married == 1 ? '已婚' : '未婚',
// ]],
// '套餐Id' => $combo_id == 0 ? null : $combo_id,
// '可选项目信息' => $check_items,
// ]);
// if (count($item_check['data']) != 1) {
// return \Yz::echoError1("体检系统提示:" . $item_check['message']);
// }
// }
//------------------------------------------------------------------------------⬆⬆⬆⬆
$plan_nmr_appointment_info = [];
//校验积分和预存款
$env = config('app.globals.Env');
$AspNet = new AspNetZhuanController();
if ($env == 'pro') { //如果是正式环境, 测试环境不抵扣
if ($jifen > 0) {
$all_jifen = $AspNet::GetJiFen_YuCunKuan(1, $person->ghzid);
if ($jifen > $all_jifen) return \Yz::echoError1("用户剩余积分不足");
}
if ($yucunkuan > 0) {
// return \Yz::echoError1("暂不支持预存款");
$all_yucunkuan = $AspNet::GetJiFen_YuCunKuan(2, $person->ghzid);
if ($yucunkuan > $all_yucunkuan) return \Yz::echoError1("用户剩余预存款不足");
}
$true_price = $true_price - ($jifen + $yucunkuan);
if ($true_price < -1) return \Yz::echoError1("预抵扣金额超过订单金额,操作失败");
if ($true_price < 0) $true_price = 0;
//如果有二线号源,根据体检时间查询二线可用号源区分上下午二线需预约体检时间1小时后
$plan_nmr_info = [];
if (isset($erxian_info) and !empty($erxian_info)) {
foreach ($erxian_info as $erxian_item) {
$temp_date = [
"item_id" => $erxian_item['item_id'],
"name" => $erxian_item['name'],
"price" => $erxian_item['price'],
"leixing"=>$erxian_item['leixing'],
];
$plan_nmr_info[] = $temp_date;
}
}
//使用优惠券
if (isset($coupon_id) and !empty($coupon_id)) {
if ($true_price == 0) return \Yz::echoError1("金额为0无需使用优惠券");
$use_quan = $this->useYouHuiQuan($true_price, $person, $coupon_id);
$quanInfo = $use_quan['quanInfo'];
$true_price = $use_quan['price'];
}
$now_datetime = date('Y-m-d H:i:s');
//构建订单号
$order_num = $this->generateOrderNumber();
$data = [
'title' => $title,
'type' => $type,
'web_user_id' => $user->id,
'checkup_type_id' => $TJ_Leixing_id,
'person_id' => $person->id,
'name' => $person->name,
'id_number' => $person->id_number,
'buy_info' => json_encode($buy_info, JSON_UNESCAPED_UNICODE),
'price' => $price,
'true_price' => number_format($true_price, 2, '.', ''),
'jifen' => $jifen,
'yucunkuan' => $yucunkuan,
'order_number' => $order_num,
'status' => 99, //先标记为异常,方法最底部更新为正常状态
'erxian_appointment_info' => json_encode($plan_nmr_info, JSON_UNESCAPED_UNICODE),
'combo_id' => $combo_id,
'hospital_id' => $hospital_id,
'doctor' => $doctor,
'phone' => $person->phone,
'sex' => $person->sex,
'birthday' => $person->birthday,
'married' => $person->married,
'wj_flag' => $wj_flag,
'youhuiquan' => $quanInfo === false ? '' : json_encode(['id' => $quanInfo['DZJID'], 'name' => $quanInfo['DZJLBMC']], JSON_UNESCAPED_UNICODE),
'created_at' => $now_datetime,
'paycheck_time' => $now_datetime,
'youxiaoqi_end' => $youxiaoqi_end
];
$insert = DB::table('orders')->insertGetId($data);
//调用接口扣除积分和预存款、优惠券
$jifen_dikou_status = true;
$yucunkuan_dikou_status = true;
$youhuiquan_dikou_status = true;
if ($env == 'pro') { //如果是正式环境
$nowDateTime = date('Y-m-d H:i:s');
if ($jifen > 0) {
$jifen_dikou_status = false;
$jifen_dikou_status = $AspNet::UseJiFen($person->ghzid, -$jifen, $yyid, $insert, 'tj_h5', '抵扣体检H5订单', $now_datetime);
}
if ($yucunkuan > 0) {
$yucunkuan_dikou_status = false;
$yucunkuan_dikou_status = $AspNet::UseYuCunKuan($person->ghzid, -$yucunkuan, $yyid, 0, $insert, 'tj_h5', '抵扣体检H5订单', $now_datetime);
}
//核销优惠券
if (isset($coupon_id) and !empty($coupon_id)) {
$youhuiquan_dikou_status = false;
$data = [
'action' => 3,
'ghzid' => $person->ghzid,
'dzjid' => $coupon_id,
'hxbz' => "抵扣体检H5订单",
'yyid' => $yyid
];
$youhuiquan_dikou_status = $AspNet::YouHuiQuan($data);
}
//核销三方券码(转赠码等)
if(!!$cha_sanfang_code and $cha_sanfang_code['status']==true){
$s= new SanFangCodeService();
$hexiao=$s->HeXiaoSanFangCode($sanfang_code['qudao_type'],$cha_sanfang_code['data']['code_num'],$insert);
if($hexiao['status']===false) return \Yz::echoError1($hexiao['msg']);
}
}
if ($insert and $jifen_dikou_status and $yucunkuan_dikou_status and $youhuiquan_dikou_status) {
//中途未报错,更新订单为待支付
DB::table('orders')->where('id', $insert)->update([
'status' => 1 //标记为待支付
]);
$action = false;
if ($true_price == 0) {
DB::table('orders')->where('id', $insert)->update([
'status' => 2 //标记为已支付
]);
}
if ($true_price > 0) {
//如果大于0则提示前端去支付
$action = "pay";
}
return \Yz::return(true, "操作成功", ['action' => $action, 'orderid' => $insert]);
} else {
return \Yz::echoError1('操作失败');
}
}
}
//预约,用于用户先支付,后期再预约
public function StartYuYue(){
$plan_id = request('plan_id');
$orderid = request('orderid');
$doctor = request('doctor');
$person_id = request('person_id');
$order = DB::table('orders')->where(['id' => $orderid])->first();
if(!$order) return \Yz::echoError1("订单不存在");
if($order->status!== 2) return \Yz::echoError1("订单不是已支付状态不能预约");
if (!isset($plan_id)) return \Yz::echoError1('请选择预约时间');
if(isset($person_id)){ //如果默认体检人与订单不符 则更换为默认体检人
$person_new=DB::table('web_user_person')->where(['id' => $person_id,'is_del' => 0])->first();
if(!!$person_new){
DB::table('orders')->where(['id' => $orderid])->update([
'person_id' => $person_id,
'name' => $person_new->name,
'phone' => $person_new->phone,
'id_number' => $person_new->id_number,
'birthday' => $person_new->birthday,
'sex'=>$person_new->sex
]);
$order->person_id=$person_id;
}
}
$person = DB::table('web_user_person')->where(['id' => $order->person_id, 'is_del' => 0])->first();
if (!$person) return \Yz::echoError1('体检人不存在');
if(empty($person->id_number)) return \Yz::echoError1("用户证件号不能为空");
$combo_info = DB::table('combos')->where(['combo_id' => $order->combo_id, 'status' => 1])->first();
if (!$combo_info) return \Yz::echoError1("套餐不存在");
if($combo_info->sex<>0 and $combo_info->sex<>$person->sex) {
return \Yz::echoError1("套餐性别与体检人性别不匹配");
}
if ($order->hospital_id == 1) {
$yyid = 6;
}
if ($order->hospital_id == 4) {
$yyid = 2;
}
//检查号源是否可用
$plan = new PlanController();
$plan_check = $plan->CheckPlan($plan_id, $order->hospital_id, $order->type, $order->sex, $order->price, $order->checkup_type_id,$order->web_user_id);
if ($plan_check['status'] === false) return \Yz::echoError1($plan_check['msg']);
$plan = $plan_check['plan'];
//校验号源日期是否超过订单有效期
if(isset($order->youxiaoqi_end) and $plan->date > $order->youxiaoqi_end){
return \Yz::echoError1("请预约".$order->youxiaoqi_end."之前的日期");
}
$plan_nmr_appointment_info=[];
//如果有二线号源,根据体检时间查询二线可用号源区分上下午二线需预约体检时间1小时后
$plan_nmr_info=[];
$erxian_info=json_decode($order->erxian_appointment_info,true);
$AspNet = new AspNetZhuanController();
if (isset($erxian_info) and !empty($erxian_info)) {
foreach ($erxian_info as $key=> $erxian_item) {
if($erxian_item['leixing']==1){
$nmrPlans=$AspNet::ErXian(['yyid'=>6,'data'=>[$plan->date],'action'=>"1",'leixing'=>$erxian_item['leixing']],uniqid());
$end_time="23:59";
if(!isset($nmrPlans[$plan->date]) or empty($nmrPlans[$plan->date])) return \Yz::echoError1("二线号源不可用,请重新选择日期");
$temp_date=[];
$planTime = new DateTime($plan->time);
$planTime->modify('+1 hour');
$plan_time=$planTime->format('H:i');
foreach ($nmrPlans[$plan->date] as $nmp_p){
if($nmp_p['Time']>=$plan_time and $nmp_p['Time']<=$end_time and $nmp_p['keyong']==="0"){
$temp_date=[
"item_id"=>$erxian_item['item_id'],
"name"=>$erxian_item['name'],
"price"=>$erxian_item['price'],
"time"=>$plan->date." ".$nmp_p['Time'],
"leixing"=>$erxian_item['leixing']
];
$plan_nmr_info[]=$temp_date;
break;
}
}
if(empty($temp_date)) return \Yz::echoError1("二线号源不可用,请重新选择日期");
}
if($erxian_item['leixing']==2 or $erxian_item['leixing']==3){
$find=false;
$startDate = $plan->date;
$date = new DateTime($startDate);
$date->modify('+1 day'); // 先跳过当天
for ($i = 0; $i < 15; $i++) {
$newdate=$date->format('Y-m-d');
$nmrPlans=$AspNet::ErXian(['yyid'=>6,'data'=>[$newdate],'action'=>"1",'leixing'=>$erxian_item['leixing']],uniqid());
if(!isset($nmrPlans[$newdate]) or empty($nmrPlans[$newdate])){
$date->modify('+1 day');
continue;
}
foreach ($nmrPlans[$newdate] as $nmp_p){
if($nmp_p['keyong']==="0"){
$temp_date=[
"item_id"=>$erxian_item['item_id'],
"name"=>$erxian_item['name'],
"price"=>$erxian_item['price'],
"time"=>$newdate." ".$nmp_p['Time'],
"leixing"=>$erxian_item['leixing']
];
$plan_nmr_info[]=$temp_date;
$find=true;
break;
}
}
if($find) break;
// 增加一天
$date->modify('+1 day');
}
}
}
}
//校验二线号源
if (isset($plan_nmr_info) and !empty($plan_nmr_info)) {
$plan_nmr_temp = [];
foreach ($plan_nmr_info as $key => $plan_nmr) {
if ($plan_nmr['item_id'] <> -1) {
$dt= explode(" ", $plan_nmr['time']);
$p_nmr_data = [
'item_id' => $plan_nmr['item_id'],
'name' => $plan_nmr['name'],
'date' => $dt[0],
'time' => $dt[1],
'price' => $plan_nmr['price'],
'leixing' => $plan_nmr['leixing']
];
//调用接口校验号源是否可用
$erxian_status = $AspNet::ErXian(['YYRQ' => $p_nmr_data['date'], 'YYSJ' => $p_nmr_data['time'], 'yyid' => $yyid, 'action' => 4,'leixing'=>$p_nmr_data['leixing']], uniqid());
if ($erxian_status !== true) return \Yz::echoError1($p_nmr_data['name'] . '号源不可用,请重新选择');
$plan_nmr_temp[] = $p_nmr_data;
}
}
$plan_nmr_appointment_info=$plan_nmr_temp;
}
//开始预约二线
$nowDateTime=date('Y-m-d H:i:s');
if (!empty($plan_nmr_appointment_info)) {
foreach ($plan_nmr_appointment_info as $key => $plan_nmr) {
//$plan_nmr=$plan_nmr_appointment_info[0];
$sex="未知";
if($person->sex==1) $sex='男';
if($person->sex==2) $sex='女';
$p_nmr_data = [
'ghzid'=>$person->ghzid,
'YYRQ'=>$plan_nmr['date'],
'YYSJ'=>$plan_nmr['time'],
'U_SFID'=>$plan_nmr['item_id'],
'U_SFMC'=>$plan_nmr['name'],
'HBXMJE'=>$plan_nmr['price'],
'CJSJ'=>$nowDateTime,
'YYDH'=>$person->phone,
'YYXM'=>$person->name,
'YYXB'=>$sex,
'YYSR'=>$person->birthday,
'YYNL'=>Tools::GetAge($person->birthday).'岁' ,
'YYZJ'=>$person->id_number,
'YYSJ_B'=>$plan_nmr['time'].':00',
'YYSJ_E'=>$plan_nmr['time'].':00',
'YYSJ_BE'=>$plan_nmr['time'].'-'.$plan_nmr['time'],
'ZCRQ'=>$nowDateTime,
'yyid'=>$yyid,
'action'=>2,
'leixing'=>$plan_nmr['leixing']
];
//调用接口预约二线
$erxian_yuyue= $AspNet::ErXian($p_nmr_data, uniqid());
if(!!$erxian_yuyue){
$plan_nmr_appointment_info[$key]['gid']=$erxian_yuyue;
}else{
return ['status' => false, 'msg' => "二线预约失败"];
}
}
}
$data=[
'appointment_date' => $plan->date,
'appointment_time' => $plan->time,
'plan_id' => $plan->id,
'plan_number' => $plan->plan_number,
'doctor' => $doctor,
'updated_at' => date('Y-m-d H:i:s')
];
if(!empty($plan_nmr_appointment_info)){
$data['erxian_appointment_info']=json_encode($plan_nmr_appointment_info, JSON_UNESCAPED_UNICODE);
}
$u = DB::table('orders')->where(['id' => $orderid])->update($data);
$up_plan = DB::table('plans')->where(['id' => $plan->id, 'status' => 1])->update([
'status' => 2
]);
if(!$u) return \Yz::echoError1("预约失败");
$Finish = new OrderController();//预约体检
$yuyue= $Finish->Finish($order->order_number);
return \Yz::Return($yuyue['status'], $yuyue['msg'], ['id' => $order->id]);
}
//转赠
public function ZhuanZeng()
{
$order_id = request('order_id');
$person = request('person_info');
$openid = request('openid');
if(!isset($person)) return \Yz::echoError1("转赠人不能为空");
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
$order = DB::table('orders')
->where('id', '=', $order_id)
->where('status', '=', 2)
->where(function($query) {
$query->where('transfer_type', '=', '')
->orWhereNull('transfer_type');
})
->where(function($query) {
$query->where('plan_id', '=', '')
->orWhereNull('plan_id');
})->first();
if(!$order) return \Yz::echoError1('此订单无法转赠他人');
if($order->web_user_id<>$user->id) return \Yz::echoError1('无权操作此订单');
if($order->id_number==$person['ID_No']) return \Yz::echoError1('被转赠人不能和原用户相同');
//查询转赠码表是否生成了对应的转赠码
$transfer_code=DB::table('transfer_code')->where(['link_order_id'=>$order->id,'is_del'=>0])->first();
if(!!$transfer_code) return \Yz::echoError1('此订单已生成转赠码,无法再次转赠');
$peis = new PEISApiController();
$comboDetail = $peis::Post('套餐详情查询', $order->hospital_id, ['套餐Id' => $order->combo_id], false);
if ($comboDetail['code'] != 0) return \Yz::echoError1("套餐详情查询失败");
$comboDetail_sex = $comboDetail['data'][0]['性别限制'];
if($comboDetail_sex=='全部') $comboDetail_sex=0;
if($comboDetail_sex=='男') $comboDetail_sex=1;
if($comboDetail_sex=='女') $comboDetail_sex=2;
if($comboDetail_sex <> 0 and $comboDetail_sex <> $person['Patsexid']) return \Yz::echoError1('性别不符,此套餐不适合转赠给此用户');
DB::beginTransaction();
$now_datetime=date('Y-m-d H:i:s');
$data = [
'source'=>'转赠from'.$order->id,
'title' => $order->title,
'type' => $order->type,
'web_user_id' => 0,
'checkup_type_id' => $order->checkup_type_id,
'person_id' => 0,
'name' => $person['Patname'],
'id_number' => $person['ID_No'],
'buy_info' => $order->buy_info,
'price' => $order->price,
'true_price' =>$order->true_price,
'order_number' => $this->generateOrderNumber(),
'status' => 2,
'erxian_appointment_info' => $order->erxian_appointment_info,
'combo_id' => $order->combo_id,
'hospital_id' => $order->hospital_id,
'phone' => $person['Fixphone'],
'sex' => $person['Patsexid'],
'birthday' => $this->formatDate($person['Patbdate']),
'married' => 0,
'created_at' => $now_datetime,
'paycheck_time' => $now_datetime,
'youxiaoqi_end' => $order->youxiaoqi_end
];
$i_order_id=DB::table('orders')->insertGetId($data);
$u=DB::table('orders')->where(['id'=>$order->id])->update(['transfer_type'=>1,'transfer_link'=>$i_order_id,'status'=>6]);
if($u and $i_order_id){
DB::commit();
return \Yz::Return(true,'操作成功',[]);
}else{
DB::rollBack();
return \Yz::echoError1('操作失败');
}
}
//接受转赠
public function ReceiveZhuanZeng(){
$order_id = request('order_id');
$openid = request('openid');
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
$order = DB::table('orders')->where(['id'=>$order_id,'status'=>2,'person_id'=>0])
->where(function($query) {
$query->where('transfer_type', '=', '')
->orWhereNull('transfer_type');
})
->first();
if(!$order) return \Yz::echoError1('接收失败');
$person=DB::table('web_user_person')->where(['user_id'=>$user->id,'id_number'=>$order->id_number,'is_del'=>0])->first();
if(!$person) return \Yz::echoError1('体检人不存在');
$u=DB::table('orders')->where(['id'=>$order_id])->update(['person_id'=>$person->id,'web_user_id'=>$user->id]);
if($u){
return \Yz::Return(true,'操作成功',[]);
}else{
return \Yz::echoError1('操作失败');
}
}
//取消转赠
public function CancelZhuanZeng(){
$order_id = request('order_id');
$s=new OrderService();
$status=$s->CancelZhuanZeng($order_id);
return \Yz::Return($status['status'],$status['msg'],['new_orderid'=>$order_id]);
}
//获取用户待预约订单
public function GetPendingOrders()
{
$openid = request('openid');
$person_id = request('person_id');
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
$list=DB::table('orders')->where(['web_user_id'=>$user->id,'status'=>2,'type'=>1])
->whereNotNull('combo_id')
->where(function($query) {
$query->where('appointment_number', '=', '')
->orWhereNull('appointment_number');
})->get();
return \Yz::Return(true,'查询完成',['list'=>$list]);
}
//检测线下预约推送订单,该通知通知,该取消取消
public function OfflineOrderCheck()
{
$type = request('type');//1通知2取消
$now = new DateTime();
$date1 = (clone $now)->modify('+1 month')->format('Y-m-d H:i:00');
$date2 = (clone $now)->modify('+23 days')->format('Y-m-d H:i:00');
// 时间点3date1 + 8 天
$date3 = (clone $now)->modify('+22 days')->format('Y-m-d H:i:00');
$currentTime = $now->format('Y-m-d H:i:00');
if($type==1){
$AspZhuan = new AspNetZhuanController();
$targetTimes = [$date1, $date2];
$orders1 = DB::table('orders')
->where('status', 1)
->whereIn(DB::raw('TIMESTAMP(appointment_date, appointment_time)'), [$date1])
->where('source', 'like', '%线下体检预约%')
->where('offline_sendmsg_status', '<>', 2)
->get();
$orders2 = DB::table('orders')
->where('status', 1)
->whereIn(DB::raw('TIMESTAMP(appointment_date, appointment_time)'), [$date2])
->where('source', 'like', '%线下体检预约%')
->where('offline_sendmsg_status', '<>', 2)
->get();
$orderids=[];
foreach ($orders1 as $order) {
$offline_sendmsg_status=0;
if($order->offline_sendmsg_status==0 or $order->offline_sendmsg_status==null or $order->offline_sendmsg_status==''){
$offline_sendmsg_status=1;
$orderids[]=$order->id;
}else{
continue;
}
DB::table('orders')->where(['id'=>$order->id])->update(['offline_sendmsg_status'=>$offline_sendmsg_status]);
$this->sendWXNotice($AspZhuan, $order);
}
foreach ($orders2 as $order) {
$offline_sendmsg_status=0;
if($order->offline_sendmsg_status<>2){
$offline_sendmsg_status=2;
$orderids[]=$order->id;
}else{
continue;
}
DB::table('orders')->where(['id'=>$order->id])->update(['offline_sendmsg_status'=>$offline_sendmsg_status]);
$this->sendWXNotice($AspZhuan, $order);
}
return \Yz::Return(true,"线下订单通知完成",['find_orderids'=>$orderids]);
}
if($type==2){
$orderids=[];
$order_cancels = DB::table('orders')
->where('status', 1)
->where('source', 'LIKE', '%线下体检预约%')
->where(function ($query) use ($date3, $currentTime) {
// 条件1offline_sendmsg_status 在 [1,2] 且 预约时间 = +6天
$query->where(function ($q) use ($date3) {
$q->whereIn('offline_sendmsg_status', [1, 2])
->whereRaw('TIMESTAMP(appointment_date, appointment_time) = ?', [$date3]);
})
// 条件2预约时间已过期不管 offline_sendmsg_status 是多少)
->orWhereRaw('TIMESTAMP(appointment_date, appointment_time) < ?', [$currentTime]);
})
->get();
$do = new OrderService();
foreach ($order_cancels as $order) {
$orderids[]=$order->id;
$do->Cancel($order);
}
return \Yz::Return(true,"线下订单自动取消完成",['find_orderids'=>$orderids]);
}
}
//奇葩需求,要求线下推送零元订单先显示“支付”,点击后调用此接口记录点击过,下次再看到这个订单按钮就显示“购买套餐”
public function ChangeOrderButtonStatus()
{
$orderid = request('orderid');
$order = DB::table('orders')->where(['id'=>$orderid])->first();
if(!!$order and $order->source=='线下体检预约-空号源' and $order->button_status==0){
$u=DB::table('orders')->where(['id'=>$order->id])->update(['button_status'=>1]);
if($u){
return \Yz::Return(true,"更新完成",['orderid'=>$orderid]);
}
}
return \Yz::echoError1('更新按钮状态失败');
}
private function sendWXNotice($aspZhuan, $order)
{
$person = DB::table('web_user_person')
->where('id_number', $order->id_number)
->where('is_del', 0)
->first();
if (!$person) return;
$reqst_data = [
'ghzid' => $person->ghzid,
'yyid' => 6,
'type' => 28,
'msg1' => $person->name,
'msg2' => $order->title,
'msg3' => number_format($order->true_price, 2, '.', ''),
'msg4' => $order->appointment_date . ' ' . $order->appointment_time,
'msg5' => "健康管理中心",
'msg6' => "",
'url' => "pages/other/entry/index?path=/pages/other/jump-h5/index&src=https%3A%2F%2Ftj-h5.hnxdfe.com%2Fh5%2F%23%2Fpages%2Fmain%2Flogin%2Flogin%3Fpath%3Dorder&scene=1014",
];
$aspZhuan::WeiXinSend($reqst_data);
}
public function item_check($hospital, $data)
{
$peis = new PEISApiController();
$check = $peis::Post('套餐项目检查', $hospital, $data);
return $check;
}
public function generateOrderNumber()
{
// 获取当前时间的时间戳(精确到毫秒)
$timestamp = round(microtime(true) * 1000);
// 生成一个随机数(可以根据需要调整长度)
$randomPart = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6);
// 拼接时间戳和随机部分
$orderNumber = date("YmdHis", $timestamp / 1000) . substr($timestamp % 1000, -3) . $randomPart;
return $orderNumber;
}
function formatDate($dateString) {
$dateTime = DateTime::createFromFormat('Y/n/j G:i:s', $dateString);
// 检查是否有错误发生
if (!$dateTime || $dateTime->format('Y/n/j G:i:s') !== $dateString) {
return "1900-01-01";
}
return $dateTime->format('Y-m-d');
}
}