添加状态信息

发布于:2025-07-22 ⋅ 阅读:(10) ⋅ 点赞:(0)

1首先在数据字典里加入可借阅和不可借阅状态

2导入数据字典

export default {
  name: "Book",
  dicts: ['book_borrow_status'],//导入数据字典
  data() {
    return {
      formData: {
        name: null,
        author: null,
        num: null,
        price: null,
        typeId: null,
        status:null//新加状态属性
      },

3设置状态按钮

<el-table-column label="是否可借阅" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
            :disabled="scope.row.bookStock === 0"//禁用按钮
          ></el-switch>
        </template>
      </el-table-column>

4设置函数实现状态功能

    handleStatusChange(row){
      let text = row.status === "0" ? "可借阅" : "不可借阅"
      this.$modal.confirm('确认要将"' + row.name + '"变为"' + text + '"吗?').then(function() {
        return changeBookStatus(row.id, row.status)
      }).then(() => {
        this.$modal.msgSuccess("设置" + text + "成功")
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0"
      })
    },

5实现changeBookStatus

// 图书状态修改
export function changeBookStatus(id, status) {
  const data = {
    id,
    status
  }
  return request({
    url: '/book/book/changeStatus',
    method: 'put',
    data: data
  })
}

6实现后端controller

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('book:book:edit')")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Book book)
    {
        return toAjax(bookService.updateBookStatus(book));
    }
}

7实现业务逻辑

    /**
     * 修改图书状态
     *
     * @param book 图书
     * @return 结果
     */
    @Override
    public int updateBookStatus(Book book)
    {
        return bookMapper.updateBookStatus(book);
    }
}

8接口连接

IBookService接口

    /**
     * 修改用户状态
     *
     * @param book 图书
     * @return 结果
     */
    public int updateBookStatus(Book book);
}

IBookMapper接口

    public int updateBookStatus(Book book);

9mapper.xml写sql逻辑

    <update id="updateBookStatus" parameterType="Book">
        update t_book set status = #{status} where id = #{id}
    </update>

10在service实现类里写业务

当书籍不是空的时候添加自动为可借阅状态

@Override
    public int insertBook(Book book)
    {
//        if(book.getStatus() != null && "".equals(book.getStatus()))
        if(book != null &&!StringUtils.hasLength(book.getStatus())){
            book.setStatus("1");
        }
        return bookMapper.insertBook(book);
    }

当仓库为0的时候设置状态为不可借阅

@Override
    public int updateBook(Book book)
    {
        if(book.getBookStock() == 0){
            book.setStatus("1");
        }
        return bookMapper.updateBook(book);
    }


网站公告

今日签到

点亮在社区的每一天
去签到