工作遇到的问题,顺手记录下
背景:插入和修改数据的其中两个字段作为关联主键不能重复(就是这两个字段不能同时与其他数据中的这两个字段一样,单个重复是没问题的)
核心思路:遍历查找是否重复(小白想问问有时间复杂度更低的写法摸)
//数据查重问题,console打印查重对象的数据,若数据未重复则为undefined
letisNameDuplicate=this.InformationList.find(item=>(item.enterpriseName==this.form.enterpriseName||item.creditCode==this.form.creditCode)&&item.tInformationId!=this.form.tInformationId);
console.log(isNameDuplicate);
一、Controller新增
@PreAuthorize("@ss.hasPermi('system:IuCooperation:list')")
@GetMapping("/listAll")
public TableDataInfo listAll()
{
startPage();
TIuCooperationInfo tIuCooperationInfo = new TIuCooperationInfo();
List<TIuCooperationInfo> list = tIuCooperationInfoService.selectTIuCooperationInfoList(tIuCooperationInfo);
return getDataTable(list);
}
二、js新增
export function listIuCooperationAll() {
return request({
url: '/system/IuCooperation/listAll',
method: 'get',
})
}
三、vue下的data新增
IuCooperationListAll: [],
四、vue下的提交按钮方法改写
submitForm() {
this.$refs["form"].validate(valid => {
if (valid) {
this.loading = true;
listIuCooperationAll().then(response =>{
this.IuCooperationListAll=response.rows;
this.loading = false;
let isNameDuplicate = this.IuCooperationListAll.find(item => (item.professionalName == this.form.professionalName&&item.cooperativeName == this.form.cooperativeName)&&item.tCooperationId != this.form.tCooperationId);
if (!isNameDuplicate) {
if (this.form.tCooperationId != null) {
updateIuCooperation(this.form).then(response => {
this.$modal.msgSuccess("修改成功");
this.open = false;
this.getList();
});
} else {
addIuCooperation(this.form).then(response => {
this.$modal.msgSuccess("新增成功");
this.open = false;
this.getList();
});
}
}else {
this.$modal.msgError("专业名称和合作企业名称关联重复,请输入正确信息!");
}
});
}
});
},