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

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

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @param sealid
     * @return
     */
    public String querySealsDataBySealId(String sealid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String dataid = "";
        try{
            String dataSql =" select id from " + Constants.sealTable+
                            " where sealid = ? and  tenant_key=? \n" +
                            " and delete_type=0 " ;

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


    public List<Map<String, Object>> querySealsDetailByRequestid(String requestid){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select l.ykyyzid,l.yz,l.yysl,l.qfzcs  \n" +
                            " from ft_jjbc k\n" +
                            " inner join ft_jjbc_mxb2 l on l.form_data_id  = k.form_data_id \n" +
                            " where k.id = ?  " +
                            " and k.tenant_key=?  " +
                            " and k.delete_type=0 " +
                            " and l.tenant_key=?  " +
                            " and l.delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(requestid);
            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);
            recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:{}", recordList.size());

        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return recordList;
    }


}
