|
@@ -11,6 +11,10 @@ use App\Models\Manager\WashConfig\ProductCategory as ProductCategoryModel;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use App\Models\Manager\WashConfig\LowPriceGoods as LowPriceGoodsModel;
|
|
use App\Models\Manager\WashConfig\LowPriceGoods as LowPriceGoodsModel;
|
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
|
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
|
|
|
+use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 数据清洗-禁止产品配置
|
|
* 数据清洗-禁止产品配置
|
|
@@ -363,4 +367,141 @@ class ViolationProduct extends Controller
|
|
|
return json_send(['code' => 'error', 'msg' => '删除失败', 'data' => $e->getMessage(), 'k' => $ViolationProduct_log]);
|
|
return json_send(['code' => 'error', 'msg' => '删除失败', 'data' => $e->getMessage(), 'k' => $ViolationProduct_log]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载导入模板
|
|
|
|
|
+ * @author 唐远望
|
|
|
|
|
+ * @version 1.0
|
|
|
|
|
+ * @date 2026-02-28
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public function download_template()
|
|
|
|
|
+ {
|
|
|
|
|
+ // 创建一个新的 Spreadsheet 对象
|
|
|
|
|
+ $spreadsheet = new Spreadsheet();
|
|
|
|
|
+ $sheet = $spreadsheet->getActiveSheet();
|
|
|
|
|
+
|
|
|
|
|
+ //合并单元格
|
|
|
|
|
+ $sheet->mergeCells('A1:R1');
|
|
|
|
|
+ $sheet->setCellValue('A1', '禁止挂网商品清洗导入模板'); // 设置合并后的单元格内容
|
|
|
|
|
+ // 获取合并后的单元格样式对象
|
|
|
|
|
+ $style = $sheet->getStyle('A1');
|
|
|
|
|
+ // 设置水平居中和垂直居中
|
|
|
|
|
+ $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
|
|
|
|
|
+ // 然后设置行高以适应两行文本
|
|
|
|
|
+ $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
|
|
|
|
|
+ // 设置表头
|
|
|
|
|
+ $sheet->setCellValue('A2', '商品名称*');
|
|
|
|
|
+ $sheet->setCellValue('B2', '商品分类名称');
|
|
|
|
|
+ $sheet->setCellValue('C2', '商品规格*');
|
|
|
|
|
+ $sheet->setCellValue('D2', '平台名称(多个逗号隔开,为空全部)');
|
|
|
|
|
+ $sheet->setCellValue('E2', '指定公司(多个逗号隔开,为空全部)');
|
|
|
|
|
+ $sheet->setCellValue('F2', '指派责任人(是/否)*');
|
|
|
|
|
+ // 生成 Excel 文件
|
|
|
|
|
+ $writer = new Xlsx($spreadsheet);
|
|
|
|
|
+
|
|
|
|
|
+ // 直接输出到浏览器(下载)
|
|
|
|
|
+ $filename = '禁止挂网商品清洗导入模板.xlsx';
|
|
|
|
|
+ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
|
|
+ header('Content-Disposition: attachment;filename="' . $filename . '"');
|
|
|
|
|
+ header('Cache-Control: max-age=0');
|
|
|
|
|
+ $writer->save('php://output');
|
|
|
|
|
+ exit;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 导入Excel数据
|
|
|
|
|
+ * @author 唐远望
|
|
|
|
|
+ * @version 1.0
|
|
|
|
|
+ * @date 2026-02-28
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public function import_data(Request $request, ViolationProductModel $ViolationProductModel, ViolationStoreModel $ViolationStoreModel, ProductCategoryModel $ProductCategoryModel)
|
|
|
|
|
+ {
|
|
|
|
|
+ $request->scene('import_data')->validate();
|
|
|
|
|
+ $admin_company_id = request('admin_company_id', '0');
|
|
|
|
|
+ $company_id = request('access_token.company_id', '0');
|
|
|
|
|
+ $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
|
|
|
|
|
+ $file = $request->file('file');
|
|
|
|
|
+ // 加载Excel文件
|
|
|
|
|
+ $spreadsheet = IOFactory::load($file->getPathname());
|
|
|
|
|
+ $sheet = $spreadsheet->getActiveSheet();
|
|
|
|
|
+ // 获取所有数据
|
|
|
|
|
+ $data = $sheet->toArray();
|
|
|
|
|
+ if (empty($data) || count($data) < 2) return json_send(['code' => 'error', 'msg' => '导入数据为空']);
|
|
|
|
|
+ $platform_data = $ViolationProductModel->platform_data();
|
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
|
+ try {
|
|
|
|
|
+ foreach ($data as $key => $item) {
|
|
|
|
|
+ if ($key < 2) continue;
|
|
|
|
|
+ //强制必传参数校验
|
|
|
|
|
+ $res_data = $this->import_data_check($key, $item);
|
|
|
|
|
+ if ($res_data) return json_send($res_data);
|
|
|
|
|
+ $category_id = '0';
|
|
|
|
|
+ if (isset($item[1])) {
|
|
|
|
|
+ $category_data = $ProductCategoryModel->where('name', $item[1])->first();
|
|
|
|
|
+ $category_id = $category_data ? $category_data->id : '0';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $platform_text = isset($item[3]) ? explode(',', $item[3]) : '0'; // 平台
|
|
|
|
|
+ $platform_id_text = '';
|
|
|
|
|
+ if (!empty($platform_text)) {
|
|
|
|
|
+ foreach ($platform_text as $key => $value) {
|
|
|
|
|
+ if (isset($platform_data[$value])) {
|
|
|
|
|
+ $platform_id_text = $platform_data[$value] . ',' . $platform_id_text;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $company_scope = isset($item[4]) ? explode(',', $item[4]) : '0'; // 公司范围
|
|
|
|
|
+ $company_id_text = '';
|
|
|
|
|
+ if (!empty($company_scope)) {
|
|
|
|
|
+ //查询所有公司ID逗号隔开
|
|
|
|
|
+ $company_id_text = $ViolationStoreModel->whereIn('company_name', $company_scope)->pluck('id')->implode(',');
|
|
|
|
|
+ }
|
|
|
|
|
+ // 权限判断
|
|
|
|
|
+ if ($is_admin != 1 && $company_id != 0) {
|
|
|
|
|
+ $insert_product_data['company_id'] = $company_id;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $insert_product_data['company_id'] = $admin_company_id;
|
|
|
|
|
+ }
|
|
|
|
|
+ $insert_product_data['product_name'] = $item[0]; // 商品名称
|
|
|
|
|
+ $insert_product_data['category_id'] =$category_id; // 商品分类
|
|
|
|
|
+ $insert_product_data['product_specs'] = $item[2]; // 商品规格
|
|
|
|
|
+ $insert_product_data['platform'] = $platform_id_text != '' ? substr($platform_id_text, 0, -1) : '0'; // 平台:0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药久久
|
|
|
|
|
+ $insert_product_data['store_scope'] = ''; // 店铺范围(为空时全部,指定时为店铺ID多个逗号隔开)
|
|
|
|
|
+ $insert_product_data['company_scope'] = $company_id_text; // 公司范围(为空时全部,指定时为公司ID多个逗号隔开)
|
|
|
|
|
+ $insert_product_data['specify_responsible_person'] = trim($item[5]) == '是' ? 0 : 1; // 指派责任人 0=开启 1=关闭
|
|
|
|
|
+ //插入数据
|
|
|
|
|
+ $ViolationProductModel->addViolationProduct($insert_product_data);
|
|
|
|
|
+ }
|
|
|
|
|
+ DB::commit();
|
|
|
|
|
+ return json_send(['code' => 'success', 'msg' => '导入成功']);
|
|
|
|
|
+ // 成功处理...
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ // 错误处理...
|
|
|
|
|
+ return json_send(['code' => 'error', 'msg' => '导入失败', 'data' => $e->getMessage()]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 导入Excel数据必传参数校验
|
|
|
|
|
+ * @author 唐远望
|
|
|
|
|
+ * @version 1.0
|
|
|
|
|
+ * @date 2025-12-31
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ private function import_data_check($key, $item)
|
|
|
|
|
+ {
|
|
|
|
|
+ $key = $key + 1;
|
|
|
|
|
+ if (!$item[0]) {
|
|
|
|
|
+ return ['code' => 'error', 'msg' => "第{$key}行商品名称不能为空", 'data' => $item];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!$item[2]) {
|
|
|
|
|
+ return ['code' => 'error', 'msg' => "第{$key}行商品规格不能为空", 'data' => $item];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!$item[5]) {
|
|
|
|
|
+ return ['code' => 'error', 'msg' => "第{$key}行指派责任人不能为空", 'data' => $item];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|