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.BasedataMontPatDevCoord;
import com.inspect.metadata.montdata.domain.BasedataMontPatDevPatRoute;
import com.inspect.metadata.montdata.service.IBasedataMontPatDevCoordService;
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-3")
public class BasedataMontPatDevCoordConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontPatDevCoordService basedataMontPatDevCoordService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private RedisService redisService;
    @Resource
    private FeignMsgClient feignMsgClient;

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

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

                basedataMontPatdevcoord.setCreateTime(nowDate);
                this.basedataMontPatDevCoordService.insertBasedataMontPatDevCoord(basedataMontPatdevcoord);
                basedataMontPatdevcoord.setCreateTime((Date) null);
            }

            HashMap<String, ArrayList<BasedataMontPatDevCoord>> 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", "montPatrolDeviceCoord");
                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/montPatrolDeviceCoord");
                String jsonString = JSON.toJSONString(returnJsonObject);
                this.redisService.setCacheObject("mont_patrol_device_coord:" + itemKey, returnJsonObject);
                this.parseMessage(jsonString);
                System.out.println("巡视设备坐标信息 websocket mont/montPatrolDeviceCoord:::" + jsonString);
            }

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

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

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

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

        return objectMap;
    }
}
