import { sjTime, isNumber } from '@/utils/sharedJsFile' import { getCookie } from '@/utils/util.js' export default { data() { return { tableData: [], shipperName: '四川省达州钢铁有限责任公司', //销售订单主表数据 saleOrderList: [ 'receiveName', 'isSelfMention', 'saleRemark', 'saleOrderReceiveCustomer', 'salerName', 'saleOrderNo', 'saleArea' ], saleOrderMapList: [ 'cxh', 'province', 'district', 'town', 'truckRemark', 'place', 'saleOrderConsigneeTel', 'saleDateOfReceipt', 'materialName', 'materialSpecification', 'materialModel', 'orderPlanWeight', 'meterNumber', 'isPoundSale', 'inboundWarehouse' ] } }, methods: { importExcel(file) { let that = this if (!file) { that.$message({ message: '文件错误!', type: 'warning' }) return } this.tableData = [] var reader = new FileReader() var data = null var workbook = null //设置读取操作 reader.onload = function(e) { data = e.target.result workbook = XLSX.read(data, { type: 'binary' }) let rows = XLSX.utils.sheet_to_json( workbook.Sheets[workbook.SheetNames[0]] ) //只取第一页 console.log(rows, 'rows') let uuid = Math.floor(Math.random() * 60 * 24) Array.from(rows).forEach((e, index) => { let map = {} try { Object.keys(e).forEach(e1 => { console.log(e1, 'e1') if (e1.includes('转运计划编号')) { map.saleOrderNo = e[e1] } if (e1.includes('物资名称')) { map.materialName = e[e1] if ( e[e1].includes('盘螺') || e[e1].includes('乱尺') || e[e1].includes('盘元') || e[e1].includes('盘圆') ) { map.isPoundSale = 0 } else { map.isPoundSale = 1 } //确定更改的物资有无米数 if (e[e1].includes('米')) { map.meterNumber = Number(e[e1].replace(/[^0.0-9.0]/gi, '')) } else { map.meterNumber = null } } if (e1.includes('规格型号')) { // console.log(e[e1]) let str = e[e1] let regex = /\((.+?)\)/g if (str.match(regex) != null) { let spa = str.replace(str.match(regex)[0], '') let model = str .match(regex)[0] .replace('(', '') .replace(')', '') map.materialModel = model if ((spa + '').includes('Φ')) { map.materialSpecification = spa } else { map.materialSpecification = 'Φ' + spa } } else { map.materialSpecification = e[e1] map.model = null } } if (e1.includes('下单客户')) { map.receiveName = e[e1] } if ( e1.includes('米数') && (map.meterNumber == null || map.meterNumber != '') ) { map.meterNumber = e[e1] } if (e1.includes('物资规格') && !e1.includes('规格型号')) { if ((e[e1] + '').includes('Φ')) { map.materialSpecification = e[e1] } else { map.materialSpecification = 'Φ' + e[e1] } } if (e1.includes('物资型号') && !e1.includes('规格型号')) { map.materialModel = e[e1] } if (e1.includes('物资件数')) { if (isNumber(e[e1])) { map.orderPlanWeight = e[e1] } else { throw new Error( `该Excel第--${index + 2}--行-“物资件数”错误,请核实后再输入(件数输入整数)` ) } } if (e1.includes('车序号')) { if (typeof e[e1] != 'undefined' && isNumber(e[e1])) { map.cxh = e[e1] } else { throw new Error( `该Excel第--${index + 2}--行-“车序号”错误,请核实后再输入(车序号输入正整数)` ) } } // if (e1.includes('收款公司')) { // if (that.receivingCompany.indexOf(e[e1]) === -1) { // throw new Error( // `该Excel第--${index + 2}--行-“收款公司”错误,请核实后再输入` // ) // } else { // map.saleOrderReceiveCustomer = e[e1] // } // } if (e1.includes('业务员')) { map.salerName = e[e1] } if (e1.includes('摘要')) { map.saleRemark = e[e1] if (typeof map.saleRemark === 'string') { // 首先中文逗号转英文逗号 map.saleRemark = map.saleRemark.replace(',', ',') let reg = /钢材.*组/g let reg1 = /直发.*(,|\s){0,1}/ let saleAreaList = map.saleRemark.match(reg) let directlyList = map.saleRemark.match(reg1) if (saleAreaList != null && saleAreaList.length > 0) { map.saleRemark = saleAreaList[0] } else { throw new Error( `该Excel第--${index + 2}--行-“摘要填写”错误,请核实后再输入(应为“钢材某某组”)` ) } if (directlyList != null && directlyList.length > 0) { map.saleRemark = map.saleRemark + ',' + directlyList[0] } } } if (e1.includes('省')) { map.province = e[e1] } if (e1.includes('市')) { map.district = e[e1] } if (e1.includes('县/区')) { map.town = e[e1] } if (e1.includes('具体收货地址')) { map.place = e[e1] } if (e1.includes('收货客户电话')) { map.saleOrderConsigneeTel = e[e1] } if (e1.includes('备注')) { map.truckRemark = e[e1] } if (e1.includes('出库库房')) { map.inboundWarehouse = e[e1] } if (e1.includes('是否自提')) { if (e[e1] === '是' || e[e1] === '否') { map.isSelfMention = e[e1] } else { map.isSelfMention = '否' } } }) map.shipperName = that.shipperName if (typeof map.isSelfMention === 'undefined') { map.isSelfMention = '否' } if (typeof map.cxh === 'undefined') { map.cxh = Number( `${Math.floor( Math.random() * Math.random() * Math.random() * Math.random() * Math.random() * new Date().getTime() * 10000000 )}${uuid}` ) } if (map.saleRemark && map.receiveName && map.materialName) { that.tableData.push(map) } } catch (es) { that.$message.error(es.message) } }) that.addExcelInport() return } reader.readAsBinaryString(file.raw) //以二进制方式读取 }, addExcelInport() { //加载虚拟 const loading = this.$loading({ lock: true, text: '正在快马加鞭为您生成销售订单,请稍候', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)' }) //遍历数组,根据摘要和下单客户自动给出销售订单编号 for (let i = 0; i < this.tableData.length; i++) { if (i === 0) { this.tableData[i].saleOrderNo = 1 } else { let sortArr = this.tableData .filter(e => { return ( typeof e.saleOrderNo != 'undefined' && e.saleOrderNo != 'undefined' && isNumber(e.saleOrderNo) ) }) .map(e => { if ( typeof e.saleOrderNo != 'undefined' && e.saleOrderNo != 'undefined' && isNumber(e.saleOrderNo) ) { return e.saleOrderNo } }) .sort((a, b) => { return a - b }) //当大于0的时候,需要遍历整个数组,与之相等的有哪些 for (let j = 0; j < i; j++) { if ( this.tableData[i].saleRemark === this.tableData[j].saleRemark && this.tableData[i].receiveName === this.tableData[j].receiveName && this.tableData[i].inboundWarehouse === this.tableData[j].inboundWarehouse ) { this.tableData[i].saleOrderNo = this.tableData[j].saleOrderNo break } else { if (j == i - 1) { this.tableData[i].saleOrderNo = sortArr[sortArr.length - 1] + 1 } } } } } // this.tableData = this.tableData.sort(this.compare("saleOrderNo")); // debugger; //需要遍历数组,以销售订单编号为分类 const map1 = this.tableData.reduce((result, item) => { result[item.saleOrderNo] = result[item.saleOrderNo] || [] result[item.saleOrderNo].push(item) return result }, {}) // result即为所求 let result = Object.values(map1) console.log(result, 'resilt') //遍历数组 let saleOrderListMap = [] result.forEach((saleOrderList, index) => { let map = {} let arr = [] saleOrderList.forEach(e => { let mapMaterial = {} Object.keys(e).forEach(e1 => { if (this.saleOrderList.includes(e1)) { map[e1] = e[e1] } if (this.saleOrderMapList.includes(e1)) { mapMaterial[e1] = e[e1] } }) arr.push(mapMaterial) }) map.shipperId = 1 map.userName = getCookie('loginName') map.mapList = arr saleOrderListMap.push(map) }) if (saleOrderListMap.length > 0) { console.log(saleOrderListMap, 'saleOrderListMap') this.axios .post('/api/v1/ams/addTransPlanList', saleOrderListMap) .then(res => { if (res.data.code == '200') { this.getRequestUrl() this.tableData = [] loading.close() this.$message.success('新增转运计划成功!') } else { console.log(res.data.data) this.$message.error(res.data.data) this.getRequestUrl() loading.close() this.tableData = [] } }) .catch(e => { loading.close() this.$message.error(e) this.tableData = [] }) } else { this.$message({ type: 'error', duration: 3500, offset: 250, message: '该Excel系统未识别到订单,请仔细检查格式。(销售订单必须在第一个sheet,且必须遵循导入模板!)' }) this.tableData = [] loading.close() } } } }