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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.sap.constant.Constants;
import com.weaver.seconddev.chapanda.sap.util.DatabaseUtils;
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;
import java.util.stream.Collectors;

@Component
public class FormFieldDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     *
     * @param fieldId
     * @return
     */
    public Map<String, Object> queryFieldOptionByFieldid(String fieldId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> optionMap = new HashMap<String, Object>();
        try{

            String dataSql =" select name,value_key from field_option where field_id=? " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(fieldId);
            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("queryFromTableField:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String name = String.valueOf(recordMap.get("name"));
                String value_key = String.valueOf(recordMap.get("value_key"));
                optionMap.put(value_key,name);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return optionMap;
    }

    /***
     *
     * @param tableName
     * @return
     */
    public Map<String,Object> queryTableFormId(String tableName){
        String sourceType = "LOGIC";
        String groupId = "weaver-ebuilder-form-service";
        Map<String, Object> recordMap = new HashMap<String, Object>();
        try{
            String dataSql =" select id,app_id from ebdf_obj where table_name=? and delete_type=0 and tenant_key=?" ;
            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(tableName);
            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("queryTableFormId:"+recordList.size());
            if(recordList.size()>0){
                recordMap = recordList.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryTableFormId:e:" + e);
        }
        return recordMap;
    }


    /**
     *
     * @param dataKeyList
     * @return
     */
    public Map<String, Object> queryFromTableField(String formTable,List<String> dataKeyList){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        try{
            String dataKey = dataKeyList.stream().collect(Collectors.joining(","));
            dataKey = "'"+dataKey.replace(",","','")+"'" ;

            String dataSql =" select id,form_id,title,data_key " +
                            " from form_field " +
                            " where form_id in( select form_id from form_table where table_name=? and delete_type=0 and tenant_key=? ) \n" +
                            " and data_key in("+dataKey+")\n" +
                            " and sub_form_id is null " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("queryFromTableField--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(formTable);
            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("queryFromTableField:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String data_key = String.valueOf(recordMap.get("data_key"));
                String id = String.valueOf(recordMap.get("id"));
                fieldMap.put(data_key,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return fieldMap;
    }

}
