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

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

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

    @Autowired
    private DatabaseUtils databaseUtils;

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

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



    public List<Map<String,Object>> queryTemplateFieldByTemplateId(String templateId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select k.zdm ,k.zdlx ,k.zdlx_obj ,k.zdz ,k.zdmc \n" +
                            " from uf_dzqmbgl t\n" +
                            " inner join uf_dzqmbgl_zdb k on k.form_data_id = t.form_data_id \n" +
                            " where t.mbid=? and t.tenant_key=? and t.delete_type=0 and k.tenant_key=? and k.delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(templateId);
            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("getDepartmentCodeById:e:" + e);
        }
        return recordList;
    }


    /***
     *
     * @return
     */
    public String queryValueByConfig(String dataSql){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String dataid = "";
        try{

            Map<String, Object> result = databaseUtils.execute(sourceType, groupId, dataSql);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            if(recordList.size() > 0){
                Map<String, Object> recordMap = recordList.get(0);
                dataid = String.valueOf(recordMap.values().stream().findFirst().orElse(null));
            }
        }catch (Exception e){
            log.error("getDepartmentCodeById:e:" + e);
        }
        return dataid;
    }


    public List<Map<String,Object>> queryTemplateFieldByType(String templateId,String type){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select k.zdm ,k.zdlx ,k.zdlx_obj ,k.zdz ,k.zdmc,k.zdz as rc \n" +
                            " from uf_dzqmbgl t\n" +
                            " inner join uf_dzqmbgl_zdb k on k.form_data_id = t.form_data_id \n" +
                            " where t.mbid=? and t.tenant_key=? and k.zdlx in("+type+") " +
                            " and t.delete_type=0 and k.tenant_key=? and k.delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(templateId);
            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("getDepartmentCodeById:e:" + e);
        }
        return recordList;
    }


}
