|
|
@ -14,11 +14,15 @@ import com.huoran.iasf.vo.req.ArticleModifiedSortReq; |
|
|
|
import com.huoran.iasf.vo.req.ContentHeavyTitleReqVO; |
|
|
|
import com.huoran.iasf.vo.req.ContentHeavyTitleReqVO; |
|
|
|
import com.huoran.iasf.vo.req.PageContentReqVO; |
|
|
|
import com.huoran.iasf.vo.req.PageContentReqVO; |
|
|
|
import com.huoran.iasf.vo.resp.PageContentRespVO; |
|
|
|
import com.huoran.iasf.vo.resp.PageContentRespVO; |
|
|
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate; |
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @描述:文章管理 服务类 |
|
|
|
* @描述:文章管理 服务类 |
|
|
@ -28,6 +32,7 @@ import java.util.*; |
|
|
|
@Service |
|
|
|
@Service |
|
|
|
public class SysContentServiceImpl extends ServiceImpl<SysContentMapper, SysContent> implements SysContentService { |
|
|
|
public class SysContentServiceImpl extends ServiceImpl<SysContentMapper, SysContent> implements SysContentService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(SysContentServiceImpl.class); |
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private SysContentMapper mapper; |
|
|
|
private SysContentMapper mapper; |
|
|
|
|
|
|
|
|
|
|
@ -148,60 +153,127 @@ public class SysContentServiceImpl extends ServiceImpl<SysContentMapper, SysCont |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public R modifiedSort(Integer sequenceNumber, Integer articleId) { |
|
|
|
public R modifiedSort(Integer sequenceNumber, Integer articleId) { |
|
|
|
|
|
|
|
|
|
|
|
if (sequenceNumber <= 0) { |
|
|
|
// 占分布式锁,去redis占坑
|
|
|
|
return R.fail("请输入正确的序号!"); |
|
|
|
// 1. 分布式锁占坑
|
|
|
|
|
|
|
|
Boolean lock = redisTemplate.opsForValue().setIfAbsent("articleSortLock", "value", 30, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
if (lock) { |
|
|
|
|
|
|
|
//加锁成功...
|
|
|
|
|
|
|
|
if (sequenceNumber <= 0) { |
|
|
|
|
|
|
|
redisTemplate.delete("articleSortLock"); //删除key,释放锁
|
|
|
|
|
|
|
|
return R.fail("请输入正确的序号!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
SysContent content = baseMapper.selectById(articleId); |
|
|
|
|
|
|
|
//获取当前排序的序号
|
|
|
|
|
|
|
|
Integer currentSerialNumber = content.getSequence(); |
|
|
|
|
|
|
|
//获取当前文章所属栏目id
|
|
|
|
|
|
|
|
Integer columnId = content.getColumnId(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (currentSerialNumber.equals(sequenceNumber)) { |
|
|
|
|
|
|
|
redisTemplate.delete("articleSortLock"); //删除key,释放锁
|
|
|
|
|
|
|
|
return R.success(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//修改当前序号
|
|
|
|
|
|
|
|
UpdateWrapper<SysContent> updateWrapper = new UpdateWrapper<>(); |
|
|
|
|
|
|
|
updateWrapper.set("sequence", sequenceNumber); |
|
|
|
|
|
|
|
updateWrapper.eq("id", articleId); |
|
|
|
|
|
|
|
//做乐观锁处理
|
|
|
|
|
|
|
|
updateWrapper.eq("sequence", currentSerialNumber); |
|
|
|
|
|
|
|
int updateCount = baseMapper.update(new SysContent(), updateWrapper); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UpdateWrapper<SysContent> updateWrapper1 = new UpdateWrapper<>(); |
|
|
|
|
|
|
|
updateWrapper1.ne("id", articleId); |
|
|
|
|
|
|
|
//currentSerialNumber 当前文章的序号
|
|
|
|
|
|
|
|
//sequenceNumber 要更改的序号
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 就要判断排序是值改小还是改大 |
|
|
|
|
|
|
|
* 大改小比如53改5那就加一个语句“大于等于5并且小于53” |
|
|
|
|
|
|
|
* 如果是小改大比如3改50那就是“大于3并且小于等于50" |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
if (currentSerialNumber > sequenceNumber) { |
|
|
|
|
|
|
|
//改大 gt >
|
|
|
|
|
|
|
|
updateWrapper1.setSql(" sequence = sequence + 1"); |
|
|
|
|
|
|
|
updateWrapper1.last(" and sequence >= " + sequenceNumber + " and sequence < " + currentSerialNumber); |
|
|
|
|
|
|
|
} else if (currentSerialNumber < sequenceNumber) { |
|
|
|
|
|
|
|
//改小
|
|
|
|
|
|
|
|
//le为<=
|
|
|
|
|
|
|
|
// updateWrapper1.le("sequence", sequenceNumber);
|
|
|
|
|
|
|
|
updateWrapper1.setSql(" sequence = sequence - 1"); |
|
|
|
|
|
|
|
updateWrapper1.last(" and sequence > " + currentSerialNumber + " and sequence <= " + sequenceNumber); |
|
|
|
|
|
|
|
//用于判断序号为本身就为1时候就不减了
|
|
|
|
|
|
|
|
// updateWrapper1.ne("sequence", 1);
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateWrapper1.eq("column_id", columnId); |
|
|
|
|
|
|
|
updateWrapper1.eq("deleted", 1); |
|
|
|
|
|
|
|
baseMapper.update(new SysContent(), updateWrapper1); |
|
|
|
|
|
|
|
redisTemplate.delete("articleSortLock"); //删除key,释放锁
|
|
|
|
|
|
|
|
return R.success(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return R.fail("当前有用户操作排序,请稍后刷新重试!"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SysContent content = baseMapper.selectById(articleId); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*@Override |
|
|
|
|
|
|
|
public R modifiedSort(Integer newIndex, Integer articleId,Integer sort) { |
|
|
|
|
|
|
|
if (newIndex <= 0) { |
|
|
|
|
|
|
|
return R.fail("请输入正确的序号!"); |
|
|
|
|
|
|
|
} |
|
|
|
//获取当前排序的序号
|
|
|
|
//获取当前排序的序号
|
|
|
|
Integer currentSerialNumber = content.getSequence(); |
|
|
|
SysContent content = baseMapper.selectById(articleId); |
|
|
|
//获取当前文章所属栏目id
|
|
|
|
//获取当前文章所属栏目id
|
|
|
|
Integer columnId = content.getColumnId(); |
|
|
|
Integer columnId = content.getColumnId(); |
|
|
|
|
|
|
|
//查看当前升序情况下全部文章信息
|
|
|
|
//获取栏目下全部文章的数量与当前输入的序号判断 如果要更改的序号大于当前有的数量,直接替换
|
|
|
|
|
|
|
|
QueryWrapper<SysContent> queryWrapper = new QueryWrapper<>(); |
|
|
|
QueryWrapper<SysContent> queryWrapper = new QueryWrapper<>(); |
|
|
|
queryWrapper.eq("column_id", columnId); |
|
|
|
queryWrapper.eq("column_id", columnId); |
|
|
|
queryWrapper.eq("deleted", 1); |
|
|
|
queryWrapper.eq("deleted", 1); |
|
|
|
List<SysContent> contentList = baseMapper.selectList(queryWrapper); |
|
|
|
|
|
|
|
if (sequenceNumber >= contentList.size()) { |
|
|
|
|
|
|
|
sequenceNumber = contentList.size(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sort==0){ |
|
|
|
|
|
|
|
queryWrapper.orderByAsc("sequence"); |
|
|
|
|
|
|
|
}else if (sort==1){ |
|
|
|
|
|
|
|
queryWrapper.orderByDesc("sequence"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (currentSerialNumber.equals(sequenceNumber)) { |
|
|
|
List<SysContent> contentList = baseMapper.selectList(queryWrapper); |
|
|
|
|
|
|
|
//获取当前序号在list中的顺序
|
|
|
|
|
|
|
|
int currentIndex = contentList.indexOf(content); |
|
|
|
|
|
|
|
//获取当前排序的序号
|
|
|
|
|
|
|
|
Integer currentSerialNumber = content.getSequence(); |
|
|
|
|
|
|
|
if (currentIndex == -1) { |
|
|
|
|
|
|
|
// 文章不存在,不做任何操作
|
|
|
|
return R.success(); |
|
|
|
return R.success(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//对于传过来的序号 处理用于匹配集合的下标
|
|
|
|
|
|
|
|
if (currentSerialNumber > newIndex) { |
|
|
|
|
|
|
|
newIndex--; |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
newIndex++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (currentIndex == newIndex) { |
|
|
|
//修改当前序号
|
|
|
|
// 新旧位置相同,不做任何操作
|
|
|
|
UpdateWrapper<SysContent> updateWrapper = new UpdateWrapper<>(); |
|
|
|
return R.success(); |
|
|
|
updateWrapper.set("sequence", sequenceNumber); |
|
|
|
} |
|
|
|
updateWrapper.eq("id", articleId); |
|
|
|
// 移除当前位置的文章
|
|
|
|
baseMapper.update(new SysContent(), updateWrapper); |
|
|
|
contentList.remove(content); |
|
|
|
|
|
|
|
if (newIndex >= contentList.size()) { |
|
|
|
UpdateWrapper<SysContent> updateWrapper1 = new UpdateWrapper<>(); |
|
|
|
// 插入到末尾
|
|
|
|
updateWrapper1.ne("id", articleId); |
|
|
|
contentList.add(content); |
|
|
|
// 序号越小值越算大
|
|
|
|
} else { |
|
|
|
//currentSerialNumber 当前文章的序号
|
|
|
|
// 插入到新位置
|
|
|
|
//sequenceNumber 要更改的序号
|
|
|
|
contentList.add(newIndex, content); |
|
|
|
if (currentSerialNumber > sequenceNumber) { |
|
|
|
|
|
|
|
//改大 gt >
|
|
|
|
|
|
|
|
updateWrapper1.setSql(" sequence = sequence + 1"); |
|
|
|
|
|
|
|
updateWrapper1.last(" and sequence >= " + sequenceNumber + " and sequence < " + currentSerialNumber); |
|
|
|
|
|
|
|
} else if (currentSerialNumber < sequenceNumber) { |
|
|
|
|
|
|
|
//改小
|
|
|
|
|
|
|
|
updateWrapper1.le("sequence", sequenceNumber); |
|
|
|
|
|
|
|
updateWrapper1.setSql(" sequence = sequence - 1"); |
|
|
|
|
|
|
|
//用于判断序号为本身就为1时候就不减了
|
|
|
|
|
|
|
|
updateWrapper1.ne("sequence", 1); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < contentList.size(); i++) { |
|
|
|
|
|
|
|
SysContent article = contentList.get(i); |
|
|
|
|
|
|
|
article.setSequence(i + 1); |
|
|
|
|
|
|
|
baseMapper.updateById(article); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateWrapper1.eq("column_id", columnId); |
|
|
|
|
|
|
|
updateWrapper1.eq("deleted", 1); |
|
|
|
|
|
|
|
baseMapper.update(new SysContent(), updateWrapper1); |
|
|
|
|
|
|
|
return R.success(); |
|
|
|
return R.success(); |
|
|
|
} |
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|