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 SealsManageDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     *
     * @return
     */
    public List<Map<String,Object>> queryLegalEntityList(){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select bh,mc from uf_legal_entity where tenant_key=? \n" +
                            " and delete_type=0" ;

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


    /***
     *
     * @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("getDepartmentCodeById:e:" + e);
        }
        return dataid;
    }

}
