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

import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.text.SimpleDateFormat;
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.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.soa.workflow.request.*;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 采购申请回传--委外加工--qingf20170328
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_ZOA_MM_PR_CREATE_WWJGAction")
public class ZOA_MM_PR_CREATE_WWJGAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_54a06806 = LoggerFactory.getLogger(ZOA_MM_PR_CREATE_WWJGAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_54a06806.error("ZOA_MM_PR_CREATE_WWJGAction 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 {
            try {
                RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                BillFieldUtil unit = new BillFieldUtil();
                // 采购申请类型
                String prtype = "ZPR8";
                // 采购组160
                String cgz = "160";
                // 申请人
                String sqr = "";
                // 申请人姓名
                String sqrxm = "";
                // 申请人工号
                String code = "";
                String[][] prdetail = null;
                int xmh = 0;
                // 流程请求ID
                String reid = request.getRequestid();
                // spr = hr.getResourcename(request.getLastoperator());//获取审批人姓名
                // 获取表单主字段信息
                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("SQR")) {
                        sqr = value;
                    }
                }
                // rs1.executeSql("select workcode from HrmResource where id = " + sqr);
                if (!sqr.equals("")) {
                    // 申请人工号
                    code = unit.getWorkcode(sqr);
                    // 获取申请人姓名
                    sqrxm = hr.getResourcename(sqr);
                }
                // 获取明细表
                DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
                // 指定明细表
                DetailTable dtq = detailtable[0];
                // 当前明细表的所有数据,按行存储
                Row[] sq = dtq.getRow();
                prdetail = new String[sq.length][8];
                for (int j = 0; j < sq.length; j++) {
                    xmh = xmh + 10;
                    // 项目号
                    prdetail[j][0] = String.valueOf(xmh);
                    // prdetail[j][4] = cgz; //采购组
                    // 申请者
                    prdetail[j][5] = code;
                    // 指定行
                    Row rq = sq[j];
                    // 每行数据再按列存储
                    Cell[] cq = rq.getCell();
                    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("WLBMX")) {
                            prdetail[j][1] = valueq.trim();
                        }
                        // 数量
                        if (nameq.equals("SL")) {
                            prdetail[j][2] = valueq;
                        }
                        // 交期
                        if (nameq.equals("JHRQ")) {
                            prdetail[j][3] = valueq.replace("-", "");
                        }
                        // 工厂
                        if (nameq.equals("GCDM")) {
                            prdetail[j][6] = valueq;
                        }
                        // 备注
                        if (nameq.equals("BZ")) {
                            prdetail[j][7] = valueq.trim();
                        }
                        // 采购组
                        if (nameq.equals("CGZ")) {
                            cgz = unit.getselectName(valueq, "100003720000030137").trim();
                            if (cgz.length() > 1) {
                                // 采购组
                                prdetail[j][4] = cgz.substring(0, 3);
                            }
                        }
                    }
                }
                String tablename = "";
                String wfid = request.getWorkflowid();
//                tablename = unit.getTablename(wfid);
                tablename = request.getRequestManager().getBillTableName();
                ZOA_MM_PR_CREATE_WWJG po = new ZOA_MM_PR_CREATE_WWJG();
                String[] fhz = po.Getexecute(prtype, prdetail);
                String prno = fhz[0];
                String sta = fhz[1];
                String meg = fhz[2].replace("'", "’");
                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 = "update e10_core_business.dbo." + tablename + " set prno ='" + prno + "', status = '" + sta + "', message = '" + output + "' where id = " + reid;
                logger_54a06806.error("logsql--" + logsql);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs1.executeSql(logsql, poolname);
                logger_54a06806.error("b--" + b);
                logger_54a06806.error(String.valueOf("start log--" + code + "--" + sqrxm));
                logger_54a06806.error(String.valueOf("------------------------------------------------------------------------"));
                logger_54a06806.error(String.valueOf("ZOA_MM_PR_CREATE_WWJGAction:" + logsql));
                logger_54a06806.error(String.valueOf("------------------------------------------------------------------------"));
                logger_54a06806.error(String.valueOf("end log--" + code + "--" + sqrxm));
            } catch (Exception e) {
                // BaseBean baseBean = new BaseBean();
                logger_54a06806.error(String.valueOf("start log"));
                logger_54a06806.error(String.valueOf("------------------------------------------------------------------------"));
                logger_54a06806.error(String.valueOf("ZOA_MM_PR_CREATE_WWJGAction error:" + e.toString()));
                logger_54a06806.error(String.valueOf("------------------------------------------------------------------------"));
                logger_54a06806.error(String.valueOf("end log"));
                return  WeaResult.fail(500, "ZOA_MM_PR_CREATE_WWJGActionerror:"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_54a06806.error("ZOA_MM_PR_CREATE_WWJGAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_MM_PR_CREATE_WWJGAction 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;
    }
}
