Selaa lähdekoodia

[智价云] 低价清洗导入模板下载&数据导入

tangyuanwang 1 viikko sitten
vanhempi
sitoutus
bc5f1ad09a

+ 145 - 1
app/Http/Controllers/Manager/WashConfig/LowPriceGoods.php

@@ -11,6 +11,10 @@ use App\Models\Manager\WashConfig\ProductCategory as ProductCategoryModel;
 use Illuminate\Support\Facades\DB;
 use App\Models\Manager\WashConfig\ViolationProduct as ViolationProductModel;
 use Illuminate\Support\Carbon;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
+use PhpOffice\PhpSpreadsheet\IOFactory;
 
 /**
  * 数据清洗-低价产品配置
@@ -241,7 +245,7 @@ class LowPriceGoods extends Controller
         $LowProduct = $LowPriceGoodsModel->where($where)->first();
         if (!$LowProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
         $oldData = $LowProduct->toarray();
-       
+
         $result =  $LowPriceGoodsModel->editLowProduct_content($LowProduct, $all_data);
         // 如果操作失败
         if (!$result)     return json_send(['code' => 'error', 'msg' => '修改失败']);
@@ -364,4 +368,144 @@ class LowPriceGoods extends Controller
             return json_send(['code' => 'error', 'msg' => '删除失败']);
         }
     }
+
+    /**
+     * 下载导入模板
+     * @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', '指定公司(多个逗号隔开,为空全部)');
+        $sheet->setCellValue('G2', '指派责任人(是/否)');
+        // 生成 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, LowPriceGoodsModel $LowPriceGoodsModel, 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 = $LowPriceGoodsModel->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[4]) ? explode(',', $item[4]) : '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[5]) ? explode(',', $item[5]) : '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['suggested_price'] = $item[3]; // 指导价格
+                $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[6]) == '是' ? 0 : 1; // 指派责任人 0=开启 1=关闭
+                //插入数据
+                $LowPriceGoodsModel->addLowProduct($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[3]) {
+            return ['code' => 'error', 'msg' => "第{$key}行监控价格不能为空", 'data' => $item];
+        }
+    }
 }

+ 2 - 0
app/Http/Requests/Manager/WashConfig/LowPriceGoods.php

@@ -35,6 +35,7 @@ class LowPriceGoods extends BaseRequest
             'suggested_price'   => 'required',
             'store_scope'       => 'required',
             'platform'          => 'required',
+            'file' => 'required|mimes:xlsx,xls,csv|max:10240'
         ];
     }
 
@@ -48,6 +49,7 @@ class LowPriceGoods extends BaseRequest
         'set_status'              => ['id', 'status'],
         'delete'                  => ['id'],
         'spec_list'             => ['page', 'limit'],
+        'import_data'           => ['file'],
     ];
 
     /**

+ 27 - 3
app/Models/Manager/WashConfig/LowPriceGoods.php

@@ -64,7 +64,7 @@ class LowPriceGoods extends Model
         try {
             $LowPriceGoodsCompanyModel = new LowPriceGoodsCompanyModel();
             $insert_data = [
-                'company_id'=> $data['company_id'],
+                'company_id' => $data['company_id'],
                 'platform' => $data['platform'],
                 'product_name' => $data['product_name'],
                 'product_specs' => $data['product_specs'],
@@ -108,7 +108,7 @@ class LowPriceGoods extends Model
      */
     public function editLowProduct_content($LowProduct, $data)
     {
-       
+
         DB::beginTransaction();
         try {
             $LowPriceGoodsCompanyModel = new LowPriceGoodsCompanyModel();
@@ -184,7 +184,7 @@ class LowPriceGoods extends Model
      */
     public function changeStatus($LowProduct, $status)
     {
-       
+
         $LowProduct->status = $status;
         $LowProduct->update_time = time();
         $LowProduct->save();
@@ -222,4 +222,28 @@ class LowPriceGoods extends Model
             return false;
         }
     }
+
+    /**
+     * 平台定义
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-31
+     */
+    public function platform_data()
+    {
+        $platform_data = [
+            '全部' => '0',
+            '淘宝' => '1',
+            '京东' => '2',
+            '拼多多' => '3',
+            '美团' => '4',
+            '药师帮' => '5',
+            '1药城' => '6',
+            '药九九' => '7',
+            '药易购' => '8',
+            '药帮忙' => '9',
+            '熊猫药药' => '10'
+        ];
+        return $platform_data;
+    }
 }

+ 4 - 0
routes/manager.php

@@ -99,6 +99,10 @@ Route::any('low_price_goods/edit', [App\Http\Controllers\Manager\WashConfig\LowP
 Route::any('low_price_goods/set_status', [App\Http\Controllers\Manager\WashConfig\LowPriceGoods::class, 'set_status']);
 // 低价商品-删除
 Route::any('low_price_goods/delete', [App\Http\Controllers\Manager\WashConfig\LowPriceGoods::class, 'delete']);
+// 低价商品-下载导入模板
+Route::any('low_price_goods/download_template', [App\Http\Controllers\Manager\WashConfig\LowPriceGoods::class, 'download_template']);
+// 低价商品-导入数据
+Route::any('low_price_goods/import_data', [App\Http\Controllers\Manager\WashConfig\LowPriceGoods::class, 'import_data']);
 
 // 强管控商品-列表
 Route::any('strong_control_goods/list', [App\Http\Controllers\Manager\WashConfig\ControlGoods::class, 'list']);