package com.apobates.forum.core.impl.service;

import com.apobates.forum.core.dao.BoardModeratorActionIndexDao;
import com.apobates.forum.core.entity.BoardModeratorActionIndex;
import com.apobates.forum.core.service.BoardModeratorActionIndexService;
import com.apobates.forum.event.elderly.ForumActionEnum;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaofanku
 * @since 20200514
 */
@Service
public class BoardModeratorActionIndexServiceImpl implements BoardModeratorActionIndexService{
    @Autowired
    private BoardModeratorActionIndexDao boardModeratorActionIndexDao;
    private final static Logger logger = LoggerFactory.getLogger(BoardModeratorActionIndexServiceImpl.class);
    
    @Override
    public Stream<ForumActionEnum> getAllByBoardId(long moderatorId) {
        return boardModeratorActionIndexDao.getAllByModerator(moderatorId).map(BoardModeratorActionIndex::getAction).distinct();
    }
    
    @Override
    public Optional<Boolean> grantModeratorActions(long moderatorId, List<ForumActionEnum> actions) {
        if (null == actions || actions.isEmpty()) {
            return Optional.empty();//("参数不合法或不被接受");
        }
        List<BoardModeratorActionIndex> bmActions = actions.stream().map(fae -> new BoardModeratorActionIndex(moderatorId, fae)).collect(Collectors.toList());
        try {
            boardModeratorActionIndexDao.batchSave(bmActions);
            return Optional.of(true);
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("版主赋权操作失败", e);
            }
        }
        return Optional.empty();
    }
    
    @Override
    public Optional<Boolean> edit(long moderatorId, List<ForumActionEnum> actions) {
        if (null == actions || actions.isEmpty()) {
            return Optional.empty();//("参数不合法或不被接受");
        }
        int affect = boardModeratorActionIndexDao.edit(moderatorId, actions);
        return affect > 0 ? Optional.of(true) : Optional.empty(); //failure("操作失败");
    }
}