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;


/***
 * 360测评
 *
 */
@Component
public class HrmAssessment360Dao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     * 判断360测评数据在Eb表是否存在
     * @return
     */
    public String getAssessment360Data(String gh,String hdid,String wjid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String id = "";
        try{
            String dataSql =" select id from " +Constants.assessment360Table +
                            " where gh = ? " +
                            " and hdid = ? " +
                            " and wjid = ? " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(gh);
            paramList.add(hdid);
            paramList.add(wjid);
            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;
    }


    /**
     * 根据工号获取人员ID
     * @param jobNum
     * @return
     */
    public String queryEmployeeJobNumData(String jobNum){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String employeeId = "";
        try{
            String dataSql =" select id from eteams.employee where job_num = ? and tenant_key=? and delete_type = 0" ;
            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(jobNum);
            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){
                employeeId = String.valueOf(recordList.get(0).get("id"));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return employeeId;
    }


    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;
    }



}
