package com.apobates.forum.letterbox.impl.dao;

import com.apobates.forum.letterbox.dao.ForumLetterDao;
import com.apobates.forum.letterbox.dao.OutboxDao;
import com.apobates.forum.letterbox.entity.ForumLetter;
import com.apobates.forum.letterbox.entity.ForumLetterReceiver;
import com.apobates.forum.letterbox.entity.ForumLetterTypeEnum;
import com.apobates.forum.letterbox.entity.Outbox;
import com.apobates.forum.letterbox.entity.proxy.ForumLetterReplica;
import com.apobates.forum.utils.DateTimeUtils;
import com.apobates.forum.utils.persistence.Page;
import com.apobates.forum.utils.persistence.Pageable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author xiaofanku
 * @since 20200511
 */
@Repository
public class ForumLetterDaoImpl implements ForumLetterDao{
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private OutboxDao outboxDao;
    private final static Logger logger = LoggerFactory.getLogger(ForumLetterDaoImpl.class);
    /**
     * [NativeSQL]
     * @param sender
     * @param receiver
     * @param pageable
     * @return 
     */
    @Override
    public Page<ForumLetter> findAll(long sender, long receiver, Pageable pageable) {
        final long count = countAll(sender, receiver);
        if (count == 0) {
            return emptyResult();
        }
        String SQL = "SELECT fl.* FROM apo_letter AS fl JOIN apo_letter_inbox AS ib ON fl.ID = ib.LETTER "
                + "WHERE (ib.MEMBER = ?1 AND ib.USABLE = ?2 AND fl.AUTHOR = ?3) "
                + "OR (ib.MEMBER = ?4 AND ib.USABLE = ?5 AND fl.AUTHOR = ?6) "
                + "ORDER BY fl.ENTRYDATETIME DESC";
        Query query = entityManager.createNativeQuery(SQL, ForumLetter.class)
                .setParameter(1, receiver)
                .setParameter(2, true)
                .setParameter(3, sender)
                .setParameter(4, sender)
                .setParameter(5, true)
                .setParameter(6, receiver);
        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getPageSize());
        
        @SuppressWarnings("unchecked")
        final List<ForumLetter> result = query.getResultList();
        return new Page<ForumLetter>() {
            @Override
            public long getTotalElements() {
                return count;
            }
            @Override
            public Stream<ForumLetter> getResult() {
                return result.stream();
            }
        };
    }
    /**
     * [NativeSQL]
     * @param sender
     * @param receiver
     * @return 
     */
    private long countAll(long sender, long receiver) {
        String SQL = "SELECT COUNT(*) FROM apo_letter AS fl JOIN apo_letter_inbox AS ib ON fl.ID = ib.LETTER "
                + "WHERE (ib.MEMBER = ?1 AND ib.USABLE = ?2 AND fl.AUTHOR = ?3) "
                + "OR (ib.MEMBER = ?4 AND ib.USABLE = ?5 AND fl.AUTHOR = ?6) ";
        try {
            Object obj = entityManager.createNativeQuery(SQL)
                    .setParameter(1, receiver)
                    .setParameter(2, true)
                    .setParameter(3, sender)
                    .setParameter(4, sender)
                    .setParameter(5, true)
                    .setParameter(6, receiver)
                    .getSingleResult();
            try {
                return ((BigDecimal) obj).longValue();
            } catch (java.lang.ClassCastException e) {
                return (Long) obj;
            }
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("[countAll][ForumLetterDao]", e);
            }
        }
        return 0L;
    }
    /**
     * [NativeSQL]
     * @param sender
     * @param receiver
     * @return 
     */
    @Override
    public Stream<ForumLetter> findAll(long sender, long receiver) {
        String SQL = "SELECT fl.* FROM apo_letter AS fl JOIN apo_letter_inbox AS ib ON fl.ID = ib.LETTER "
                + "WHERE (ib.MEMBER = ?1 AND ib.USABLE = ?2 AND fl.AUTHOR = ?3) "
                + "OR (ib.MEMBER = ?4 AND ib.USABLE = ?5 AND fl.AUTHOR = ?6) "
                + "ORDER BY fl.ENTRYDATETIME ASC";
        return entityManager.createNativeQuery(SQL, ForumLetter.class)
                .setParameter(1, receiver)
                .setParameter(2, true)
                .setParameter(3, sender)
                .setParameter(4, sender)
                .setParameter(5, true)
                .setParameter(6, receiver)
                .getResultStream();
    }
    
    @Override
    public Stream<ForumLetter> getTodayClaim() {
        LocalDateTime start = DateTimeUtils.getTodayEarlyMorning();
        LocalDateTime finish = DateTimeUtils.getTodayMidnight();
        
        return entityManager.createQuery("SELECT fl FROM ForumLetter fl WHERE fl.typed = ?1 AND fl.entryDateTime BETWEEN ?2 AND ?3", ForumLetter.class)
                .setParameter(1, ForumLetterTypeEnum.CLAIM)
                .setParameter(2, start)
                .setParameter(3, finish)
                .getResultStream();
    }
    
    @Override
    public Page<ForumLetter> findAll(Pageable pageable) {
        return emptyResult();
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void save(ForumLetter entity) {
        entityManager.persist(entity);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Optional<ForumLetterReplica> pushLetter(ForumLetter letter, Collection<ForumLetterReceiver> receiveres) {
        entityManager.persist(letter);
        entityManager.flush(); //不刷新下面代码取不到id
        if(letter.getId()<=0){
            return Optional.empty();
        }
        
        ForumLetterReplica fr = Optional.of(letter).map(ForumLetterReplica.copyLetter).orElseGet(ForumLetterReplica::new);
        if(null!=receiveres && !receiveres.isEmpty() && fr.getId()>0){
            final long letterId = fr.getId();
            Set<Outbox> letterSendRecords = receiveres.stream().map(rc -> new Outbox(letter.getAuthor(), rc.getMember(), rc.getMemberNickname(), letterId)).collect(Collectors.toSet());
            outboxDao.batchSave(letterSendRecords);
            fr.setReceivers(new HashSet<>(receiveres));
        }
        return Optional.of(fr);
    }
    
    @Override
    public Optional<ForumLetter> findOne(Long primaryKey) {
        return Optional.ofNullable(entityManager.find(ForumLetter.class, primaryKey));
    }
    
    @Override
    public Optional<Boolean> edit(ForumLetter updateEntity) {
        return Optional.empty();
    }
    
    @Override
    public Stream<ForumLetter> findAll() {
        return Stream.empty();
    }
    
    @Override
    public long count() {
        return 0L;
    }
}