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.BasedataMontPatDevStaData;
import com.inspect.metadata.montdata.montenum.PatrolDeviceStaDataEnum;
import com.inspect.metadata.montdata.service.IBasedataMontPatDevStaDataService;
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-1")
public class BasedataMontPatDevStaDataConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontPatDevStaDataService basedataMontPatDevStaDataService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private FeignMsgClient feignMsgClient;
    @Resource
    RedisService redisService;

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

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

                String type = basedataMontPatdevstadata.getType();
                basedataMontPatdevstadata.setTypeName(PatrolDeviceStaDataEnum.getTargetValueByTypeCode(type));
                String value = basedataMontPatdevstadata.getValue();
                basedataMontPatdevstadata.setValueName(PatrolDeviceStaDataEnum.getTargetValueByTypeCode(type + "-" + value));
                basedataMontPatdevstadata.setCreateTime(nowDate);
                this.basedataMontPatDevStaDataService.insertBasedataMontPatDevStaData(basedataMontPatdevstadata);
                basedataMontPatdevstadata.setCreateTime(null);
            }

            HashMap<String, ArrayList<BasedataMontPatDevStaData>> 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", "montPatrolDeviceStaData");
                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/montPatrolDeviceStaData");
                String jsonString = JSON.toJSONString(returnJsonObject);
                this.redisService.setCacheObject("mont_patrol_device_stadata:" + itemKey, returnJsonObject);
                this.parseMessage(jsonString);
                System.out.println("巡视设备状态数据信息 websocket mont/montPatrolDeviceStaData:::" + jsonString);
            }
        }
    }

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

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

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

        return objectMap;
    }
}
