com.github.rexsheng.springboot.faster.system.post.infrastructure.PostGatewayImpl Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.post.infrastructure;
import com.github.rexsheng.springboot.faster.common.domain.PagedList;
import com.github.rexsheng.springboot.faster.system.entity.Post;
import com.github.rexsheng.springboot.faster.system.entity.table.PostTableDef;
import com.github.rexsheng.springboot.faster.system.mapper.PostMapper;
import com.github.rexsheng.springboot.faster.system.post.domain.SysPost;
import com.github.rexsheng.springboot.faster.system.post.domain.gateway.PostGateway;
import com.github.rexsheng.springboot.faster.system.post.domain.gateway.QueryPostDO;
import com.github.rexsheng.springboot.faster.system.utils.PageConverter;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.util.UpdateEntity;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.stream.Collectors;
@Repository
@ConditionalOnClass(BaseMapper.class)
public class PostGatewayImpl implements PostGateway {
@Resource
private PostMapper postMapper;
@Override
public void insertPost(SysPost post) {
Post target=new Post();
target.setName(post.getPostName());
target.setOrder(post.getPostOrder());
target.setStatus(post.getStatus());
target.setIsDel(post.getDel());
target.setCreateTime(post.getCreateTime());
target.setCreateUser(post.getCreateUserId());
postMapper.insert(target);
}
@Override
public List queryPosts(QueryPostDO query) {
QueryWrapper queryWrapper=QueryWrapper.create().select()
.where(PostTableDef.POST.IS_DEL.eq(false))
.orderBy(PostTableDef.POST.ORDER,true)
.orderBy(PostTableDef.POST.ID,true)
;
if(query.getStatus()!=null){
queryWrapper.and(PostTableDef.POST.STATUS.eq(query.getStatus()));
}
if(StringUtils.hasText(query.getName())){
queryWrapper.and(PostTableDef.POST.NAME.like(query.getName()));
}
List dataList=postMapper.selectListByQuery(queryWrapper);
return dataList.stream().map(this::map).collect(Collectors.toList());
}
@Override
public PagedList paginatePosts(QueryPostDO query) {
QueryWrapper queryWrapper=QueryWrapper.create().select()
.where(PostTableDef.POST.IS_DEL.eq(false))
;
if(query.getStatus()!=null){
queryWrapper.and(PostTableDef.POST.STATUS.eq(query.getStatus()));
}
if(StringUtils.hasText(query.getName())){
queryWrapper.and(PostTableDef.POST.NAME.like(query.getName()));
}
queryWrapper.orderBy(PostTableDef.POST.ORDER,true).orderBy(PostTableDef.POST.ID,true);
Page sourceList=postMapper.paginate(query.getPageIndex(),query.getPageSize(),queryWrapper);
return PageConverter.convert(sourceList,this::map);
}
@Override
public SysPost getPost(Integer postId) {
Post entity=postMapper.selectOneById(postId);
return map(entity);
}
private SysPost map(Post source){
SysPost target=new SysPost();
target.setPostId(source.getId());
target.setPostName(source.getName());
target.setPostOrder(source.getOrder());
target.setStatus(source.getStatus());
target.setDel(source.getIsDel());
target.setCreateTime(source.getCreateTime());
target.setCreateUserId(source.getCreateUser());
target.setUpdateTime(source.getUpdateTime());
target.setUpdateUserId(source.getUpdateUser());
return target;
}
@Override
public void updatePostById(SysPost post) {
Post entity= UpdateEntity.of(Post.class,post.getPostId());
entity.setName(post.getPostName());
entity.setOrder(post.getPostOrder());
entity.setUpdateTime(post.getUpdateTime());
entity.setUpdateUser(post.getUpdateUserId());
postMapper.update(entity);
}
@Override
public void updatePostStatus(List posts) {
// SysPost row=posts.get(0);
// UpdateChain.of(postMapper).set(Post::getStatus, row.getStatus())
// .set(Post::getUpdateTime,row.getUpdateTime())
// .set(Post::getUpdateUser,row.getUpdateUserId())
// .where(Post::getId).in(posts.stream().map(SysPost::getPostId).collect(Collectors.toList()))
// .update();
Db.executeBatch(posts, 100, PostMapper.class
, (mapper, row) -> {
UpdateChain.of(mapper)
.set(Post::getStatus, row.getStatus())
.set(Post::getUpdateTime,row.getUpdateTime())
.set(Post::getUpdateUser,row.getUpdateUserId())
.where(Post::getId).eq(row.getPostId())
.update();
});
}
@Override
public void deletePosts(List posts) {
Db.executeBatch(posts, 100, PostMapper.class
, (mapper, row) -> {
UpdateChain.of(mapper)
.set(Post::getIsDel, row.getDel())
.set(Post::getUpdateTime,row.getUpdateTime())
.set(Post::getUpdateUser,row.getUpdateUserId())
.where(Post::getId).eq(row.getPostId())
.update();
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy