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

import java.text.SimpleDateFormat;
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.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 财务客户主数据--操作--qingf20170726
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_ZOA_FI_CUSTOMER_MAINTAINAction")
public class ZOA_FI_CUSTOMER_MAINTAINAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_30bd33f8 = LoggerFactory.getLogger(ZOA_FI_CUSTOMER_MAINTAINAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_30bd33f8.error("ZOA_FI_CUSTOMER_MAINTAINAction 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 {
            // 流程请求ID
            String reid = request.getRequestid();
            RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
            BillFieldUtil unit = new BillFieldUtil();
            try {
                // 申请业务
                String sqyw = "";
                // 动作类型
                String edit_flag = "";
                // 付款冻结
                String zahls = "";
                String[] prdetail = new String[40];
                // 获取表单主字段信息
                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("SQYW")) {
                        sqyw = value;
                    }
                    // 公司代码
                    if (name.equals("GSDM")) {
                        prdetail[1] = value;
                    }
                    // 客户代码
                    if (name.equals("KHDM")) {
                        prdetail[2] = value;
                    }
                    // 科目组
                    if (name.equals("KMZNOGH")) {
                        prdetail[3] = value;
                    }
                    // 称谓
                    if (name.equals("CWGH")) {
                        prdetail[4] = value;
                    }
                    // 名称
                    if (name.equals("MCGH")) {
                        prdetail[5] = value;
                    }
                    // 简称-搜索项
                    if (name.equals("JCGH")) {
                        prdetail[6] = value;
                    }
                    // 电话
                    if (name.equals("LXDHGH")) {
                        prdetail[7] = value;
                    }
                    // 邮件
                    if (name.equals("EMAILGH")) {
                        prdetail[8] = value;
                    }
                    // 手机
                    if (name.equals("SJGH")) {
                        prdetail[9] = value;
                    }
                    // 传真
                    if (name.equals("CZGH")) {
                        prdetail[10] = value;
                    }
                    // 国家
                    if (name.equals("GJNOGH")) {
                        prdetail[11] = value;
                    }
                    // 地区
                    if (name.equals("DQNOGH")) {
                        prdetail[12] = value;
                    }
                    // 语言
                    if (name.equals("YYNOGH")) {
                        prdetail[13] = value;
                    }
                    // 城市
                    if (name.equals("CSGH")) {
                        prdetail[14] = value;
                    }
                    // 地址
                    if (name.equals("DZGH")) {
                        prdetail[15] = value;
                    }
                    // 邮政编号
                    if (name.equals("YZBHGH")) {
                        prdetail[16] = value;
                    }
                    // 开户行行号
                    if (name.equals("KHHHHGH")) {
                        prdetail[17] = value;
                    }
                    // 开户行名称
                    if (name.equals("KHHMCGH")) {
                        prdetail[18] = value;
                    }
                    // 开户行国家
                    if (name.equals("KHHGJNOGH")) {
                        prdetail[19] = value;
                    }
                    // 开户行账号
                    if (name.equals("KHHZHGH")) {
                        prdetail[20] = value;
                    }
                    // 银行开户行账号
                    if (name.equals("YHKHHZHGH")) {
                        prdetail[37] = value;
                    }
                    // 纳税登记证代码
                    if (name.equals("NSDJZDMGH")) {
                        prdetail[21] = value;
                    }
                    // 统驭科目
                    if (name.equals("TYKMNOGH")) {
                        prdetail[22] = value;
                    }
                    // 付款条件
                    if (name.equals("FKTJNOGH")) {
                        prdetail[23] = value;
                    }
                    // 排序码
                    if (name.equals("PXMGH")) {
                        prdetail[24] = value;
                    }
                    // 付款方式
                    if (name.equals("FKFSNOGH")) {
                        prdetail[25] = value;
                    }
                    // 批准组
                    if (name.equals("PZZNOGH")) {
                        prdetail[26] = value;
                    }
                    // 付款冻结
                    if (name.equals("FKDJ")) {
                        zahls = value;
                    }
                }
                if (zahls == "1") {
                    prdetail[27] = "X";
                } else {
                    prdetail[27] = "";
                }
                if (sqyw.equals("0")) {
                    edit_flag = "CREATE";
                } else if (sqyw.equals("1")) {
                    edit_flag = "MODIFY";
                } else if (sqyw.equals("2")) {
                    edit_flag = "BLOCK";
                } else if (sqyw.equals("3")) {
                    edit_flag = "UNBLOCK";
                } else if (sqyw.equals("4")) {
                    edit_flag = "DELETE";
                } else if (sqyw.equals("5")) {
                    edit_flag = "UNDELETE";
                } else {
                    edit_flag = "";
                }
                // 动作类型
                prdetail[0] = edit_flag;
                String tablename = "";
                String wfid = request.getWorkflowid();
//                tablename = unit.getTablename(wfid);
                tablename = request.getRequestManager().getBillTableName();
                ZOA_FI_CUSTOMER_MAINTAIN po = new ZOA_FI_CUSTOMER_MAINTAIN();
                String[][] fhz = po.Getexecute(prdetail);
                String sta = fhz[0][0];
                String meg = fhz[0][1].replace("'", "’");
                // 客户编号
                String kunnr = Util.null2String(fhz[0][2]).trim();
                Date now = new Date();
                SimpleDateFormat datetype = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat timetype = new SimpleDateFormat("HH:mm");
                String date = datetype.format(now);
                String time = timetype.format(now);
                String output = "----" + date + "," + time + "," + meg;
                String logsql = "";
                if (kunnr.equals("")) {
                    logsql = "update e10_core_business.dbo." + tablename + " set sta ='" + sta + "', meg = '" + output + "' where id = " + reid;
                    logger_30bd33f8.error(String.valueOf("start log--" + edit_flag + "--" + reid));
                } else {
                    logsql = "update e10_core_business.dbo." + tablename + " set sta ='" + sta + "', khdm = '" + kunnr + "', meg = '" + output + "' where id = " + reid;
                    logger_30bd33f8.error(String.valueOf("start log--" + edit_flag + "--" + reid));
                }
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs1.executeSql(logsql, poolname);
                logger_30bd33f8.error(String.valueOf("b--" + b));
                logger_30bd33f8.error(String.valueOf("start log--" + edit_flag + "--" + reid));
                logger_30bd33f8.error(String.valueOf("------------------------------------------------------------------------"));
                logger_30bd33f8.error(String.valueOf("ZOA_FI_CUSTOMER_MAINTAINAction:" + logsql));
                logger_30bd33f8.error(String.valueOf("------------------------------------------------------------------------"));
                logger_30bd33f8.error(String.valueOf("end log----"));
                if (!sta.equals("S")) {
                    request.getRequestManager().setMessageid("100001");
                    request.getRequestManager().setMessagecontent("返回消息----" + meg);
                    return  WeaResult.fail(500, "ZOA_FI_CUSTOMER_MAINTAINAction meg"+ meg, getResultMapForAction(weaverResultMap, "result",meg, request.getRequestManager()));
                }
            } catch (Exception e) {
                // BaseBean baseBean = new BaseBean();
                logger_30bd33f8.error(String.valueOf("start log----" + reid));
                logger_30bd33f8.error(String.valueOf("------------------------------------------------------------------------"));
                logger_30bd33f8.error(String.valueOf("ZOA_FI_CUSTOMER_MAINTAINAction error:" + e.toString()));
                logger_30bd33f8.error(String.valueOf("------------------------------------------------------------------------"));
                logger_30bd33f8.error(String.valueOf("end log"));
                return  WeaResult.fail(500, "ZOA_FI_CUSTOMER_MAINTAINAction err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_30bd33f8.error("ZOA_FI_CUSTOMER_MAINTAINAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_FI_CUSTOMER_MAINTAINAction 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;
    }
}
