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

import com.alibaba.fastjson.JSON;
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.BasedataMontPatDevAlmAbn;
import com.inspect.metadata.montdata.service.IBasedataMontPatDevAlmAbnService;
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 com.inspect.base.redis.service.RedisService;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

import javax.annotation.Resource;

@Slf4j
@Component("basedataMont-5")
public class BasedataMontPatDevAlmAbnConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontPatDevAlmAbnService basedataMontPatDevAlmAbnService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    RedisService redisService;
    @Resource
    private FeignMsgClient feignMsgClient;

    public void handleMontData(JSONObject jsonObject) {
        log.info("handleMontData-5: {}", jsonObject.toString());
        JSONArray jsonObjectItems = jsonObject.getJSONArray("Items");
        List<BasedataMontPatDevAlmAbn> list = jsonObjectItems.toJavaList(BasedataMontPatDevAlmAbn.class);
        if (null != list && !list.isEmpty()) {
            String Type = jsonObject.getString("Type");
            Date nowDate = DateUtils.getNowDate();

            for (BasedataMontPatDevAlmAbn basedataMontPatdevalmabn : list) {
                BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
                basedataEqpbook.setPatrolDeviceCode(basedataMontPatdevalmabn.getPatrolDeviceCode());
                List<BasedataEqpBook> basedataEqpBookList = this.basedataEqpBookService.selectBasedataEqpBookList(basedataEqpbook);
                if (!basedataEqpBookList.isEmpty()) {
                    basedataMontPatdevalmabn.setEqpBookId(basedataEqpBookList.get(0).getEqpBookId());
                }

                basedataMontPatdevalmabn.setCreateTime(nowDate);
                this.basedataMontPatDevAlmAbnService.insertBasedataMontPatDevAlmAbn(basedataMontPatdevalmabn);
                basedataMontPatdevalmabn.setCreateTime(null);
            }

            HashMap<String, ArrayList<BasedataMontPatDevAlmAbn>> patrolDeviceDataMap = this.getPatrolDeviceData(list);
            String sendCode = jsonObject.getString("SendCode");

            for (String itemKey : patrolDeviceDataMap.keySet()) {
                JSONObject returnJsonObject = new JSONObject();
                JSONObject jsonObjectData = new JSONObject();
                jsonObjectData.put("SendCode", String.valueOf(jsonObject.get("SendCode")));
                jsonObjectData.put("msgType", "montPatrolDeviceAlmabnData");
                jsonObjectData.put("Items", patrolDeviceDataMap.get(itemKey));
                returnJsonObject.put("data", jsonObjectData);
                String dateTimeNow = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
                returnJsonObject.put("receiveTime", dateTimeNow);
                returnJsonObject.put("event", "mont/montPatrolDeviceAlmabnData");
                String jsonString = JSON.toJSONString(returnJsonObject);
                this.redisService.setCacheObject("mont_patrol_device_almabn:" + itemKey, returnJsonObject);
                this.parseMessage(jsonString);
                System.out.println("巡视设备异常报警到 websocket mont/montPatrolDeviceAlmabnData:::" + jsonString);
            }

            this.feignMsgClient.sendMessageToUpperSystem(Type, jsonObject.toJSONString());
            this.feignMsgClient.sendAbnormalMessageToTask(Type, jsonObject.toJSONString());
        }
    }

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

    private HashMap<String, ArrayList<BasedataMontPatDevAlmAbn>> getPatrolDeviceData(List<BasedataMontPatDevAlmAbn> list) {
        HashMap<String, ArrayList<BasedataMontPatDevAlmAbn>> objectMap = new HashMap<>();

        for (BasedataMontPatDevAlmAbn BasedataMontPatdevalmabn : list) {
            String patrolDeviceCode = BasedataMontPatdevalmabn.getPatrolDeviceCode();
            ArrayList dataList;
            if (objectMap.containsKey(patrolDeviceCode)) {
                dataList = objectMap.get(patrolDeviceCode);
                dataList.add(BasedataMontPatdevalmabn);
            } else {
                dataList = new ArrayList();
                dataList.add(BasedataMontPatdevalmabn);
                objectMap.put(patrolDeviceCode, dataList);
            }
        }

        return objectMap;
    }
}
