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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.feishu.constant.Constants;
import com.weaver.seconddev.chapanda.feishu.util.DatabaseUtils;
import org.apache.commons.lang3.StringUtils;
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 WorkflowDegreeLevelDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public Map<String,Object> queryDegreeLevel(){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
        Map<String,Object> dataMap = new HashMap<String,Object>();
        try{
            String dataSql =" select custom_name,code " +
                            " from wfp_customlevel  " +
                            " where delete_type = 0 \n" +
                            " and tenant_key = ? " ;

            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 custom_name = String.valueOf(recordMap.get("custom_name"));
                dataMap.put(code,custom_name);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:" + e);
        }
        return dataMap;
    }

    public Long queryEmployeeByCode(String staffcode){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Long employeeId = 0L;
        try{
            if(StringUtils.isNotBlank(staffcode)){
                String dataSql =" select id,username " +
                        " from eteams.employee  " +
                        " where job_num = ? \n" +
                        " and delete_type = 0 \n" +
                        " and tenant_key = ? " ;

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


}
