Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵增煜
/
tzt-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ed50e42a
authored
Dec 11, 2024
by
赵增煜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用法用量导入
parent
4cda14e4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
0 deletions
+126
-0
app/Admin/Controllers/DosageController.php
+6
-0
app/Admin/Extensions/ToolBar/Actions/PharmacyDosageImportAction.php
+21
-0
app/Admin/Extensions/ToolBar/Forms/PharmacyDosageImportForm.php
+99
-0
No files found.
app/Admin/Controllers/DosageController.php
View file @
ed50e42a
...
...
@@ -9,6 +9,7 @@
use
Dcat\Admin\Http\Controllers\AdminController
;
use
Dcat\Admin\Layout\Content
;
use
Dcat\Admin\Show
;
use
App\Admin\Extensions\ToolBar\Actions\PharmacyDosageImportAction
;
// 用法用量
class
DosageController
extends
AdminController
...
...
@@ -34,6 +35,11 @@ protected function grid()
$grid
->
column
(
'dosage_desc'
);
$grid
->
column
(
'dosage_show'
);
// 工具栏普通按钮
$grid
->
tools
(
function
(
$tools
)
{
$tools
->
append
(
new
PharmacyDosageImportAction
());
// 导入药品信息
});
$grid
->
filter
(
function
(
Grid\Filter
$filter
)
{
$filter
->
panel
();
// 更改为 panel 布局
$filter
->
expand
();
// 默认展开搜索框
...
...
app/Admin/Extensions/ToolBar/Actions/PharmacyDosageImportAction.php
0 → 100644
View file @
ed50e42a
<?php
namespace
App\Admin\Extensions\ToolBar\Actions
;
use
App\Admin\Extensions\ToolBar\Forms\PharmacyDosageImportForm
;
use
Dcat\Admin\Grid\Tools\AbstractTool
;
use
Dcat\Admin\Widgets\Modal
;
class
PharmacyDosageImportAction
extends
AbstractTool
{
public
$title
=
'导入用法用量'
;
public
function
html
()
:
Modal
{
return
Modal
::
make
()
->
lg
()
->
title
(
$this
->
title
)
->
body
(
new
PharmacyDosageImportForm
())
->
button
(
"<button class='btn btn-success'><i class='feather icon-upload'></i>
{
$this
->
title
}
</button>"
);
}
}
app/Admin/Extensions/ToolBar/Forms/PharmacyDosageImportForm.php
0 → 100644
View file @
ed50e42a
<?php
namespace
App\Admin\Extensions\ToolBar\Forms
;
use
App\Models\DrugModel
;
use
Dcat\Admin\Http\JsonResponse
;
use
Dcat\Admin\Widgets\Form
;
use
Dcat\EasyExcel\Excel
;
use
Dcat\EasyExcel\Support\SheetCollection
;
use
Exception
;
set_time_limit
(
1800
);
ini_set
(
'memory_limit'
,
'-1'
);
ini_set
(
'max_execution_time'
,
10800
);
ini_set
(
'max_input_time'
,
10800
);
class
PharmacyDosageImportForm
extends
Form
{
/**
* 处理表单提交逻辑.
*/
public
function
handle
(
array
$input
)
:
JsonResponse
{
// 表单参数
$file
=
$input
[
'import_file'
];
$filePath
=
storage_path
(
'app/'
.
$file
);
try
{
// 每100行数据为一批数据进行读取
$chunkSize
=
10
;
$successNum
=
0
;
$failNum
=
0
;
Excel
::
import
(
$filePath
)
->
first
()
->
chunk
(
$chunkSize
,
function
(
SheetCollection
$collection
)
use
(
&
$successNum
)
{
// 此处的数组下标依然是excel表中数据行的行号
$rows
=
$collection
->
toArray
();
foreach
(
$rows
as
$row
)
{
$item
=
array_map
(
function
(
$value
)
{
return
is_string
(
$value
)
?
trim
(
$value
)
:
$value
;
},
$row
);
$drugModel
=
null
;
if
(
isset
(
$item
[
'君元ID'
])
&&
$item
[
'君元ID'
])
{
$drugModel
=
DrugModel
::
where
(
'product_id'
,
$item
[
'君元ID'
])
->
first
();
}
if
(
!
$drugModel
)
{
$drugModel
=
new
DrugModel
;
}
$drugModel
->
name
=
$item
[
'通用名'
];
$drugModel
->
product_name
=
$item
[
'商品名'
];
if
(
$drugModel
->
save
())
{
$successNum
++
;
}
}
});
$return
=
$this
->
response
()
->
success
(
"导入成功
{
$successNum
}
条"
)
->
refresh
();
unlink
(
$filePath
);
}
catch
(
Exception
$e
)
{
$return
=
$this
->
response
()
->
error
(
"导入失败
{
$failNum
}
条:"
.
$e
->
getMessage
());
}
return
$return
;
}
/**
* 构造表单.
*/
public
function
form
()
{
$this
->
file
(
'import_file'
,
'文件'
)
->
disk
(
'local'
)
->
accept
(
'xlsx,csv'
)
->
maxSize
(
1024
*
30
)
->
autoUpload
()
->
uniqueName
()
->
required
()
->
help
(
"导入要求:<br />
<span style='color:red;'>
1、支持xlsx、csv两种格式<br \>
2、更新的时候根据本位码唯一更新药品信息
</span>"
);
$downloadUrl
=
admin_url
(
'drug-template'
);
$this
->
html
(
"<a target='_blank' href='
{
$downloadUrl
}
'>下载药品导入模板</a>"
);
}
private
static
function
toBool
(
$value
)
{
// 定义一个数组,包含所有需要转换的值及其对应的结果
$mapping
=
[
'是'
=>
1
,
'Y'
=>
1
,
'y'
=>
1
,
'1'
=>
1
,
'否'
=>
0
,
'N'
=>
0
,
'n'
=>
0
,
'0'
=>
0
,
];
// 将输入值转换为小写,以便不区分大小写
$value
=
strtolower
(
$value
);
// 检查值是否存在于映射数组中,如果存在则返回对应的值,否则返回null或自定义的默认值
return
isset
(
$mapping
[
$value
])
?
$mapping
[
$value
]
:
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment