|
@@ -219,6 +219,92 @@ class FilesManager extends Model
|
|
|
return $sheetList;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从Excel获取订单数据
|
|
|
+ *
|
|
|
+ * @param \Illuminate\Http\UploadedFile $file 传入的文件
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function excelToProduct($file)
|
|
|
+ {
|
|
|
+ // 获取文件后缀名
|
|
|
+ $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
|
|
|
+ // 验证文件格式
|
|
|
+ if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
|
|
|
+ // 读取文件
|
|
|
+ $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
|
|
|
+ // 提取数据
|
|
|
+ $sheetList = $reader->getActiveSheet()->toArray('');
|
|
|
+ // 表格列标题
|
|
|
+ $column = array_shift($sheetList);
|
|
|
+ // 列标题换字段
|
|
|
+ $column = $this->columnToProduct($column);
|
|
|
+ // 字段值
|
|
|
+ if( !$column ) return ['error' => '请检查内容表头格式'];
|
|
|
+ // 循环表格数据
|
|
|
+ foreach ($sheetList as $row=>$value) {
|
|
|
+ // 获取对应的数据
|
|
|
+ $order = [];
|
|
|
+ // 循环每个字段,获取值
|
|
|
+ foreach ($value as $kk => $vv) {
|
|
|
+ // 根据字段索引获取对应的值,转存到订单
|
|
|
+ if( isset($column[$kk]) ) $order[$column[$kk]] = $vv;
|
|
|
+ }
|
|
|
+ // 如果存在的话
|
|
|
+ if( $order['grade'] ) {
|
|
|
+ // 转数组
|
|
|
+ $order['grade'] = explode(',',$order['grade']);
|
|
|
+ // 适读
|
|
|
+ $order['grade'] = empty($order['grade']) ? '' : '【适读'. $order['grade'][0].'-'.$order['grade'][count($order['grade'])-1].'年级】';
|
|
|
+ }
|
|
|
+ // 分类ID
|
|
|
+ $value['category_id'] = 1;
|
|
|
+ // 时间转换
|
|
|
+ $order['insert_time'] = $order['insert_time'] ? strtotime($order['insert_time']) : 0;
|
|
|
+ $order['update_time'] = $order['update_time'] ? strtotime($order['update_time']) : 0;
|
|
|
+ // 库存默认
|
|
|
+ $order['stock'] = 99999;
|
|
|
+ // 适读
|
|
|
+ $order['name'] = $order['grade'];
|
|
|
+ // 描述
|
|
|
+ $order['description'] = str_ireplace('http://img.duibaoduikan.com/','https://edu.dfwy.tech/',$order['description']);
|
|
|
+ // 删除数据
|
|
|
+ unset($order['grade']);
|
|
|
+ // 追加到订单列表
|
|
|
+ $sheetList[$row] = $order;
|
|
|
+ }
|
|
|
+ // 返回结果
|
|
|
+ return $sheetList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取列对应的数据库字段名
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private function columnToProduct($column)
|
|
|
+ {
|
|
|
+ // 字段值
|
|
|
+ $field = [];
|
|
|
+ // 循环列标题
|
|
|
+ foreach ($column as $key => $value) {
|
|
|
+
|
|
|
+ if( $value == 'goods_name') $field[$key] = 'name';
|
|
|
+ if( $value == 'category_id') $field[$key] = 'type_id';
|
|
|
+ if( $value == 'goods_main_img')$field[$key] = 'thumb';
|
|
|
+ if( $value == 'goods_specs') $field[$key] = 'spec';
|
|
|
+ if( $value == 'goods_price') $field[$key] = 'price';
|
|
|
+ if( $value == 'fit_grade' ) $field[$key] = 'grade';
|
|
|
+ if( $value == 'magazine_info') $field[$key] = 'description';
|
|
|
+ if( $value == 'goods_sort') $field[$key] = 'sort';
|
|
|
+ if( $value == 'status') $field[$key] = 'status';
|
|
|
+ if( $value == 'create_time') $field[$key] = 'insert_time';
|
|
|
+ if( $value == 'update_time') $field[$key] = 'update_time';
|
|
|
+ }
|
|
|
+ // 返回字段值
|
|
|
+ return $field;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取列对应的数据库字段名
|
|
|
*
|