package com.weaver.seconddev.interfaces.workflow.action;

import java.lang.*;
import java.util.*;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import java.util.Map;
import java.util.HashMap;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 财务客户主数据--操作--libin20171128
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_ZOA_FI_CONUNIT_REDUNCHECKAction")
public class ZOA_FI_CONUNIT_REDUNCHECKAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;

    private static final Logger logger_7e39b657 = LoggerFactory.getLogger(ZOA_FI_CONUNIT_REDUNCHECKAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_7e39b657.error("ZOA_FI_CONUNIT_REDUNCHECKAction start");
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            // 流程请求ID
            String reid = request.getRequestid();
            try {
                // 申请业务
                String sqyw = "";
                String[] prdetail = new String[6];
                // 获取表单主字段信息
                Property[] properties = request.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    // 主表数据
                    // 字段名
                    String name = properties[i].getName().toUpperCase();
                    // 值
                    String value = Util.null2String(properties[i].getValue());
                    // 申请业务
                    if (name.equals("SQYW")) {
                        sqyw = value;
                    }
                    // 公司代码
                    if (name.equals("GSDM")) {
                        prdetail[0] = value;
                    }
                    // 供应商或债权人的账号
                    if (name.equals("GYSDM")) {
                        prdetail[1] = value;
                        prdetail[5] = "1";
                        prdetail[2] = "";
                    }
                    // 客户编号
                    if (name.equals("KHDM")) {
                        prdetail[2] = value;
                        prdetail[5] = "2";
                        prdetail[1] = "";
                    }
                    // 纳税登记证代码
                    if (name.equals("NSDJZDMGH")) {
                        prdetail[3] = value;
                    }
                    // 名称
                    if (name.equals("MCGH")) {
                        prdetail[4] = value;
                    }
                }
                logger_7e39b657.error(String.valueOf("ZOA_FI_CONUNIT_REDUNCHECKAction log----sqyw=" + sqyw));
                if ("0".equals(sqyw)) {
                    // 申请业务为新增时 查重判断
                    ZOA_FI_CONUNIT_REDUNCHECK po = new ZOA_FI_CONUNIT_REDUNCHECK();
                    String[][] fhz = po.Getexecute(prdetail);
                    String sta = fhz[0][0];
                    String meg = fhz[0][1].replace("'", "’");
                    logger_7e39b657.error(String.valueOf("ZOA_FI_CONUNIT_REDUNCHECKAction log----sta=" + sta + ",meg=" + meg));
                    if (!sta.equals("S")) {
                        request.getRequestManager().setMessageid("100001");
                        request.getRequestManager().setMessagecontent("返回消息----" + meg);
                        return  WeaResult.fail(500, "ZOA_FI_CONUNIT_REDUNCHECKAction meg"+ meg, getResultMapForAction(weaverResultMap, "result",meg, request.getRequestManager()));

                    }
                } else {
                    logger_7e39b657.error(String.valueOf("ZOA_FI_CONUNIT_REDUNCHECKAction log----不需要执行接口"));
                }
            } catch (Exception e) {
                logger_7e39b657.error(String.valueOf("start log----" + reid));
                logger_7e39b657.error(String.valueOf("------------------------------------------------------------------------"));
                logger_7e39b657.error(String.valueOf("ZOA_FI_CONUNIT_REDUNCHECKAction error:" + e.toString()));
                logger_7e39b657.error(String.valueOf("------------------------------------------------------------------------"));
                logger_7e39b657.error(String.valueOf("end log"));
                return  WeaResult.fail(500, "ZOA_FI_CONUNIT_REDUNCHECKAction err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));

            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_7e39b657.error("ZOA_FI_CONUNIT_REDUNCHECKAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_FI_CONUNIT_REDUNCHECKAction e {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
        }
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {
        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }
}
