|
@@ -57,7 +57,7 @@ class Excel
|
|
|
$result['head'][0] = ['source_name', 'channel_name', 'cate_parent_name', 'cate_child_name', 'source_type', 'sku_code', 'sku_name', 'sku_price', 'sku_pic'];
|
|
|
} else {
|
|
|
# 这是导入时的模板
|
|
|
- $result['head'][0] = ['资源名称', '资源频道', '资源主分类', '资源子分类', '资源类型', '规格编码', '规格名称', '规格售价', '规格图片'];
|
|
|
+ $result['head'][0] = ['资源名称', '资源频道', '资源主分类', '资源子分类', '资源类型', '规格编码', '规格名称', '规格售价', '规格图片(插入-图片-本地图片-裁剪-锁定纵横比-宽度设置2厘米,不要内嵌到单元格中)'];
|
|
|
}
|
|
|
$result['body'] = [];
|
|
|
if ($out) {
|
|
@@ -224,4 +224,160 @@ class Excel
|
|
|
|
|
|
return $dateStr . $line;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ # 导出一个模板出来
|
|
|
+ public function getJiuTemplate($out = false, $import = false)
|
|
|
+ {
|
|
|
+ $result = [];
|
|
|
+ if ($import) {
|
|
|
+ # 导入时的字段,一定要和下面的else里的对应
|
|
|
+ $result['head'][0] = ['source_name', 'cate_parent_name', 'cate_child_name', 'color', 'size', 'sku_price', 'sku_pic'];
|
|
|
+ } else {
|
|
|
+ # 这是导入时的模板
|
|
|
+ $result['head'][0] = ['资源名称', '资源主分类', '资源子分类', '颜色', '尺寸', '规格售价', '规格图片'];
|
|
|
+ }
|
|
|
+ $result['body'] = [];
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 导入
|
|
|
+ public function importJiu_commit(){}
|
|
|
+ public function importJiu($file)
|
|
|
+ {
|
|
|
+ # 当前登录的管理员id
|
|
|
+ $muid = Dever::getData('muser')['id'];
|
|
|
+ $mname = Dever::getData('muser')['name'];
|
|
|
+ $mdesc = $mname . '在' . date('Y-m-d H:i:s') . '导入';
|
|
|
+
|
|
|
+ $type = array_flip(Dever::db('psource/info')->config['struct']['type']['value']);
|
|
|
+
|
|
|
+ $result = $this->getJiuTemplate(false, true);
|
|
|
+
|
|
|
+ $file = Dever::load(\Upload\Lib\View::class)->local($file);
|
|
|
+ $import = Dever::load(\Excel\Lib\Import::class);
|
|
|
+ $i = 0;
|
|
|
+ foreach ($import->act($file, 0, 1, $result['head'][0]) as $row) {
|
|
|
+ if (!$row['source_name']) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $info = Dever::db('psource/info')->find(['name' => $row['source_name']]);
|
|
|
+ if (isset($row['sku_pic'][0])) {
|
|
|
+ $row['sku_pic'] = $row['sku_pic'][0];
|
|
|
+ }
|
|
|
+ $source = [];
|
|
|
+ $source['cate'] = [];
|
|
|
+
|
|
|
+ # 频道
|
|
|
+ $channel_id = 1;
|
|
|
+ $source['cate'][] = $channel_id;
|
|
|
+
|
|
|
+ # 父级分类
|
|
|
+ $cate_parent_id = 0;
|
|
|
+ if ($row['cate_parent_name']) {
|
|
|
+ $cate = Dever::db('psource/cate')->find(['name' => $row['cate_parent_name']]);
|
|
|
+ if ($cate) {
|
|
|
+ $cate_parent_id = $cate['id'];
|
|
|
+ } else {
|
|
|
+ $cate_parent_id = Dever::db('psource/cate')->insert(['name' => $row['cate_parent_name'], 'channel_id' => $channel_id]);
|
|
|
+ }
|
|
|
+ $source['cate'][] = $cate_parent_id;
|
|
|
+ $source['cate_id'] = $cate_parent_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 子分类
|
|
|
+ $cate_child_id = 0;
|
|
|
+ if ($row['cate_child_name']) {
|
|
|
+ $cate = Dever::db('psource/cate')->find(['name' => $row['cate_child_name']]);
|
|
|
+ if ($cate) {
|
|
|
+ $cate_child_id = $cate['id'];
|
|
|
+ } else {
|
|
|
+ $cate_child_id = Dever::db('psource/cate')->insert(['name' => $row['cate_child_name'], 'channel_id' => $channel_id, 'parent_id' => $cate_parent_id]);
|
|
|
+ }
|
|
|
+ $source['cate'][] = $cate_child_id;
|
|
|
+ $source['cate_id'] = $cate_child_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ $source['name'] = $row['source_name'];
|
|
|
+ $source['type'] = 1;
|
|
|
+ $source['channel_id'] = $channel_id;
|
|
|
+ $source['cate_parent_id'] = $cate_parent_id;
|
|
|
+ $source['cate_child_id'] = $cate_child_id;
|
|
|
+ $source['cate'] = implode(',', $source['cate']);
|
|
|
+
|
|
|
+ if ($info) {
|
|
|
+ if ($row['sku_pic']) {
|
|
|
+ $pic = explode(',', $info['pic']);
|
|
|
+ $pic[] = $row['sku_pic'];
|
|
|
+ $source['pic'] = implode(',', $pic);
|
|
|
+ }
|
|
|
+ if ($info['price'] > $row['sku_price']) {
|
|
|
+ $source['price'] = $row['sku_price'];
|
|
|
+ }
|
|
|
+ Dever::db('psource/info')->update($info['id'], $source);
|
|
|
+ $id = $info['id'];
|
|
|
+ } else {
|
|
|
+ $source['pic'] = $row['sku_pic'];
|
|
|
+ $source['price'] = $row['sku_price'];
|
|
|
+ $id = Dever::db('psource/info')->insert($source);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($id) {
|
|
|
+ # 建立规格
|
|
|
+ $row['sku_name'] = '颜色:' . $row['color'] . ' & 尺寸:' . $row['size'];
|
|
|
+ if (isset($row['sku_name']) && $row['sku_name']) {
|
|
|
+ $temp = explode('&', $row['sku_name']);
|
|
|
+ $spec_value_id = [];
|
|
|
+ foreach ($temp as $v) {
|
|
|
+ # 颜色:白色 & 尺寸:35
|
|
|
+ $v = trim($v);
|
|
|
+ list($spec_name, $spec_value) = explode(':', $v);
|
|
|
+
|
|
|
+ $spec_data = ['info_id' => $id, 'name' => $spec_name];
|
|
|
+ $spec = Dever::db('psource/spec')->find($spec_data);
|
|
|
+ if ($spec) {
|
|
|
+ $spec_id = $spec['id'];
|
|
|
+ } else {
|
|
|
+ $spec_data['sort'] = $i;
|
|
|
+ $spec_id = Dever::db('psource/spec')->insert($spec_data);
|
|
|
+ }
|
|
|
+ if ($spec_id) {
|
|
|
+ $spec_data = ['info_id' => $id, 'spec_id' => $spec_id, 'value' => $spec_value];
|
|
|
+ $spec_value = Dever::db('psource/spec_value')->find($spec_data);
|
|
|
+ if ($spec_value) {
|
|
|
+ $spec_value_id[] = $spec_value['id'];
|
|
|
+ } else {
|
|
|
+ $spec_data['sort'] = $i;
|
|
|
+ $spec_value_id[] = Dever::db('psource/spec_value')->insert($spec_data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $update = [];
|
|
|
+ if ($spec_value_id) {
|
|
|
+ $spec_value_id = implode(',', $spec_value_id);
|
|
|
+ $update['spec_type'] = 3;
|
|
|
+ } else {
|
|
|
+ $spec_value_id = -1;
|
|
|
+ $update['spec_type'] = 2;
|
|
|
+ }
|
|
|
+ $row['sku_code'] = $this->createCode($i);
|
|
|
+ $sku_data = ['info_id' => $id, 'key' => $spec_value_id];
|
|
|
+ $sku = Dever::db('psource/sku')->find($sku_data);
|
|
|
+ $sku_data += ['pic' => $row['sku_pic'], 'price' => $row['sku_price'], 'code' => $row['sku_code']];
|
|
|
+ if ($sku) {
|
|
|
+ Dever::db('psource/sku')->update($sku['id'], $sku_data);
|
|
|
+ } else {
|
|
|
+ Dever::db('psource/sku')->insert($sku_data);
|
|
|
+ }
|
|
|
+
|
|
|
+ $sku = Dever::load(\Api\Lib\Sku::class)->getPrice(['info_id' => $id], $update['spec_type'], 'psource');
|
|
|
+ $update['price'] = $sku['price'];
|
|
|
+ $update['sku_id'] = $sku['id'];
|
|
|
+ Dever::db('psource/info')->update($id, $update);
|
|
|
+
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|