Prechádzať zdrojové kódy

[智价云] 采集商品配置功能

tangyuanwang 2 mesiacov pred
rodič
commit
e302f94890

+ 253 - 0
app/Http/Controllers/Manager/Collect/Product.php

@@ -0,0 +1,253 @@
+<?php
+
+namespace App\Http\Controllers\Manager\Collect;
+
+use App\Http\Controllers\Controller;
+use App\Http\Requests\Manager\Collect\Product as Request;
+use App\Models\Manager\Collect\Product as ProductModel;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Carbon;
+
+/**
+ * 采集配置-商品管理
+ * @author    唐远望
+ * @version   1.0
+ * @date      2025-12-30
+ */
+class Product extends Controller
+{
+    /**
+     * 列表
+     * @author    唐远望
+     * @version   1.0
+     * @date      2025-12-30
+     * 
+     */
+    public function list(Request $request, ProductModel $ProductModel)
+    {
+        $request->scene('list')->validate();
+        // 查询条件
+        $map  = [];
+        $limit = request('limit', config('page_num', 10));
+        $status    = request('status', '');
+        $start_time = request('start_time', '');
+        $end_time = request('end_time', '');
+        $product_name = request('product_name', '');
+        $platform = request('platform', '');
+        // 时间条件
+        if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
+        if ($end_time) $map[]   = ['insert_time', '<=', strtotime($end_time)];
+        // 其他条件
+        if (is_numeric($status)) $map[] = ['status', '=', $status];
+        if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
+        if ($platform) $map[] = ['platform', 'like', "%$platform%"];
+        // 查询数据
+        $result = $ProductModel->query()
+            ->where($map)
+            ->orderByDesc('id')
+            ->paginate($limit)->toarray();
+        // 分配数据
+        if (!$result)  return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
+        if (isset($result['data']) && count($result['data']) > 0) {
+            foreach ($result['data'] as $key => $value) {
+                $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
+            }
+        }
+        // 加载模板
+        return        json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
+    }
+
+    /**
+     * 详情
+     * @author    唐远望
+     * @version   1.0
+     * @date      2025-12-30
+     */
+    public function detail(Request $request, ProductModel $ProductModel)
+    {
+        $request->scene('detail')->validate();
+        // 接收参数
+        $id = request('id', 0);
+        $map = ['id' => $id];
+        $data = $ProductModel->where($map)->first();
+        if (!$data)     return json_send(['code' => 'error', 'msg' => '记录不存在']);
+
+        $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
+        // 加载模板
+        return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
+    }
+
+    /**
+     * 添加
+     * @author    唐远望
+     * @version   1.0
+     * @date      2025-12-30
+     * 
+     */
+    public function add(Request $request, ProductModel $ProductModel)
+    {
+        $request->scene('add')->validate();
+        //商品启用数量
+        $product_count = $ProductModel->where('status', 0)->count();
+        //判断是否超过限制
+        if ($product_count >= 50) {
+            return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
+        }
+        // 接收数据
+        $all_data = request()->all();
+        //采集信息配置
+        $minimum_order_quantity = request('minimum_order_quantity', 0);
+        $sampling_cycle = request('sampling_cycle', '0');
+        $sampling_start_time = request('sampling_start_time', '');
+        $sampling_end_time = request('sampling_end_time', '');
+        $all_data['sampling_cycle'] = $sampling_cycle;
+        $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
+        $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
+        $all_data['minimum_order_quantity'] = $minimum_order_quantity;
+        $allow_sampling_time = Carbon::tomorrow()->startOfDay()->getTimestamp(); // 明天的开始时间(允许开始采集时间校验)
+        if ($all_data['sampling_start_time']  && $all_data['sampling_start_time'] < $allow_sampling_time) return json_send(['code' => 'error', 'msg' => '采集最早开始时间为明天']);
+        //查询是否存在
+        $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
+        $data = $ProductModel->where($map)->first();
+        if ($data)     return json_send(['code' => 'error', 'msg' => '记录已存在']);
+        // 写入数据表
+        $result     =  $ProductModel->addProduct($all_data);
+        // 如果操作失败
+        if (!$result)     return json_send(['code' => 'error', 'msg' => '新增失败']);
+        // 记录行为
+        $admin_id   = request('access_token.uid', 0); //用户ID
+        $table_name = $ProductModel->getTable();
+        $notes_type = 1; //操作类型,1添加,2修改,3=删除
+        $this->addAdminHistory('采集配置-商品管理', $admin_id, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
+        // 告知结果
+        return json_send(['code' => 'success', 'msg' => '新增成功']);
+    }
+
+    /**
+     * 修改
+     * @author    唐远望
+     * @version   1.0
+     * @date      2025-12-30
+     * 
+     */
+    public function edit(Request $request, ProductModel $ProductModel)
+    {
+        $request->scene('edit')->validate();
+        // 接收参数
+        $id         = request('id', 0);
+        // 接收数据
+        $all_data = request()->all();
+        //采集信息配置
+        $minimum_order_quantity = request('minimum_order_quantity', 0);
+        $sampling_cycle = request('sampling_cycle', '0');
+        $sampling_start_time = request('sampling_start_time', '');
+        $sampling_end_time = request('sampling_end_time', '');
+        $all_data['sampling_cycle'] = $sampling_cycle;
+        $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
+        $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
+        $all_data['minimum_order_quantity'] = $minimum_order_quantity;
+        //查询是否存在
+        $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
+        $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
+        if ($data)     return json_send(['code' => 'error', 'msg' => '记录已存在']);
+        // 更新数据表
+        $where = ['id' => $id];
+        $Product = $ProductModel->where($where)->first();
+        if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
+        $oldData = $Product->toarray();
+        //如果修改采集周期,则校验采集时间是否在明天以后
+        if ($sampling_cycle != $Product->sampling_cycle) {
+            $allow_sampling_time = Carbon::tomorrow()->startOfDay()->getTimestamp(); // 明天的开始时间(允许开始采集时间校验)
+            if ($all_data['sampling_start_time']  && $all_data['sampling_start_time'] < $allow_sampling_time) return json_send(['code' => 'error', 'msg' => '采集最早开始时间为明天']);
+        }
+        $result =  $ProductModel->editProduct_content($Product, $all_data);
+        // 如果操作失败
+        if (!$result)     return json_send(['code' => 'error', 'msg' => '修改失败']);
+        // 记录行为
+        $admin_id   = request('access_token.uid', 0); //用户ID
+        $table_name = $ProductModel->getTable();
+        $notes_type = 2; //操作类型,1添加,2修改,3=删除
+        $this->addAdminHistory('采集配置-商品管理', $admin_id, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
+        // 告知结果
+        return json_send(['code' => 'success', 'msg' => '修改成功']);
+    }
+
+    /**
+     * 修改状态
+     * @author    唐远望
+     * @version   1.0
+     * @date      2025-12-30
+     * 
+     */
+    public function set_status(Request $request, ProductModel $ProductModel)
+    {
+        // 验证参数
+        $request->scene('set_status')->validate();
+        // 接收数据
+        $id                = request('id', 0);
+        $status            = request('status', 0);
+        if ($status == 0) {
+            //获取商品启用数量
+            $product_count = $ProductModel->where('status', 0)->count();
+            //判断是否超过限制
+            if ($product_count >= 50) {
+                return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
+            }
+        }
+        // 查询用户
+        $where = ['id' => $id];
+        $Product = $ProductModel->where($where)->first();
+        if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
+        // 执行修改
+        $result            =  $ProductModel->changeStatus($Product, $status);
+        // 提示新增失败
+        if (!$result)    return json_send(['code' => 'error', 'msg' => '设置失败']);
+        // 记录行为
+        $admin_id   = request('access_token.uid', 0); //用户ID
+        $table_name = $ProductModel->getTable();
+        $notes_type = 2; //操作类型,1添加,2修改,3=删除
+        $this->addAdminHistory('采集配置-商品管理', $admin_id, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
+        // 告知结果
+        return             json_send(['code' => 'success', 'msg' => '设置成功']);
+    }
+
+
+    /**
+     * 删除
+     * @author    唐远望
+     * @version   1.0
+     * @date      2025-12-30
+     * 
+     */
+    public function delete(Request $request, ProductModel $ProductModel)
+    {
+        // 验证参数
+        $request->scene('delete')->validate();
+        // 接收数据
+        $id = request('id', 0);
+        // 查询用户
+        $where = ['id' => $id];
+        // 执行删除
+        $Product = $ProductModel->where($where)->first();
+        if (!$Product) {
+            return false;
+        }
+        DB::beginTransaction();
+        try {
+            $Product->delete();
+            // 记录行为
+            $admin_id   = request('access_token.uid', 0); //用户ID
+            $table_name = $ProductModel->getTable();
+            $notes_type = 3; //操作类型,1添加,2修改,3=删除
+            $this->addAdminHistory('采集配置-商品管理', $admin_id, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
+            // 告知结果
+            DB::commit();
+            return             json_send(['code' => 'success', 'msg' => '删除成功']);
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            // 错误处理...
+            return             json_send(['code' => 'error', 'msg' => '删除失败']);
+        }
+    }
+}

+ 81 - 0
app/Http/Requests/Manager/Collect/Product.php

@@ -0,0 +1,81 @@
+<?php
+
+namespace App\Http\Requests\Manager\Collect;
+
+use App\Http\Requests\BaseRequest;
+
+/**
+ * 采集配置-商品信息验证
+ * @author 唐远望
+ * @version 1.0
+ * @date 2025-1230
+ * 
+ */
+class Product extends BaseRequest
+{
+    /**
+     * 获取应用于请求的规则
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        // 返回结果
+        return      [
+            'name'                 => 'required',
+            'id'                => 'required|integer|gt:0',
+            'status'            => 'required|integer|in:0,1',
+            'page'              => 'integer|min:1',
+            'limit'             => 'integer|min:1',
+            'image_url'         => 'required',
+            'link_url'          => 'required',
+            'sort'              => 'required|integer|min:0',
+            'product_name'      => 'required',
+            'product_specs'     => 'required',
+            'suggested_price'   => 'required',
+            'store_scope'       => 'required',
+            'platform'          => 'required',
+        ];
+    }
+
+
+    // 场景列表
+    protected   $scenes         = [
+        'detail'             => ['id'],
+        'list'               => ['page', 'limit'],
+        'add'                      => ['platform','product_name', 'product_specs'],
+        'edit'                  => ['id','platform','product_name', 'product_specs'],
+        'set_status'              => ['id', 'status'],
+        'delete'                  => ['id'],
+    ];
+
+    /**
+     * 获取已定义验证规则的错误消息
+     *
+     * @return array
+     */
+    public function messages()
+    {
+        return [
+            'name.required'     => '名称必填',
+            'id.required'       => 'ID未知',
+            'id.integer'        => 'ID格式错误',
+            'id.gt'               => 'ID格式错误',
+            'status.required'   => '状态未知',
+            'status.integer'    => '状态格式错误',
+            'status.in'         => '状态格式错误',
+            'page.integer'      => '页码格式错误',
+            'page.min'          => '页码格式错误',
+            'limit.integer'     => '每页数量格式错误',
+            'limit.min'         => '每页数量格式错误',
+            'image_url.required'    => '图片链接未知',
+            'link_url.required'     => '链接地址未知',
+            'sort.required'         => '排序未知',
+            'sort.integer'          => '排序格式错误',
+            'sort.min'              => '排序格式错误',
+            'product_name.required' => '商品名称未知',
+            'product_specs.required'    => '商品规格未知',
+            'platform.required'      => '平台未知',
+        ];
+    }
+}

+ 183 - 0
app/Models/Manager/Collect/Product.php

@@ -0,0 +1,183 @@
+<?php
+
+namespace App\Models\manager\Collect;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * 采集配置-商品管理
+ * @author: 唐远望
+ * @version: 1.0
+ * @date: 2025-12-30
+ */
+class Product extends Model
+{
+    use HasFactory;
+    // 与模型关联的表名
+    protected $table = 'collect_product';
+    // 是否主动维护时间戳
+    public $timestamps = false;
+    // 定义时间戳字段名
+    // const CREATED_AT = 'insert_time';
+    // const UPDATED_AT = 'update_time';
+
+
+    /**
+     * 添加
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-30
+     */
+    public function addProduct_content($data)
+    {
+        $insert_data = [
+            'platform' => $data['platform'],
+            'product_name' => $data['product_name'],
+            'product_specs' => $data['product_specs'],
+            'minimum_order_quantity' => $data['minimum_order_quantity'],
+            'sampling_cycle' => $data['sampling_cycle'],
+            'sampling_start_time' => $data['sampling_start_time'],
+            'sampling_end_time' => $data['sampling_end_time'],
+            'insert_time' => time(),
+        ];
+        $Product_id = $this->insertGetId($insert_data);
+        return $Product_id;
+    }
+
+
+    /**
+     * 写入数据
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-30
+     * @param $data
+     * @return bool
+     */
+    public function addProduct($data)
+    {
+        DB::beginTransaction();
+        try {
+            $insert_data = [
+                'platform' => $data['platform'],
+                'product_name' => $data['product_name'],
+                'product_specs' => $data['product_specs'],
+                'minimum_order_quantity' => $data['minimum_order_quantity'],
+                'sampling_cycle' => $data['sampling_cycle'],
+                'sampling_start_time' => $data['sampling_start_time'],
+                'sampling_end_time' => $data['sampling_end_time'],
+                'insert_time' => time(),
+            ];
+            $Product_id = $this->insertGetId($insert_data);
+            DB::commit();
+            return $Product_id;
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            // 错误处理...
+            return false;
+        }
+    }
+
+
+    /**
+     * 编辑内容
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-30
+     * @param $data
+     * @return bool
+     */
+    public function editProduct_content($Product, $data)
+    {
+        DB::beginTransaction();
+        try {
+            $Product->platform = $data['platform'];
+            $Product->product_name = $data['product_name'];
+            $Product->product_specs = $data['product_specs'];
+            $Product->minimum_order_quantity = $data['minimum_order_quantity'];
+            $Product->sampling_cycle = $data['sampling_cycle'];
+            $Product->sampling_start_time = $data['sampling_start_time'];
+            $Product->sampling_end_time = $data['sampling_end_time'];
+            $Product->update_time = time();
+            $Product->save();
+            DB::commit();
+            return true;
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            // 错误处理...
+            return false;
+        }
+    }
+
+
+
+    /**
+     * 更新数据
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-30
+     * @param $data
+     * @return bool
+     */
+    public function updateProduct($where, $data)
+    {
+        DB::beginTransaction();
+        try {
+            $this->editProduct_content($where, $data);
+            DB::commit();
+            return true;
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            // 错误处理...
+            return false;
+        }
+    }
+
+    /**
+     * 修改状态
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-30
+     * @param $id
+     * @param $status
+     * @return bool
+     */
+    public function changeStatus($Product, $status)
+    {
+        $Product->status = $status;
+        $Product->update_time = time();
+        $Product->save();
+        return true;
+    }
+
+    /**
+     * 删除数据
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-30
+     * @param $id
+     * @return bool
+     */
+    public function deleteProduct($where)
+    {
+        $Product = $this->where($where)->first();
+        if (!$Product) {
+            return false;
+        }
+        DB::beginTransaction();
+        try {
+            $Product->delete();
+            DB::commit();
+            return true;
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            // 错误处理...
+            return false;
+        }
+    }
+}

+ 15 - 1
routes/manager.php

@@ -266,4 +266,18 @@ Route::any('statistics/overview_panel/product_trend_export', [App\Http\Controlle
 //报表统计-商家数量趋势
 Route::any('statistics/overview_panel/get_store_trend', [App\Http\Controllers\Manager\Statistics\OverviewPanel::class, 'get_store_trend']);
 //报表统计-商家数量趋势-导出
-Route::any('statistics/overview_panel/store_trend_export', [App\Http\Controllers\Manager\Statistics\OverviewPanel::class, 'store_trend_export']);
+Route::any('statistics/overview_panel/store_trend_export', [App\Http\Controllers\Manager\Statistics\OverviewPanel::class, 'store_trend_export']);
+
+//采集配置-商品管理-列表
+Route::any('collect/product/list', [App\Http\Controllers\Manager\Collect\Product::class, 'list']);
+//采集配置-商品管理-添加
+Route::any('collect/product/add', [App\Http\Controllers\Manager\Collect\Product::class, 'add']);
+//采集配置-商品管理-详情
+Route::any('collect/product/detail', [App\Http\Controllers\Manager\Collect\Product::class, 'detail']);
+//采集配置-商品管理-修改
+Route::any('collect/product/edit', [App\Http\Controllers\Manager\Collect\Product::class, 'edit']);
+//采集配置-商品管理-删除
+Route::any('collect/product/delete', [App\Http\Controllers\Manager\Collect\Product::class, 'delete']);
+//采集配置-商品管理-状态修改
+Route::any('collect/product/set_status', [App\Http\Controllers\Manager\Collect\Product::class,'set_status']);
+