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.BasedataMontPatDevRunData;
import com.inspect.metadata.montdata.montenum.PatrolDeviceRunDataEnum;
import com.inspect.metadata.montdata.service.IBasedataMontPatDevRunDataService;
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-2")
public class BasedataMontPatDevRunDataConsumerHandle implements IBasedataMontDataHandleService {
    @Resource
    private IBasedataMontPatDevRunDataService basedataMontPatDevRunDataService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    @Resource
    private FeignMsgClient feignMsgClient;
    @Resource
    RedisService redisService;

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

            for (BasedataMontPatDevRunData basedataMontPatdevrundata : list) {
                basedataMontPatdevrundata.setTypeName(PatrolDeviceRunDataEnum.getTargetValueByTypeCode(basedataMontPatdevrundata.getType()));
                BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
                basedataEqpbook.setPatrolDeviceCode(basedataMontPatdevrundata.getPatrolDeviceCode());
                List<BasedataEqpBook> basedataEqpBookList = this.basedataEqpBookService.selectBasedataEqpBookList(basedataEqpbook);
                if (!basedataEqpBookList.isEmpty()) {
                    basedataMontPatdevrundata.setEqpBookId(basedataEqpBookList.get(0).getEqpBookId());
                }

                basedataMontPatdevrundata.setCreateTime(nowDate);
                this.basedataMontPatDevRunDataService.insertBasedataMontPatDevRunData(basedataMontPatdevrundata);
                basedataMontPatdevrundata.setCreateTime(null);
            }

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

        }
    }

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

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

        for (BasedataMontPatDevRunData 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;
    }
}
