package com.weaver.seconddev.meishi.esb.dao;

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.meishi.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.*;


@Component
public class PerformanceSubmitDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;


    public Map<String,Object> queryMainDataByFormDataId(String tenant_key,String form_data_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> recordMap = new HashMap<String,Object>();
        try{
            String dataSql =" select yjtbnf,zq,bm,bmlx " +
                            " from ft_1090784675255934977 " +
                            " where form_data_id = ?\n" +
                            " and tenant_key=?\n" +
                            " and delete_type=0 \n" ;

            log.error("querykhfl--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(form_data_id);
            paramList.add(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("queryMainDataByFormDataId-recordList:"+recordList.size());
            if(recordList.size()>0){
                recordMap = recordList.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return recordMap;
    }

    /***
     *
     * @param tenant_key
     * @return
     */
    public List<Map<String, Object>> queryCreateUserByFormDataId(String tenant_key,String bmcj,String sqrbm){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();

        try{
            String dataSql =" select h.id,h.username,h.department,k.name as deptname,k.parent as sjbm,t2.bmfzr as sjbmfzr \n" +
                            " from eteams.employee h \n" +
                            " left join eteams.department k on k.id = h.department\n" +
                            " left join eteams.department k2 on k2.id = k.parent\n" +
                            " left join ft_1090502818035523594 t2 on k2.formdata = t2.form_data_id" +
                            " where h.id in(" +
                            "   select p.bmfzr \n" +
                            "   from ft_1090502818035523594 p\n" +
                            "   inner join eteams.department k on k.formdata = p.form_data_id\n" +
                            "   where p.bmcj = ? " +
                            "   and p.tenant_key=? \n" +
                            "   and p.delete_type=0 \n" +
                            "   and k.type='department' \n" +
                            "   and k.parent=? \n" +
                            "   and k.tenant_key=? \n" +
                            "   and k.delete_type=0 " +
                            " )  " +
                            " and h.tenant_key=? \n" +
                            " and h.delete_type=0 \n" +
                            " and t2.delete_type=0 \n"+
                            " and t2.tenant_key=? \n" +
                            " and k.type='department' "+
                            " and k.tenant_key=? \n" +
                            " and k.delete_type=0 \n"+
                            " and k2.type='department' "+
                            " and k2.tenant_key=? \n" +
                            " and k2.delete_type=0 \n";

            log.error("querykhfl--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(bmcj);
            paramList.add(tenant_key);
            paramList.add(sqrbm);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(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){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return recordList;
    }


    /**
     *
     * @param tenant_key
     * @param form_data_id
     * @return
     */
    public List<Map<String, Object>> queryFormtableDt2ByTopLeader(String tenant_key,String form_data_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select kt,qz \n" +
                            " from ft_1090784_mbtx \n" +
                            " where 1=1\n" +
                            " and tenant_key=? \n" +
                            " and delete_type=0\n" +
                            " and form_data_id =? " ;

            log.error("querykhfl--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(tenant_key);
            paramList.add(form_data_id+"");

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            Map<String, Object> result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
            recordList = databaseUtils.getDataSourceList(result);
        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return recordList;
    }


    public Map<String,Object> queryDeptManagerByDeptid(String tenant_key,String deptId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> recordMap = new HashMap<String,Object>();
        String bmfzr = "";
        try{
            String dataSql =" select t.bmfzr,k.parent sjbm,t2.bmfzr sjbmfzr\n" +
                            " from ft_1090502818035523594 t\n" +
                            " inner join eteams.department k on k.formdata=t.form_data_id\n" +
                            " left join eteams.department k2 on k2.id = k.parent \n" +
                            " left join ft_1090502818035523594 t2 on k2.formdata = t2.form_data_id\n" +
                            " where k.type='department' \n" +
                            " and k2.type='department' \n" +
                            " and k.id =? \n" +
                            " and t.tenant_key=? \n" +
                            " and t.delete_type=0\n" +
                            " and k.tenant_key=? \n" +
                            " and k.delete_type=0\n" +
                            " and k2.tenant_key=? \n" +
                            " and k2.delete_type=0\n" +
                            " and t2.tenant_key=? \n" +
                            " and t2.delete_type=0\n" ;

            log.error("querykhfl--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(deptId);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(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("querykhfl-recordList:"+recordList.size());
            if(recordList.size()>0){
                recordMap = recordList.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return recordMap;
    }

    /***
     *
     * @param tenant_key
     * @param form_data_id
     * @return
     */
    public Map<String,List<String>> queryFormtableDt2(String tenant_key,String form_data_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,List<String>> dataMap = new HashMap<String,List<String>>();
        try{
            String dataSql =" select bmxz_t3k6 as bmxz,ryxz_odvm as ryxz,kt \n" +
                            " from ft_1090784_mbtx \n" +
                            " where 1=1\n" +
                            " and tenant_key=? \n" +
                            " and delete_type=0\n" +
                            " and form_data_id =? " ;

            log.error("querykhfl--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(tenant_key);
            paramList.add(form_data_id+"");

            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("querykhfl-recordList:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ){
                String bmxz = String.valueOf(recordMap.get("bmxz"));
//                String ryxz = String.valueOf(recordMap.get("ryxz"));
                String kt = String.valueOf(recordMap.get("kt"));

                String[] bmArray = bmxz.split(",");
                for(int i=0;i<bmArray.length;i++){
                    String bmid = bmArray[i];
                    if(dataMap.containsKey(bmid)){
                        List<String> ktList = dataMap.get(bmid);
                        if(!ktList.contains(kt)){
                            ktList.add(kt);
                        }
                        dataMap.put(bmid,ktList);
                    }else{
                        List<String> ktList = new ArrayList<String>();
                        ktList.add(kt);
                        dataMap.put(bmid,ktList);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return dataMap;
    }

    /***
     *
     * @param tenant_key
     * @param employeeId
     * @return
     */
    public Map<String, Object> queryUserInfoByMiddleLeader(String tenant_key,String employeeId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> recordMap = new HashMap<String, Object>();
        try{
            String dataSql =" select h.id,h.username,h.department,k.name as deptname\n" +
                            " from eteams.employee h\n" +
                            " inner join eteams.department k on k.id = h.department\n" +
                            " where h.id = ?" +
                            " and k.tenant_key=? \n" +
                            " and k.delete_type=0 " +
                            " and k.type='department' \n" +
                            " and h.tenant_key=? \n" +
                            " and h.delete_type=0 \n" ;

            log.error("querykhfl--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(employeeId);
            paramList.add(tenant_key);
            paramList.add(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("queryUserInfo-recordList:"+recordList.size());
            if(recordList.size()>0){
                recordMap = recordList.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return recordMap;
    }


    /**
     *
     * @param tenant_key
     * @param form_id
     * @return
     */
    public Map<String, Object> queryMainTableField(String tenant_key,Long form_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        try{
            String dataSql =" select id,form_id,title,data_key " +
                            " from form_field " +
                            " where form_id=? \n" +
                            " and data_key in('tjr','bm','yjtbnf','zq','bmlx','sfzdcf','sjbm','sjbmfzr')\n" +
                            " and sub_form_id is null " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("queryFromTableField--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(form_id+"");
            paramList.add(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("queryFromTableField:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String data_key = String.valueOf(recordMap.get("data_key"));
                String id = String.valueOf(recordMap.get("id"));
                fieldMap.put(data_key,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return fieldMap;
    }


    public Map<String, Object> queryDetailTableField(String tenant_key,Long form_id,String sub_form_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        try{
            String dataSql =" select id,form_id,title,data_key " +
                    " from form_field " +
                    " where form_id=? \n" +
                    " and data_key in('kt','qz')\n" +
                    " and sub_form_id in('"+sub_form_id+"')\n"+
                    " and tenant_key=? \n" +
                    " and delete_type=0" ;

            log.error("queryFromTableField--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(form_id+"");
            paramList.add(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("queryDetailTableField:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String data_key = String.valueOf(recordMap.get("data_key"));
                String id = String.valueOf(recordMap.get("id"));
                fieldMap.put(data_key,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return fieldMap;
    }



    public  Map<String, Object> updateRequestid2FormTable(String tenant_key,String requestids,String lcid){

        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";

        String dataSql = " update ft_1090784675255934977 set zlc= ? where id = ?  and tenant_key = ? and delete_type = 0  " ;
        List<String> paramList2 = new ArrayList<>(100);
        paramList2.add(requestids);
        paramList2.add(lcid+"");
        paramList2.add(tenant_key);
        List<SqlParamEntity> sqlParamList2 = databaseUtils.querySqlParamEntity(paramList2);
        Map<String, Object> result2 = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList2);
        return result2;
    }



}
