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.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.List;
import java.util.Map;

@Component
public class WorkflowBotMessageDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public String queryUserBotMessageId(String requestid,String nodeid,String operatorid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String message_id = "";
        try{
            String dataSql =" select czzid,message_id \n" +
                            " from uf_bot_message \n" +
                            " where lcid= ? and jdid = ? and czzid = ? " +
                            " and delete_type = 0 and tenant_key = ? " +
                            " order by create_time desc " ;

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

}
