新增:主键重复就改为【修改】

业务需求:在所做的出入管理系统中,车牌号为主键;要求新增新的记录时,根据车牌号判断数据库中是否有记录,有就将当前操作改为【修改】,没有就是【新增】

/** 查询车辆信息管理列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
/**新建一个函数用于根据 cardId 查询数据*/
    queryCarByCardId(cardId, callback) {
      const queryParams = { cardId: cardId }; // 构建查询参数
      listCar(queryParams).then(response => {
        const carData = response.rows[0]; // 假设查询结果只取第一条数据
        callback(carData); // 将查询结果传递给回调函数
      });
    }
/** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cardId != null) {
            // 调用新函数查询数据
            this.queryCarByCardId(this.form.cardId, carData => {
              if (carData) {
                // 查询到数据,走 updateCar 分支
                updateCar(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                // 没有查询到数据,走 addCar 分支
                addCar(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            });
          }
        }
      });
    }

这样,根据cardId去查询,查询结果有数据就走updateCar分支,没有数据就走addCar分支

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值