CreateTaskMessage.java 1.77 KB
package com.huaheng.pc.task.taskHeader.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.huaheng.common.constant.Constants;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.pc.monitor.message.domain.Messages;
import com.huaheng.pc.monitor.message.service.MessagesService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Enzo Cotter on 2020/4/20.
 */
@Service
public class CreateTaskMessage {

    @Resource
    private MessagesService messagesService;
    @Resource
    private RabbitTaskSender rabbitTaskSender;

    public void createTask(TaskHeader taskHeader){
        Messages messages = new Messages();
        messages.setMsgType("任务");
        messages.setMsgSubType(String.valueOf(taskHeader.getTaskType()));
        messages.setMsgFrom("WMS");
        messages.setMsgTo("WCS");
        messages.setMsgSubject("");
        messages.setMsgBody(JSON.toJSONString(taskHeader));
        messages.setEnable(Constants.SEND_SUCCESS);
        messages.setTryCount(0);
        messages.setCreated(new Date());
        messages.setCreatedBy(taskHeader.getCreatedBy());;
        messages.setQueue("task");
        messages.setRoutingKey("task.Issued");
        messages.setNextRetry(DateUtils.addMinutes(messages.getCreated(), Constants.ORDER_TIMEOUT));
        messages.setUpdateTime(new Date());
        if (messagesService.save(messages)) {
            rabbitTaskSender.sendTask(messages);
        } else {
            throw new ServiceException("消息发送失败");
        }
    }
}