Commit 7b884187 by lujunyi

用法用量导入

parent 301bc685
......@@ -4,12 +4,17 @@
use App\Admin\Extensions\ToolBar\Actions\PharmacyDosageImportAction;
use App\Admin\Repositories\DosageRepository;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\EasyExcel\Excel;
use Illuminate\Http\Request;
// 用法用量
class DosageController extends AdminController
......@@ -111,4 +116,25 @@ protected function form()
$form->disableViewButton(); // 去掉跳转详情页按钮
});
}
/**
* 药品导入模板
*
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function exportDosageTemplate(Request $request)
{
$sheet1Head = ['用法用量', '显示内容'];
$sheet1Data = [['220ug qd 每鼻孔2喷', '220ug qd 每鼻孔2喷']];
$sheet1 = Excel::createSheet($sheet1Data, '用法用量信息', $sheet1Head)->row(function (array $row) {
$style = (new StyleBuilder)
->setBackgroundColor(Color::YELLOW)
->build();
return WriterEntityFactory::createRowFromArray($row, $style);
});
return Excel::export([$sheet1])->headings(false)->download('用法用量导入模板.xlsx');
}
}
......@@ -2,7 +2,8 @@
namespace App\Admin\Extensions\ToolBar\Forms;
use App\Models\DrugModel;
use App\Models\DosageModel;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Widgets\Form;
use Dcat\EasyExcel\Excel;
......@@ -20,6 +21,10 @@ class PharmacyDosageImportForm extends Form
*/
public function handle(array $input): JsonResponse
{
if (! Admin::user()->isRole('pharmacy') || ! Admin::user()->pharmacy_id) {
admin_exit(Content::make()->withError(trans('admin.deny')));
}
$pharmacyId = Admin::user()->pharmacy_id;
// 表单参数
$file = $input['import_file'];
$filePath = storage_path('app/'.$file);
......@@ -28,24 +33,25 @@ public function handle(array $input): JsonResponse
$chunkSize = 10;
$successNum = 0;
$failNum = 0;
Excel::import($filePath)->first()->chunk($chunkSize, function (SheetCollection $collection) use (&$successNum) {
Excel::import($filePath)->first()->chunk($chunkSize, function (SheetCollection $collection) use (&$successNum, $pharmacyId) {
// 此处的数组下标依然是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();
$dosageModel = null;
if (isset($item['用法用量']) && $item['用法用量']) {
$dosageModel = DosageModel::where('dosage_desc', $item['用法用量'])->first();
}
if (! $drugModel) {
$drugModel = new DrugModel;
if (! $dosageModel) {
$dosageModel = new DosageModel;
}
$drugModel->name = $item['通用名'];
$drugModel->product_name = $item['商品名'];
$dosageModel->pharmacy_id = $pharmacyId;
$dosageModel->dosage_desc = $item['用法用量'];
$dosageModel->dosage_show = $item['显示内容'] ?? '';
if ($drugModel->save()) {
if ($dosageModel->save()) {
$successNum++;
}
}
......@@ -73,25 +79,9 @@ public function form()
->required()
->help("导入要求:<br />
<span style='color:red;'>
1、支持xlsx、csv两种格式<br \>
2、更新的时候根据本位码唯一更新药品信息
1、支持xlsx、csv两种格式
</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;
$downloadUrl = admin_url('dosage-template');
$this->html("<a target='_blank' href='{$downloadUrl}'>下载用法用量导入模板</a>");
}
}
......@@ -68,6 +68,8 @@
$router->resource('prescription-print', 'PrescriptionPrintController');
// 用法用量
$router->resource('dosage', 'DosageController');
// 下载导出模板
$router->get('dosage-template', 'DosageController@exportDosageTemplate');
// 搜索生成处方单
$router->post('prescription-search', 'PrescriptionPrintController@search');
// 打印处方单
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment