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

import java.util.*;
import java.rmi.RemoteException;
import java.text.*;
import java.math.*;
import org.apache.axis2.AxisFault;
import corp.openapicalls.contract.setapproval.request.PassengerDetail;
import  com.weaver.seconddev.interfaces.swfa.*;
import  com.weaver.seconddev.interfaces.swfa.sl.*;
import  com.weaver.seconddev.interfaces.oactrip.OaCtrip;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
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.conn.*;
import com.weaver.verupgrade.general.TimeUtil;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.conn.RecordSet;

/**
 * 出差申请接口--商旅订票申请--qingf20170216
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_ZOA_SL_DP_Action")
public class ZOA_SL_DP_Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private OaCtrip oactrip;

    private static final Logger logger_a5b8e7d5 = LoggerFactory.getLogger(ZOA_SL_DP_Action.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 {
            try {
                // 携程信息
                // 审批单号请求ID
                String approvalnumber = "";
                // 员工编号
                String employeeid = "";
                // 申请日期
                String expiredtime = "";
                // 开始日期
                String ksrq = "";
                // 结束日期
                String jsrq = "";
                // 开始日期时间
                Date begindate = new Date();
                // 开始日期时间
                Date enddate = new Date();
                String sqr = "";
                // 流程请求ID
                String reid = request.getRequestid();
                // 流程请求ID
                approvalnumber = reid;
                logger_a5b8e7d5.error(String.valueOf("ZOA_SL_DP_Action-Request: " + reid + "开始"));
                RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                BillFieldUtil unit = new BillFieldUtil();
                // 出差类型0国内，1国外
                String cclx = "0";
                // 封装表单信息
                // 获取表单主字段信息
                Property[] properties = request.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    // 主表数据
                    // 字段名
                    String name = properties[i].getName().toLowerCase();
                    // 值
                    String value = Util.null2String(properties[i].getValue());
                    // 单据主信息使用
                    if (name.equals("sqr")) {
                        // 出差人
                        sqr = value;
                        // 出差人工号
                        employeeid = unit.getWorkcode(sqr);
                    }
                    if (name.equals("sqrq")) {
                        // 申请日期
                        expiredtime = value;
                    }
                    if (name.equals("ksrq")) {
                        // 开始日期
                        ksrq = value;
                    }
                    if (name.equals("jsrq")) {
                        // 结束日期
                        jsrq = value;
                    }
                    if (name.equals("cclx")) {
                        // 出差类型
                        cclx = value;
                    }
                }
                // 携程接口出行人信息
                ArrayList<PassengerDetail> passengerdetails = new ArrayList<PassengerDetail>();
                ArrayList<PassengerDetail> gwpassengerdetails = new ArrayList<PassengerDetail>();
                // 获取明细表
                String xm = "";
                String zjhm = "";
                String hzm = "";
                String hzh = "";
                DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
                // 指定明细表
                DetailTable dtq = detailtable[0];
                // 当前明细表的所有数据,按行存储
                Row[] sq = dtq.getRow();
                for (int j = 0; j < sq.length; j++) {
                    // 指定行
                    Row rq = sq[j];
                    // 每行数据再按列存储
                    Cell[] cq = rq.getCell();
                    xm = "";
                    zjhm = "";
                    hzm = "";
                    hzh = "";
                    for (int k = 0; k < cq.length; k++) {
                        // 指定列
                        Cell cq1 = cq[k];
                        // 明细字段名称
                        String nameq = cq1.getName().toLowerCase();
                        // 明细字段的值
                        String valueq = Util.null2String(cq1.getValue());
                        // 姓名
                        if (nameq.equals("xm")) {
                            if (!valueq.equals("")) {
                                xm = valueq.trim();
                                xm = xm.replace("&#160;", " ");
                                xm = xm.replace("&nbsp;", " ");
                            }
                        }
                        // 证件号
                        if (nameq.equals("zjhm")) {
                            if (!valueq.equals("")) {
                                zjhm = valueq.trim();
                                zjhm = zjhm.replace("&#160;", " ");
                                zjhm = zjhm.replace("&nbsp;", " ");
                            }
                        }
                        // 护照名
                        if (nameq.equals("hzm")) {
                            if (!valueq.equals("")) {
                                hzm = valueq.trim();
                                hzm = hzm.replace("&#160;", " ");
                                hzm = hzm.replace("&nbsp;", " ");
                            }
                        }
                        // 护照号
                        if (nameq.equals("hzh")) {
                            if (!valueq.equals("")) {
                                hzh = valueq.trim();
                                hzh = hzh.replace("&#160;", " ");
                                hzh = hzh.replace("&nbsp;", " ");
                            }
                        }
                    }
                    logger_a5b8e7d5.error(String.valueOf("----" + xm + "----" + zjhm + "----" + hzm + "----" + hzh + "----"));
                    if (!xm.equals("") && !zjhm.equals("")) {
                        PassengerDetail passengerdetail = new PassengerDetail();
                        passengerdetail.setName(xm);
                        passengerdetail.setCredentialsNumber(zjhm);
                        passengerdetails.add(passengerdetail);
                        // writeLog("----" + xm + "----"+ zjhm + "----");
                    }
                    if (!hzm.equals("") && !hzh.equals("")) {
                        PassengerDetail gwpassengerdetail = new PassengerDetail();
                        gwpassengerdetail.setName(hzm);
                        gwpassengerdetail.setCredentialsNumber(hzh);
                        gwpassengerdetails.add(gwpassengerdetail);
                        // writeLog("----" + xm + "----"+ zjhm + "----");
                    }
                }
                // 写入携程系统的开始结束日期时间拼接
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                begindate = dateFormat.parse(ksrq + " " + "00:00:00");
                enddate = dateFormat.parse(jsrq + " " + "23:59:59");
                String tablename = "";
                String status = "S";
                String message = "";
                String returnCode = "";
                String wfid = request.getWorkflowid();
//                tablename = unit.getTablename(wfid);
                tablename = request.getRequestManager().getBillTableName();
                ;
                try {
                    logger_a5b8e7d5.error(String.valueOf("调用携程接口开始 "));
//                    OaCtrip oactrip = new OaCtrip();
                    oactrip.setPassengerDetails(passengerdetails);
                    oactrip.setGWPassengerDetails(gwpassengerdetails);
                    String[] strreturn = oactrip.call(approvalnumber, employeeid, expiredtime, begindate, enddate, cclx);
                    if (status.equals("S")) {
                        status = strreturn[0];
                    }
                    message = "--携程接口回写说明--ReturnStatus:" + strreturn[0] + "；ReturnERRORMessage:" + strreturn[1] + "；ReturnErrorCode:" + strreturn[2];
                    message = message.replace("'", "’");
                    if (!tablename.equals("")) {
                        String logsql2 = "update e10_core_business.dbo." + tablename + " set status ='" + status + "', message = isnull(cast(message as varchar(8000)),'') + '" + message + "' where id = " + reid;
                        logger_a5b8e7d5.error(String.valueOf("logsql2:" + logsql2));
                        String poolname = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b = rs1.executeSql(logsql2, poolname);
                        logger_a5b8e7d5.error(String.valueOf("b1:" + b));
                    }
                } catch (Exception e) {
                    logger_a5b8e7d5.error(String.valueOf("Exception:" + e.getMessage()));
                    return  WeaResult.fail(500, "Exception e: {}" + e, getResultMapForAction(weaverResultMap, "result", "", request.getRequestManager()));

                    // e.printStackTrace();
                }
                logger_a5b8e7d5.error(String.valueOf("调用接口完成 "));
            } catch (Exception e) {
                logger_a5b8e7d5.error(String.valueOf("Exception:" + e.getMessage()));
                return  WeaResult.fail(500, "Exception" + e, getResultMapForAction(weaverResultMap, "result", "", request.getRequestManager()));

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