关于置顶上移下移功能
sql实现:
-- 注意:以下是通过desc顺序示例的,如果是asc,下面sql取反
一、--全表desc的sql示例:
--mysql查询方式:
select * from column_manage where id>4 ORDER BY id asc LIMIT 1 -- 上移
select * from column_manage where id<4 ORDER BY id desc LIMIT 1 -- 下移
select * from column_manage where id>4 ORDER BY id desc LIMIT 1 -- 置顶
--sqlserve查询方式:
--上移
with tabs as (select id,title,publish_time,sort,is_delete,row_number() over (order by id asc)rows
from column_manage where is_delete=0 and id>3)
select * from tabs where rows = 1
--下移
with tabs as (select id,title,publish_time,sort,is_delete,row_number() over (order by id desc)rows
from column_manage where is_delete=0 and id<3)
select * from tabs where rows = 1
-- 置顶
with tabs as (select id,title,publish_time,sort,is_delete,row_number() over (order by id desc)rows
from column_manage where is_delete=0 and id>3)
select * from tabs where rows = 1
二、--全表asc的sql示例:
--mysql查询方式:
select * from column_manage where id<5 ORDER BY id desc LIMIT 1 -- 上移
select * from column_manage where id>4 ORDER BY id asc LIMIT 1 -- 下移
select * from column_manage where id<6 ORDER BY id asc LIMIT 1 -- 置顶
--sqlserve查询方式:
-- 上移
with tabs as (select id,title,publish_time,sort,is_delete,row_number() over (order by id desc)rows
from column_manage where is_delete=0 and id<3)
select * from tabs where rows = 1
--下移
with tabs as (select id,title,publish_time,sort,is_delete,row_number() over (order by id asc)rows
from column_manage where is_delete=0 and id>3)
select * from tabs where rows = 1
--置顶
with tabs as (select id,title,publish_time,sort,is_delete,row_number() over (order by id asc)rows
from column_manage where is_delete=0 and id<3)
select * from tabs where rows = 1
Service层业务代码:
前端传当前id,通过标识来区分置顶上移下移的操作,前面sql是通过当前的id查询上下置顶的数据,把上下置顶的数据id与当前的数据id 交换就行了
// 操作标识:0->置顶,1->上移,2->下移
if (columnManagePrm.getFlag() == 0){
ColumnManagePo invertCurrent = columnManageMapper.selectOne(new QueryWrapper<ColumnManagePo>().eq("id", id));
ColumnManagePo invertTop = columnManageMapper.findTopColumnManage(invertCurrent.getId());
if (invertTop == null){
return HttpResult.success(400, "已经到顶了,不能再置顶了", null);
}
Integer invertCurrentId = invertCurrent.getId();
Integer invertTopId = invertTop.getId();
invertCurrent.setId(invertTopId);
invertTop.setId(invertCurrentId);
columnManageMapper.update(invertCurrent,new QueryWrapper<ColumnManagePo>().eq("id",invertCurrent.getId()));
columnManageMapper.update(invertTop,new QueryWrapper<ColumnManagePo>().eq("id",invertTop.getId()));
}
if (columnManagePrm.getFlag() == 1){
ColumnManagePo invertCurrent = columnManageMapper.selectOne(new QueryWrapper<ColumnManagePo>().eq("id", id));
ColumnManagePo invertUp = columnManageMapper.findUpColumnManage(invertCurrent.getId());
if (invertUp == null){
return HttpResult.success(400, "已经到顶了,不能再上移了", null);
}
Integer invertCurrentId = invertCurrent.getId();
Integer invertUpId = invertUp.getId();
invertCurrent.setId(invertUpId);
invertUp.setId(invertCurrentId);
columnManageMapper.update(invertCurrent,new QueryWrapper<ColumnManagePo>().eq("id",invertCurrent.getId()));
columnManageMapper.update(invertUp,new QueryWrapper<ColumnManagePo>().eq("id",invertUp.getId()));
}
if (columnManagePrm.getFlag() == 2){
ColumnManagePo invertCurrent = columnManageMapper.selectOne(new QueryWrapper<ColumnManagePo>().eq("id", id));
ColumnManagePo invertNext = columnManageMapper.findNextColumnManage(invertCurrent.getId());
if (invertNext == null){
return HttpResult.success(400, "已经到底了,不能再下移了", null);
}
Integer invertCurrentId = invertCurrent.getId();
Integer invertNextId = invertNext.getId();
invertCurrent.setId(invertNextId);
invertNext.setId(invertCurrentId);
columnManageMapper.update(invertCurrent,new QueryWrapper<ColumnManagePo>().eq("id",invertCurrent.getId()));
columnManageMapper.update(invertNext,new QueryWrapper<ColumnManagePo>().eq("id",invertNext.getId()));
}
return HttpResult.success(200, "操作成功", null);
}