package com.jay.monitor.data.server.utils;

import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.core.model.response.PageInfoModel;
import com.jay.monitor.data.core.model.response.PageResponseModel;
import com.jay.monitor.data.core.model.serializable.MQDataDTO;
import com.jay.monitor.data.core.model.serializable.SQLMonitorDTO;
import com.jay.monitor.data.core.model.serializable.URLDataDTO;
import com.jay.monitor.data.server.models.entity.JayMonitorMq;
import com.jay.monitor.data.server.models.entity.JayMonitorSql;
import com.jay.monitor.data.server.models.entity.JayMonitorUrl;
import com.jay.monitor.data.server.store.AbstractManager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.HashMap;
import java.util.List;

public class JayDataUtils {
    private static Logger logger = LoggerFactory.getLogger(JayDataUtils.class);

    public static JayMonitorUrl builderJayMonitorUrl(URLDataDTO request) {
        String url = request.getUrl();
        if (StringUtils.isNotEmpty(request.getUrl())) {
            url = url.replaceAll("//", "/");
        }
        JayMonitorUrl monitorUrl = new JayMonitorUrl();
        BeanUtils.copyProperties(request, monitorUrl);
        monitorUrl.setUrl(url);
        monitorUrl.setApplicationName(request.getApplicationName());
        monitorUrl.setApplicationIp(request.getIp());
        monitorUrl.setCreatedTime(request.getCreateDate());
        monitorUrl.setDataGroupName(request.getGroupName());
        monitorUrl.setDataGroupKey(request.getGroupKeyName());
        monitorUrl.setDataId(request.getDataId());
        monitorUrl.setClientIp(request.getClientIp());
        monitorUrl.setUserId(request.getUserId());
        if (request.getRequestTime() != null) {
            monitorUrl.setRequestTime(request.getRequestTime().intValue());
        }
        return monitorUrl;
    }

    public static JayMonitorSql builderJayMonitorSql(SQLMonitorDTO request) {
        JayMonitorSql sqlData = new JayMonitorSql();
        try {
            BeanUtils.copyProperties(request, sqlData);
            sqlData.setApplicationName(request.getApplicationName());
            sqlData.setApplicationIp(request.getIp());
            sqlData.setCreatedTime(request.getCreateDate());
            sqlData.setDataGroupName(request.getGroupName());
            sqlData.setDataGroupKey(request.getGroupKeyName());
            sqlData.setDataId(request.getDataId());
            sqlData.setLogId(request.getLogId());
            sqlData.setInvokeType(request.getCommandType());
            if (request.getRequestTime() != null) {
                sqlData.setRequestTime(request.getRequestTime().intValue());
            }
            sqlData.setAffectedRows(request.getAffectedRows());
            sqlData.setTableName(request.getTable());
            sqlData.setSqlId(request.getSqlId());
        } catch (Exception e) {
            logger.error("builderJayMonitorSql 失败", e);
        }
        return sqlData;
    }

    public static JayMonitorMq builderMonitorMQ(MQDataDTO request) {
        JayMonitorMq mqData = new JayMonitorMq();
        try {
            BeanUtils.copyProperties(request, mqData);
            mqData.setApplicationName(request.getApplicationName());
            mqData.setApplicationIp(request.getIp());
            mqData.setCreatedTime(request.getCreateDate());
            mqData.setDataGroupName(request.getGroupName());
            mqData.setDataGroupKey(request.getGroupKeyName());
            mqData.setDataId(request.getDataId());
            mqData.setLogId(request.getLogId());
            mqData.setInvokeType(request.getInvokeType().name());
            mqData.setRequestTime(request.getRequestTime());
        } catch (Exception e) {
            logger.error("builderJayMonitorMq", e);
        }
        return mqData;
    }

    public static <R> PageResponseModel<R> builderPageResponseModel(Integer pageNo, Integer pageSize, Integer total,
        List<R> list) {
        PageResponseModel<R> pageResponseModel = new PageResponseModel<>();
        PageInfoModel<R> pageInfoModel = new PageInfoModel<>();
        pageInfoModel.setResultSet(list);
        pageInfoModel.setCount(pageNo);
        pageInfoModel.setPageSize(pageSize);
        pageInfoModel.setTotal(total);
        pageResponseModel.setPageModel(pageInfoModel);
        return pageResponseModel;
    }

    public static HashMap builderInitMap(List<? extends AbstractManager> storeProcessList) {
        HashMap processMap = new HashMap<>();
        if (storeProcessList != null && storeProcessList.size() > 0) {
            storeProcessList.forEach((process) -> {
                MsgTypeEnums msgTypeEnums = process.msgTypeEnums();
                processMap.put(msgTypeEnums, process);
            });
        }
        return processMap;
    }

    public static String getTableName(String tableName, String date) {
        return tableName + "_" + date;
    }

}
