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

import com.apobates.forum.member.dao.MemberDao;
import com.apobates.forum.member.dao.MemberPenalizeRecordsDao;
import com.apobates.forum.member.entity.MemberPenalizeRecords;
import com.apobates.forum.member.impl.event.MemberPenalizeEvent;
import com.apobates.forum.member.service.MemberPenalizeRecordsService;
import com.apobates.forum.utils.DateTimeUtils;
import com.apobates.forum.utils.persistence.Page;
import com.apobates.forum.utils.persistence.Pageable;
import java.util.Optional;
import java.util.stream.Stream;
import com.github.davidmarquis.redisq.producer.MessageProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaofanku
 * @since 20200515
 */
@Service
public class MemberPenalizeRecordsServiceImpl implements MemberPenalizeRecordsService{
    @Autowired
    private MemberPenalizeRecordsDao memberPenalizeRecordsDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired @Qualifier("penalizeEventProducer")
    private MessageProducer<MemberPenalizeEvent> penalizeEventProducer;
    private final static Logger logger = LoggerFactory.getLogger(MemberPenalizeRecordsServiceImpl.class);
    
    @Override
    public Optional<Boolean> expired(long id)throws IllegalArgumentException,IllegalStateException {
        MemberPenalizeRecords obj = get(id).orElseThrow(()->new IllegalArgumentException("会员惩罚记录不存在"));
        //
        if (DateTimeUtils.isFeatureDate(obj.getRebirthDateTime())) {
            throw new IllegalStateException("会员惩罚进行中,不可以作废");
        }
        Optional<Boolean> result = memberPenalizeRecordsDao.expired(obj.getId());
        result.ifPresent(bol-> {
            if(bol){memberDao.editMemberStatus(obj.getMemberId(), obj.getOriginal());}
        });
        return result;
    }
    
    @Override
    public long create(MemberPenalizeRecords entity)throws IllegalArgumentException {
        if (memberPenalizeRecordsDao.countForMember(entity.getMemberId()) > 0) {
            throw new IllegalArgumentException("会员已经处于惩罚期");
        }
        try {
            memberPenalizeRecordsDao.save(entity);
            if (entity.getId() > 0) {
                // 应该在惩罚生效时发送,暂不支持预定日期的惩罚,只支持即可生效
                MemberPenalizeEvent mpe = new MemberPenalizeEvent(entity.getMemberId(),
                                entity.getMemberNickname(),
                                entity.getArrive(),
                                entity.getDuration(),
                                entity.getJudger(),
                                entity.getJudgeNickname(),
                                entity.getReason());
                penalizeEventProducer.create(mpe).submit();
                return entity.getId();
            }
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("创建会员惩罚记录失败", e);
            }
        }
        return 0L;
    }
    
    @Override
    public Page<MemberPenalizeRecords> getAll(Pageable pageable) {
        return memberPenalizeRecordsDao.findAll(pageable);
    }
    
    @Override
    public Page<MemberPenalizeRecords> getAll(long buildMemberId, Pageable pageable) {
        return memberPenalizeRecordsDao.findAll(buildMemberId, pageable);
    }
    
    @Override
    public Stream<MemberPenalizeRecords> getByMember(long memberId) {
        return memberPenalizeRecordsDao.findAllByMember(memberId);
    }
    
    @Override
    public Optional<MemberPenalizeRecords> get(long id) {
        return memberPenalizeRecordsDao.findOne(id);
    }
}