package com.inspect.metadata.montdatamqhandle.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.constant.Color;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.metadata.montdata.domain.BasedataMontTaskStaData;
import com.inspect.metadata.montdata.service.IBasedataMontTaskStaDataService;
import com.inspect.metadata.montdatamqhandle.service.IBasedataMontDataHandleService;
import com.inspect.metadata.mqutil.FeignMsgClient;
import com.inspect.base.websocket.configure.WsPool;
import com.inspect.base.core.utils.DateUtils;

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

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component("basedataMont-41")
public class BasedataMontTaskStaDataConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontTaskStaDataService basedataMontTaskStaDataService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private FeignMsgClient feignMsgClient;

    public void handleMontData(JSONObject jsonObject) {
        log.info(Color.BLUE + "handleMontData-41: {}" + Color.END, jsonObject.toString());
        String Type = jsonObject.getString("Type");
        JSONArray jsonObjectItems = jsonObject.getJSONArray("Items");
        List<BasedataMontTaskStaData> list = jsonObjectItems.toJavaList(BasedataMontTaskStaData.class);
        if (null != list && !list.isEmpty()) {
            Date nowDate = DateUtils.getNowDate();
            for (BasedataMontTaskStaData basedataMontTaskstadata : list) {
                basedataMontTaskstadata.setCreateTime(nowDate);
                basedataMontTaskStaDataService.insertBasedataMontTaskStaData(basedataMontTaskstadata);
                basedataMontTaskstadata.setCreateTime(null);
            }

            feignMsgClient.sendMessageToMainService(Type, jsonObject.toJSONString());
        }
    }

    public void parseMessage(String order) {
        WsPool.sendMessageToAll(order);
    }
}
