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

import afu.org.checkerframework.checker.oigj.qual.O;
import com.cat.file.message.internal.DefaultMessageTree;
import com.jay.monitor.data.core.model.serializable.base.BaseDTO;
import com.jay.monitor.data.core.model.serializable.base.ErrorInfoData;
import com.jay.monitor.data.core.model.serializable.base.JsonTextData;
import com.jay.monitor.data.server.costs.ServerConstants;
import com.jay.monitor.data.server.store.AbstractStoreProcess;
import com.jay.monitor.data.server.utils.DataUtils;
import org.apache.commons.beanutils.BeanUtils;

import java.util.List;

public abstract class BigTextStoreProcess<T extends BaseDTO, E> extends AbstractStoreProcess<T, E> {

    @Override
    protected void storeAfter(List<T> requestList, List<E> resultList, boolean success) throws Exception {
        super.storeAfter(requestList, resultList, success);

        // 记录一些大文本数据
        for (int i = 0; i < resultList.size(); i++) {
            T request = requestList.get(i);
            E result = resultList.get(i);
            String id = BeanUtils.getProperty(result, "id");
            if (request instanceof JsonTextData) {
                DefaultMessageTree messageTree = DataUtils.getJsonContentMessageTree(request, Integer.valueOf(id));
                if (messageTree != null) {
                    request.getExtMap().put(ServerConstants.JSON_LOG_ID, messageTree.getFormatMessageId());
                    this.messageManagerProcess.insert(messageTree);
                }
            }

            if (request instanceof ErrorInfoData) {
                DefaultMessageTree messageTree = DataUtils.getErrorMessageTree(request, Integer.valueOf(id));
                if (messageTree != null) {
                    request.getExtMap().put(ServerConstants.ERROR_LOG_ID, messageTree.getFormatMessageId());
                    this.messageManagerProcess.insert(messageTree);
                }
            }
        }
    }

    /**
     * 根据信息编号查询请求内容信息
     *
     * @param date
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public String searchContentById(String date, String id) throws Exception {
        E e = getMapper().selectPartitionById(date, Integer.valueOf(id));
        String applicationName = org.apache.commons.beanutils.BeanUtils.getProperty(e, "applicationName");
        return getMessage(e, applicationName);
    }
}
