第3章 预约管理-检查组管理
1. 需求分析
检查组其实就是多个检查项的集合,例如有一个检查组为“一般检查”,这个检查组可以包括多个检查项:身高、体重、收缩压、舒张压等。所以在添加检查组时需要选择这个检查组包括的检查项。
检查组对应的实体类为CheckGroup,对应的数据表为t_checkgroup。检查组和检查项为多对多关系,所以需要中间表t_checkgroup_checkitem进行关联。
2. 新增检查组
2.1 完善页面
检查组管理页面对应的是checkgroup.html页面,根据产品设计的原型已经完成了页面基本结构的编写,现在需要完善页面动态效果。
2.1.1 弹出新增窗口
页面中已经提供了新增窗口,只是出于隐藏状态。只需要将控制展示状态的属性dialogFormVisible改为true即可显示出新增窗口。点击新建按钮时绑定的方法为handleCreate,所以在handleCreate方法中修改dialogFormVisible属性的值为true即可。同时为了增加用户体验度,需要每次点击新建按钮时清空表单输入项。
由于新增检查组时还需要选择此检查组包含的检查项,所以新增检查组窗口分为两部分信息:基本信息和检查项信息,如下图:
新建按钮绑定单击事件,对应的处理函数为handleCreate
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
// 重置表单
resetForm() {
this.formData = {};
this.checkitemIds = [];
},
// 弹出添加窗口
handleCreate() {
this.resetForm();
this.dialogFormVisible = true;
}
2.1.2 动态展示检查项列表
现在虽然已经完成了新增窗口的弹出,但是在检查项信息标签页中需要动态展示所有的检查项信息列表数据,并且可以进行勾选。具体操作步骤如下:
(1)定义模型数据
tableData:[],//新增和编辑表单中对应的检查项列表数据
checkitemIds:[],//新增和编辑表单中检查项对应的复选框,基于双向绑定可以进行回显和数据提交
(2)动态展示检查项列表数据,数据来源于上面定义的tableData模型数据
<table class="datatable">
<thead>
<tr>
<th>选择</th>
<th>项目编码</th>
<th>项目名称</th>
<th>项目说明</th>
</tr>
</thead>
<tbody>
<tr v-for="c in tableData">
<td>
<input :id="c.id" v-model="checkitemIds" type="checkbox" :value="c.id">
</td>
<td><label :for="c.id">{{c.code}}</label></td>
<td><label :for="c.id">{{c.name}}</label></td>
<td><label :for="c.id">{{c.remark}}</label></td>
</tr>
</tbody>
</table>
(3)完善handleCreate方法,发送ajax请求查询所有检查项数据并将结果赋值给tableData模型数据用于页面表格展示
// 弹出添加窗口
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
//默认切换到第一个标签页(基本信息)
this.activeName='first';
//发送ajax请求查询所有检查项信息
axios.get("/checkitem/findAll").then((response)=> {
if(response.data.flag){
//将检查项列表数据赋值给模型数据用于页面表格展示
this.tableData = response.data.data;
}else{
this.$message.error(response.data.message);
}
});
}
(4)分别在CheckItemController、CheckItemService、CheckItemServiceImpl中扩展方法查询所有检查项数据
CheckItemController:
@ApiOperation("获取所有检查项")
@GetMapping("/findAll")
public Result findAll(){
List<CheckItem> list = checkItemService.findAll();
if (list==null||list.size()==0){
throw new BusinessException("检查项列表为空");
}
return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
}
CheckItemService:
public List<CheckItem> findAll()throws BusinessException;
CheckItemServiceImpl:
@Override
public List<CheckItem> findAll() {
List<CheckItem> list = checkItemMapper.selectList(null);
return list;
}
2.1.3 提交请求
当用户点击新增窗口中的确定按钮时发送ajax请求将数据提交到后台进行数据库操作。提交到后台的数据分为两部分:检查组基本信息(对应的模型数据为formData)和检查项id数组(对应的模型数据为checkitemIds)。
为确定按钮绑定单击事件,对应的处理函数为handleAdd
<el-button type="primary" @click="handleAdd()">确定</el-button>
完善handleAdd方法
//添加
handleAdd () {
//发送ajax请求将模型数据提交到后台处理
axios.post("/checkgroup/add?checkitemIds=" + this.checkitemIds,this.formData)
.then((response)=> {
//关闭新增窗口
this.dialogFormVisible = false;
if(response.data.flag){
//新增成功,弹出成功提示
this.$message({
message: response.data.message,
type: 'success'
});
}else{
//新增失败,弹出错误提示
this.$message.error(response.data.message);
}
}).finally(()=> {
this.findPage();
});
}
2.2 缓存优化
2.2.1 redis.yaml
在health_common添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
在nacos上添加配置文件redis.yaml,group为COMMON_GROUP
添加redis后,查询的时候将检查项列表和检查组列表放入到redis缓存中,以便再次查询时提高查询性能。需要注意的是为了保证数据一致性,在增删改的时候删除对应的缓存。
spring:
redis:
host: 127.0.0.1
port: 6379
timeout: 5000ms #连接超时时间
lettuce:
pool:
max-active: -1 #最大连接数,使用-1表示没有限制
max-idle: 10 #最大空闲连接数
max-wait: 5000ms #连接池最大阻塞等待时间
min-idle: 5 #连接池最小空闲连接数
<img src='5.png' style="zoom:80%;" >
2.2.2 在provider、backend工程添加bootstrap.yml
注意namespace换成自己的id:
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
name: redis
group: COMMON_GROUP
file-extension: yaml
namespace: 8a162108-2be1-4b51-a28c-b5d54d728ccf
2.2.3 在common工程中添加redis配置类
package cn.yunhe.config;
/**
* 整合RedisTemplate
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//使用jackson进行序列化
Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//规定序列化规则
ObjectMapper objectMapper = new ObjectMapper();
/**
* 第一个参数指的是序列化的域,ALL指的是字段、get和set方法、构造方法
* 第二个参数指的是序列化哪些访问修饰符,默认是public,ANY指任何访问修饰符
*/
objectMapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
//指定序列化输入的类型,类必须是非final修饰的类
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jsonRedisSerializer.setObjectMapper(objectMapper);
//序列化key value
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
2.2.4 在common工程中添加redis常量类
package cn.yunhe.constant;
/**
* Redis常量
*/
public class RedisConstant {
/**查询所有检查项的key*/
public static final String QUERY_CHECKITEMS_KEY="CheckItems:allList";
/**查询所有检查组的key*/
public static final String QUERY_CHECKGROUPS_KEY="CheckGroups:allList";
}
2.2.5 修改CheckItemServiceImpl方法添加redis缓存
(1) 修改findAll()方法,将检查项加入到缓存中,以便提高查询效率
@Autowired
private RedisTemplate redisTemplate;
@Override
public List<CheckItem> findAll() {
String key = RedisConstant.QUERY_CHECKITEMS_KEY;
List<CheckItem> list = null;
if (redisTemplate.hasKey(key)){
log.info("==========CheckItem的findAll方法走了redis缓存!");
list = (List<CheckItem>) redisTemplate.opsForValue().get(key);
}else {
list = checkItemMapper.selectList(null);
log.info("==========CheckItem的findAll方法走了数据库!");
redisTemplate.opsForValue().set(key,list);
}
return list;
}
(2) 修改增删改方法,在做写操作时删除检查项列表缓存,以便保证数据一致性.
在增删改的第一行添加以下代码:
String key = RedisConstant.QUERY_CHECKITEMS_KEY;
if (redisTemplate.hasKey(key)){
redisTemplate.delete(key);
}
(3) 修改CheckItemServiceImpl的findById方法
查询缓存中是否包含此id,如果有返回缓存数据,如果没有则查询数据库:
@Override
public CheckItem findById(Integer id) throws BusinessException {
//查询缓存,如果有则遍历,返回id相等的缓存数据
String key = RedisConstant.QUERY_CHECKITEMS_KEY;
if (redisTemplate.hasKey(key)){
List<CheckItem> list = (List<CheckItem>) redisTemplate.opsForValue().get(key);
for (CheckItem checkItem : list) {
if (id.equals(checkItem.getId())){
return checkItem;
}
}
}
//如果没有查询数据库
return checkItemMapper.selectById(id);
}
2.2.6 Controller
在health_backend工程中创建CheckGroupController
package cn.yunhe.controller;
@Api(tags = "检查组管理")
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
@Reference
private CheckGroupService checkGroupService;
@ApiOperation("添加检查组")
@PostMapping("/add")
public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
checkGroupService.add(checkitemIds,checkGroup);
return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
}
}
2.2.7 服务接口
在health_interface工程中创建CheckGroupService接口
package com.itheima.service;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import java.util.List;
/**
* 检查组服务接口
*/
public interface CheckGroupService {
/**
* 添加检查组
* @param checkItemIds 检查项id数组
* @param checkGroup 检查组
*/
void add(Integer[] checkItemIds, CheckGroup checkGroup)throws BusinessException;
}
2.2.8 服务实现类
在health_service_provider工程中创建CheckGroupServiceImpl实现类
package cn.yunhe.service;
@Service
public class CheckGroupServiceImpl implements CheckGroupService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private CheckGroupMapper checkGroupMapper;
@Autowired
private CheckGroupAndItemMapper checkGroupAndItemMapper;
@Override
@Transactional
public void add(Integer[] checkItemIds, CheckGroup checkGroup) throws BusinessException {
String key = RedisConstant.QUERY_CHECKGROUPS_KEY;
if (redisTemplate.hasKey(key)){
redisTemplate.delete(key);
}
if (checkGroup==null){
throw new BusinessException("检查组信息为空");
}
//判断检查组名称、编号是否重复
if (checkCode(checkGroup.getCode())||checkName(checkGroup.getName())){
throw new BusinessException("检查组名称或者编码已经存在");
}
//添加检查组,并获取id
checkGroupMapper.insert(checkGroup);
if (checkItemIds!=null&&checkItemIds.length>0){
Integer id = checkGroup.getId();
//添加到关系表
for (int checkItemId : checkItemIds) {
CheckGroupAndItem checkGroupAndItem = new CheckGroupAndItem();
checkGroupAndItem.setCheckgroupId(id);
checkGroupAndItem.setCheckitemId(checkItemId);
checkGroupAndItemMapper.insert(checkGroupAndItem);
}
}
}
/**
* 编码是否存在
* @param code
*/
private boolean checkCode(String code){
Integer count = checkGroupMapper.selectCount(
new LambdaQueryWrapper<CheckGroup>().eq(CheckGroup::getCode, code)
);
return count>0;
}
/**
* 检查组名称是否存在
* @param name
*/
private boolean checkName(String name){
Integer count = checkGroupMapper.selectCount(
new LambdaQueryWrapper<CheckGroup>().eq(CheckGroup::getName, name)
);
return count>0;
}
}
2.2.9 Mapper接口
创建CheckGroupMapper接口
public interface CheckGroupMapper extends BaseMapper<CheckGroup> {
}
2.2.10 修改CheckGroup实体类
由于mybatisplus默认开启了驼峰映射,所以在做添加的时候helpCode属性会被映射为help_code字段,但是表中的字段名是healCode,所以需要进行以下修改:
@TableField("helpCode")
private String helpCode;//助记码
checkItems属性在表中不存在需进行以下修改:
@TableField(exist = false)
private List<CheckItem> checkItems;//一个检查组合包含多个检查
3. 检查组分页
3.1 完善页面
3.1.1 定义分页相关模型数据
pagination: {//分页相关模型数据
currentPage: 1,//当前页码
pageSize:10,//每页显示的记录数
total:0,//总记录数
queryString:null//查询条件
},
dataList: [],//当前页要展示的分页列表数据
3.1.2 定义分页方法
在页面中提供了findPage方法用于分页查询,为了能够在checkgroup.html页面加载后直接可以展示分页数据,可以在VUE提供的钩子函数created中调用findPage方法
//钩子函数,VUE对象初始化完成后自动执行
created() {
this.findPage();
}
//分页查询
findPage() {
//请求后台
axios.post("/checkgroup/findPage",this.pagination).then((response)=> {
//为模型数据赋值,基于VUE的双向绑定展示到页面
this.dataList = response.data.rows;
this.pagination.total = response.data.total;
});
}
3.1.3 完善分页方法执行时机
除了在created钩子函数中调用findPage方法查询分页数据之外,当用户点击查询按钮或者点击分页条中的页码时也需要调用findPage方法重新发起查询请求。
为查询按钮绑定单击事件,调用findPage方法
<el-button @click="findPage()" class="dalfBut">查询</el-button>
为分页条组件绑定current-change事件,此事件是分页条组件自己定义的事件,当页码改变时触发,对应的处理函数为handleCurrentChange
<el-pagination
class="pagiantion"
@current-change="handleCurrentChange"
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
layout="total, prev, pager, next, jumper"
:total="pagination.total">
</el-pagination>
定义handleCurrentChange方法
//切换页码
handleCurrentChange(currentPage) {
//currentPage为切换后的页码
this.pagination.currentPage = currentPage;
this.findPage();
}
3.2 后台代码
3.2.1 Controller
在CheckGroupController中增加分页查询方法
@ApiModelProperty("分页条件查询检查组")
@PostMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
PageResult pageResult = checkGroupService.findPage(queryPageBean);
return pageResult;
}
3.2.2 服务接口
在CheckGroupService服务接口中扩展分页查询方法
/**
* 分页查询检查组
*/
PageResult findPage(QueryPageBean queryPageBean)throws BusinessException;
3.2.3 服务实现类
在CheckGroupServiceImpl服务实现类中实现分页查询方法,基于MybatisPlus分页助手插件实现分页
@Override
public PageResult findPage(QueryPageBean queryPageBean) {
Page<CheckGroup> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
LambdaQueryWrapper<CheckGroup> queryWrapper = null;
String queryString = queryPageBean.getQueryString();
if (!StringUtils.isEmpty(queryString)){
queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CheckGroup::getCode,queryString)
.or()
.eq(CheckGroup::getHelpCode,queryString)
.or()
.like(CheckGroup::getName,queryString);
}
IPage<CheckGroup> iPage = checkGroupMapper.selectPage(page, queryWrapper);
return new PageResult(iPage.getTotal(), iPage.getRecords());
}
4. 编辑检查组
4.1 完善页面
用户点击编辑按钮时,需要弹出编辑窗口并且将当前记录的数据进行回显,用户修改完成后点击确定按钮将修改后的数据提交到后台进行数据库操作。此处进行数据回显的时候,除了需要检查组基本信息的回显之外,还需要回显当前检查组包含的检查项(以复选框勾选的形式回显)。
4.1.1 绑定单击事件
需要为编辑按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
handleUpdate(row) {
alert(row);
}
4.1.2 弹出编辑窗口回显数据
当前页面的编辑窗口已经提供好了,默认处于隐藏状态。在handleUpdate方法中需要将编辑窗口展示出来,并且需要发送多个ajax请求分别查询当前检查组数据、所有检查项数据、当前检查组包含的检查项id用于基本数据回显
handleUpdate(row) {
//弹出编辑窗口
this.dialogFormVisible4Edit = true;
//默认选中第一个标签页
this.activeName='first';
//发送ajax请求根据id查询检查组信息,用于基本信息回显
axios.get("/checkgroup/findById/" + row.id).then((res)=>{
if(res.data.flag){
//为模型数据赋值,通过VUE数据双向绑定进行信息的回显
this.formData = res.data.data;
//发送ajax请求查询所有的检查项信息,用于检查项表格展示
axios.get("/checkitem/findAll").then((res)=> {
if(res.data.flag){
//为模型数据赋值,通过VUE数据双向绑定进行信息的回显
this.tableData = res.data.data;
//查询当前检查组包含的所有检查项id,用于页面回显
axios.get("/checkgroup/findCheckItemIdsByCheckGroupId/" + row.id).then((res)=> {
//为模型数据赋值,通过VUE数据双向绑定进行信息的回显
if (res.data.flag){
if(res.data.data==null){
this.checkitemIds = [];
}else{
this.checkitemIds=res.data.data
}
}else{
this.$message.error(res.data.message);
}
});
}else{
this.$message.error(res.data.message);
}
});
}else{
this.$message.error("获取数据失败,请刷新当前页面");
}
});
}
4.1.3 发送请求
在编辑窗口中修改完成后,点击确定按钮需要提交请求,所以需要为确定按钮绑定事件并提供处理函数handleEdit
<el-button type="primary" @click="handleEdit()">确定</el-button>
//编辑
handleEdit() {
//发送ajax请求,提交模型数据
axios.put("/checkgroup/edit?checkitemIds="+this.checkitemIds,this.formData).
then((response)=> {
//隐藏编辑窗口
this.dialogFormVisible4Edit = false;
if(response.data.flag){
this.$message({
message: response.data.message,
type: 'success'
});
}else{
this.$message.error(response.data.message);
}
}).finally(()=> {
this.findPage();
});
}
4.2 后台代码
4.2.1 Controller
在CheckGroupController中增加方法
@ApiOperation("根据id查询检查组基本信息")
@GetMapping("/findById/{id}")
public Result findById(@PathVariable("id")Integer id){
CheckGroup checkGroup = checkGroupService.findById(id);
return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
}
@ApiOperation("根据检查组ID查询检查组对应的检查项ID集合")
@GetMapping("/findCheckItemIdsByCheckGroupId/{id}")
public Result findCheckItemIdsByCheckGroupId(@PathVariable("id")Integer id){
List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
}
@ApiOperation("编辑检查组")
@ApiImplicitParams({
@ApiImplicitParam(value = "checkitemIds",name = "检查项id数组",paramType = "query"),
@ApiImplicitParam(name = "检查组基本信息",paramType = "body")
})
@PutMapping("/edit")
public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
checkGroupService.edit(checkitemIds,checkGroup);
return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
}
4.2.2 服务接口
在health_common添加工具类RedisUtil
package cn.yunhe.util;
import org.springframework.data.redis.core.RedisTemplate;
public class RedisUtil {
/**
* 删除缓存
* @param redisTemplate 工具类
* @param key 缓存的key
*/
public static void deleteCache(RedisTemplate redisTemplate,String key){
if (redisTemplate.hasKey(key)){
redisTemplate.delete(key);
}
}
}
在CheckGroupService服务接口中扩展方法
/**
* 根据id获取检查组
* @param id
*/
CheckGroup findById(Integer id)throws BusinessException;
/**
* 根据检查组id获取对应的检查项id集合
* @param id 检查组id
*/
List<Integer> findCheckItemIdsByCheckGroupId(Integer id)throws BusinessException;
/**
* 编辑检查组
* @param checkitemIds 检查项id集合
* @param checkGroup 检查组
*/
void edit(Integer[] checkitemIds, CheckGroup checkGroup)throws BusinessException;
4.2.3 服务实现类
在CheckGroupServiceImpl实现类中实现编辑方法
编辑流程:
1.判断是否有检查组列表缓存,有则删除
2.编辑时判断name和code不能重复,但是可以是现在的
3.修改检查组
4.判断检查项id集合是否为空
5.不为空,先删除中间表的关系,再进行添加
@Override
public CheckGroup findById(Integer id) {
//查询缓存
String key = RedisConstant.QUERY_CHECKGROUPS_KEY;
if (redisTemplate.hasKey(key)){
List<CheckGroup> list = (List<CheckGroup>) redisTemplate.opsForValue().get(key);
for (CheckGroup checkGroup : list) {
if (checkGroup.getId().equals(id)){
log.info("================>>查询检查组详情走了缓存");
return checkGroup;
}
}
}
//否,查询数据库
log.info("================>>查询检查组详情走了数据库");
return checkGroupMapper.selectById(id);
}
@Override
public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
List<CheckGroupAndItem> checkGroupAndItems = checkGroupAndItemMapper.selectList(
new LambdaQueryWrapper<CheckGroupAndItem>()
.select(CheckGroupAndItem::getCheckitemId)
.eq(CheckGroupAndItem::getCheckgroupId, id)
);
List<Integer> list = new ArrayList<>();
if(checkGroupAndItems!=null&&checkGroupAndItems.size()>0){
for (CheckGroupAndItem checkGroupAndItem : checkGroupAndItems) {
list.add(checkGroupAndItem.getCheckitemId());
}
}
return list;
}
@Override
@Transactional
public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
//1.删除缓存
String key = RedisConstant.QUERY_CHECKGROUPS_KEY;
if (redisTemplate.hasKey(key)){
redisTemplate.delete(key);
}
//2.编辑时判断编码和名称不能重复
if (checkGroup==null){
throw new BusinessException("检查组为null");
}
String name = checkGroup.getName();
String code = checkGroup.getCode();
if (StringUtils.isEmpty(name)||StringUtils.isEmpty(code)){
throw new BusinessException("名称或编码为空");
}
Integer id = checkGroup.getId();
//获取原有检查组
CheckGroup group = this.findById(id);
String oname = group.getName();//原有的名称
String ocode = group.getCode();//原有的编码
if ((!name.equals(oname)&&checkName(name))||(!code.equals(ocode)&&checkCode(code))){
throw new BusinessException("名称或编码已存在");
}
//3.开始编辑
checkGroupMapper.updateById(checkGroup);
//4.修改关系表
//4.1.先删除
LambdaQueryWrapper<CheckGroupAndItem> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CheckGroupAndItem::getCheckgroupId,id);
checkGroupAndItemMapper.delete(wrapper);
//4.2.再添加
if (checkitemIds!=null&&checkitemIds.length>0){
for (Integer checkitemId : checkitemIds) {
CheckGroupAndItem checkGroupAndItem = new CheckGroupAndItem();
checkGroupAndItem.setCheckitemId(checkitemId);
checkGroupAndItem.setCheckgroupId(id);
checkGroupAndItemMapper.insert(checkGroupAndItem);
}
}
}
5.删除检查组
5.1完善页面
为了防止用户误操作,点击删除按钮时需要弹出确认删除的提示,用户点击取消则不做任何操作,用户点击确定按钮再提交删除请求。
5.1.1 绑定单击事件
需要为删除按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
// 删除
handleDelete(row) {
}
5.1.2 弹出确认操作提示
弹出确认提示信息,并发送axios请求
// 删除
handleDelete(row) {
this.$confirm("确认删除吗?","提示",{type:'warning'}).then(()=>{
//点击确定按钮时只需此处代码
//alert('用户点击的是确定按钮');
axios.delete("/checkgroup/delete/"+row.id).then((res)=> {
if(!res.data.flag){
//删除失败
this.$message.error(res.data.message);
}else{
//删除成功
this.$message({
message: res.data.message,
type: 'success'
});
//调用分页,获取最新分页数据
this.findPage();
}
});
});
}
5.2 后台代码
5.2.1 Controller
在CheckGroupController中增加删除方法
@ApiOperation("根据ID删除检查组")
@DeleteMapping("/delete/{id}")
public Result delete(@PathVariable("id")Integer id){
checkGroupService.delete(id);
return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
}
5.2.2 服务接口
/**
* 删除检查组
* @param id
*/
void delete(Integer id)throws BusinessException;
5.2.2 实现类接口
@Override
@Transactional
public void delete(Integer id) throws BusinessException{
//删除缓存
String key = RedisConstant.QUERY_CHECKGROUPS_KEY;
RedisUtil.deleteCache(redisTemplate, key);
//判断是否可以删除
Integer count = setmealCheckgroupMapper.selectCount(
new LambdaQueryWrapper<SetmealCheckgroup>().eq(SetmealCheckgroup::getCheckgroupId, id)
);
if (count>0){
throw new BusinessException("有数据关联,不能删除");
}
//删除关系表
checkGroupAndItemMapper.delete(
new LambdaQueryWrapper<CheckGroupAndItem>().eq(CheckGroupAndItem::getCheckgroupId,id)
);
//删除检查组
checkGroupMapper.deleteById(id);
}
5.2.3 Mapper接口
public interface SetMealAndCheckGroupMapper extends BaseMapper<SetMealCheckGroup> {
}