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;

@Component
public class HrmDepartmentDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     *
     * @param deptId
     * @return
     */
    public String getDepartmentCodeById(String deptId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String code = "";
        try{
            String dataSql =" select code from eteams.department where id=? " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

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


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