package com.foreverwin.mesnac.equip.consumer;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.equip.dto.MQResponseDto;
import com.foreverwin.mesnac.equip.service.EdcDataResourceService;
import com.foreverwin.mesnac.equip.service.ResourceFaultReceiveService;
import com.foreverwin.mesnac.equip.service.ResourceStatusReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 设备接口业务处理
 *
 *
 * @author Leon
 * @date 2021-07-21
 */
@Component
public class ResourceMQProcess {

    @Autowired
    private ResourceStatusReceiveService resourceStatusReceiveService;

    @Autowired
    private ResourceFaultReceiveService resourceFaultReceiveService;

    @Autowired
    private EdcDataResourceService edcDataResourceService;

    /**
     * 设备状态变更
     *
     * @param text
     * @return
     */
    public String resourceStatusProcess(String text) {
        MQResponseDto mqResponseDto = new MQResponseDto();
        try {
            String handle = resourceStatusReceiveService.updateResourceStatusByMq(text);
            mqResponseDto.setHANDLE(handle);
            mqResponseDto.setSTATUS("Success");
            mqResponseDto.setMESSAGE("设备状态修改成功！");
        } catch (Exception e) {
            mqResponseDto.setSTATUS("Error");
            mqResponseDto.setMESSAGE("设备状态修改失败：" + e.getMessage());
        }
        return JSONObject.toJSONString(mqResponseDto);
    }

    /**
     * 设备故障信息记录
     * @param text
     * @return
     */
    public String resourceFaultProcess(String text){
        MQResponseDto mqResponseDto = new MQResponseDto();
        try {
            String handle = resourceFaultReceiveService.parseResourceFaultInfo(text);
            mqResponseDto.setHANDLE(handle);
            mqResponseDto.setSTATUS("Success");
            mqResponseDto.setMESSAGE("设备状态修改成功！");
        } catch (Exception e) {
            mqResponseDto.setSTATUS("Error");
            mqResponseDto.setMESSAGE("设备状态修改失败：" + e.getMessage());
        }
        return JSONObject.toJSONString(mqResponseDto);
    }
    /**
     * 设备参数信息记录
     * @param text
     * @return
     */
    public String resourceParamProcess(String text){
        MQResponseDto mqResponseDto = new MQResponseDto();
        try {
            String handle = edcDataResourceService.saveResourceParamByMq(text);
            mqResponseDto.setHANDLE(handle);
            mqResponseDto.setSTATUS("Success");
            mqResponseDto.setMESSAGE("设备状态修改成功！");
        } catch (Exception e) {
            mqResponseDto.setSTATUS("Error");
            mqResponseDto.setMESSAGE("设备状态修改失败：" + e.getMessage());
        }
        return JSONObject.toJSONString(mqResponseDto);
    }

}
