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

import  com.weaver.seconddev.interfaces.easoa.FeeApplicationService;
import  com.weaver.seconddev.interfaces.easoa.SI_FeeApplicationSync_OUTServiceStub.CostStandardHead;
import  com.weaver.seconddev.interfaces.easoa.SI_FeeApplicationSync_OUTServiceStub.CostStandardLines;
import  com.weaver.seconddev.interfaces.easoa.SI_FeeApplicationSync_OUTServiceStub.CostStandardSynchronizationServiceResponse;
import  com.weaver.seconddev.interfaces.easoa.SI_FeeApplicationSync_OUTServiceStub.ResultInfoLines;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
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.Cell;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    @Autowired
    private FeeApplicationService service ;
    private static final Logger logger_587b7053 = LoggerFactory.getLogger(EASPhoneBillAction.class);

    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 request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = request.getRequestid();
            String lcbh = "";
            String tbr = "";
            String tbbm = "";
            String tbdw = "";
            String tbrq = "";
            String tbsj = "";
            try {
                logger_587b7053.error(String.valueOf(requestid + "调用报账系统话费接口同步数据开始"));
                logger_587b7053.error("requestIdTemp == " +requestIdTemp);
                logger_587b7053.error("userIdTemp == " +userIdTemp);
                BillFieldUtil util = new BillFieldUtil();
                Property[] properties = request.getMainTableInfo().getProperty();
                CostStandardHead param = new CostStandardHead();
                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)) {
                        tbr = util.getName(value);
                    }
                    if ("TBBM".equals(name)) {
                        tbbm = util.getDepartmentname(value);
                    }
                    if ("TBDW".equals(name)) {
                        tbdw = util.getSubcompanyname(value);
                    }
                    if ("TBRQ".equals(name)) {
                        tbrq = value;
                    }
                    if ("TBSJ".equals(name)) {
                        tbsj = value;
                    }
                }
                param.setCheckFlage("0");
                param.setFlowId(requestid);
                param.setFlowType("1");
                param.setFlowCode(lcbh);
                param.setApplicantName(tbr);
                param.setApplicantDepartment(tbbm);
                param.setApplicantCompany(tbdw);
                param.setApplicantDate(tbrq + " " + tbsj + ":00");
                DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
                DetailTable dtq = detailtable[0];
                Row[] sq = dtq.getRow();
                CostStandardLines[] lines = new CostStandardLines[sq.length];
                for (int j = 0; j < sq.length; j++) {
                    // 指定行
                    Row rq = sq[j];
                    // 每行数据再按列存储
                    Cell[] cq = rq.getCell();
                    CostStandardLines line = new CostStandardLines();
                    for (int k = 0; k < cq.length; k++) {
                        // 指定列
                        Cell cq1 = cq[k];
                        // 明细字段名称
                        String nameq = cq1.getName().toLowerCase();
                        String valueq = Util.null2String(cq1.getValue());
                        if ("gsbh".equals(nameq)) {
                            line.setCompanyCode(valueq);
                        }
                        if ("xm".equals(nameq)) {
                            line.setEmployeeName(util.getName(valueq));
                        }
                        if ("gh".equals(nameq)) {
                            line.setEmployeeNumber(valueq);
                        }
                        if ("sfyx".equals(nameq)) {
                            String sfqy = "";
                            if ("0".equals(valueq)) {
                                sfqy = "Y";
                            } else {
                                sfqy = "N";
                            }
                            line.setEnabledFlag(sfqy);
                        }
                        if ("ed".equals(nameq)) {
                            line.setUsableAmount(valueq);
                        }
                        if ("sxrq".equals(nameq)) {
                            if (!"".equals(valueq)) {
                                line.setEfectiveDate(valueq.substring(0, 7));
                            }
                        }
                        if ("sjh".equals(nameq)) {
                            line.setMobliePhone(valueq);
                        }
                        if ("sffg".equals(nameq)) {
                            String sffg = "";
                            if ("0".equals(valueq)) {
                                sffg = "Y";
                            } else {
                                sffg = "N";
                            }
                            line.setIfCovered(sffg);
                        }
                    }
                    lines[j] = line;
                }
//                FeeApplicationService service = new FeeApplicationService();
                param.setCostStandardLinesArr(lines);
                CostStandardSynchronizationServiceResponse returnData = service.call(param);

                logger_587b7053.error("returnData  == " + returnData);
                String state = returnData.get_return().getISTAT();
                String message = returnData.get_return().getMESSAGE();
                if (!"1".equals(state)) {
                    ResultInfoLines[] info = returnData.get_return().getResultInfoLinesArr();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < info.length; i++) {
                        sb.append(info[i].getMESSAGE() + ";");
                    }
                    logger_587b7053.error(String.valueOf("报账系统话费接口返回处理信息：" + sb.toString() + message));
                    request.getRequestManager().setMessageid("ERROR");
                    request.getRequestManager().setMessagecontent("报账系统返回处理信息：" + sb.toString());
                }
                logger_587b7053.error(String.valueOf(requestid + "调用报账系统话费接口同步数据结束"));
            } catch (Exception e) {
                logger_587b7053.error(String.valueOf("调用报账系统话费接口出错：" + e.toString()));
                request.getRequestManager().setMessageid("ERROR");
                request.getRequestManager().setMessagecontent("报账系统返回错误信息：" + e.toString());
                return  WeaResult.fail(500, "报账系统返回错误信息："+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));

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