You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

711 lines
32 KiB

5 months ago
<template>
<div class="container">
<!-- 产品新增 -->
<basic-crumb></basic-crumb>
<div class="title">产品新增</div>
<div>
<div class="btn_box">
<div>
<el-button type="primary" @click="addTable">插入一行</el-button>
<!-- <el-button type="danger" @click="delTable">批量删除</el-button> -->
<el-button type="primary" @click="handleMatch">一键匹配</el-button>
</div>
<div>
<el-button type="primary" @click="saveProduct">保存产品</el-button>
</div>
</div>
5 months ago
<el-table :data="tableData" ref="multipleTable" style="width: 100%;" height="570" @row-click="rowClick" border @selection-change="selectionChangeProject" @select="selectChange">
5 months ago
<!-- type="selection" -->
<el-table-column align="center" width="55px" >
<template slot-scope="scope">
<div @click="deleteColumn(scope.row, scope.$index)"
style="width:30px;height: 30px;background: #f56c6c;border-radius: 50%;cursor: pointer;display: flex;align-items: center;justify-content: center;">
<i style="color:#fff;font-size: 20px;" class="el-icon-delete"></i>
</div>
</template>
</el-table-column>
<el-table-column label="#" type="index" width="50" align="center"></el-table-column>
<el-table-column prop="code" label="编号" align="center" width="150">
<!-- <template #header>
<span><i style="color:red">*</i>物料编号</span>
</template> -->
<template #default="scope">
<el-input v-model="scope.row.code" :placeholder="!scope.row.code && scope.row.nameCn && showError ? '请输入编号' : ''" :class="{ 'is-error': !scope.row.code && scope.row.nameCn && showError }" :disabled="scope.row.bizType == 2" />
</template>
</el-table-column>
<el-table-column prop="nameCn" label="产品名称" align="center">
<template #default="scope">
<el-input v-model="scope.row.nameCn" :placeholder="!scope.row.nameCn && scope.row.nameCn && showError ? '请输入产品名称' : ''" :class="{ 'is-error': !scope.row.nameCn && scope.row.code && showError }" :disabled="scope.row.bizType == 2" />
</template>
</el-table-column>
<el-table-column prop="specCn" label="规格说明" align="center">
<template #default="scope">
<el-input type="textarea" v-model="scope.row.specCn" :disabled="scope.row.bizType == 2" />
</template>
</el-table-column>
<el-table-column prop="unitCn" label="单位" align="center" width="80">
<template #default="scope">
<el-input v-model="scope.row.unitCn" :disabled="scope.row.bizType == 2" />
</template>
</el-table-column>
<el-table-column prop="multiple" label="倍率" align="center" width="80">
<template #default="scope">
<!-- <el-input-number style="width:90%" v-model="scope.row.multiple" controls-position="right" @change="handleChange"
:min="1"></el-input-number> -->
<el-input v-model="scope.row.multiple" @input="handleScale(scope.row.multiple, scope.$index)" :disabled="scope.row.bizType == 2"></el-input>
</template>
</el-table-column>
<el-table-column prop="standardPrice" label="公司指导价" align="center" width="140">
<template #default="scope">
<el-input v-model="scope.row.standardPrice" v-if="scope.row.bizType == 1 || !scope.row.bizType" @input="handlePrice(scope.row.standardPrice, scope.$index)" @focus="clickPrice(scope.row.standardPrice, scope.$index)" @blur="blurPrice(scope.row.standardPrice, scope.$index)"></el-input>
<el-input v-model="scope.row.bizPrice" v-if="scope.row.bizType == 2" @input="handlePrice(scope.row.standardPrice, scope.$index)" @focus="clickBizPrice(scope.row.standardPrice, scope.$index)" @blur="blurBizPrice(scope.row.standardPrice, scope.$index)"></el-input>
<!-- <el-input-number style="width:90%" v-if="scope.row.bizType == 1 || !scope.row.bizType" v-model="scope.row.standardPrice" controls-position="right" @change="handleChange"
:min="1"></el-input-number>
<el-input-number style="width:90%" v-if="scope.row.bizType == 2" v-model="scope.row.bizPrice" controls-position="right" @change="handleChange"
:min="1"></el-input-number> -->
</template>
</el-table-column>
<el-table-column prop="remarkCn" label="备注" align="center">
<template #default="scope">
<el-input v-if="scope.row.bizType == 1 || !scope.row.bizType" type="textarea" v-model="scope.row.remarkCn" :disabled="scope.row.bizType == 2"></el-input>
<el-input v-if="scope.row.bizType == 2" type="textarea" v-model="scope.row.bizRemark" :disabled="scope.row.bizType == 2"></el-input>
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" align="center" width="180">
<template slot-scope="scope">
<el-button type="text" size="small" v-if="scope.row.bizType !=2" @click="editProduct(scope.row)">编辑</el-button>
<el-button type="text" size="small" @click="changePeople(scope.row,scope.$index)">{{scope.row.bizType == 2 ? scope.row.saleUserRealName : '指定业务员'}}</el-button>
</template>
</el-table-column>
</el-table>
</div>
<el-dialog
append-to-body
title="指定业务员"
:visible.sync="addDialog"
width="550px"
>
<el-form
ref="addForm"
:model="addForm"
label-width="120px"
:rules="formRules"
>
<el-form-item label="业务员" prop="saleUserId">
<el-select v-model="addForm.saleUserId" placeholder="请选择业务员" @change="changeSale">
<el-option v-for="item in saleData" :key="item.id" :value="item.id" :label="item.realName"></el-option>
</el-select>
</el-form-item>
<el-form-item label="客户" prop="customerId">
<el-select v-model="addForm.customerId" placeholder="请选择客户">
<el-option v-for="item in customerData" :key="item.id" :value="item.id" :label="item.companyName"></el-option>
</el-select>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="addDialog = false"> </el-button>
<el-button type="primary" @click="submit"> </el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
import {getProduct,changeProduct,deleteProduct,getSaleList,getCustomer,changeUser,matchData,deletePrice} from '@/api/product/product'
import basicCrumb from "@/components/basic-crumb/main"
import { mapGetters } from "vuex";
export default {
components:{
basicCrumb,
},
data(){
return{
tableData:[],
selectionProjectList:[],
subcategoryId:'',
addDialog:false,
addForm:{},
formRules:{
saleUserId:[{required:true,message:'请选择业务员',trigger:'blur'}],
customerId:[{required:true,message:'请选择客户',trigger:'blur'}],
},
saleData:[],
customerData:[],
originData:[],
pickData:[],
showError:false
}
},
computed: {
...mapGetters(["routeData"])
},
mounted(){
let tmp = this.routeData.find(item => item.url == this.$route.path)
let arr = this.routeData.filter(item => item.url != this.$route.path)
this.$store.commit('SET_ROUTE_DATA',[...arr])
this.getProductList(this.$route.query.subcategoryId ? this.$route.query.subcategoryId : tmp.catalogId)
this.getSaleData()
// this.getCustomerData()
},
methods:{
handleScale(value,index){
// 只允许数字和小数点(如果你想限制小数点后的位数,可以更复杂地处理)
// // 移除非数字字符,只允许数字输入
console.log("value.replace(/[^\d]/g, '')=====================",value.replace(/[^\d]/g, '') <= 0 ? 1 : value.replace(/[^\d]/g, ''))
// : value.replace(/[^\d]/g, '') > 100 ? 100
this.tableData[index].multiple = value.replace(/[^\d]/g, '') <= 0 ? 1 : value.replace(/[^\d]/g, '');
},
handlePrice(value,index){
// 允许空、0、数字、小数点,但限制格式
let cleaned = value.replace(/[^\d.]/g, ''); // 只保留数字和点
// 防止多个小数点
const parts = cleaned.split('.');
if (parts.length > 2) {
cleaned = parts[0] + '.' + parts.slice(1).join('');
}
// 限制小数部分最多2位
if (cleaned.includes('.')) {
const decimalPart = cleaned.split('.')[1];
if (decimalPart.length > 2) {
cleaned = cleaned.slice(0, cleaned.indexOf('.') + 3); // 保留 . + 2 位
}
}
// 禁止以 . 开头(如 .5 → 自动补 0.5)
if (cleaned.startsWith('.')) {
cleaned = '0' + cleaned;
}
// 禁止 0 后跟非小数的数字(如 0123 → 改为 0.123?通常应清掉前导零)
// 这里简化处理:允许 0.x,但不允许 0123
if (/^0\d+$/.test(cleaned)) {
// 如果是 0 开头且后面有整数(无小数点),则只保留第一个 0
cleaned = '0';
}
this.tableData[index].standardPrice = cleaned;
},
clickPrice(value,index){
if(value != 0){
this.tableData[index].standardPrice = this.tableData[index].standardPrice + ''
this.tableData[index].standardPrice = this.tableData[index].standardPrice.replace(/,/g, '');
}
},
blurPrice(value,index){
if(value != 0){
this.tableData[index].standardPrice = this.tableData[index].standardPrice + ''
this.tableData[index].standardPrice = this.tableData[index].standardPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
},
clickBizPrice(value,index){
if(value != 0){
this.tableData[index].bizPrice = this.tableData[index].bizPrice + ''
this.tableData[index].bizPrice = this.tableData[index].bizPrice.replace(/,/g, '');
}
},
blurBizPrice(value,index){
if(value != 0){
this.tableData[index].bizPrice = this.tableData[index].bizPrice + ''
this.tableData[index].bizPrice = this.tableData[index].bizPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
},
toggleColumn(){
this.$refs.multipleTable.toggleRowSelection(row);
},
changeSale(val){
5 months ago
this.addForm = {
...this.addForm,
customerId:''
}
5 months ago
getCustomer({createUserId:val}).then(res =>{
this.customerData = res.data.data
})
},
getSaleData(){
getSaleList().then(res =>{
this.saleData = res.data.data
if(this.saleData.length != 0){
getCustomer({createUserId:this.saleData[0].id}).then(res =>{
this.customerData = res.data.data
})
}
})
},
getCustomerData(){
getCustomer().then(res =>{
this.customerData = res.data.data
})
},
getProductList(id){
this.subcategoryId = id
getProduct({
catalogId:id,
size:9999,
current:1
}).then(res =>{
this.tableData = res.data.data.records
this.tableData.map(item =>{
if(item.bizType == 2){
item.bizPrice = item.bizPrice + ''
item.bizPrice = item.bizPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}else{
item.standardPrice = item.standardPrice + ''
item.standardPrice = item.standardPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
})
if(this.tableData.length < 10){
const needValue = 10 - this.tableData.length
for(let i = 0;i < needValue;i++)
this.tableData.push({multiple:1})
}
this.originData = JSON.parse(JSON.stringify(res.data.data.records))
})
},
// 插入一行
addTable() {
// 新增一行
const record = { _select:false,multiple:1 };
this.tableData.push(record)
this.$nextTick(() => {
this.$refs.multipleTable.bodyWrapper.scrollTop = this.$refs.multipleTable.bodyWrapper.scrollHeight;
})
},
selectionChangeProject(list){
this.selectionProjectList = list
},
selectChange(list,val){
val._select = !val._select
},
delTable(){
let arr = this.tableData.filter(item => item._select)
if(arr.length != 0){
this.$confirm('确定将选择数据删除?', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}).then(() =>{
let deleteArr = this.tableData.filter(item => !item._select)
let delNoIdArr = deleteArr.filter(item => !item.id)
let noIdArr = arr.filter(item => !item.id)
let idArr = arr.filter(item => item.id && item.id != '')
if(idArr.length != 0){
5 months ago
deleteProduct({ids:idArr.map(item => item.id).join(','),catalogId:this.subcategoryId}).then(res =>{
5 months ago
if(res.data.code == 200){
this.$message.success('删除成功')
getProduct({current:1,size:9999,catalogId:this.subcategoryId}).then(result =>{
this.tableData = [...result.data.data.records,...deleteArr]
})
}
})
}else{
this.tableData = deleteArr
}
})
}else{
this.$message.error('请至少选择一条数据进行操作!')
}
},
deleteColumn(row,index){
this.$confirm('确定将选择数据删除?', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}).then(() =>{
if(row.id){
console.log('row------------',row)
if(row.bizType == 2){
deletePrice({ids:row.bizPriceId}).then(res =>{
if(res.data.code == 200){
this.$message.success('删除成功')
getProduct({current:1,size:9999,catalogId:this.subcategoryId}).then(result =>{
// this.tableData = [...result.data.data.records,...deleteArr]
this.tableData.splice(index, 1);
})
}
})
}else{
5 months ago
deleteProduct({ids:row.id,catalogId:this.subcategoryId}).then(res =>{
5 months ago
if(res.data.code == 200){
this.$message.success('删除成功')
getProduct({current:1,size:9999,catalogId:this.subcategoryId}).then(result =>{
// this.tableData = [...result.data.data.records,...deleteArr]
this.tableData.splice(index, 1);
})
}
})
}
}else{
this.$message.success('删除成功')
this.tableData.splice(index, 1);
}
})
},
saveProduct(){
const invalidRows = this.tableData.filter(row => {
return (!row.nameCn && row.code) || (!row.code && row.nameCn);
});
if (invalidRows.length > 0) {
this.showError = true
this.$message.error(`${invalidRows.length} 行数据未填写完整!`);
return;
}
let arr = this.tableData.filter(item => item && (item.nameCn && item.nameCn != '') || (item.code && item.code != ''))
arr.map(item =>{
item.catalogId = this.subcategoryId,
item.bizPrice = item.bizPrice && item.bizPrice != 0 ? (item.bizPrice + '').replace(/,/g, '') : ''
item.standardPrice = item.standardPrice && item.standardPrice != 0 ? (item.standardPrice + '').replace(/,/g, '') : ''
})
console.log('tableData-------------------',)
changeProduct(arr).then(res =>{
if(res.data.code == 200){
this.$message.success('保存成功')
this.getProductList(this.subcategoryId)
}
})
},
editProduct(row){
// if(!row.id){
// this.$message.error('请先保存产品!')
// return
if(row.nameCn && !row.code){
this.$message.error('请填写产品编号!')
return
}
if(row.code && !row.nameCn){
this.$message.error('请填写产品名称!')
return
}
// }
// if(!row.id){
let arr = this.tableData.filter(item => item && (item.nameCn && item.nameCn != '') || (item.code && item.code != ''))
arr.map(item =>{
item.catalogId = this.subcategoryId
item.bizPrice = item.bizPrice && item.bizPrice != 0 ? (item.bizPrice + '').replace(/,/g, '') : ''
item.standardPrice = item.standardPrice && item.standardPrice != 0 ? (item.standardPrice + '').replace(/,/g, '') : ''
})
changeProduct(arr).then(res =>{
if(res.data.code == 200){
// this.$message.success('保存成功')
// this.getProductList(this.subcategoryId)
getProduct({
catalogId:this.subcategoryId,
size:9999,
current:1
}).then(res =>{
this.tableData = res.data.data.records
this.tableData.map(item =>{
if(item.bizType == 2){
item.bizPrice = item.bizPrice + ''
item.bizPrice = item.bizPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}else{
item.standardPrice = item.standardPrice + ''
item.standardPrice = item.standardPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
})
if(this.tableData.length < 10){
const needValue = 10 - this.tableData.length
for(let i = 0;i < needValue;i++)
this.tableData.push({multiple:1})
}
this.originData = JSON.parse(JSON.stringify(res.data.data.records))
let tmp = this.tableData.find(item => item.nameCn == row.nameCn)
this.$router.push({ path: '/edit',query:{
name:row.nameCn,
code:row.code,
id:tmp.id,
subcategoryId:this.subcategoryId
}});
let arr = []
arr.push({
name:row.nameCn,
url:'/add',
catalogId:this.subcategoryId,
query:{
subcategoryId:this.subcategoryId
}
})
this.$store.commit('SET_ROUTE_DATA',[...this.routeData,...arr])
})
}
})
// }else{
// this.$router.push({ path: '/edit',query:{
// name:row.nameCn,
// code:row.code,
// id:row.id,
// subcategoryId:this.subcategoryId
// }});
// let arr = []
// arr.push({
// name:row.nameCn,
// url:'/add',
// catalogId:this.subcategoryId,
// query:{
// subcategoryId:this.subcategoryId
// }
// })
// this.$store.commit('SET_ROUTE_DATA',[...this.routeData,...arr])
// }
},
// 指定业务员
changePeople(row,index){
if(!row.id){
if(row.nameCn && !row.code){
this.$message.error('请填写产品编号!')
return
}
if(row.code && !row.nameCn){
this.$message.error('请填写产品名称!')
return
}
let arr = this.tableData.filter(item => item && (item.nameCn && item.nameCn != '') || (item.code && item.code != ''))
arr.map(item =>{
item.catalogId = this.subcategoryId
item.bizPrice = item.bizPrice && item.bizPrice != 0 ? (item.bizPrice + '').replace(/,/g, '') : ''
item.standardPrice = item.standardPrice && item.standardPrice != 0 ? (item.standardPrice + '').replace(/,/g, '') : ''
})
changeProduct(arr).then(res =>{
if(res.data.code == 200){
getProduct({
catalogId:this.subcategoryId,
size:9999,
current:1
}).then(res =>{
this.tableData = res.data.data.records
this.tableData.map(item =>{
if(item.bizType == 2){
item.bizPrice = item.bizPrice + ''
item.bizPrice = item.bizPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}else{
item.standardPrice = item.standardPrice + ''
item.standardPrice = item.standardPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
})
if(this.tableData.length < 10){
const needValue = 10 - this.tableData.length
for(let i = 0;i < needValue;i++)
this.tableData.push({multiple:1})
}
this.originData = JSON.parse(JSON.stringify(res.data.data.records))
this.addForm.productId = this.tableData[index].id
this.addForm.bizPrice = this.tableData[index].standardPrice && this.tableData[index].standardPrice != 0 ? this.tableData[index].standardPrice.replace(/,/g, '') : this.tableData[index].standardPrice
this.addForm = {
...this.addForm,
saleUserId:this.saleData.length != 0 ? this.saleData[0].id : '',
customerId:this.customerData != 0 ? this.customerData[0].id : ''
}
5 months ago
getCustomer({createUserId:this.addForm.saleUserId}).then(res =>{
this.customerData = res.data.data
this.addDialog = true
})
5 months ago
})
}
})
}else{
5 months ago
this.addForm.bizPrice = row.standardPrice && row.standardPrice != 0 ? (row.standardPrice + '').replace(/,/g, '') : row.standardPrice
5 months ago
if(row.bizType == 2){
this.addForm = {
...this.addForm,
saleUserId:row.saleUserId,
customerId:row.customerId,
5 months ago
productId:row.id,
5 months ago
id:row.bizPriceId,
bizPrice:row.bizPrice && row.bizPrice != 0 ? row.bizPrice.replace(/,/g, '') : row.bizPrice
}
}else{
this.addForm = {
...this.addForm,
saleUserId:this.saleData.length != 0 ? this.saleData[0].id : '',
5 months ago
customerId:this.customerData != 0 ? this.customerData[0].id : '',
productId:row.id,
5 months ago
}
}
5 months ago
getCustomer({createUserId:this.addForm.saleUserId}).then(res =>{
this.customerData = res.data.data
this.addDialog = true
})
5 months ago
}
},
submit(){
this.$refs.addForm.validate(valid =>{
console.log('add-----------------',this.addForm)
if(valid){
changeUser(this.addForm).then(res =>{
if(res.data.code == 200){
this.$message.success('设置成功')
this.getProductList(this.subcategoryId)
this.addDialog = false
}
})
}
})
},
// 一键匹配
handleMatch(){
let addData = this.tableData.filter(item => !item.id)
// 获取 arr1 中 name 不在 arr2 中的对象
const onlyInArr1 = this.originData.filter(item1 =>
!this.tableData.some(item2 => item1.id == item2.id && item2.nameCn === item1.nameCn)
);
// 获取 arr2 中 name 不在 arr1 中的对象
const onlyInArr2 = this.tableData.filter(item2 =>
!this.originData.some(item1 => item1.id == item2.id && item2.nameCn === item1.nameCn)
);
// 合并结果(可选)
const diffByName = [...onlyInArr1, ...onlyInArr2];
console.log('diffByName---------------',diffByName)
matchData(diffByName).then(res =>{
console.log('res ==============',res)
if(res.data.msg != '操作成功'){
this.$message.error(res.data.msg)
return
}
this.$message.success('匹配成功')
// if(this.pickData.length != 0){
// console.log('this.pickData===================',this.pickData)
// let newArr = this.mergeArraysByIdOrName(this.pickData,res.data.data)
// console.log('new------------',newArr)
// }else{
let arr = JSON.parse(JSON.stringify(this.tableData))
let idArr = arr.filter(item => item.id && item.id != '')
this.tableData = [...idArr,...res.data.data]
this.tableData.map(item =>{
if(item.bizType == 2){
item.bizPrice = item.bizPrice + ''
item.bizPrice = item.bizPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}else{
item.standardPrice = item.standardPrice + ''
item.standardPrice = item.standardPrice.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
})
this.pickData = this.tableData
// }
if(this.tableData.length < 10){
const needValue = 10 - this.tableData.length
for(let i = 0;i<needValue;i++){
this.tableData.push({multiple:1})
}
}
console.log('tab------------',this.tableData)
})
},
mergeArraysByIdOrName(A, B) {
// 创建 A 的索引映射,便于快速查找
const idMap = new Map(); // id -> 对象(仅当 id 存在时)
const nameMap = new Map(); // name -> 对象(所有对象都建 name 索引)
for (const item of A) {
if (item.id != null) {
idMap.set(item.id, item);
}
if (item.nameCn != null) {
nameMap.set(item.nameCn, item);
}
}
// 遍历 B,尝试覆盖 A 中的对象
for (const bItem of B) {
let targetInA = null;
if (bItem.id != null) {
// 优先按 id 匹配
const candidate = idMap.get(bItem.id);
if (candidate && candidate.nameCn === bItem.nameCn) {
targetInA = candidate;
}
} else if (bItem.nameCn != null) {
// 没有 id 时,按 name 匹配
targetInA = nameMap.get(bItem.nameCn);
}
// 如果找到匹配项,则覆盖属性(保留原引用)
if (targetInA) {
Object.assign(targetInA, bItem);
}else{
A.push(bItem)
}
}
return A; // A 被原地修改,也可返回
}
}
}
</script>
<style scoped lang="scss">
.container {
width: 99%;
5 months ago
// height: 99%;
5 months ago
background: rgb(255, 255, 255);
margin: 0 auto;
margin-top: 8px;
padding: 20px !important;
.title {
// font-size: 30px;
font-size: 1.5vw;
margin-top: 20px;
width: 100%;
text-align: center;
}
.btn_box{
margin-bottom: 12px;
margin-top:12px;
display: flex;
justify-content: space-between;
}
/* 手动添加红色边框样式 */
.is-error{
background: red;
::v-deep .el-input__inner {
box-shadow: 0 0 0 1px #f56c6c inset !important;
}
}
.is-error ::v-deep input::placeholder {
color: #ff6b6b;
opacity: 1;
}
// ::v-deep .el-table{
// font-size: 24px;
// }
// ::v-deep .el-input__inner{
// font-size: 24px;
// }
// ::v-deep .el-button{
// font-size: 24px;
// }
}
</style>