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.BasedataMontPatDevPatRoute;
import com.inspect.metadata.montdata.service.IBasedataMontPatDevPatRouteService;
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-4")
public class BasedataMontPatDevPatRouteConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontPatDevPatRouteService basedataMontPatDevPatRouteService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    RedisService redisService;
    @Resource
    private FeignMsgClient feignMsgClient;

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

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

                basedataMontPatdevpatroute.setCreateTime(nowDate);
                this.basedataMontPatDevPatRouteService.insertBasedataMontPatDevPatRoute(basedataMontPatdevpatroute);
                basedataMontPatdevpatroute.setCreateTime(null);
            }

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

            for (String itemKey : patrolDeviceDataMap.keySet()) {
                JSONObject returnJsonObject = new JSONObject();
                JSONObject montJsonObject = new JSONObject();
                montJsonObject.put("SendCode", sendCode);
                montJsonObject.put("msgType", "montPatDevPatRoute");
                montJsonObject.put("Items", patrolDeviceDataMap.get(itemKey));
                returnJsonObject.put("data", montJsonObject);
                String dateTimeNow = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
                returnJsonObject.put("receiveTime", dateTimeNow);
                returnJsonObject.put("event", "mont/montPatDevPatRoute");
                String jsonString = JSON.toJSONString(returnJsonObject);
                this.redisService.setCacheObject("mont_patdev_patroute:" + itemKey, returnJsonObject);
                this.parseMessage(jsonString);
                System.out.println("巡视设备巡检路线信息 websocket mont/montPatDevPatRoute:::" + jsonString);
            }

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

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

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

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

        return objectMap;
    }
}
