Browse Source

[智价云] 采集启用更新

public 1 day ago
parent
commit
e63f3e0903
1 changed files with 126 additions and 198 deletions
  1. 126 198
      app/Http/Controllers/Manager/Collect/Product.php

+ 126 - 198
app/Http/Controllers/Manager/Collect/Product.php

@@ -312,75 +312,51 @@ class Product extends Controller
             foreach ($platforms as $platform) {
                 // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
                 $product_specs_log = $ProductModel
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product, $sampling_start_time) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
-                                });
-                            });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
-                            $querys->where('sampling_start_time', '>=', $sampling_start_time)->where('sampling_start_time', '<=', $sampling_start_time);
-                        }
+                    ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
+                    ->where([
+                        'product_brand' => $Product->product_brand,
+                        'product_name' => $Product->product_name,
+                        'status' => '0',
+                        'company_id' => $Product->company_id
+                    ])
+                    ->where(function ($query) use ($product_specs) {
+                        $query->where(function ($query_li) use ($product_specs) {
+                            if (!empty($product_specs)) {
+                                foreach ($product_specs as $product_spec) {
+                                    if (empty($product_spec)) continue;
+                                    $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
+                                }
+                            } else {
+                                // 如果没有规格条件,添加一个永远为假的条件
+                                $query_li->whereRaw('1 = 0');
+                            }
+                        });
                     })
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product, $sampling_end_time) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
-                                });
+                    // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
+                    ->where(function ($query) use ($Product, $sampling_start_time, $sampling_end_time) {
+                        // 新增记录有完整的时间段
+                        if ($sampling_start_time > 0 && $sampling_end_time > 0) {
+                            $query->where(function ($q) use ($sampling_start_time,$sampling_end_time ) {
+                                $q->where('sampling_start_time', '<=', $sampling_end_time)
+                                ->where('sampling_end_time', '>=', $sampling_start_time);
                             });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
-                            $querys->where('sampling_end_time', '>=', $sampling_end_time)->where('sampling_end_time', '<=', $sampling_end_time);
-                        }
-                    })
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where('id', '!=', $Product->id)
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
+                        } 
+                        // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
+                        elseif ($sampling_start_time > 0 && $sampling_end_time == 0) {
+                            $query->where(function ($q) use ($sampling_start_time) {
+                                // 已存在记录的开始时间 >= 新增记录的开始时间
+                                // 或者已存在记录的结束时间 >= 新增记录的开始时间(且已存在记录尚未结束)
+                                $q->where('sampling_end_time', '>=', $sampling_start_time)
+                                ->orWhere(function ($q2) use ($sampling_start_time) {
+                                    $q2->where('sampling_start_time', '>=', $sampling_start_time)
+                                        ->where('sampling_end_time', 0);
                                 });
                             });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time == 0) {
-                            $querys->where(function ($query_li) use ($Product) {
-                                $query_li->where('sampling_start_time', '>=', $Product->sampling_start_time);
+                        }
+                        // 新增记录只有结束时间
+                        elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
+                            $query->where(function ($q) use ($Product) {
+                                $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
                             });
                         }
                     })
@@ -487,75 +463,52 @@ class Product extends Controller
             foreach ($platforms as $platform) {
                 // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
                 $product_specs_log = $ProductModel
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product, $sampling_start_time) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
-                                });
-                            });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
-                            $querys->where('sampling_start_time', '>=', $sampling_start_time)->where('sampling_start_time', '<=', $sampling_start_time);
-                        }
+                    ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
+                    ->where([
+                        'product_brand' => $Product->product_brand,
+                        'product_name' => $Product->product_name,
+                        'status' => '0',
+                        'company_id' => $Product->company_id
+                    ])
+                    ->where('id', '!=', $Product->id)
+                    ->where(function ($query) use ($product_specs) {
+                        $query->where(function ($query_li) use ($product_specs) {
+                            if (!empty($product_specs)) {
+                                foreach ($product_specs as $product_spec) {
+                                    if (empty($product_spec)) continue;
+                                    $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
+                                }
+                            } else {
+                                // 如果没有规格条件,添加一个永远为假的条件
+                                $query_li->whereRaw('1 = 0');
+                            }
+                        });
                     })
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product, $sampling_end_time) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
-                                });
+                    // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
+                    ->where(function ($query) use ($Product, $sampling_start_time, $sampling_end_time) {
+                        // 新增记录有完整的时间段
+                        if ($sampling_start_time > 0 && $sampling_end_time > 0) {
+                            $query->where(function ($q) use ($sampling_start_time,$sampling_end_time ) {
+                                $q->where('sampling_start_time', '<=', $sampling_end_time)
+                                ->where('sampling_end_time', '>=', $sampling_start_time);
                             });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
-                            $querys->where('sampling_end_time', '>=', $sampling_end_time)->where('sampling_end_time', '<=', $sampling_end_time);
-                        }
-                    })
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where('id', '!=', $Product->id)
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
+                        } 
+                        // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
+                        elseif ($sampling_start_time > 0 && $sampling_end_time == 0) {
+                            $query->where(function ($q) use ($sampling_start_time) {
+                                // 已存在记录的开始时间 >= 新增记录的开始时间
+                                // 或者已存在记录的结束时间 >= 新增记录的开始时间(且已存在记录尚未结束)
+                                $q->where('sampling_end_time', '>=', $sampling_start_time)
+                                ->orWhere(function ($q2) use ($sampling_start_time) {
+                                    $q2->where('sampling_start_time', '>=', $sampling_start_time)
+                                        ->where('sampling_end_time', 0);
                                 });
                             });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time == 0) {
-                            $querys->where(function ($query_li) use ($Product) {
-                                $query_li->where('sampling_start_time', '>=', $Product->sampling_start_time);
+                        }
+                        // 新增记录只有结束时间
+                        elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
+                            $query->where(function ($q) use ($Product) {
+                                $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
                             });
                         }
                     })
@@ -639,84 +592,59 @@ class Product extends Controller
             foreach ($platforms as $platform) {
                 // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
                 $product_specs_log = $ProductModel
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where('id', '!=', $Product->id)
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
-                                });
-                            });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
-                            $querys->where('sampling_start_time', '>=', $Product->sampling_start_time)->where('sampling_end_time', '<=', $Product->sampling_start_time);
-                        }
+                    ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
+                    ->where([
+                        'product_brand' => $Product->product_brand,
+                        'product_name' => $Product->product_name,
+                        'status' => '0',
+                        'company_id' => $Product->company_id
+                    ])
+                    ->where('id', '!=', $Product->id)
+                    ->where(function ($query) use ($product_specs) {
+                        $query->where(function ($query_li) use ($product_specs) {
+                            if (!empty($product_specs)) {
+                                foreach ($product_specs as $product_spec) {
+                                    if (empty($product_spec)) continue;
+                                    $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
+                                }
+                            } else {
+                                // 如果没有规格条件,添加一个永远为假的条件
+                                $query_li->whereRaw('1 = 0');
+                            }
+                        });
                     })
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where('id', '!=', $Product->id)
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
-                                });
-                            });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
+                    // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
+                    ->where(function ($query) use ($Product) {
+                        // 新增记录有完整的时间段
                         if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
-                            $querys->where('sampling_end_time', '>=', $Product->sampling_end_time)->where('sampling_end_time', '<=', $Product->sampling_end_time);
-                        }
-                    })
-                    ->orWhere(function ($querys) use ($product_specs, $platform, $Product) {
-                        $querys->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                            ->where([
-                                'product_brand' => $Product->product_brand,
-                                'product_name' => $Product->product_name,
-                                'status' => '0',
-                                'company_id' => $Product->company_id
-                            ])
-                            ->where('id', '!=', $Product->id)
-                            ->where(function ($query) use ($product_specs) {
-                                $query->where(function ($query_li) use ($product_specs) {
-                                    if (!empty($product_specs)) {
-                                        foreach ($product_specs as $product_spec) {
-                                            if (empty($product_spec)) continue;
-                                            $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                        }
-                                    }
+                            $query->where(function ($q) use ($Product ) {
+                                $q->where('sampling_start_time', '<=', $Product->sampling_end_time)
+                                ->where('sampling_end_time', '>=', $Product->sampling_start_time);
+                            });
+                        } 
+                        // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
+                        elseif ($Product->sampling_start_time > 0 && $Product->sampling_end_time == 0) {
+                            $query->where(function ($q) use ($Product) {
+                                // 已存在记录的开始时间 >= 新增记录的开始时间
+                                // 或者已存在记录的结束时间 >= 新增记录的开始时间(且已存在记录尚未结束)
+                                $q->where('sampling_end_time', '>=', $Product->sampling_start_time)
+                                ->orWhere(function ($q2) use ($Product) {
+                                    $q2->where('sampling_start_time', '>=', $Product->sampling_start_time)
+                                        ->where('sampling_end_time', 0);
                                 });
                             });
-                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
-                        if ($Product->sampling_start_time > 0 && $Product->sampling_end_time == 0) {
-                            $querys->where(function ($query_li) use ($Product) {
-                                $query_li->where('sampling_start_time', '>=', $Product->sampling_start_time);
+                        }
+                        // 新增记录只有结束时间
+                        elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
+                            $query->where(function ($q) use ($Product) {
+                                $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
                             });
                         }
                     })
                     ->first();
                 
                 $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
-                if (!empty($product_specs_log) && $status == 0) {
+                if (!empty($product_specs_log)) {
                     return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
                 }
             }