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.BasedataMontNestStaData;
import com.inspect.metadata.montdata.montenum.NestStaDataEnum;
import com.inspect.metadata.montdata.service.IBasedataMontNestStaDataService;
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.Iterator;
import java.util.List;

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

import javax.annotation.Resource;

@Slf4j
@Component("basedataMont-20001")
public class BasedataMontNestStaDataConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontNestStaDataService basedataMontNestStaDataService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private FeignMsgClient feignMsgClient;
    @Resource
    private RedisService redisService;

    public void handleMontData(JSONObject jsonObject) {
        log.info("handleMontData-20001: {}", jsonObject.toString());
        String Type = jsonObject.getString("Type");
        JSONArray jsonObjectItems = jsonObject.getJSONArray("Items");
        List<BasedataMontNestStaData> list = jsonObjectItems.toJavaList(BasedataMontNestStaData.class);
        if (null != list && !list.isEmpty()) {
            Date nowDate = DateUtils.getNowDate();
            Iterator<BasedataMontNestStaData> iterator = list.iterator();
            String dateTimeNow;
            while (iterator.hasNext()) {
                BasedataMontNestStaData basedataMontNeststadata = iterator.next();
                BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
                basedataEqpbook.setPatrolDeviceCode(basedataMontNeststadata.getNestCode());
                List<BasedataEqpBook> basedataEqpBookList = this.basedataEqpBookService.selectBasedataEqpBookList(basedataEqpbook);
                if (!basedataEqpBookList.isEmpty()) {
                    basedataMontNeststadata.setEqpBookId(basedataEqpBookList.get(0).getEqpBookId());
                }

                String type = basedataMontNeststadata.getType();
                basedataMontNeststadata.setTypeName(NestStaDataEnum.getTargetValueByTypeCode(type));
                String value = basedataMontNeststadata.getValue();
                dateTimeNow = NestStaDataEnum.getTargetValueByTypeCode(type + "-" + value);
                basedataMontNeststadata.setValueName(dateTimeNow);
                basedataMontNeststadata.setValueUnit(dateTimeNow);
                basedataMontNeststadata.setCreateTime(nowDate);
                this.basedataMontNestStaDataService.insertBasedataMontNestStaData(basedataMontNeststadata);
                basedataMontNeststadata.setCreateTime(null);
            }

            HashMap<String, ArrayList<BasedataMontNestStaData>> 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", "montNestStaData");
                montJsonObject.put("Items", patrolDeviceDataMap.get(itemKey));
                returnJsonObject.put("data", montJsonObject);
                dateTimeNow = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
                returnJsonObject.put("receiveTime", dateTimeNow);
                returnJsonObject.put("event", "mont/montNestStaData");
                String jsonString = JSON.toJSONString(returnJsonObject);
                this.redisService.setCacheObject("mont_nest_stadata:" + itemKey, returnJsonObject);
                this.parseMessage(jsonString);
                System.out.println("机巢状态数据到websocket mont/montNestStaData:::" + jsonString);
            }

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

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

    private HashMap<String, ArrayList<BasedataMontNestStaData>> getPatrolDeviceData(List<BasedataMontNestStaData> list) {
        HashMap<String, ArrayList<BasedataMontNestStaData>> objectMap = new HashMap<>();
        for (BasedataMontNestStaData BasedataMontNeststadata : list) {
            String patrolDeviceCode = BasedataMontNeststadata.getNestCode();
            ArrayList dataList;
            if (objectMap.containsKey(patrolDeviceCode)) {
                dataList = objectMap.get(patrolDeviceCode);
                dataList.add(BasedataMontNeststadata);
            } else {
                dataList = new ArrayList();
                dataList.add(BasedataMontNeststadata);
                objectMap.put(patrolDeviceCode, dataList);
            }
        }

        return objectMap;
    }
}
