package com.weaver.seconddev.chapanda.esign.esb;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.esign.entity.OkHttpDto;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignReminderRequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("Esb2EsignReminderRequestAction")
public class Esb2EsignReminderRequestAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2EsignReminderRequestAction.class);

    @Autowired
    Esb2EsignReminderRequestUtil esb2EsignReminderRequestUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        String requestid = String.valueOf(params.get("requestid"));
        String form_data_id = String.valueOf(params.get("form_data_id"));
        String flowId = String.valueOf(params.get("flowId"));

        log.error("flowId:{}",flowId);
        log.error("requestid:{}",requestid);

        if(StringUtils.isBlank(flowId)){
            return WeaResult.fail(500,"入参参数flowId为空");
        }

        int errCode = 0;
        if(StringUtils.isNotBlank(flowId)){
            OkHttpDto okHttpDto = esb2EsignReminderRequestUtil.reminderRequest(flowId);

            if(okHttpDto.getResponseCode() == 200){
                errCode = okHttpDto.getDataCode();
                if(errCode != 0){
                    return WeaResult.fail(500,"流程催办失败,"+okHttpDto.getDataMsg());
                }
            }else{
                return WeaResult.fail(500,"流程催办接口异常,"+okHttpDto.getDataMsg());
            }
        }

//        if(!"0".equals(errCode)){
//            return WeaResult.fail(500,"流程催办失败");
//        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("requestid",requestid);
        actionMap.put("form_data_id",form_data_id);
        actionMap.put("flowId",flowId);
        actionMap.put("errCode",errCode);
        return WeaResult.success(actionMap);
    }
}
