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

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

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public Map<String,Object> queryEmployeeById(String staffcode){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> recordMap = new HashMap<String,Object>();
        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.size()>0){
                    recordMap = recordList.get(0);
                }else{
                    recordMap.put("id","");
                    recordMap.put("username","");
                }
            }else{
                recordMap.put("id","");
                recordMap.put("username","");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:" + e);
        }
        return recordMap;
    }

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


}
