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

@Component
public class SignFlowCallBackDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     *
     * @return
     */
    public Map<String,Object> queryFlowInfo(String flowId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> dataMap = new HashMap<String,Object>();
        try{
            String dataSql =" select form_data_id,glhtlylc from uf_jcl_rshtgl where qslcid =? and tenant_key=? \n" +
                            " and delete_type=0" ;

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


    public String queryRequestName(String requestid){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
        String requestname = "";
        try{
            String dataSql =" select requestname from wfc_requestbase wr where requestid =? and tenant_key=? \n" +
                            " and delete_type=0" ;

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


    public void updateFormTableInfo(String form_data_id,Long fileid,String finishFileKey){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        try{
            String dataSql =" update uf_jcl_rshtgl set wjid='"+finishFileKey+"',fj_6pgb='"+String.valueOf(fileid)+"' where form_data_id ='"+form_data_id+"' and tenant_key='"+Constants.TENANT_KEY+"' \n" +
                            " and delete_type=0" ;

            log.error("dataSql:{}" , dataSql);
            Map<String, Object> result = databaseUtils.executeUpdate(sourceType, groupId, dataSql);
            for (Map.Entry<String, Object> entry : result.entrySet()) {
                log.error(entry.getKey() + ": " + String.valueOf(entry.getValue()));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
    }
}
