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.

91 lines
3.6 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\His;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CheckItemController extends Controller
{
public function UpdateCheckItem(){
$url='http://192.168.80.39:7801/roc/order-service/api/v1/order/order-term/undrug/query';
$termClassId = request('termClassId');
if(!isset($termClassId)){
return \Yz::echoError1('类别id不能为空');
}
$data=[];
$where=[];
if($termClassId<>'all'){
$data=[
'termClassId' => $termClassId,
];
$where=['termClassId'=>$termClassId];
}
$res=\Tools::Get($url,$data,'更新检查项目');
if($res['code']==200){
$res_data=$res['data'];
if(isset($res_data['list']) and !empty($res_data['list'])){
$db_data=DB::table('s_check_item')->where($where)->get();//库里的数据
//根据 接收数据的undrugId和库里的item_code进行比对如果库里存在则更新如果库里没有则添加如果库里有但是接收数据里没有则在库里标记is_del=0
$db_map = [];
foreach ($db_data as $db_item) {
$db_map[$db_item->item_code] = $db_item;
}
// 用于记录接收到的数据中的 item_code方便后面判断哪些需要删除
$received_ids = [];
foreach ($res_data['list'] as $item) {
// 假设远程返回字段是 undrugId 和 undrugName对应数据库字段是 item_code 和 item_name
$item_code = $item['undrugId'];
$item_name = $item['termName'];
// 添加到接收 ID 列表
$received_ids[] = $item_code;
// 快速查找是否已存在该 item_code
if (isset($db_map[$item_code])) {
// 存在则更新
DB::table('s_check_item')
->where('item_code', $item_code)
->update([
'termClassId'=>$item['termClassId'],
'item_name' => $item_name,
'item_desc' => $item_name,
'is_del' => 0, // 取消删除标记
]);
} else {
// 不存在则插入新记录
DB::table('s_check_item')->insert([
'termClassId'=>$item['termClassId'],
'item_code' => $item_code,
'item_name' => $item_name,
'item_desc' => $item_name,
]);
}
}
// 处理数据库中存在但远程没有返回的数据,标记为删除
foreach ($db_map as $item_code => $db_item) {
if (!in_array($item_code, $received_ids)) {
DB::table('s_check_item')
->where('item_code', $item_code)
->update([
'is_del' => 1
]);
}
}
return \Yz::return(true,'更新检查项目成功',[]);
}else{
return \Yz::echoError1('His接口返回数据为空');
}
}else{
return \Yz::echoError1('更新失败');
}
}
}