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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.esign.constant.Constants;
import com.weaver.seconddev.chapanda.esign.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")).toLowerCase();
                String id = String.valueOf(recordMap.get("id")).toLowerCase();
                fieldMap.put(data_key,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return fieldMap;
    }

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

            String dataSql =" select code,id from eteams.department and tenant_key=? and delete_type=0" ;
            log.error("queryDepartmentList--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            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("queryDepartmentList:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String code = String.valueOf(recordMap.get("code"));
                String id = String.valueOf(recordMap.get("id"));
                dataMap.put(code,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryDepartmentList:{} ", e);
        }
        return dataMap;
    }

    public String queryEmailByEmployeeId(String employeeId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String gryx = "";
        try{

            String dataSql =" select k.gryx from eteams.employee t\n" +
                    " inner join eteams.ft_1152026012537184302 k on k.id = t.formdata\n" +
                    " where t.id=? " +
                    " and t.tenant_key=? \n" +
                    " and t.delete_type=0" +
                    " and k.tenant_key=? \n" +
                    " and k.delete_type=0";

            log.error("queryEmailByEmployeeId--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(employeeId);
            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("queryEmailByEmployeeId:"+recordList.size());
            if(recordList.size() > 0){
                gryx = String.valueOf(recordList.get(0).get("gryx"));
            }
        }catch (Exception e){
            log.error("queryEmailByEmployeeId:e:" + e);
        }
        return gryx;
    }


    public Map<String,Object> queryDepartmentData(){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> dataMap = new HashMap<String,Object>();
        try{
            String dataSql =" select code,id from eteams.department where status = 1 " +
                    " and tenant_key=? \n" +
                    " and delete_type=0 " ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            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());
            for(int i=0;i<recordList.size();i++){
                Map<String, Object> recordMap = recordList.get(i);
                String code = String.valueOf(recordMap.get("code"));
                String id = String.valueOf(recordMap.get("id"));
                dataMap.put(code,id);
            }
        }catch (Exception e){
            log.error("e:" + e);
        }
        return dataMap;
    }


    public Map<String,Object> queryEmployeeData(){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> dataMap = new HashMap<String,Object>();

        try{
            String dataSql =" select job_num,id from eteams.employee where tenant_key=? and delete_type = 0" ;
            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            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());
            for(int i=0;i<recordList.size();i++){
                Map<String, Object> recordMap = recordList.get(i);
                String job_num = String.valueOf(recordMap.get("job_num"));
                String id = String.valueOf(recordMap.get("id"));
                dataMap.put(job_num,id);
            }
        }catch (Exception e){
            log.error("e:" + e);
        }
        return dataMap;
    }


    public Long queryTableFormIdByEb(String tableName){
        String sourceType = "LOGIC";
        String groupId = "weaver-ebuilder-form-service";
        Long form_id = 0L;

        try{
            String dataSql =" select form_id from form_table 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){
                Map<String, Object> recordMap = recordList.get(0);
                form_id = Long.valueOf(String.valueOf(recordMap.get("form_id")));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryTableFormId:e:" + e);
        }
        return form_id;
    }


    public Map<String, Object> queryDetailTableField(String mainTable,String detailTable,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 in(select form_id from form_table where table_name=? and delete_type=0 and tenant_key=? )\n"+
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("queryFromTableField--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(mainTable);
            paramList.add(Constants.TENANT_KEY);
            paramList.add(detailTable);
            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("queryDetailTableField:"+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;
    }



}
