All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.notice.infrastructure.NoticeGatewayImpl Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.notice.infrastructure;

import com.github.rexsheng.springboot.faster.common.domain.PagedList;
import com.github.rexsheng.springboot.faster.system.entity.Notice;
import com.github.rexsheng.springboot.faster.system.entity.NoticeUser;
import com.github.rexsheng.springboot.faster.system.entity.table.NoticeTableDef;
import com.github.rexsheng.springboot.faster.system.entity.table.NoticeUserTableDef;
import com.github.rexsheng.springboot.faster.system.mapper.NoticeMapper;
import com.github.rexsheng.springboot.faster.system.mapper.NoticeUserMapper;
import com.github.rexsheng.springboot.faster.system.notice.domain.SysNotice;
import com.github.rexsheng.springboot.faster.system.notice.domain.gateway.NoticeGateway;
import com.github.rexsheng.springboot.faster.system.notice.domain.gateway.QueryNoticeDO;
import com.github.rexsheng.springboot.faster.system.notice.domain.gateway.QueryUserNoticeDO;
import com.github.rexsheng.springboot.faster.system.utils.PageConverter;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.RawQueryColumn;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.util.UpdateEntity;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Repository
@ConditionalOnClass(BaseMapper.class)
public class NoticeGatewayImpl implements NoticeGateway {

    private static final Logger logger= LoggerFactory.getLogger(NoticeGatewayImpl.class);

    @Resource
    private NoticeMapper noticeMapper;

    @Resource
    private NoticeUserMapper noticeUserMapper;

    @Override
    @Transactional
    public void insertNotice(SysNotice notice) {
        Notice target=new Notice();
        target.setTitle(notice.getNoticeTitle());
        target.setType(notice.getNoticeType());
        target.setContent(notice.getNoticeContent());
        target.setEndTime(notice.getEndTime());
        target.setStatus(notice.getStatus());
        target.setIsDel(notice.getDel());
        target.setCreateTime(notice.getCreateTime());
        target.setCreateUser(notice.getCreateUserId());
        noticeMapper.insert(target);
        notice.setNoticeId(target.getId());
    }

    @Override
    @Transactional
    public void insertInternalNotice(SysNotice notice) {
        if(!ObjectUtils.isEmpty(notice.getNoticeUsers())){
            final List noticeUsers=new ArrayList<>();
            String batch=UUID.randomUUID().toString();
            for (SysNotice.SysNoticeUser noticeUser : notice.getNoticeUsers()) {
                NoticeUser entity=new NoticeUser();
                entity.setContentBatch(batch);
                entity.setContent(notice.getNoticeContent());
                entity.setTargetUser(noticeUser.getUserId());
                entity.setReadState(noticeUser.getReadState());
                entity.setCreateUser(noticeUser.getCreateUserId());
                entity.setCreateTime(noticeUser.getCreateTime());
                noticeUsers.add(entity);
            }
            noticeUserMapper.insertBatch(noticeUsers,100);
            QueryWrapper queryWrapper=QueryWrapper.create().where(NoticeUserTableDef.NOTICE_USER.CONTENT_BATCH.eq(batch));
            List list=noticeUserMapper.selectListByQuery(queryWrapper);
            if(!ObjectUtils.isEmpty(list)){
                final Map idMap=list.stream().collect(Collectors.toMap(a->a.getTargetUser(),a->a.getId(),(a1,a2)->a2));
                notice.getNoticeUsers().forEach(noticeUser->noticeUser.setId(idMap.get(noticeUser.getUserId())));
            }
        }
    }

    @Override
    public SysNotice getNotice(Long noticeId) {
        Notice entity=noticeMapper.selectOneById(noticeId);
        if(entity==null){
            return null;
        }
        return map(entity);
    }

    @Override
    public PagedList paginateNotices(QueryNoticeDO query) {
        QueryWrapper queryWrapper=QueryWrapper.create().select()
                .where(NoticeTableDef.NOTICE.IS_DEL.eq(false))
                ;
        queryWrapper.and(NoticeTableDef.NOTICE.TITLE.like(query.getTitle(),StringUtils::hasText));

        Page sourceList=noticeMapper.paginate(query.getPageIndex(),query.getPageSize(),queryWrapper);
        return PageConverter.convert(sourceList,this::map);
    }

    @Override
    @Transactional
    public void updateNoticeById(SysNotice data) {
        Notice entity= UpdateEntity.of(Notice.class,data.getNoticeId());
        entity.setTitle(data.getNoticeTitle());
        entity.setType(data.getNoticeType());
        entity.setContent(data.getNoticeContent());
        entity.setEndTime(data.getEndTime());
        entity.setUpdateTime(data.getUpdateTime());
        entity.setUpdateUser(data.getUpdateUserId());
        noticeMapper.update(entity);
    }

    @Override
    public void updateNoticeStatus(List list) {
        Db.executeBatch(list, 100, NoticeMapper.class
                , (mapper, row) -> {
                    UpdateChain.of(mapper)
                            .set(Notice::getStatus, row.getStatus())
                            .set(Notice::getUpdateTime,row.getUpdateTime())
                            .set(Notice::getUpdateUser,row.getUpdateUserId())
                            .where(Notice::getId).eq(row.getNoticeId())
                            .update();
                });
    }

    @Override
    public void deleteNotices(List list) {
        Db.executeBatch(list, 100, NoticeMapper.class
                , (mapper, row) -> {
                    UpdateChain.of(mapper)
                            .set(Notice::getIsDel, row.getDel())
                            .set(Notice::getUpdateTime,row.getUpdateTime())
                            .set(Notice::getUpdateUser,row.getUpdateUserId())
                            .where(Notice::getId).eq(row.getNoticeId())
                            .update();
                });
    }

    private SysNotice map(Notice source){
        SysNotice target=new SysNotice();
        target.setNoticeId(source.getId());
        target.setNoticeTitle(source.getTitle());
        target.setNoticeType(source.getType());
        target.setNoticeContent(source.getContent());
        target.setEndTime(source.getEndTime());
        target.setStatus(source.getStatus());
        target.setDel(source.getIsDel());
        target.setCreateTime(source.getCreateTime());
        target.setCreateUserId(source.getCreateUser());
        target.setUpdateTime(source.getUpdateTime());
        target.setUpdateUserId(source.getUpdateUser());
        if(source instanceof NoticeDO noticeDO){
            target.setReadTime(noticeDO.getReadTime());
            SysNotice.SysNoticeUser noticeUser=new SysNotice.SysNoticeUser();
            noticeUser.setReadState(noticeDO.getReadState());
            noticeUser.setCreateTime(noticeDO.getTargetCreateTime());
            noticeUser.setCreateUserId(noticeDO.getTargetCreateUser());
            if(Integer.valueOf("2").equals(noticeDO.getRowType())){
                target.setMessageId(noticeDO.getId());
            }
            target.setNoticeUsers(Arrays.asList(noticeUser));
        }
        return target;
    }

    @Override
    public PagedList queryUserNotices(QueryUserNoticeDO query) {
        if(Boolean.TRUE.equals(query.getReaded())){
            QueryWrapper countQuery=QueryWrapper.create()
                    .select(QueryMethods.count().as("count"))
                    .from(NoticeUserTableDef.NOTICE_USER)
                    .innerJoin(NoticeTableDef.NOTICE).on(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.eq(NoticeTableDef.NOTICE.ID))
                    .where(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId()))
                    .and(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(true))
                    .and(NoticeTableDef.NOTICE.IS_DEL.eq(false))
                    .unionAll(
                            QueryWrapper.create()
                                    .select(QueryMethods.count())
                                    .from(NoticeUserTableDef.NOTICE_USER)
                                    .where(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(true))
                                    .and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId()))
                                    .and(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.isNull())
                    );
            QueryWrapper countQueryTotal=QueryWrapper.create().select(QueryMethods.sum("count")).from(countQuery).as("a");
            List totalRows=noticeMapper.selectListByQueryAs(countQueryTotal,Long.class);
            Long totalRowCount=0L;
            PagedList pagedList=new PagedList<>(query.getPageIndex(), query.getPageSize(), totalRowCount);
            if(totalRows!=null && totalRows.size()>0 && totalRows.get(0)>0L){
                pagedList.setTotalCount(totalRows.get(0));
                QueryWrapper selectQuery=QueryWrapper.create()
                        .select(new RawQueryColumn("1").as(NoticeDO::getRowType),NoticeTableDef.NOTICE.ID,NoticeTableDef.NOTICE.TITLE,NoticeTableDef.NOTICE.CREATE_TIME,NoticeUserTableDef.NOTICE_USER.READ_TIME.as(NoticeDO::getReadTime))
                        .from(NoticeUserTableDef.NOTICE_USER)
                        .innerJoin(NoticeTableDef.NOTICE).on(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.eq(NoticeTableDef.NOTICE.ID))
                        .where(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId()))
                        .and(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(true))
                        .and(NoticeTableDef.NOTICE.IS_DEL.eq(false))
                        .unionAll(
                                QueryWrapper.create()
                                        .select(new RawQueryColumn("2").as(NoticeDO::getRowType),NoticeUserTableDef.NOTICE_USER.ID,NoticeUserTableDef.NOTICE_USER.CONTENT,NoticeUserTableDef.NOTICE_USER.CREATE_TIME,NoticeUserTableDef.NOTICE_USER.READ_TIME)
                                        .from(NoticeUserTableDef.NOTICE_USER)
                                        .where(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(true))
                                        .and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId()))
                                        .and(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.isNull())
                        );
                QueryWrapper dataQuery=QueryWrapper.create().from(selectQuery).as("a").orderBy(NoticeTableDef.NOTICE.CREATE_TIME.getName(),false).limit(query.getPageSize()).offset((query.getPageIndex()-1)*query.getPageSize());
                List dataList=noticeMapper.selectListByQueryAs(dataQuery,NoticeDO.class).stream().map(this::map).collect(Collectors.toList());
                pagedList.setDataList(dataList);
            }
            return pagedList;
        }
        else{
            QueryWrapper countQuery=QueryWrapper.create()
                    .select(QueryMethods.count().as("count"))
                    .from(NoticeTableDef.NOTICE)
                    .leftJoin(NoticeUserTableDef.NOTICE_USER).on(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.eq(NoticeTableDef.NOTICE.ID)
                            .and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId())))
                    .where(NoticeUserTableDef.NOTICE_USER.ID.isNull())
                    .and(NoticeTableDef.NOTICE.END_TIME.isNull().or(NoticeTableDef.NOTICE.END_TIME.ge(DateUtil.currentDateTime())))
                    .and(NoticeTableDef.NOTICE.IS_DEL.eq(false))
                    .unionAll(
                            QueryWrapper.create()
                                    .select(QueryMethods.count())
                                    .from(NoticeUserTableDef.NOTICE_USER)
                                    .where(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(false))
                                    .and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId()))
                                    .and(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.isNull())
                    );
            QueryWrapper countQueryTotal=QueryWrapper.create().select(QueryMethods.sum("count")).from(countQuery).as("a");
            List totalRows=noticeMapper.selectListByQueryAs(countQueryTotal,Long.class);
            Long totalRowCount=0L;
            PagedList pagedList=new PagedList<>(query.getPageIndex(), query.getPageSize(), totalRowCount);
            if(totalRows!=null && totalRows.size()>0 && totalRows.get(0)>0L){
                pagedList.setTotalCount(totalRows.get(0));
                QueryWrapper selectQuery=QueryWrapper.create()
                        .select(new RawQueryColumn("1").as(NoticeDO::getRowType),NoticeTableDef.NOTICE.ID,NoticeTableDef.NOTICE.TITLE,NoticeTableDef.NOTICE.CREATE_TIME)
                        .from(NoticeTableDef.NOTICE)
                        .leftJoin(NoticeUserTableDef.NOTICE_USER).on(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.eq(NoticeTableDef.NOTICE.ID)
                                .and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId())))
                        .where(NoticeUserTableDef.NOTICE_USER.ID.isNull())
                        .and(NoticeTableDef.NOTICE.END_TIME.isNull().or(NoticeTableDef.NOTICE.END_TIME.ge(DateUtil.currentDateTime())))
                        .and(NoticeTableDef.NOTICE.IS_DEL.eq(false))
                        .unionAll(
                                QueryWrapper.create()
                                        .select(new RawQueryColumn("2").as(NoticeDO::getRowType),NoticeUserTableDef.NOTICE_USER.ID,NoticeUserTableDef.NOTICE_USER.CONTENT,NoticeUserTableDef.NOTICE_USER.CREATE_TIME)
                                        .from(NoticeUserTableDef.NOTICE_USER)
                                        .where(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(false))
                                        .and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(query.getUserId()))
                                        .and(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.isNull())
                        );
                QueryWrapper dataQuery=QueryWrapper.create().from(selectQuery).as("a").orderBy(NoticeTableDef.NOTICE.CREATE_TIME.getName(),false).limit(query.getPageSize()).offset((query.getPageIndex()-1)*query.getPageSize());
                List dataList=noticeMapper.selectListByQueryAs(dataQuery,NoticeDO.class).stream().map(this::map).collect(Collectors.toList());
                pagedList.setDataList(dataList);
            }
            return pagedList;
        }
    }

    @Override
    public void updateReaded(SysNotice notice) {
        if(notice.getMessageId()!=null){
            NoticeUser noticeUser= UpdateEntity.of(NoticeUser.class,notice.getMessageId());
            noticeUser.setUpdateTime(notice.getUpdateTime());
            noticeUser.setUpdateUser(notice.getUpdateUserId());
            noticeUser.setReadTime(notice.getUpdateTime());
            noticeUser.setReadState(true);
            noticeUserMapper.update(noticeUser);
        }
        else if(notice.getNoticeId()!=null){
            NoticeUser noticeUser=new NoticeUser();
            noticeUser.setTargetUser(notice.getUpdateUserId());
            noticeUser.setReadState(true);
            noticeUser.setReadTime(notice.getUpdateTime());
            noticeUser.setNoticeId(notice.getNoticeId());
            noticeUser.setUpdateTime(notice.getUpdateTime());
            noticeUser.setUpdateUser(notice.getUpdateUserId());

            QueryWrapper queryWrapper=QueryWrapper.create()
                    .where(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(notice.getUpdateUserId()))
                    .and(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.eq(notice.getNoticeId()))
                    .and(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(true));
            if(noticeUserMapper.updateByQuery(noticeUser,queryWrapper)==0){
                noticeUser.setTargetUser(notice.getUpdateUserId());
                noticeUser.setReadState(true);
                noticeUser.setReadTime(notice.getUpdateTime());
                noticeUser.setNoticeId(notice.getNoticeId());
                noticeUser.setCreateTime(notice.getCreateTime());
                noticeUser.setCreateUser(notice.getCreateUserId());
                noticeUser.setUpdateTime(null);
                noticeUser.setUpdateUser(null);
                noticeUserMapper.insert(noticeUser);
            }
        }
    }

    @Override
    @Transactional
    public void updateAllReadedByUserId(Long userId) {
        QueryWrapper queryWrapper=QueryWrapper.create()
                .select(NoticeTableDef.NOTICE.ID)
                .from(NoticeTableDef.NOTICE)
                .leftJoin(NoticeUserTableDef.NOTICE_USER).on(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.eq(NoticeTableDef.NOTICE.ID).and(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(userId)))
                .where(NoticeUserTableDef.NOTICE_USER.ID.isNull())
                .and(NoticeTableDef.NOTICE.END_TIME.isNull().or(NoticeTableDef.NOTICE.END_TIME.ge(DateUtil.currentDateTime())))
                .and(NoticeTableDef.NOTICE.IS_DEL.eq(false));
        List noticeList=noticeMapper.selectListByQuery(queryWrapper);
        if(!ObjectUtils.isEmpty(noticeList)){
            for(Notice notice:noticeList){
                NoticeUser noticeUser=new NoticeUser();
                noticeUser.setTargetUser(userId);
                noticeUser.setReadState(true);
                noticeUser.setReadTime(DateUtil.currentDateTime());
                noticeUser.setNoticeId(notice.getId());
                noticeUser.setCreateTime(DateUtil.currentDateTime());
                noticeUser.setCreateUser(userId);
                noticeUserMapper.insert(noticeUser);
            }
        }
        NoticeUser noticeUser=new NoticeUser();
        noticeUser.setReadState(true);
        noticeUser.setReadTime(DateUtil.currentDateTime());
        noticeUser.setUpdateTime(DateUtil.currentDateTime());
        noticeUser.setUpdateUser(userId);

        QueryWrapper updateWrapper=QueryWrapper.create()
                .where(NoticeUserTableDef.NOTICE_USER.TARGET_USER.eq(userId))
                .and(NoticeUserTableDef.NOTICE_USER.NOTICE_ID.isNull())
                .and(NoticeUserTableDef.NOTICE_USER.READ_STATE.eq(false));
        noticeUserMapper.updateByQuery(noticeUser,updateWrapper);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy