添加状态信息 - 实践

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设置状态按钮

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 t_book set status = #{status} where id = #{id}

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);
}

posted @ 2025-07-30 09:56  yfceshi  阅读(4)  评论(0)    收藏  举报