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.

607 lines
21 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\Web;
use App\Http\Controllers\API\AspNetZhuanController;
use App\Http\Controllers\API\H5\PersonController;
use App\Http\Controllers\API\H5\PlanController;
use App\Http\Controllers\API\PEISApiController;
use App\Http\Controllers\Controller;
use DateInterval;
use DatePeriod;
use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class OrderController extends Controller
{
//创建订单
public function Create()
{
$hospital_id = request('hospital');
$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
$plan_id = request('plan_id');//号源id
$doctor = request('doctor');//预约的医生名字
$appdoctor_department = request('appdoctor_department');//预约的医生的科室
$appdoctor = request('appdoctor');//协助预约的医生名字
$person = request('person'); //sex,birthday,married,name,id_number,phone
$married_array = ["未婚", "已婚", "丧偶", "离婚", "其他"];
if (isset($person['married'])) {
$key = array_search($person['married'], $married_array);
if ($key !== false) {
$person['married'] = $key;
} else {
$person['married'] = 4;
}
}
if (!isset($hospital_id)) return \Yz::echoError1('医院id不能为空');
if (!isset($type)) return \Yz::echoError1('type体检类型不能为空');
if ($type != 1 && $type != 2) {
return \Yz::echoError1('type参数体检类型错误');
}
if (!isset($plan_id)) return \Yz::echoError1('号源id不能为空');
if ($type == 2 and !isset($group_id)) return \Yz::echoError1('团检group_id不能为空');
if ($type == 1 and isset($group_id)) return \Yz::echoError1('体检类型:个检 与group_id冲突');
$title = "自选项目";
$price = 0;
$true_price = 0;//订单真实支付金额
$buy_info = [
'combo' => [
'id' => 0,
'name' => $title,
'price' => 0,
],
'items' => [],
'group' => [
'id' => '',
],
'nmr_list' => []
];
//如果是套餐
$TJ_Leixing_id = 1;//存储用体检类型
$checkup_type_id = false; //体检类型id
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("套餐未关联体检类型");
$checkup_type_id = $combo_info->checkup_type_id;
$TJ_Leixing_id = $checkup_type_id;
$price += $combo_info->price;
$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,
];
}
}
$missingIds = array_diff($item_ids, $existingIds);
if (count($missingIds) > 0) return \Yz::echoError1("部分自选项目不可用Id:" . implode(', ', $missingIds));
$item_price = $item_price * config('app.globals.ZiXuan_ZheKou'); //所有自选项目打8折
$price += $item_price;
}
$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];
$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'],
];
$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' => $item->item_id,
'name' => $item->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 = new PlanController();
$plan_check = $plan->CheckPlan($plan_id, $hospital_id, $type, $person['sex'], $price, $checkup_type_id);
if ($plan_check['status'] === false) return \Yz::echoError1($plan_check['msg']);
$plan = $plan_check['plan'];
//如果是团检 判断号源在 团检登记人的有效时间范围内
$plan_datetime = $plan->date . ' ' . $plan->time;
if (!!$group_info) {
if (!($plan_datetime > $group_info['start_time'] . ' 00:00:00' and $plan_datetime < $group_info['end_time'] . ' 23:59:59')) {
return \Yz::echoError1("预约日期不在单位有效时间范围内,请重新选择");
}
}
//构建订单号
$order_num = $this->generateOrderNumber();
$data = [
'title' => $title,
'type' => $type,
'source' => 'web',
'web_user_id' => 0,
'checkup_type_id' => $TJ_Leixing_id,
'person_id' => 0,
'name' => $person['name'],
'id_number' => $person['id_number'],
'buy_info' => json_encode($buy_info, JSON_UNESCAPED_UNICODE),
'price' => $price,
'true_price' => $true_price,
'order_number' => $order_num,
'status' => 1,
'appointment_date' => $plan->date,
'appointment_time' => $plan->time,
'plan_id' => $plan->id,
'plan_number' => $plan->plan_number,
'combo_id' => $combo_id,
'hospital_id' => $hospital_id,
'doctor' => $doctor,
'appdoctor' => $appdoctor,
'appdoctor_department' => $appdoctor_department,
'phone' => $person['phone'],
'sex' => $person['sex'],
'birthday' => $person['birthday'],
'married' => $person['married'],
];
DB::beginTransaction();
$insert = DB::table('orders')->insertGetId($data);
$up_plan = DB::table('plans')->where(['id' => $plan->id, 'status' => 1])->update([
'status' => 2
]);
if ($insert and $up_plan) {
DB::commit();
$action = false;
if ($true_price === 0) {
//如果是免费的,直接去预约
$yuyue = self::Finish($order_num);
if ($yuyue['status'] === true) {
return \Yz::return(true, "操作成功", ['action' => $action, 'orderid' => $insert]);
} else {
return \Yz::echoError1($yuyue['msg']);
}
}
if ($true_price > 0) {
//如果大于0则提示前端去支付
$action = "pay";
}
$AspZhuan = new AspNetZhuanController();
$reqst_data = [
'ghzid' => $person['ghzid'],
'yyid' => 6,
'type' => 28,
'msg1' => $person['name'],
'msg2' => $title,
'msg3' => number_format($true_price, 2, '.', ''),
'msg4' => $plan->date . ' ' . $plan->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);
return \Yz::return(true, "操作成功", ['action' => $action, 'orderid' => $insert]);
} else {
DB::rollBack();
return \Yz::echoError1('操作失败');
}
}
//客服预约,只占用号源
public function CreateYuYueOrder()
{
$hospital_id = request('hospital');
$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
$plan_id = request('plan_id');//号源id
$doctor = request('doctor');//预约的医生名字
$appdoctor = request('appdoctor');//协助预约的医生名字
$person = request('person'); //sex,birthday,married,name,id_number,phone
$married_array = ["未婚", "已婚", "丧偶", "离婚", "其他"];
if (isset($person['married'])) {
$key = array_search($person['married'], $married_array);
if ($key !== false) {
$person['married'] = $key;
} else {
$person['married'] = 4;
}
}
if (!isset($hospital_id)) return \Yz::echoError1('医院id不能为空');
if (!isset($type)) return \Yz::echoError1('type体检类型不能为空');
if ($type != 1 && $type != 2) {
return \Yz::echoError1('type参数体检类型错误');
}
if (!isset($plan_id)) return \Yz::echoError1('号源id不能为空');
if ($type == 2 and !isset($group_id)) return \Yz::echoError1('团检group_id不能为空');
if ($type == 1 and isset($group_id)) return \Yz::echoError1('体检类型:个检 与group_id冲突');
$title = "自选项目";
$price = 0;
$true_price = 0;//订单真实支付金额
$buy_info = [
'combo' => [
'id' => 0,
'name' => $title,
'price' => 0,
],
'items' => [],
'group' => [
'id' => '',
],
'nmr_list' => []
];
//如果是套餐
$TJ_Leixing_id = 1;//存储用体检类型
$checkup_type_id = false; //体检类型id
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("套餐未关联体检类型");
$checkup_type_id = $combo_info->checkup_type_id;
$TJ_Leixing_id = $checkup_type_id;
$price += $combo_info->price;
$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 = [];
foreach ($items_list as $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,
];
}
}
$missingIds = array_diff($item_ids, $existingIds);
if (count($missingIds) > 0) return \Yz::echoError1("部分自选项目不可用Id:" . implode(', ', $missingIds));
}
$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];
$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'],
];
$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' => $item->item_id,
'name' => $item->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 = new PlanController();
$plan_check = $plan->CheckPlan($plan_id, $hospital_id, $type, $person['sex'], $price, $checkup_type_id);
if ($plan_check['status'] === false) return \Yz::echoError1($plan_check['msg']);
$plan = $plan_check['plan'];
//如果是团检 判断号源在 团检登记人的有效时间范围内
$plan_datetime = $plan->date . ' ' . $plan->time;
if (!!$group_info) {
if (!($plan_datetime > $group_info['start_time'] . ' 00:00:00' and $plan_datetime < $group_info['end_time'] . ' 23:59:59')) {
return \Yz::echoError1("预约日期不在单位有效时间范围内,请重新选择");
}
}
//构建订单号
$order_num = $this->generateOrderNumber();
$data = [
'title' => $title,
'type' => $type,
'source' => 'web',
'web_user_id' => 0,
'checkup_type_id' => $TJ_Leixing_id,
'person_id' => 0,
'name' => $person['name'],
'id_number' => $person['id_number'],
'buy_info' => json_encode($buy_info, JSON_UNESCAPED_UNICODE),
'price' => $price,
'true_price' => $true_price,
'order_number' => $order_num,
'status' => 1,
'appointment_date' => $plan->date,
'appointment_time' => $plan->time,
'plan_id' => $plan->id,
'plan_number' => $plan->plan_number,
'combo_id' => $combo_id,
'hospital_id' => $hospital_id,
'doctor' => $doctor,
'appdoctor' => $appdoctor,
'phone' => $person['phone'],
'sex' => $person['sex'],
'birthday' => $person['birthday'],
'married' => $person['married'],
];
DB::beginTransaction();
$insert = DB::table('orders_yuyue')->insertGetId($data);
$up_plan = DB::table('plans')->where(['id' => $plan->id, 'status' => 1])->update([
'status' => 2
]);
if ($insert and $up_plan) {
DB::commit();
return \Yz::return(true, "操作成功", ['orderid' => $insert]);
} else {
DB::rollBack();
return \Yz::echoError1('操作失败');
}
}
public function SearchOrder(Request $request)
{
$type = $request->post('type');
$order_start = $request->post('order_start');
$order_end = $request->post('order_end');
$doctor = $request->post('doctor');
$list = DB::table($type == '1' ? 'orders' : 'orders_yuyue');
$list = $list->where('created_at', '>=', $order_start);
$list = $list->where('created_at', '<=', $order_end);
$list = $list->where('source', 'web');
if (!!$doctor) {
$list = $list->where('appdoctor', $doctor);
}
$list = $list->orderBy('id', 'desc')->get();
return \Yz::return(true, "操作成功", ['list' => $list]);
}
public function export()
{
$dates = request('dates');
$key = request('key');
if ($key <> "ddXPpBrM3C93PHFdWDXbMX2vQ2AJQFFy") {
return \Yz::echoErrorJson('无权访问');
}
if (!$dates) {
return \Yz::echoErrorJson('请选择日期');
}
$table_list = DB::table('orders')->where(['source' => 'web'])->whereBetween('created_at', [$dates[0] . ' 00:00:00', $dates[1] . ' 23:59:59'])
->select(DB::raw("*, CASE WHEN sex = 1 THEN '男' WHEN sex = 2 THEN '女' ELSE '未知' END as sex"))
->get();
$status_array = [
1 => '未支付',
2 => '已支付',
3 => '已取消',
4 => '到检',
5 => '已退款',
];
foreach ($table_list as $list_item) {
$list_item->status = $status_array[$list_item->status];
}
$template_path = Storage::path('public/excel/hiszhuanzhen.xlsx');
$spreadsheet = IOFactory::load($template_path);
$worksheet = $spreadsheet->getActiveSheet();
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => 'FF000000'],
],
],
];
$row = 3;
$col = [
'A' => 'title',
'B' => 'name',
'C' => 'id_number',
'D' => 'price',
'E' => 'appdoctor',
'F' => 'phone',
'G' => 'sex',
'H' => 'appointment_date',
'I' => 'appointment_time',
'J' => 'status',
];
foreach ($table_list as $table_item) {
foreach ($col as $index => $key) {
$worksheet->setCellValueExplicit($index . $row, $table_item->$key, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}
$row++;
}
$file_name = Str::orderedUuid();
$dir_path = "public/excel/" . date('Ym') . '/' . $file_name;
Storage::makeDirectory($dir_path);
$name_date = date('n.j', strtotime($dates[1] . ' 00:00:00'));
$excel_path = $dir_path . "/His转诊$name_date.xlsx";
$writer = new Xlsx($spreadsheet);
$writer->save(Storage::path($excel_path));
$url = Storage::url($excel_path);
return ['status' => true, 'msg' => '查询完成', 'url' => env('APP_URL') . $url];
}
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;
}
}