|
@@ -546,27 +546,40 @@ class ImportService extends Service
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ //产品分类
|
|
|
$category_list = ProductCategory::where('del_time',0)
|
|
|
+ ->select('id','title','parent_id')
|
|
|
->where('top_depart_id',$top_depart_id)
|
|
|
->get()->toArray();
|
|
|
|
|
|
+ // 创建一个以 id 为键的数据映射
|
|
|
+ $idMap = [];
|
|
|
+ foreach ($category_list as $item) {
|
|
|
+ $idMap[$item['id']] = $item;
|
|
|
+ }
|
|
|
+ // 创建最终的结果数组
|
|
|
+ $category_parent = [];
|
|
|
+ foreach ($idMap as $id => $item) {
|
|
|
+ // 如果没有子项,则处理
|
|
|
+ if (! $this->hasChildren($id, $idMap)) {
|
|
|
+ $category_parent[$id] = $this->getAncestors($id, $idMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $category_tree = $this->makeTree(0,$category_list);
|
|
|
+ $category_map = $this->generateTitleToIdMap($category_tree);
|
|
|
+
|
|
|
+ //基础类型
|
|
|
$basic = (new BasicTypeService())->getMyBasicList($user, 20);
|
|
|
$basic = array_column($basic,'id','title');
|
|
|
|
|
|
- $category = ProductCategory::whereIn('title',array_unique(array_column($array,'2')))
|
|
|
- ->where('del_time',0)
|
|
|
- ->where('top_depart_id',$top_depart_id)
|
|
|
- ->pluck('id','title')
|
|
|
- ->toArray();
|
|
|
- $category_parent = array_unique(array_column($category,'parent'));
|
|
|
-
|
|
|
$time = time();
|
|
|
-
|
|
|
+ //表头
|
|
|
$table_head = $this->product([],$user);
|
|
|
$heads = $table_head[1];
|
|
|
$head_value = array_column($heads,'value');
|
|
|
if($head_value !== $upload) return [false, '产品模板错误,请重新下载模板导入'];
|
|
|
|
|
|
+ //写入数据库的字段
|
|
|
$tmp = array_column($heads,'key');
|
|
|
$tmp = array_fill_keys($tmp, '');
|
|
|
$tmp['product_category'] = '';
|
|
@@ -575,7 +588,6 @@ class ImportService extends Service
|
|
|
$top_message = Depart::where('parent_id',0)
|
|
|
->pluck('title','id')
|
|
|
->toArray();
|
|
|
-
|
|
|
$upload = array_flip($upload);
|
|
|
$map = [];
|
|
|
foreach ($heads as $value){
|
|
@@ -598,16 +610,14 @@ class ImportService extends Service
|
|
|
return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
$tmp['title'] = $value['0'];
|
|
|
$tmp['code'] = $value['1'];
|
|
|
- if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
|
|
|
- $tmp['product_category_id'] = $category[$value['2']];
|
|
|
- if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
|
|
|
- $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
|
|
|
- array_unshift($parentsId, $tmp['product_category_id']);
|
|
|
- $result = array_reverse($parentsId);
|
|
|
- $tmp['product_category'] = json_encode($result);
|
|
|
+
|
|
|
+ if(empty($category_map[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
|
|
|
+ $tmp['product_category_id'] = $category_map[$value['2']];
|
|
|
+ if(! isset($category_parent[$tmp['product_category_id']])) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
|
|
|
+ $parentsId = $category_parent[$tmp['product_category_id']];
|
|
|
+ $tmp['product_category'] = json_encode($parentsId);
|
|
|
$tmp['size'] = $value['3'];
|
|
|
if($value['4']){
|
|
|
if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
|
|
@@ -716,6 +726,48 @@ class ImportService extends Service
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
+ function generateTitleToIdMap($tree, $prefix = '') {
|
|
|
+ $result = [];
|
|
|
+
|
|
|
+ foreach ($tree as $node) {
|
|
|
+ $currentTitle = $prefix . $node['title'];
|
|
|
+ $result[$currentTitle] = $node['id'];
|
|
|
+
|
|
|
+ if (!empty($node['children'])) {
|
|
|
+ $result = array_merge(
|
|
|
+ $result,
|
|
|
+ $this->generateTitleToIdMap($node['children'], $currentTitle . '-')
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ function getAncestors($id, &$idMap) {
|
|
|
+ if (!isset($idMap[$id])) return [];
|
|
|
+
|
|
|
+ $ancestors = [$id];
|
|
|
+ $parentId = $idMap[$id]['parent_id'];
|
|
|
+
|
|
|
+ if ($parentId !== null) {
|
|
|
+ // 递归调用自己来添加父级 ID
|
|
|
+ $ancestors = array_merge($this->getAncestors($parentId, $idMap), $ancestors);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ancestors;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否拥有子项
|
|
|
+ function hasChildren($id, &$idMap) {
|
|
|
+ foreach ($idMap as $item) {
|
|
|
+ if ($item['parent_id'] == $id) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
public function salesOnlineImport($array, $user){
|
|
|
$head = $user['head']['id'] ?? 0;
|
|
|
|