package com.jay.monitor.data.server.store.mysql;

import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.server.models.entity.JayMonitorUrl;
import com.jay.monitor.data.server.models.request.trace.URLRequest;
import com.jay.monitor.data.server.models.response.trace.URLTraceResponse;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class MysqlURLSearchProcess extends AbstractMysqlSearchProcess<JayMonitorUrl, URLRequest, URLTraceResponse> {

    @Override
    public List<URLTraceResponse> builderResponse(List<JayMonitorUrl> resultList) throws Exception {
        List<URLTraceResponse> responseList = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {
            URLTraceResponse traceResponse = new URLTraceResponse();
            JayMonitorUrl jayMonitorUrl = resultList.get(i);
            BeanUtils.copyProperties(jayMonitorUrl, traceResponse);
            traceResponse.setCreateDate(jayMonitorUrl.getCreatedTime());
            traceResponse.setIp(jayMonitorUrl.getApplicationIp());
            traceResponse.setGroupKeyName(jayMonitorUrl.getDataGroupKey());
            traceResponse.setGroupName(jayMonitorUrl.getDataGroupName());
            responseList.add(traceResponse);
        }
        return responseList;
    }

    @Override
    public JayMonitorUrl builderQuery(URLRequest query) throws Exception {
        JayMonitorUrl jayMonitorUrl = new JayMonitorUrl();
        BeanUtils.copyProperties(query, jayMonitorUrl);
        jayMonitorUrl.setDataGroupKey(query.getGroupKeyName());
        jayMonitorUrl.setDataGroupName(query.getGroupName());
        jayMonitorUrl.setApplicationIp(query.getApplicationIp());
        return jayMonitorUrl;
    }

    @Override
    public MsgTypeEnums msgTypeEnums() {
        return MsgTypeEnums.URL;
    }
}
