package com.weaver.seconddev.chapanda.feishu.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.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.List;
import java.util.Map;

@Component
public class HrmQueryOpenIdDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public String queryHrmOpenId(String employeeId){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
        String openId = "";
        try{
            String dataSql =" select u.open_id\n" +
                            " from eb_hrm_user u\n" +
                            " inner join eteams.employee e on e.mobile  = u.mobile \n" +
                            " where e.delete_type = 0\n" +
                            " and u.delete_type  = 0\n" +
                            " and e.id =? and u.tenant_key = ? and e.tenant_key = ? " ;

            log.error("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("recordList:{}",recordList.size());
            if(recordList.size() > 0){
                openId = String.valueOf(recordList.get(0).get("open_id"));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:" + e);
        }
        return openId;
    }

}
