package com.tofly.gpsboot.service.mq.impl;

import com.tofly.api.message.MessageClient;
import com.tofly.api.message.MessageStateClient;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.MessageSateEnum;
import com.tofly.gpsboot.exception.FeignException;
import com.tofly.gpsboot.service.mq.IMessageRecipientService;
import com.tofly.pojo.message.Messagepush;
import com.tofly.pojo.message.Messagerecipient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @author BZ-DJJ
 */
@Service
public class MessageRecipientServiceImpl implements IMessageRecipientService {
    @Autowired
    private MessageClient messageClient;

    @Autowired
    private MessageStateClient messageStateClient;

    @Override
    public void setMessageRecipient(Messagepush messagepush, Long userId) {
        try {
        Long  messagePushId = messageClient.save(messagepush).getResult();
        Messagerecipient messagerecipient = new Messagerecipient();
        messagerecipient.setMessageId(messagePushId);
        messagerecipient.setRecipient(userId);
        messagerecipient.setFlag(MessageSateEnum.MESSAGE_SATE_NO.getCode());
        Boolean resultState = messageStateClient.save(messagerecipient).getResult();
            if (null == messagepush || !resultState) {
                throw new FeignException(Constant.FEIGN_MESSAGE_CLIENT_ERROR_CODE, Constant.FEIGN_MESSAGE_CLIENT_ERROR);
            }
        } catch (FeignException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

    }
}
