Commit af73c5b4 by wuqian

三个导入

parent 422dd729
......@@ -5,7 +5,7 @@
<div class="upload-area" @click="triggerFileInput">
<el-icon class="el-icon--upload"><UploadFilled /></el-icon>
<div class="el-upload__text">
将文件拖到此处,或<span style="color: #409eff">点击上传</span>
<span style="color: #409eff">点击上传</span>
</div>
</div>
<input
......@@ -46,7 +46,14 @@
<script setup lang="ts">
import * as XLSX from 'xlsx'
import { ref, watch, defineProps, defineEmits, computed } from 'vue'
import {
ref,
watch,
defineProps,
defineEmits,
computed,
defineExpose,
} from 'vue'
import { UploadFilled, Document, Close } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { uploadFileApi } from '@/api/common'
......@@ -89,7 +96,7 @@ watch(
const triggerFileInput = () => {
if (loading.value) return
fileInputRef.value?.click()
if (fileInputRef.value) fileInputRef.value?.click()
}
const downloadFile = (path: string) => {
window.open(path)
......@@ -120,8 +127,17 @@ const onFileChange = async (e: Event) => {
ElMessage.error('文件大小不能超过50MB')
return
}
if (props.importType === 'local') {
// 本地解析
if (props.importType === 'localAndUpload') {
// 本地直接获取上传内容
emit('imported', { path: file.name, data: file })
if (fileInputRef.value) {
fileInputRef.value.value = ''
}
loading.value = false
return
}
if (props.importType === 'localAndXlsx') {
// 本地解析,并自行处理xlsx数据
const reader = new FileReader()
reader.onload = (evt) => {
const data = evt.target?.result
......@@ -134,6 +150,7 @@ const onFileChange = async (e: Event) => {
fileList.value = [{ path: file.name, filename: file.name }]
value.value = file.name || ''
ElMessage.success('导入成功')
loading.value = false
}
reader.readAsBinaryString(file)
if (fileInputRef.value) fileInputRef.value.value = ''
......@@ -143,7 +160,18 @@ const onFileChange = async (e: Event) => {
await fileUpload(file)
if (fileInputRef.value) fileInputRef.value.value = ''
}
const resetUpload = () => {
fileList.value = []
value.value = ''
if (fileInputRef.value) {
fileInputRef.value.value = ''
}
}
// 暴露重置方法给父组件
defineExpose({
resetUpload,
})
const fileUpload = async (file: File) => {
const formData = new FormData()
const filename = file.name
......
......@@ -400,10 +400,16 @@
:close-on-click-modal="false"
>
<div class="import-dialog">
<div class="import-header">
<el-button type="primary" link @click="downloadTemplate">
<el-icon><Download /></el-icon>
下载模板
</el-button>
</div>
<div class="import-content">
<UploadExcel
v-model="importedFileUrl"
:import-type="'local'"
:import-type="'localAndXlsx'"
@imported="handleLocalImport"
/>
</div>
......@@ -791,7 +797,7 @@
<script setup lang="ts">
import { ElMessage, ElRadioGroup, ElTree } from 'element-plus'
import { CirclePlusFilled } from '@element-plus/icons-vue'
import { CirclePlusFilled,Download } from '@element-plus/icons-vue'
import splitDiv from '@/components/splitDiv/splitDiv.vue'
import { ElTable } from 'element-plus'
import usePageList from '@/utils/hooks/usePageList'
......@@ -951,6 +957,14 @@ function getStartTime() {
const day = date.getDate()
return `${year}-${month}-${day} 00:00:00`
}
const downloadTemplate = () => {
const link = document.createElement('a')
link.href = '/files/outboundOrder.xlsx'
link.download = 'outboundOrder.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
const selectSku = ref('')
const treeData = ref<InterWarehouseTree[]>()
const [searchForm, resetSearchForm] = useValue<warehouseSearchForm>({})
......@@ -1097,9 +1111,9 @@ const batchAddCommodity = async (sku: string): Promise<InterskuList[]> => {
}
}
interface InterImportData {
warehouseSku: string;
remark?: string | null;
buyStored?: string | number | null | object; // 扩大 buyStored 类型,以兼容原始数据
warehouseSku: string
remark?: string | null
buyStored?: string | number | null | object // 扩大 buyStored 类型,以兼容原始数据
[propName: string]: string | number | boolean | undefined | unknown
}
// 前端导入Excel
......@@ -1122,91 +1136,108 @@ const handleLocalImport = async ({
data: Record<string, any>[]
}) => {
// 1. 将原始导入数据映射到 InterImportData[]
const importedData: InterImportData[] = data.map(item => {
const obj: InterImportData = { warehouseSku: '' };
Object.keys(excelFieldMap).forEach(excelKey => {
const field = excelFieldMap[excelKey];
const value = item[excelKey];
const importedData: InterImportData[] = data
.map((item) => {
const obj: InterImportData = { warehouseSku: '' }
Object.keys(excelFieldMap).forEach((excelKey) => {
const field = excelFieldMap[excelKey]
const value = item[excelKey]
if (field === 'warehouseSku') {
obj[field] = typeof value === 'string' ? value : String(value ?? '');
obj[field] = typeof value === 'string' ? value : String(value ?? '')
} else if (field === 'remark') {
obj[field] = typeof value === 'string' ? value : (value === null || value === undefined ? null : String(value));
obj[field] =
typeof value === 'string'
? value
: value === null || value === undefined
? null
: String(value)
} else if (field === 'buyStored') {
// 处理 buyStored: 确保它是一个数字、数字字符串,否则设置为 null
if (typeof value === 'number') {
obj[field] = String(value); // 将数字转换为字符串
} else if (typeof value === 'string' && !isNaN(Number(value))) {
obj[field] = value; // 保持有效的数字字符串
} else {
// 如果不是数字或有效的数字字符串,则设置为 null
obj[field] = null;
}
// 处理 buyStored: 确保它是一个数字、数字字符串,否则设置为 null
if (typeof value === 'number') {
obj[field] = String(value) // 将数字转换为字符串
} else if (typeof value === 'string' && !isNaN(Number(value))) {
obj[field] = value // 保持有效的数字字符串
} else {
// 如果不是数字或有效的数字字符串,则设置为 null
obj[field] = null
}
} else {
obj[field] = value;
obj[field] = value
}
});
return obj;
}).filter(item => item.warehouseSku);
})
return obj
})
.filter((item) => item.warehouseSku)
if (importedData.length === 0) {
ElMessage.warning('导入数据中没有有效的商品SKU');
importDialogVisible.value = false;
return;
ElMessage.warning('导入数据中没有有效的商品SKU')
importDialogVisible.value = false
return
}
// 2. 提取导入的 SKU 列表
const importedSkus = importedData.map(item => item.warehouseSku).join(',');
const importedSkus = importedData.map((item) => item.warehouseSku).join(',')
// 3. 调用 batchAddCommodity 获取商品的完整信息并过滤掉已有的 SKU
const filteredSkusList = await batchAddCommodity(importedSkus);
const filteredSkusList = await batchAddCommodity(importedSkus)
if (filteredSkusList.length === 0) {
ElMessage.warning('导入的商品SKU已存在或无效');
importedFileUrl.value = path;
importDialogVisible.value = false;
return;
ElMessage.warning('导入的商品SKU已存在或无效')
importedFileUrl.value = path
importDialogVisible.value = false
return
}
// 4. 将备注信息合并到获取到的商品列表中
const mergedProductList = filteredSkusList.map(skuItem => {
const importedItem = importedData.find(item => item.warehouseSku === skuItem.warehouseSku);
let outCountValueForBigNumber: string | number = '0'; // 初始化为安全默认值
const mergedProductList = filteredSkusList.map((skuItem) => {
const importedItem = importedData.find(
(item) => item.warehouseSku === skuItem.warehouseSku,
)
if (importedItem?.buyStored !== null && importedItem?.buyStored !== undefined) {
if (typeof importedItem.buyStored === 'string' && !isNaN(Number(importedItem.buyStored))) {
outCountValueForBigNumber = importedItem.buyStored;
} else if (typeof importedItem.buyStored === 'number') {
outCountValueForBigNumber = importedItem.buyStored;
} else {
// 如果是对象或其他意外类型,则默认为 '0'
outCountValueForBigNumber = '0';
}
let outCountValueForBigNumber: string | number = '0' // 初始化为安全默认值
if (
importedItem?.buyStored !== null &&
importedItem?.buyStored !== undefined
) {
if (
typeof importedItem.buyStored === 'string' &&
!isNaN(Number(importedItem.buyStored))
) {
outCountValueForBigNumber = importedItem.buyStored
} else if (typeof importedItem.buyStored === 'number') {
outCountValueForBigNumber = importedItem.buyStored
} else {
// 如果是对象或其他意外类型,则默认为 '0'
outCountValueForBigNumber = '0'
}
const calculatedOutCount = new BigNumber(outCountValueForBigNumber).toNumber();
}
return {
skuImage: skuItem.image,
warehouseSku: skuItem.warehouseSku,
skuName: skuItem.skuName,
productNo: skuItem.productNumber,
locationCode: skuItem.locationCode ?? '',
locationId: skuItem.locationId ?? null,
costPrice: skuItem.price,
outCount: calculatedOutCount,
totalPrice: new BigNumber(calculatedOutCount).multipliedBy(skuItem.price ?? 0).toNumber(),
usableInventory: skuItem.usableInventory,
inventoryId: skuItem.id,
remark: importedItem?.remark ?? skuItem.remark ?? null,
} as InterProductList;
});
const calculatedOutCount = new BigNumber(
outCountValueForBigNumber,
).toNumber()
return {
skuImage: skuItem.image,
warehouseSku: skuItem.warehouseSku,
skuName: skuItem.skuName,
productNo: skuItem.productNumber,
locationCode: skuItem.locationCode ?? '',
locationId: skuItem.locationId ?? null,
costPrice: skuItem.price,
outCount: calculatedOutCount,
totalPrice: new BigNumber(calculatedOutCount)
.multipliedBy(skuItem.price ?? 0)
.toNumber(),
usableInventory: skuItem.usableInventory,
inventoryId: skuItem.id,
remark: importedItem?.remark ?? skuItem.remark ?? null,
} as InterProductList
})
// 5. 更新 otherPurchaseData
otherPurchaseData.value = [...otherPurchaseData.value, ...mergedProductList];
otherPurchaseData.value = [...otherPurchaseData.value, ...mergedProductList]
importedFileUrl.value = path
importDialogVisible.value = false
......@@ -1974,7 +2005,7 @@ $border: solid 1px #ddd;
}
.import-content {
padding: 20px 0;
padding-bottom: 20px;
}
}
......
......@@ -432,16 +432,16 @@
:close-on-click-modal="false"
>
<div class="import-dialog">
<!-- <div class="import-header">
<div class="import-header">
<el-button type="primary" link @click="downloadTemplate">
<el-icon><Download /></el-icon>
下载模板
</el-button>
</div> -->
</div>
<div class="import-content">
<UploadExcel
v-model="importedFileUrl"
:import-type="'local'"
:import-type="'localAndXlsx'"
@imported="handleLocalImport"
/>
</div>
......@@ -827,7 +827,7 @@
<script setup lang="ts">
import { ElMessage, ElRadioGroup, ElTree } from 'element-plus'
import { CirclePlusFilled } from '@element-plus/icons-vue'
import { CirclePlusFilled,Download } from '@element-plus/icons-vue'
import splitDiv from '@/components/splitDiv/splitDiv.vue'
import { ElTable } from 'element-plus'
import usePageList from '@/utils/hooks/usePageList'
......@@ -986,6 +986,14 @@ function getStartTime() {
const day = date.getDate()
return `${year}-${month}-${day} 00:00:00`
}
const downloadTemplate = () => {
const link = document.createElement('a')
link.href = '/files/warehousingEntry.xlsx'
link.download = 'warehousingEntry.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
const selectSku = ref('')
const treeData = ref<InterWarehouseTree[]>()
const [searchForm, resetSearchForm] = useValue<warehouseSearchForm>({})
......@@ -1936,7 +1944,7 @@ $border: solid 1px #ddd;
}
.import-content {
padding: 20px 0;
padding-bottom: 20px;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment