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.

587 lines
20 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\Controller;
use App\Services\ConfigService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ComboController extends Controller
{
public function select(Request $request)
{
$combo = DB::table('combo_type')->select('id as value', 'name as label')->get();
$person = DB::table('combo_crowd')->select('id as value', 'name as label')->get();
// $combo = [[
// 'label' => '类型一',
// 'value' => '1',
// ], [
// 'label' => '类型二',
// 'value' => '2',
// ]];
// $person = [[
// 'label' => '类型一',
// 'value' => '1',
// ], [
// 'label' => '类型二',
// 'value' => '2',
// ]];
return \Yz::Return(true, '获取成功', [
'combo' => $combo,
'person' => $person
]);
}
public function list(Request $request)
{
$hospital = $request->post('hospital');
$doctor = $request->post('doctor');
$openid = $request->post('openid');
$combo_sort = $request->post('combo_sort');
$combo_type = $request->post('combo_type');
$combo_crowd = $request->post('combo_crowd');
$combo_price = $request->post('combo_price');
$combo_item = $request->post('combo_item');
$checkup_type_id = $request->post('checkup_type_id');
$hospital = DB::table('hospitals')->select('id', 'name', 'address', 'latitude', 'longitude')->where(['id' => $hospital])->first();
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('openid对应用户不存在');
$person = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0, 'is_default' => 1])->first();
if (!$person) return \Yz::echoError1("请选择就诊人");
//用户绑定就诊人数量
$personCount = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0])->count();
$canshu = [];
$canshu[]=$person->sex;
$sql = '';
if (isset($combo_price)) {
$price_list = [
"1" => [0, 299],
"2" => [300, 999],
"3" => [1000, 1499],
"4" => [1500, 2999],
"5" => [3000, 999999],
];
$sql = " and (a.price>=? and a.price<=?) ";
$canshu[] = $price_list[$combo_price][0];
$canshu[] = $price_list[$combo_price][1];
}
if(isset($checkup_type_id)){
$sql = $sql . " and a.checkup_type_id=? ";
$canshu[] = $checkup_type_id;
}
if (isset($combo_type)) {
$sql = $sql . " and a.type_id=? ";
$canshu[] = $combo_type;
}
if (isset($combo_crowd)) {
$sql = $sql . " and a.crowd_id=? ";
$canshu[] = $combo_crowd;
}
if (isset($combo_sort)) {
if ($combo_sort == 1) {
$sql = $sql . " ";
}
if ($combo_sort == 2) {
$sql = $sql . " ";
}
if ($combo_sort == 3) {
$sql = $sql . " order by a.price";
}
if ($combo_sort == 4) {
$sql = $sql . " order by a.price desc";
}
}
if(isset($combo_item)){
$combo_ids=DB::table('combo_items')->whereIn('item_id',$combo_item)->where(['status'=>1])
->groupBy('combo_id')
->havingRaw('COUNT(DISTINCT item_id) = '.count($combo_item))
->pluck('combo_id')->toArray();
$count = count($combo_ids);
$placeholders = implode(', ', array_fill(0, $count, '?'));
$sql = $sql . " and a.combo_id in ($placeholders) ";
$canshu =array_merge($canshu, $combo_ids);
}
$combos = DB::select("select * from combos as a LEFT JOIN (
select combo_id as c_id,count(*) as count from orders where status in(2,4) group by combo_id
) as b on a.combo_id=b.c_id where a.status=1 and a.sex in(?,0)" . $sql, $canshu);
foreach ($combos as $key => $combo) {
$combo->count=$combo->count?$combo->count:0;
$tags = json_decode($combo->tags, true);
$combo->tags2 = json_decode($combo->tags2, true);
foreach ($tags as $k => $tag) {
$tags[$k] = ['text' => $tag,
'text_color' => '#47ABD8',
'color' => '#EBF5FC'
];
}
$combo->tag = $tags;
$combo->tag[] = [
'text' => $combo->item_count . '个项目',
'text_color' => '#34C292',
'color' => '#E9F8F3',
];
//多选一
$duo_xuan_yi=[];
if(json_decode($combo->duo_xuan_yi)){
$N1=json_decode($combo->duo_xuan_yi);
foreach ($N1 as $k=>$v){
$duo_xuan_yi[$k][]=[
'zu_name'=>$v->组名称,
'item_list'=>[]
];
foreach ($v->包含项目 as $k2=>$v2){
$duo_xuan_yi[$k]['item_list'][]=
[
'item_id' =>$v2->Id,
'item_name' => $v2->名称,
'price'=>$v2->价格
];
}
}
}
$combo->duo_xuan_yi =$duo_xuan_yi;
}
$hospital_info = $hospital;
$info = [
'name' => $person->name,
'sex' => $person->sex,
'count' => $personCount
];
$doctor_info = [
'id' => $doctor,
'name' => '张大夫'
];
return \Yz::Return(true, '获取成功', [
'list' => $combos,
'hospital' => $hospital_info,
'doctor' => $doctor_info,
'info' => $info,
]);
}
//获取套餐类型、适用人群
public function GetComboSort()
{
$type = DB::table('combo_type')->get();
$crowd = DB::table('combo_crowd')->get();
$sort_list = [
["id" => 1, 'name' => '综合排序'],
["id" => 2, 'name' => '预约最多'],
["id" => 3, 'name' => '低价优先'],
["id" => 4, 'name' => '高价优先'],
];
$price_list = [
['name' => '300以下', 'id' => 1],
['name' => '300-1000', 'id' => 2],
['name' => '1000-1500', 'id' => 3],
['name' => '1500-3000', 'id' => 4],
['name' => '3000以上', 'id' => 5],
];
$item_label = [
["id" => 3468454, 'name' => '内科检查'],
["id" => 1812, 'name' => '尿常规'],
["id" => 1955, 'name' => '肝功能'],
["id" => 1958, 'name' => '肾功能'],
];
return \Yz::Return(true, "查询成功", ['sort_list' => $sort_list, 'combo_type' => $type, 'combo_crowd' => $crowd, 'combo_price' => $price_list, 'combo_item' => $item_label]);
}
//获取购买详情内容(详情页信息)
public function BuyInfo()
{
$hospital_id = request('hospital');
$combo_id = request('combo_id'); //购买的套餐id
$item_ids = request('item_ids'); //自选项目ids
$duo_xuan_yi = request('duo_xuan_yi'); //多选1
$group_id = request('group_id'); //团检登记id
$wj_flag= request('wj'); //问卷标记
if (!isset($hospital_id)) return \Yz::echoError1("医院id不能为空");
$hospital = DB::table('hospitals')->where(['id' => 1, 'status' => 1, 'is_del' => 0])->first();
if(isset($wj_flag) and $wj_flag==1){
$wj_zhekou=config('app.globals.Wj_ZheKou');//问卷过来的折扣率
}
$combo_info = false;//套餐信息
$pay_item_count = 0;//需自费项目个数
$all_original_price = 0;
$true_price = 0;
$nmr_list=[];
if (isset($combo_id) and $combo_id != 0) {
// $combo=DB::table('combos')->where(['hospital_id'=>$hospital_id,'combo_id'=>$combo_id,'status'=>1])->first();
$combo = DB::select("select a.*,b.*,c.name as crowd_name from combos as a LEFT JOIN (
select combo_id as c_id,count(*) as sale_count from orders where status in(2,4) group by combo_id
) as b on a.combo_id=b.c_id left join combo_crowd as c on a.crowd_id=c.id where a.combo_id=? and a.status=1 ", [$combo_id]);
if (!$combo) return \Yz::echoError1("套餐不存在");
$combo = $combo[0];
//构建多选一数据
$Nx1_arrInfo=[];
if(isset($duo_xuan_yi) and !empty($duo_xuan_yi)){
$combo_Nx1=json_decode($combo->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['科室名称']=='影像科'){
$nmr_list[]=[
'item_id' => $v2['Id'],
'name' => $v2['名称'],
];
}
$Nx1_arrInfo[] = [
'id' => $v2['Id'],
'name' => $v2['名称'],
'desc' => $v2['简介'],
'keshi_name' => $v2['科室名称'],
];
}
}
}
}
}
}
$combo_info['hospital_name'] = $hospital->name;
$combo_info['combo_name'] = $combo->name;
$combo_info['crowd_name'] = $combo->crowd_name;
$combo_info['img'] = $combo->cover;
$combo_info['sale_count'] = $combo->sale_count==null?0:$combo->sale_count;
$tags = json_decode($combo->tags, true);
$combo->tags2 = json_decode($combo->tags2, true);
foreach ($tags as $k => $tag) {
$tags[$k] = ['text' => $tag,
'text_color' => '#47ABD8',
'color' => '#EBF5FC'
];
}
$combo->tag = $tags;
$combo->tag[] = [
'text' => $combo->item_count . '个项目',
'text_color' => '#34C292',
'color' => '#E9F8F3',
];
$combo_info['tags'] = $combo->tag;
$combo_info['tags2'] = $combo->tags2;
$combo_info['price'] = $combo->price;
$combo_info['original_price'] = $combo->original_price;
$all_original_price += $combo_info['original_price'];
$combo_items = json_decode($combo->items, true);
$combo_items=array_merge($combo_items,$Nx1_arrInfo); //合并多选一
$groupedData = [];
foreach ($combo_items as $item) {
$keshiName = $item['keshi_name'];
if (!isset($groupedData[$keshiName])) {
$groupedData[$keshiName] = [];
}
$groupedData[$keshiName][] = $item;
}
foreach ($groupedData as $keshiName => $children) {
$combo_info['items'][] = [
'keshi_name' => $keshiName,
'children' => $children
];
}
$pay_item_count += $combo->item_count;
$true_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){
$nmr_list[]=[
'item_id' => $item->item_id,
'name' => $item->name,
];
}
}
}
$items_info = false;//自选项目信息
if (isset($item_ids) and !empty($item_ids)) {
$item_price = 0;
$items_original_price = 0;
$items = DB::table('items')->whereIn('item_id', $item_ids)->where(['status' => 1])->get();
$groupedData = [];
foreach ($items as $item) {
$item_price = bcadd($item_price, $item->price, 2);
// $all_original_price+=$item->original_price;
$all_original_price = bcadd($all_original_price, $item->original_price, 2);
// $items_original_price+=$item->original_price;
$items_original_price = bcadd($items_original_price, $item->original_price, 2);
$keshiName = $item->keshi_name;
if (!isset($groupedData[$keshiName])) {
$groupedData[$keshiName] = [];
}
$groupedData[$keshiName][] = ['desc' => $item->jianjie, 'name' => $item->name,'price'=>$item->price];
//如果有影像科则存储在nmr_list字段
if($item->keshi_name=='影像科'){
$nmr_list[]=[
'item_id' => $item->item_id,
'name' => $item->name,
];
}
}
if(isset($wj_flag) and $wj_flag==1){
$item_price=$item_price*$wj_zhekou;
}
$items_info['price'] =number_format($item_price, 2, '.', '') ;
$items_info['original_price'] = $items_original_price;
foreach ($groupedData as $keshiName => $children) {
$items_info['items'][] = [
'keshi_name' => $keshiName,
'children' => $children
];
}
$pay_item_count += count($items);
$true_price += $items_info['price'];
}
//调用his接口查询用户积分和预存款计算可以抵扣的金额
$integral_money = 90;//积分抵扣金额
$save_money = 150;//预存款抵扣金额
$coupon_money = 50;//优惠券抵扣金额
//用户真实支付价格,应减去抵扣(二期实现)
$group_info = false;
$lose_price=0;
if (!!$group_id) {
$P = new PersonController();
$data = [
'电话号码' => null,
'证件号码' => null,
'预约Id' => $group_id
];
$group_info = $P->group_info($hospital_id, $data);
if(count($group_info)==0) return \Yz::echoError1("获取团检登记信息失败");
//如果是团检统收限额大于0并且没有自带项目则判断剩余金额
if($group_info[0]['tongshou_xiane']>0 and count($group_info[0]['items'])==0){
$lose_price=$group_info[0]['tongshou_xiane']- $true_price;
if($lose_price<0) $lose_price=0;
}
$true_price = $true_price + $group_info[0]['sixi_zong_ji_jin_e'];
$true_price = ($true_price - $group_info[0]['tongshou_xiane']) > 0 ? $true_price - $group_info[0]['tongshou_xiane'] : 0;
$all_items = DB::table('items')->where(['status' => 1])->get();
$item_new = [];
foreach ($group_info[0]['items'] as $item) {
$item['keshi_name'] = '其他';
$item['desc'] = '';
foreach ($all_items as $it) {
if ($it->item_id == $item['id']) {
$item['keshi_name'] = $it->keshi_name;
$item['desc'] = $it->jianjie;
if($it->keshi_name=='影像科'){
$nmr_list[]=[
'item_id' => $it->item_id,
'name' => $it->name,
];
}
break;
}
}
$item_new[] = $item;
}
$groupedData = [];
foreach ($item_new as $item) {
$keshiName = $item['keshi_name'];
if (!isset($groupedData[$keshiName])) {
$groupedData[$keshiName] = [];
}
$groupedData[$keshiName][] = ['desc' => $item['desc'], 'name' => $item['name'], 'id' => $item['id'], 'keshi_name' => $item['keshi_name']];
}
$group_info[0]['items'] = [];//清空原来的item
foreach ($groupedData as $keshiName => $children) {
$group_info[0]['items'][] = [
'keshi_name' => $keshiName,
'children' => $children
];
}
}
$temp_nmr='';
foreach ($nmr_list as $key=>$nmr){
$temp_nmr=$temp_nmr.$nmr['name']."/";
}
if(count($nmr_list)>0){
$nmr_list=[
['item_id' => '999999',
'name' =>$temp_nmr]
];
}
$data = [
'group_info' => $group_info,
'combo_info' => $combo_info,
'items_info' => $items_info,
'integral_money' => number_format($integral_money, 2, '.', ''),
'save_money' =>number_format($save_money, 2, '.', ''),
'coupon_money' => number_format($coupon_money, 2, '.', ''),
'true_price' =>number_format($true_price, 2, '.', ''),//需要用户支付的金的
'original_price' =>number_format($all_original_price, 2, '.', '') ,//总原价
'pay_item_count' => $pay_item_count,//需要付费的项目数量
'lose_price'=>number_format($lose_price, 2, '.', ''),//剩余的不用就会浪费的金额
'nmr_list' => $nmr_list//核磁项目列表
];
return \Yz::Return(true, "查询成功", $data);
}
public function ComboCompare()
{
$combo_ids = request('combo_ids'); //购买的套餐id
$combos = DB::table('combos as a')
->leftJoin('checkup_type as b', 'a.checkup_type_id', '=', 'b.id')
->select('a.*', 'b.name as checkup_type_name')
->whereIn('a.combo_id', $combo_ids)->where(['a.status' => 1])->get();
foreach ($combos as $key => $combo) {
$combo->items = json_decode($combo->items, true);
$item_list=[];
foreach ($combo->items as $key => $item) {
if($item['keshi_name']<>'材料费'){
$item_list[]=$item;
}
}
$combo->items=$item_list;
$count = DB::table('orders')->where(['combo_id' => $combo->combo_id])->whereIn('status', [2, 4])->count();
$combo->saleCount = $count;
}
foreach ($combos as $key => $combo) {
$tags = json_decode($combo->tags, true);
$combo->tags2 = json_decode($combo->tags2, true);
foreach ($tags as $k => $tag) {
$tags[$k] = ['text' => $tag,
'text_color' => '#47ABD8',
'color' => '#EBF5FC'
];
}
$combo->tags = $tags;
$combo->tags[] = [
'text' => $combo->item_count . '个项目',
'text_color' => '#34C292',
'color' => '#E9F8F3',
];
}
return \Yz::Return(true, "查询完成", ['list' => $combos]);
}
//推荐套餐
public function ComboRecommend()
{
$item_ids = request('item_ids'); //购买的套餐id
$person_id = request('person_id'); //就诊人id
$person=DB::table('web_user_person')->where(['id' => $person_id,'is_del'=>0])->first();
if(!$person) return \Yz::echoError1("用户不存在");
//统计勾选的项目总额
$item_all_price=DB::table('items')->whereIn('item_id',$item_ids)->where(['status' => 1])->sum('price');
$allCombos = DB::table('combos as a')->leftJoin('combo_items as b','a.combo_id','=','b.combo_id')->whereIn('a.sex',[$person->sex,0])->where(['a.status'=>1])->get();
$combo_temp=[];
foreach ($allCombos as $key => $combo) {
$c_id=$combo->combo_id;
if (!isset($combo_temp[$c_id])) {
$groupedData[$c_id] = [];
}
$combo_temp[$c_id][] = $combo->item_id;
}
// 用于存储每个数组及其与目标数组的相似度
$similarities = [];
foreach ($combo_temp as $index => $array) {
$similarity = $this->jaccard_similarity($item_ids, $array);
$similarities[$index] = $similarity;
}
// 对相似度进行排序,保持索引关联
arsort($similarities);
// 获取最相似的前两个数组
$topTwoIndexes = array_keys($similarities, current($similarities), true); // 第一个最大值
next($similarities);
$topTwoIndexes[] = key($similarities); // 第二个最大值
$ids= "'" . implode("','", $topTwoIndexes) . "'";
$combos = DB::select("select * from combos as a LEFT JOIN (
select combo_id as c_id,count(*) as count from orders where status in(2,4) group by combo_id
) as b on a.combo_id=b.c_id where a.status=1 and a.combo_id in (".$ids.")");
foreach ($combos as $key => $combo) {
$tags = json_decode($combo->tags, true);
$combo->tags2 = json_decode($combo->tags2, true);
foreach ($tags as $k => $tag) {
$tags[$k] = ['text' => $tag,
'text_color' => '#47ABD8',
'color' => '#EBF5FC'
];
}
$combo->tags = $tags;
$combo->tags[] = [
'text' => $combo->item_count . '个项目',
'text_color' => '#34C292',
'color' => '#E9F8F3',
];
$combo->recommend = [
'xiangsidu' => (string)(number_format($similarities[$combo->combo_id],2)*100).'%',
'count' => $combo->item_count-count($item_ids),
'money' => number_format($combo->price - $item_all_price,2)
];
}
$list=[];
foreach ($combos as $key => $combo) {
if($combo->recommend['xiangsidu']<>'0%'){
$list[]=$combo;
}
}
return \Yz::Return(true, "查询完成", ['combos' => $list]);
}
function jaccard_similarity($set1, $set2) {
// 计算交集
$intersection = array_intersect($set1, $set2);
// 计算并集
$union = array_unique(array_merge($set1, $set2));
// 避免除数为0的情况
if (count($union) == 0) return 0;
// 返回Jaccard相似度
return count($intersection) / count($union);
}
}