package com.jwipc.api.interceptor;

import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import com.dtflys.forest.reflection.ForestMethod;
import com.jwipc.api.dto.MesBaseParamDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Base64;


/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class MesInterceptor implements Interceptor<String> {
    @Value("${sys.mes-user}")
    private String name;
    @Value("${sys.mes-pw}")
    private String password;
    @Value("${sys.mes-sndprn}")
    private String SNDPRN;
    @Value("${sys.mes-rcvprn}")
    private String RCVPRN;


    @Override
    public boolean beforeExecute(ForestRequest request) {
        final String encodeToString = Base64.getEncoder().encodeToString((name + ":" + password).getBytes());
        String authHeader = "Basic " + encodeToString;
        request.addHeader("Authorization", authHeader);
        return true;
    }

    @Override
    public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
        if (args[0] instanceof MesBaseParamDTO) {
            final MesBaseParamDTO arg = (MesBaseParamDTO) args[0];
            arg.getIS_REQ().SNDPRN = SNDPRN;
            arg.getIS_REQ().RCVPRN = RCVPRN;
            arg.getIS_REQ().REQUSER = "";
            if(StringUtils.isEmpty(arg.getIS_REQ().NOTE1)){
                arg.getIS_REQ().NOTE1 = "";
            }
            arg.getIS_REQ().NOTE2 = "";
            arg.getIS_REQ().NOTE3 = "";
        }
    }
}
