package cn.novalon.manage.db.repository;

import cn.novalon.manage.notify.core.domain.SysUserMessage;
import cn.novalon.manage.notify.core.repository.ISysUserMessageRepository;
import cn.novalon.manage.db.converter.SysUserMessageConverter;
import cn.novalon.manage.db.entity.SysUserMessageEntity;
import cn.novalon.manage.db.dao.SysUserMessageDao;
import cn.novalon.manage.db.dao.QueryUtil;
import cn.novalon.manage.db.entity.query.SysUserMessageQueryCriteria;
import org.springframework.data.domain.Sort;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 用户消息仓储实现类
 * 
 * @author 张翔
 * @date 2026-03-13
 */
@Repository
public class SysUserMessageRepository implements ISysUserMessageRepository {

    private final SysUserMessageDao sysUserMessageDao;
    private final SysUserMessageConverter sysUserMessageConverter;
    private final R2dbcEntityTemplate r2dbcEntityTemplate;

    public SysUserMessageRepository(SysUserMessageDao sysUserMessageDao,
            SysUserMessageConverter sysUserMessageConverter, R2dbcEntityTemplate r2dbcEntityTemplate) {
        this.sysUserMessageDao = sysUserMessageDao;
        this.sysUserMessageConverter = sysUserMessageConverter;
        this.r2dbcEntityTemplate = r2dbcEntityTemplate;
    }

    @Override
    public Flux<SysUserMessage> findByUserIdOrderByCreateTimeDesc(Long userId) {
        SysUserMessageQueryCriteria criteria = new SysUserMessageQueryCriteria();
        criteria.setUserId(userId);

        org.springframework.data.relational.core.query.Query dbQuery = QueryUtil.getQuery(criteria);
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        dbQuery = dbQuery.sort(sort);

        return r2dbcEntityTemplate.select(SysUserMessageEntity.class)
                .matching(dbQuery)
                .all()
                .map(sysUserMessageConverter::toDomain);
    }

    @Override
    public Flux<SysUserMessage> findByUserIdAndIsReadOrderByCreateTimeDesc(Long userId, String isRead) {
        SysUserMessageQueryCriteria criteria = new SysUserMessageQueryCriteria();
        criteria.setUserId(userId);
        criteria.setIsRead(isRead);

        org.springframework.data.relational.core.query.Query dbQuery = QueryUtil.getQuery(criteria);
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        dbQuery = dbQuery.sort(sort);

        return r2dbcEntityTemplate.select(SysUserMessageEntity.class)
                .matching(dbQuery)
                .all()
                .map(sysUserMessageConverter::toDomain);
    }

    @Override
    public Mono<Long> countByUserIdAndIsRead(Long userId, String isRead) {
        SysUserMessageQueryCriteria criteria = new SysUserMessageQueryCriteria();
        criteria.setUserId(userId);
        criteria.setIsRead(isRead);

        org.springframework.data.relational.core.query.Query dbQuery = QueryUtil.getQuery(criteria);

        return r2dbcEntityTemplate.count(dbQuery, SysUserMessageEntity.class);
    }

    @Override
    public Mono<SysUserMessage> save(SysUserMessage message) {
        SysUserMessageEntity entity = sysUserMessageConverter.toEntity(message);
        return sysUserMessageDao.save(entity)
                .map(sysUserMessageConverter::toDomain);
    }

    @Override
    public Mono<SysUserMessage> findById(Long id) {
        return sysUserMessageDao.findById(id)
                .map(sysUserMessageConverter::toDomain);
    }

    @Override
    public Mono<Void> deleteById(Long id) {
        return sysUserMessageDao.deleteById(id);
    }
}