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

import  com.weaver.seconddev.interfaces.easoa.DelayDateService;
import  com.weaver.seconddev.interfaces.easoa.DelayDateSyncServiceStub;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
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.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.soa.workflow.request.*;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_DelayDateSyncAction")
public class DelayDateSyncAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private DelayDateService service;
    private static final Logger logger_46fa23c3 = LoggerFactory.getLogger(DelayDateSyncAction.class);

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        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 requestInfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = requestInfo.getRequestid();
            logger_46fa23c3.error(String.valueOf("DelayDateSyncAction--" + requestid + "--start"));
            try {
                String companyCode = "";
                String workcode = "";
                String lcbh = "";
                Property[] properties = requestInfo.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    // 主表数据
                    // 字段名
                    String name = properties[i].getName().toUpperCase();
                    // 值
                    String value = Util.null2String(properties[i].getValue());
                    if ("LCBH".equals(name)) {
                        lcbh = value;
                    }
                    if ("TBR".equals(name)) {
                        ResourceComInfo rc = I18nContextUtil.getBean(ResourceComInfo.class);
                        workcode = rc.getWorkcode(value);
                        logger_46fa23c3.error(String.valueOf("DelayDateSyncAction--" + requestid + "--" + workcode));
                    }
                    if ("GSBM".equals(name)) {
                        companyCode = value;
                    }
                }
                DetailTable[] detailtable = requestInfo.getDetailTableInfo().getDetailTable();
                // 指定明细表
                DetailTable dtq = detailtable[0];
                Row[] sq = dtq.getRow();
                DelayDateSyncServiceStub.SyncDelayDateInfo info = new DelayDateSyncServiceStub.SyncDelayDateInfo();
                DelayDateSyncServiceStub.DelayDateInfo[] items = new DelayDateSyncServiceStub.DelayDateInfo[sq.length];
                for (int j = 0; j < sq.length; j++) {
                    // 指定行
                    Row rq = sq[j];
                    // 每行数据再按列存储
                    Cell[] cq = rq.getCell();
                    DelayDateSyncServiceStub.DelayDateInfo param = new DelayDateSyncServiceStub.DelayDateInfo();
                    for (int k = 0; k < cq.length; k++) {
                        // 指定列
                        Cell cq1 = cq[k];
                        // 明细字段名称
                        String nameq = cq1.getName().toUpperCase();
                        // 明细字段的值
                        String valueq = Util.null2String(cq1.getValue());
                        if ("BZDH".equals(nameq)) {
                            param.setBoeNum(valueq);
                        }
                        if ("YHXRQ".equals(nameq)) {
                            param.setOriginalDate(valueq);
                        }
                        if ("YQHXRQ".equals(nameq)) {
                            param.setDelayDate(valueq);
                        }
                    }
                    items[j] = param;
                }
                info.setWorkflow(lcbh);
                info.setEmployeeNumber(workcode);
                info.setCompanyCode(companyCode);
                info.setRequestId(requestid);
                info.setSystemFlag("OA");
                info.setItems(items);
//                DelayDateService service = new DelayDateService();
                String[] str = service.callService(info);
                logger_46fa23c3.error(String.valueOf("DelayDateSyncAction--" + requestid + "--return[" + str[0] + "|" + str[1] + "]"));
                if (!"S".equals(str[0])) {
                    requestInfo.getRequestManager().setMessageid("ERROR");
                    requestInfo.getRequestManager().setMessagecontent("报账系统返回错误信息：" + str[1]);
                    return WeaResult.fail(500, "报账系统返回错误信息"+ str[1], getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, requestInfo.getRequestManager()));

                }
            } catch (Exception e) {
                requestInfo.getRequestManager().setMessageid("ERROR");
                requestInfo.getRequestManager().setMessagecontent("数据同步报账系统失败，" + e.toString());
                logger_46fa23c3.error(String.valueOf("DelayDateSyncAction--" + requestid + "--end"));
                return WeaResult.fail(500, "数据同步报账系统失败"+e, getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, requestInfo.getRequestManager()));

            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_46fa23c3.error("DelayDateSyncAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "DelayDateSyncAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), requestInfo.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;
    }
}
