package com.persagy.proxy.migration.model;

import cn.hutool.core.collection.CollUtil;
import lombok.Builder;
import lombok.Data;

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

/**
 * @author lvxianyun
 * @title: DataMigrationResponse
 * @projectName adm-middleware
 * @date 2021/12/15 17:09
 */
@Data
@Builder
public class DataMigrationResponse {

    public final static String SUCCESS = "success";
    public final static String FAILURE = "failure";
    /** 结果 */
    private String result;

    List<?> data;
    List<String> errorMsg;

    public void addData(List list){
        if(data == null){
            data = new ArrayList<>();
        }
        if(CollUtil.isNotEmpty(list)){
            data.addAll(list);
        }
    }

    public void addError(List<String> error){
        if(errorMsg == null){
            result = FAILURE;
            errorMsg = new ArrayList<>();
        }
        if(CollUtil.isNotEmpty(error)){
            errorMsg.addAll(error);
        }
    }


    public static DataMigrationResponse success(List<?> data) {
        return new DataMigrationResponse(SUCCESS,CollUtil.isEmpty(data) ? new ArrayList<>(1): data,null);
    }

    public static DataMigrationResponse success() {
        return new DataMigrationResponse(SUCCESS,null,null);
    }

    public static DataMigrationResponse error(String errorMsg) {
        return new DataMigrationResponse(FAILURE,null, Arrays.asList(errorMsg));
    }
}
