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.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.metadata.montdata.domain.BasedataMontTaskResult;
import com.inspect.metadata.montdata.service.IBasedataMontTaskResultService;
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-61")
public class BasedataMontTaskResultConsumerHandle implements IBasedataMontDataHandleService {

    @Resource
    private IBasedataMontTaskResultService basedataMontTaskResultService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private FeignMsgClient feignMsgClient;

    public void handleMontData(JSONObject jsonObject) {
        log.info(Color.GREEN + "handleMontData-61: {}" + Color.END, jsonObject.toString());
        String Type = jsonObject.getString("Type");
        JSONArray jsonObjectItems = jsonObject.getJSONArray("Items");
        List<BasedataMontTaskResult> list = jsonObjectItems.toJavaList(BasedataMontTaskResult.class);
        if (null != list && !list.isEmpty()) {
            Date nowDate = DateUtils.getNowDate();
            for (BasedataMontTaskResult basedataMontTaskResult : list) {
                BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
                basedataEqpbook.setPatrolDeviceCode(basedataMontTaskResult.getPatrolDeviceCode());
                List<BasedataEqpBook> basedataEqpBookList = this.basedataEqpBookService.selectBasedataEqpBookList(basedataEqpbook);
                if (!basedataEqpBookList.isEmpty()) {
                    basedataMontTaskResult.setEqpBookId(basedataEqpBookList.get(0).getEqpBookId());
                }

                basedataMontTaskResult.setCreateTime(nowDate);
                this.basedataMontTaskResultService.insertBasedataMontTaskResult(basedataMontTaskResult);
                basedataMontTaskResult.setCreateTime(null);
            }

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

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