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

import com.alibaba.fastjson.JSONObject;
//import com.cloudstore.dev.api.bean.MessageBean;
//import com.cloudstore.dev.api.bean.MessageType;
//import com.cloudstore.dev.api.util.Util_Message;
import  com.weaver.seconddev.interfaces.eipoa.DidiApprovalOperate;
import java.util.*;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import java.lang.*;

import com.weaver.seconddev.interfaces.prop.ConfigProp;
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.company.DepartmentComInfo;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    @Autowired
    private DidiApprovalOperate operate;

    @Autowired
    private ConfigProp configProp;
    private static final Logger logger_3ff37d5e = LoggerFactory.getLogger(DidiYongCheAction.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_3ff37d5e.error(String.valueOf("DidiYongCheAction-start:" + requestid));
            try {
                Map<String, String> map = new HashMap<String, String>();
                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());
                    map.put(name, value);
                }
                if ("1".equals(map.get("PCFS"))) {
                    // 用车人
                    String ycr = map.get("SQR");
                    // 乘车人
                    String ccr = map.get("CCRY");
                    // 费用结算部门
                    String bm = map.get("SQBM");
                    DepartmentComInfo dept = I18nContextUtil.getBean(DepartmentComInfo.class);
                    List<String> sendlist = new ArrayList<String>();
                    sendlist.add(ycr);
                    String spdlx = map.get("SPDLX");
//                    String ruleId = getPropValue("htportaloaUrl", "didiRuleId_" + map.get("YCZD"));
                    String ruleId = "";
                    if(map.get("YCZD").equals("0")){
                        ruleId=configProp.getDidiRuleId_0();
                    }else if(map.get("YCZD").equals("1")){
                        ruleId=configProp.getDidiRuleId_1();
                    }
                    String startTime = map.get("DDKSRQ") + " 00:00:00";
                    String endTime = map.get("DDJSRQ") + " 23:59:59";
                    int tripTimes = 0;
                    JSONObject object = new JSONObject();
                    object.put("uid", map.get("SQRGH"));
                    object.put("phone", map.get("SQRYLXDH"));
                    object.put("ruleId", ruleId);
                    object.put("requestId", requestid);
                    object.put("approveType", Integer.parseInt(spdlx) + 1 + "");
                    object.put("reason", map.get("SQSY"));
                    object.put("startTime", startTime);
                    object.put("endTime", endTime);
                    object.put("deptGuid", dept.getDepartmentCode(bm));
                    if ("1".equals(spdlx)) {
                        tripTimes = Integer.parseInt(map.get("TRIPTIMES"));
                        if (tripTimes == 0) {
                            requestinfo.getRequestManager().setMessageid("ERROR");
                            requestinfo.getRequestManager().setMessagecontent("商务出行按次数，次数需大于0！");
                            return  WeaResult.fail(500, "商务出行按次数，次数需大于0！" , getResultMapForAction(weaverResultMap, "result", "商务出行按次数，次数需大于0", requestinfo.getRequestManager()));
                        }
                        object.put("tripTimes", tripTimes);
                    }
//                    DidiApprovalOperate operate = new DidiApprovalOperate();
                    String result = operate.operteAppr(object, "didicreate");
                    logger_3ff37d5e.error(String.valueOf(requestid + "-DidiYongCheAction-result:" + result));
                    if (!"E".equals(result)) {
                        JSONObject jsonObject = JSONObject.parseObject(result);
                        String flag = jsonObject.getString("success");
                        String message = jsonObject.getString("message");
                        if (!flag.equals("true")) {
                            requestinfo.getRequestManager().setMessageid("ERROR");
                            requestinfo.getRequestManager().setMessagecontent("滴滴返回错误信息：" + message);
                            return  WeaResult.fail(500, "滴滴返回错误信息：" + message , getResultMapForAction(weaverResultMap, "result", "滴滴返回错误信息：" + message, requestinfo.getRequestManager()));

                        } else {
//                            String content = getPropValue("htportaloaUrl", "didiremindtext");
                            // Map<String,Object> urlmap = new HashMap<String,Object>();
                            // urlmap.put("msgurl", getPropValue("htportaloaUrl","didiremindurl"));
//                            String dataid = getPropValue("htportaloaUrl", "didireminddataid");
                            // InterfaceUtil.sendMsg(sendlist,","+dataid+",", "", content,16,urlmap);


                            //TODO custom 消息
//                            MessageType messageType = MessageType.newInstance(522);
//                            MessageBean messageBean = Util_Message.createMessage(messageType, new HashSet(sendlist), content, content, "", getPropValue("htportaloaUrl", "didiremindurl"));
//                            messageBean.setCreater(1);
//                            Util_Message.store(messageBean);
                        }
                    } else {
                        requestinfo.getRequestManager().setMessageid("ERROR");
                        requestinfo.getRequestManager().setMessagecontent("数据同步滴滴出错，请联系管理员！");
                        return  WeaResult.fail(500, "数据同步滴滴出错，请联系管理员！" , getResultMapForAction(weaverResultMap, "result", "数据同步滴滴出错，请联系管理员！", requestinfo.getRequestManager()));

                    }
                }
            } catch (Exception e) {
                requestinfo.getRequestManager().setMessageid("ERROR");
                requestinfo.getRequestManager().setMessagecontent("数据同步滴滴出错，请联系管理员！");
                logger_3ff37d5e.error(String.valueOf("DidiYongCheAction出错：" + e.toString()));
                return  WeaResult.fail(500, "DidiYongCheAction出错：" + e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), requestinfo.getRequestManager()));

            }
            logger_3ff37d5e.error(String.valueOf("DidiYongCheAction-end:" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_3ff37d5e.error("DidiYongCheAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "DidiYongCheAction 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;
    }



}
