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.BasedataMontEvnData;
import com.inspect.metadata.montdata.montenum.MicroMeteInfoEnum;
import com.inspect.metadata.montdata.service.IBasedataMontEvnDataService;
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-21")
public class BasedataMontEvnDataConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontEvnDataService basedataMontEvnDataService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private RedisService redisService;
    @Resource
    private FeignMsgClient feignMsgClient;

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

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

                basedataMontEvndata.setCreateTime(nowDate);
                basedataMontEvndata.setTypeName(MicroMeteInfoEnum.getTargetValueByTypeCode(basedataMontEvndata.getType()));
                this.basedataMontEvnDataService.insertBasedataMontEvnData(basedataMontEvndata);
                basedataMontEvndata.setCreateTime(null);
            }

            HashMap<String, ArrayList<BasedataMontEvnData>> patrolDeviceDataMap = this.getPatrolDeviceData(list);
            String sendCode = jsonObject.getString("SendCode");
            String dateTimeNow = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");

            for (String itemKey : patrolDeviceDataMap.keySet()) {
                JSONObject returnJsonObject = new JSONObject();
                JSONObject montJsonObject = new JSONObject();
                montJsonObject.put("SendCode", sendCode);
                montJsonObject.put("msgType", "montEvnInfo");
                montJsonObject.put("Items", patrolDeviceDataMap.get(itemKey));
                returnJsonObject.put("data", montJsonObject);
                returnJsonObject.put("receiveTime", dateTimeNow);
                returnJsonObject.put("event", "mont/montEvnInfo");
                String jsonString = JSON.toJSONString(returnJsonObject);
                this.redisService.setCacheObject("mont_evn_info:" + itemKey, returnJsonObject);
                this.parseMessage(jsonString);
                System.out.println("发送环境信息到websocket mont/montEvnInfo:::" + jsonString);
            }

        }
    }

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

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

        return objectMap;
    }

    public static void main(String[] args) {
        new BasedataMontEvnDataConsumerHandle();
    }

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