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.BasedataMontNestRunData;
import com.inspect.metadata.montdata.montenum.NestRunDataEnum;
import com.inspect.metadata.montdata.service.IBasedataMontNestRunDataService;
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-10004")
public class BasedataMontNestRunDataConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontNestRunDataService basedataMontNestRunDataService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private RedisService redisService;
    @Resource
    private FeignMsgClient feignMsgClient;

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

            String value_unit;
            while (iterator.hasNext()) {
                BasedataMontNestRunData basedataMontNestrundata = iterator.next();
                BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
                basedataEqpbook.setPatrolDeviceCode(basedataMontNestrundata.getNestCode());
                List<BasedataEqpBook> basedataEqpBookList = basedataEqpBookService.selectBasedataEqpBookList(basedataEqpbook);
                if (!basedataEqpBookList.isEmpty()) {
                    basedataMontNestrundata.setEqpBookId(basedataEqpBookList.get(0).getEqpBookId());
                }

                String type = basedataMontNestrundata.getType();
                basedataMontNestrundata.setTypeName(NestRunDataEnum.getTargetValueByTypeCode(type));
                if ("2".equals(type) || "3".equals(type)) {
                    String value = basedataMontNestrundata.getValue();
                    value_unit = NestRunDataEnum.getTargetValueByTypeCode(type + "-" + value);
                    basedataMontNestrundata.setValueName(value_unit);
                    basedataMontNestrundata.setValueUnit(value_unit);
                }

                basedataMontNestrundata.setCreateTime(nowDate);
                basedataMontNestRunDataService.insertBasedataMontNestRunData(basedataMontNestrundata);
                basedataMontNestrundata.setCreateTime(null);
            }

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

        }
    }

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

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

        return objectMap;
    }
}
