package com.sw.mes.config.rabbit;

import com.alibaba.fastjson.JSON;
import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.core.annotation.ServiceLogs;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.dto.MessageDto;
import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.core.utils.StringUtils;
import com.sw.mes.fb.aio.dto.EapRetreatFeedbackDto;
import com.sw.mes.fb.aio.dto.GrindingBlankingDto;
import com.sw.mes.fb.aio.entity.EapRetreatFeedback;
import com.sw.mes.fb.aio.entity.GrindingBlanking;
import com.sw.mes.mcs.dto.NoDetectionDto;
import com.sw.mes.mcs.utils.RequestDto;
import com.sw.mes.mcs.utils.RequestUtils;
import com.sw.mes.mcs.utils.ResultDto;
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 lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

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

    @Autowired
    private FbInspectDataMsgProducer fbInspectDataMsgProducer;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    ModelMapper modelMapper;
    @Autowired
    private RabbitMqService rabbitMqService;

    @Autowired
    RequestUtils requestUtils;

    // 不检测下发mcs
    private final String noDetectionUrl = "/MCS_SERVER_DATA/MK/WMS/horizontalStoreroom/matchingCutInfo";

    /**
     * 检验数据发生变动
     *
     * @param fbCheckOutData 检验数据
     */
    @Async
    public void fbCheckOutDataChange(FbCheckOutData fbCheckOutData) {
        MessageDto messageDto = new MessageDto();
        messageDto.setContent(JSON.toJSONString(fbCheckOutData));
        messageDto.setGmtCreate(new Date());
        messageDto.setUserCreate(BaseContextHandler.getUsername());
        //检验数据变动，发送消息，处理检验数据报警
        try {
            fbInspectDataMsgProducer.sendMsgFbInspectData(JSON.toJSONString(messageDto));
//             spc获取方棒检验数据
//            fbInspectDataMsgProducer.sendSpcMsgFbInspectData(JSON.toJSONString(messageDto));
            //todo song
            log.info("给SPC发送方棒检验数据");
            rabbitMqService.sendMessage(JSON.toJSONString(messageDto), ExchangeEnum.EXCHANGE_SPC_FB_PRODUCT_INSPECT, QueueEnum.QUEUE_SPC_FB_PRODUCT_INSPECT, RoutingKey.KEY_FB_SPC_PRODUCT_INSPECT);
            log.info("给SPC发送方棒检验数据为：" + messageDto);
        } catch (Exception e) {
            log.error("检验数据异常消息发送失败：", e);
        }
    }

    @Async
    public void sendData(EapRetreatFeedback eapRetreatFeedback){
        EapRetreatFeedbackDto eapRetreatFeedbackDto = modelMapper.map(eapRetreatFeedback, EapRetreatFeedbackDto.class);
        Equipment equipment = equipmentService.findByCode(eapRetreatFeedbackDto.getMachineName());
        if(null != equipment){
            eapRetreatFeedbackDto.setArea(equipment.getArea());
            eapRetreatFeedbackDto.setSeries(equipment.getSeries());
        }

        // spc 获取切方数据
        MessageDto messageDto = new MessageDto();
        messageDto.setContent(JSON.toJSONString(eapRetreatFeedbackDto));
        messageDto.setGmtCreate(new Date());
        messageDto.setUserCreate(BaseContextHandler.getUsername());
        //todo song
//        fbInspectDataMsgProducer.sendSpcMsgFbMonitorRetreatFeedbackData(JSON.toJSONString(messageDto));
        try {
            log.info("给SPC发送开方数据");
            rabbitMqService.sendMessage(JSON.toJSONString(messageDto), ExchangeEnum.EXCHANGE_SPC_FB_MONITOR_RETREAT_FEEDBACK, QueueEnum.QUEUE_SPC_FB_MONITOR_RETREAT_FEEDBACK, RoutingKey.KEY_SPC_MONITOR_RETREAT_FEEDBACK);
        } catch (Exception e) {
            log.error("开方数据异常消息发送失败：", e);
        }

    }

    @Async
    public void sendData(GrindingBlanking grindingBlanking){
        GrindingBlankingDto grindingBlankingDto = modelMapper.map(grindingBlanking, GrindingBlankingDto.class);
        Equipment equipment = equipmentService.findByCode(grindingBlankingDto.getEquipNo());
        if(null != equipment){
            grindingBlankingDto.setArea(equipment.getArea());
            grindingBlankingDto.setSeries(equipment.getSeries());
        }
        // spc 获取磨削数据
        MessageDto messageDto = new MessageDto();
        messageDto.setContent(JSON.toJSONString(grindingBlankingDto));
        messageDto.setGmtCreate(new Date());
        messageDto.setUserCreate(BaseContextHandler.getUsername());
//        fbInspectDataMsgProducer.sendSpcMsgFbMonitorGrindingBlankingData(JSON.toJSONString(messageDto));
        try {
            log.info("给SPC发送磨削数据");
            rabbitMqService.sendMessage(JSON.toJSONString(messageDto), ExchangeEnum.EXCHANGE_SPC_FB_MONITOR_GRINDING_BLANKING, QueueEnum.QUEUE_SPC_FB_MONITOR_GRINDING_BLANKING, RoutingKey.KEY_SPC_MONITOR_GRINDING_BLANKING);
        } catch (Exception e) {
            log.error("磨削数据异常消息发送失败：", e);
        }

    }

    @ServiceLogs(description = "不抽检下发MCS")
    @Async
    public ResultDto noDetection(NoDetectionDto noDetectionDto) {
        ResultDto resultDto = new ResultDto();
        resultDto.setStatus(200);
        Boolean isFbSizeCheck = noDetectionDto.getIsFbSizeCheck();
        // 不抽检下发mcs
        if (
//                !isFbSizeCheck &&
//                !StringUtils.isEmpty(noDetectionDto.getPoint())
                StringUtils.isNotEmpty(noDetectionDto.getPoint()) && !noDetectionDto.getPoint().equals("无")
        ) {
            resultDto = requestUtils.postForMcs(new RequestDto(noDetectionUrl, noDetectionDto));
            return resultDto;
        }
        resultDto.setMessage(noDetectionDto.getPoint());
        return resultDto;
    }
}
