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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import com.weaver.seconddev.chapanda.beisen.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 HrmAssessmentNewDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     *
     * @return
     */
    public String getAssessmentData(String khnf,String khzq,String jxhdid,String bkhr){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String id = "";
        try{
            String dataSql =" select id from uf_jxsjtz " +
                            " where khnf = ? " +
                            " and khzq = ? " +
                            " and jxhdid = ? " +
                            " and bkhr = ? " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(khnf);
            paramList.add(khzq);
            paramList.add(jxhdid);
            paramList.add(bkhr);
            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){
                id = String.valueOf(recordList.get(0).get("id"));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return id;
    }

    /***
     *
     * @param tableName
     * @return
     */
    public String queryTableFormId(String tableName){
        String sourceType = "LOGIC";
        String groupId = "weaver-ebuilder-form-service";
        String form_id = "";
        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 = String.valueOf(recordMap.get("form_id"));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryTableFormId:e:" + e);
        }
        return form_id;
    }


    /**
     *
     * @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(","));

            String dataSql =" select id,form_id,title,data_key " +
                            " from eteams.form_field " +
                            " where form_id in( select form_id from eteams.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){
            log.error("queryFromTableField:e:" + e);
        }
        return fieldMap;
    }



    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> queryEmployeeAssessmentOtherInfo(String employeeid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> employeeMap = new HashMap<String, Object>();
        try{

            String dataSql =" select hiredate as rzrq,zhgzr as lzrq,zzrq,personnel_status as ryzt,subcompany as gs,yjbm as bm1,ejbm as bm2,sanjbm as bm3,sijbm as bm4,rylb,location as gzdd" +
                            " from uf_jcl_employee_information \n" +
                            " where id = ? " +
                            " and tenant_key = ?  \n" +
                            " and delete_type = 0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(employeeid);
            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){
                employeeMap = recordList.get(0);
            }
        }catch (Exception e){
            log.error("e:{}", e);
        }
        return employeeMap;
    }


    public List<Map<String, Object>>  queryEmployeeEmail(){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";

        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{

            String dataSql =" select id,email from eteams.employee \n" +
                            " where tenant_key=? \n" +
                            " and delete_type=0 " ;

            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);
            recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:{}",recordList.size());
        }catch (Exception e){
            log.error("e:{}" ,e);
        }
        return recordList;
    }


}
