package com.engine.htsc.cgscfyqr.dao;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class getDt4DataCMD extends AbstractCommonCommand<List<Map<String, Object>>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public getDt4DataCMD(Map<String, Object> params) {
        this.params = params;
    }

    /*
     * 入参:String-deptCode-zz001129;
     * String-type-C1018684;
     * String-requestid
     * 出参:List<Map<String,Object>>-needshow
     * */
    @Override
    public List<Map<String, Object>> execute(CommandContext commandContext) {
        List<Map<String, Object>> dtTenData = new ArrayList<>();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String sql;
        String deptCode = Util.null2String(params.get("deptCode"));
        bb.writeLog("deptCode:" + deptCode);
        String type = Util.null2String(params.get("type"));
        bb.writeLog("type:" + type);
        String requestid = Util.null2String(params.get("requestid"));
        bb.writeLog("requestid:" + requestid);
        String mainTableName = getMainTableName(requestid);
        bb.writeLog("mainTableName:" + mainTableName);
        sql = "select * from "
                + mainTableName + "_dt4 where mainid = " +
                "(select id from " + mainTableName + " where requestid = '" + requestid + "') " +
                "and DWBH = '" + deptCode + "' and MTRTYPECODE = '" + type + "'";
        bb.writeLog("即将执行的SQL代码是：" + sql);
        rs.executeQuery(sql);
        String[] columnNames = rs.getColumnName();
        bb.writeLog("columnNames:" + JSONObject.toJSONString(columnNames));
        int count = 0;
        while (rs.next()) {
            count += 1;
            Map<String, Object> oneRow = new HashMap<>();
            for (String columnName : columnNames) {
                if (!"ID".equals(columnName) && !"MAINID".equals(columnName)) {
                    oneRow.put(columnName.toLowerCase(), rs.getString(columnName.toLowerCase()));
                }
            }
            dtTenData.add(oneRow);
        }
        bb.writeLog(dtTenData.size()+"=============");
        return dtTenData;
    }
    private String getMainTableName(String requestid) {
        RecordSet rs = new RecordSet();
        String tableName = "";
        String sql;
        sql = "select tablename from workflow_base a,workflow_bill b,workflow_requestbase c where a.formid=b.id and a.id=c.workflowid and c.requestid='" + requestid + "'";
        rs.executeQuery(sql);
        if (rs.next()) {
            tableName = rs.getString("tablename");
        }
        return tableName;
    }
}
