package com.weaver.seconddev.chapanda.feishu.dao;

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.cost.constant.Constants;
import com.weaver.seconddev.chapanda.cost.util.DatabaseUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class WorkflowTableDao {

    private final static Logger log = LoggerFactory.getLogger(WorkflowTableDao.class);

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public String queryWorkflowTable(String workflowId){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
       String formTable = "";
        try{
            String dataSql =" select table_name \n" +
                            " from form_table \n" +
                            " where form_id in(select relatekey from wfp_relateform where workflowid=? and delete_type = 0 and tenant_key = ? ) " +
                            " and delete_type = 0 and tenant_key = ? " ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(workflowId);
            paramList.add(Constants.TENANT_KEY);
            paramList.add(Constants.TENANT_KEY);
            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            Map<String, Object> result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:"+recordList.size());
            if(recordList.size() > 0){
                formTable = String.valueOf(recordList.get(0).get("table_name"));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:" + e);
        }
        return formTable;
    }

}
