Forráskód Böngészése

[智价云] 采集商品新增校验更新

tangyuanwang 1 napja
szülő
commit
d59b5e8f9f
1 módosított fájl, 128 hozzáadás és 48 törlés
  1. 128 48
      app/Http/Controllers/Manager/Collect/Product.php

+ 128 - 48
app/Http/Controllers/Manager/Collect/Product.php

@@ -294,40 +294,79 @@ class Product extends Controller
             $map['company_id'] = $admin_company_id;
             $map['company_id'] = $admin_company_id;
             $all_data['company_id'] = $admin_company_id;
             $all_data['company_id'] = $admin_company_id;
         }
         }
-        $data = $ProductModel->where($map)->first();
-        if (!empty($data)) {
+        $Product = $ProductModel->where($map)->first();
+        if (!empty($Product)) {
             //继续校验规格是否存在
             //继续校验规格是否存在
-            $product_specs_tring = $all_data['product_specs'];
-            $product_specs = $product_specs_tring ?  explode(',', $product_specs_tring) : '';
+            $product_specs_tring = $Product->product_specs;
+            $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
             $product_specs = $product_specs ? array_unique($product_specs) : '';
             $product_specs = $product_specs ? array_unique($product_specs) : '';
             //继续校平台是否存在
             //继续校平台是否存在
 
 
-            $platforms = $platform_string ?  explode(',', $platform_string) : '';
+            $platform_string = $Product->platform;
+            $platforms = $platform_string ? explode(',', $platform_string) : '';
             $platforms = $platforms ? array_unique($platforms) : '';
             $platforms = $platforms ? array_unique($platforms) : '';
 
 
             $platform_data = $ProductModel->platform_index_data();
             $platform_data = $ProductModel->platform_index_data();
-            $map_where = ['product_name' => $all_data['product_name'], 'status' => '0', 'company_id' => $all_data['company_id']];
-            if ($product_brand != '') $map_where['product_brand'] = $product_brand;
-            if (!empty($platforms) && $platforms != '') {
-                foreach ($platforms as $platform) {
-                    $product_specs_log = $ProductModel
-                        ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                        ->where($map_where)
-                        ->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_specs)) continue;
-                                        $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
+            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(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]);
+                                        }
                                     }
                                     }
-                                }
+                                });
                             });
                             });
-                        })->first();
-                    $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
-                    if (!empty($product_specs_log)) {
-                        return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
-                    }
+                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
+                        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)
+                                    ->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]);
+                                        }
+                                    }
+                                });
+                            });
+                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
+                        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);
+                            });
+                        }
+                    })
+                    ->first();
+                
+                $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
+                if (!empty($product_specs_log) && $status == 0) {
+                    return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
                 }
                 }
             }
             }
         }
         }
@@ -408,39 +447,80 @@ class Product extends Controller
             $map['company_id'] = $admin_company_id;
             $map['company_id'] = $admin_company_id;
         }
         }
         $all_data['company_id'] = $map['company_id'];
         $all_data['company_id'] = $map['company_id'];
-        $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
-        if (!empty($data)) {
+        $Product = $ProductModel->where($map)->where('id', '!=', $id)->first();
+        if (!empty($Product)) {
             //继续校验规格是否存在
             //继续校验规格是否存在
-            $product_specs_tring = $all_data['product_specs'];
-            $product_specs = $product_specs_tring ?  explode(',', $product_specs_tring) : '';
+            $product_specs_tring = $Product->product_specs;
+            $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
             $product_specs = $product_specs ? array_unique($product_specs) : '';
             $product_specs = $product_specs ? array_unique($product_specs) : '';
             //继续校平台是否存在
             //继续校平台是否存在
 
 
-            $platforms = $platform_string ?  explode(',', $platform_string) : '';
+            $platform_string = $Product->platform;
+            $platforms = $platform_string ? explode(',', $platform_string) : '';
             $platforms = $platforms ? array_unique($platforms) : '';
             $platforms = $platforms ? array_unique($platforms) : '';
 
 
             $platform_data = $ProductModel->platform_index_data();
             $platform_data = $ProductModel->platform_index_data();
-            $map_where = ['product_name' => $all_data['product_name'], 'status' => '0', 'company_id' => $all_data['company_id']];
-            if ($product_brand != '') $map_where['product_brand'] = $product_brand;
             foreach ($platforms as $platform) {
             foreach ($platforms as $platform) {
+                // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
                 $product_specs_log = $ProductModel
                 $product_specs_log = $ProductModel
-                    ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
-                    ->where($map_where)
-                    ->where('id', '!=', $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_specs)) continue;
-                                    $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
-                                }
-                            }
-                        });
-                    })->first();
+                    ->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(function ($query_li) use ($Product) {
+                                $query_li->where('sampling_start_time', '<=', $Product->sampling_start_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]);
+                                        }
+                                    }
+                                });
+                            });
+                        //记录的开始跟结束时间是否在当前记录的采集开始时间结束时间之内
+                        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);
+                            });
+                        }
+                    })
+                    ->first();
+                
                 $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
                 $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
-                if (!empty($product_specs_log)) {
-                    return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
+                if (!empty($product_specs_log) && $status == 0) {
+                    return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
                 }
                 }
             }
             }
         }
         }