package com.sw.mes.config.rabbit;

import com.alibaba.fastjson.JSON;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.dto.MessageDto;
import com.sw.mes.inspect.entity.YbInspectData;
import com.sw.mes.rabbitmq.constants.RoutingKey;
import com.sw.mes.rabbitmq.enums.ExchangeEnum;
import com.sw.mes.rabbitmq.enums.QueueEnum;
import com.sw.mes.rabbitmq.service.RabbitMqService;
import com.sw.mes.yb.check.entity.PieceCheck;
import com.sw.mes.yb.production.entity.LongCrystal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author zhaowenjie
 */
@Slf4j
@Component
public class InspectDataBaseChangeAsync {

    @Autowired
    private RabbitMqService rabbitMqService;

    @Async
    public void inspectDataBaseChange(YbInspectData inspectDataBase) {
        MessageDto messageDto = new MessageDto();
        messageDto.setContent(JSON.toJSONString(inspectDataBase));
        messageDto.setGmtCreate(new Date());
        messageDto.setUserCreate(BaseContextHandler.getUsername());
        //检验数据变动，发送消息，处理检验数据报警
        String json = JSON.toJSONString(messageDto);
        try {
            //inspectDataMsgProducer.sendMsgInspectData(json);
            rabbitMqService.sendMessage(json, ExchangeEnum.INSPECT_DATA_EXCHANGE, QueueEnum.INSPECT_DATA_QUEUE, RoutingKey.INSPECT_DATA);
            // SPC 发送检验数据
//            inspectDataMsgProducer.sendSpcMsgInspectData(json);

            rabbitMqService.sendMessage(json, ExchangeEnum.EXCHANGE_SPC_YB_PRODUCT_INSPECT, QueueEnum.QUEUE_SPC_YB_PRODUCT_INSPECT,RoutingKey.KEY_SPC_PRODUCT_INSPECT);
        } catch (Exception e) {
            log.error("检验数据异常消息发送失败：", e);
        }
    }

    /**
     * 发送样片数据
     * @param pieceCheckList
     */
    @Async
    public void pieceCheckDataBaseChange(List<PieceCheck> pieceCheckList) {
        MessageDto messageDto = new MessageDto();
        messageDto.setContent(JSON.toJSONString(pieceCheckList));
        messageDto.setGmtCreate(new Date());
        messageDto.setUserCreate(BaseContextHandler.getUsername());
        String json = JSON.toJSONString(messageDto);
        try {
            //SPC发送样片数据
            rabbitMqService.sendMessage(json, ExchangeEnum.EXCHANGE_SPC_YB_PRODUCT_PIECE_CHECK, QueueEnum.QUEUE_SPC_YB_PRODUCT_PIECE_CHECK, RoutingKey.KEY_SPC_PRODUCT_PIECE_CHECK);
        } catch (Exception e) {
            log.error("样片数据异常消息发送失败：", e);
        }
    }

    /**
     * 发送长晶棒产出数据
     * @param longCrystalData
     */
    @Async
    public void longCrystalDataBaseChange(LongCrystal longCrystalData) {
        MessageDto messageDto = new MessageDto();
        messageDto.setContent(JSON.toJSONString(longCrystalData));
        messageDto.setGmtCreate(new Date());
        messageDto.setUserCreate(BaseContextHandler.getUsername());
        String json = JSON.toJSONString(messageDto);
        try {
            //SPC发送长晶棒产出数据
            rabbitMqService.sendMessage(json, ExchangeEnum.LONG_CRYSTAL_EXCHANGE, QueueEnum.QUEUE_SPC_LONG_CRYSTAL, RoutingKey.LONG_CRYSTAL);
        } catch (Exception e) {
            log.error("长晶棒产出数据异常消息发送失败：", e);
        }
    }
}
