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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import  com.weaver.seconddev.interfaces.eipoa.AccountService;
import  com.weaver.seconddev.interfaces.eipoa.SI_HTPortalUAMGMT2_OUTServiceStub;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
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.conn.RecordSet;
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_CloseAccountAction")
public class CloseAccountAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_ac56032a = LoggerFactory.getLogger(CloseAccountAction.class);

    @Autowired
    private AccountService service;

    public CloseAccountAction() {
    }

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_ac56032a.error("CloseAccountAction 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 {
            String requestId = request.getRequestid();
            String wfid = request.getWorkflowid();
//            BillFieldUtil bill = new BillFieldUtil();
//            String tablename = bill.getTablename(wfid);
            String tablename = request.getRequestManager().getBillTableName();
            logger_ac56032a.error("tablename--" + tablename);
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            String status = "";
            String message = "";
            String sql_error = "";
            //TODO custom 不启用
//            try {
//                // 用户工号
//                String yhgh = "";
//                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("YHGH")) {
//                        yhgh = value;
//                    }
//                }
//                DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
//                DetailTable dtq = detailtable[0];
//                Row[] sq = dtq.getRow();
//                SI_HTPortalUAMGMT2_OUTServiceStub.UserSyncInfo[] param = new SI_HTPortalUAMGMT2_OUTServiceStub.UserSyncInfo[sq.length];
//                SI_HTPortalUAMGMT2_OUTServiceStub.UserSyncInfo paramInfo = new SI_HTPortalUAMGMT2_OUTServiceStub.UserSyncInfo();
//                logger_ac56032a.error("sq -- "+ sq);
//                for (int j = 0; j < sq.length; j++) {
//                    Row rq = sq[j];
//                    Cell[] cq = rq.getCell();
//                    paramInfo.setMail("");
//                    paramInfo.setOperation("disable");
//                    paramInfo.setUserid(yhgh);
//                    for (int k = 0; k < cq.length; k++) {
//                        Cell cq1 = cq[k];
//                        String nameq = cq1.getName().toUpperCase();
//                        String valueq = Util.null2String(cq1.getValue());
//                        if (nameq.equals("XTBH")) {
//                            paramInfo.setSystemid(valueq);
//                        }
//                    }
//                    param[j] = paramInfo;
//                }
////                AccountService service = new AccountService();
//                logger_ac56032a.error("param--"+ param);
//                StringBuilder sb = new StringBuilder();
//                String[] array = service.account("HT01EIP006", param);
//                logger_ac56032a.error("array--"+ array);
//                if ("E".equals(array[0])) {
//                    status = array[0];
//                    message = array[1];
//                    logger_ac56032a.error(String.valueOf(requestId + "---" + yhgh + "调用批量销户接口出错：" + array[1]));
//                } else {
//                    JSONObject jsonObject = JSONObject.parseObject(array[1]);
//                    JSONArray jsonArray = jsonObject.getJSONArray("returnMsg");
//                    for (int i = 0; i < jsonArray.size(); i++) {
//                        JSONObject jsonObjectone = jsonArray.getJSONObject(i);
//                        String result = jsonObjectone.getString("result");
//                        String systemid = jsonObjectone.getString("systemid");
//                        String stat = jsonObjectone.getString("stat");
//                        if ("0".equals(stat)) {
//                            sb.append(systemid + ":" + result + ";");
//                        }
//                    }
//                    if (sb.length() > 0) {
//                        status = "E";
//                        message = sb.toString();
//                        logger_ac56032a.error(String.valueOf(requestId + "---" + yhgh + "批量销户失败" + message));
//                    } else {
//                        status = "S";
//                        message = "销户成功";
//                        logger_ac56032a.error(String.valueOf(requestId + "---" + yhgh + "批量销户成功"));
//                    }
//                }
//                sql_error = "update e10_core_business.dbo." + tablename + " set status='" + status + "',message='" + message + "' where requestId=" + requestId;
//                logger_ac56032a.error("sql_error -- " + sql_error);
//                String poolname_1n = CONN_TYPE.workflow.getType();
//                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                rs.executeSql(sql_error, poolname_1n);
//                if ("E".equals(status)) {
//                    logger_ac56032a.error(String.valueOf(requestId + "---" + yhgh + "批量销户[" + message + "]"));
//                    request.getRequestManager().setMessageid("ERROR");
//                    request.getRequestManager().setMessagecontent("门户返回错误信息：" + message);
//                }
//            } catch (Exception e) {
//                sql_error = "update e10_core_business.dbo." + tablename + " set status='E',message='" + e.toString() + "' where requestId=" + requestId;
//                logger_ac56032a.error("sql_error -- " + sql_error);
//                String poolname = CONN_TYPE.workflow.getType();
//                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                rs.executeSql(sql_error, poolname);
//                logger_ac56032a.error(String.valueOf(requestId + "批量销户失败：" + e.toString()));
//                request.getRequestManager().setMessageid("ERROR");
//                request.getRequestManager().setMessagecontent("门户返回错误信息：" + e.toString());
//                return  WeaResult.fail(500, "出错啦", getResultMapForAction(weaverResultMap, "result", "", request.getRequestManager()));
//            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_ac56032a.error("CloseAccountAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "CloseAccountAction 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;
    }
}
