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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.meishi.util.DatabaseUtils;
import org.apache.commons.lang3.StringUtils;
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 CreatePerformanceDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @param tenant_key
     * @param form_data_id
     * @return
     */
    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 tjr,tjrbm,rq_urvy,zq " +
                    " from ft_mbtxrysjb " +
                    " where form_data_id = ?\n" +
                    " and tenant_key=?\n" +
                    " and delete_type=0 \n" ;

            log.error("queryMainDataByFormDataId--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("queryMainDataByFormDataId-e:" + e);
        }
        return recordMap;
    }


    /***
     *
     * @param tenant_key
     * @param form_data_id
     * @return
     */
    public List<Map<String,Object>> queryDt1DataByFormDataId(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 t.id,t.xm,t4.username,t4.location,t.bm,k.name as deptname,k2.id as sjbm,t2.bmfzr as sjbmfzr,t3.bmcj,t3.bmfzr \n" +
                            " from ft_mbtxrys_rymx t\n" +
                            " inner join (select id,username,location from eteams.employee where delete_type=0 and tenant_key=?) as t4 on t.xm = t4.id " +
                            " inner join eteams.department k on k.id = t.bm\n" +
                            " left join (select form_data_id,bmfzr,bmcj from ft_1090502818035523594 where delete_type=0 and tenant_key=?) as t3 on k.formdata = t3.form_data_id\n" +
                            " left join (select id,formdata,tenant_key from eteams.department where type='department' and delete_type=0 and tenant_key=?) as k2 on k2.id = k.parent \n" +
                            " left join (select form_data_id,bmfzr from ft_1090502818035523594 where delete_type=0 and  tenant_key=? ) as t2 on k2.formdata = t2.form_data_id\n" +
                            " where t.form_data_id = ?\n" +
                            " and t.sfcj = 0 \n" +
                            " and t.gllc is null \n" +
                            " and t.tenant_key=? \n" +
                            " and t.delete_type=0 \n" +
                            " and k.type='department'\n" +
                            " and k.tenant_key=?\n" +
                            " and k.delete_type=0\n" ;

            log.error("queryDt1DataByFormDataId--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(tenant_key);
            paramList.add(form_data_id);
            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);
            log.error("queryDt1DataByFormDataId-recordList:"+recordList.size());

        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }
        return recordList;
    }

    /**
     *
     * @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','sjbm','sjbmfzr','zlc','zc','sfbmfzr','ryxz_3ezm','bgdd')\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;
    }

    /***
     *
     * @param tenant_key
     * @param requestid
     * @param dtid
     * @param msg
     * @return
     */
    public  Map<String, Object> updateRequestid2FormTable(String tenant_key,String requestid,String dtid,String msg){

        log.error("updateRequestid2FormTable:requestid:"+requestid);
        log.error("updateRequestid2FormTable:dtid:"+dtid);
        log.error("updateRequestid2FormTable:msg:"+msg);

        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result2 = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(requestid)){
            String dataSql = " update ft_mbtxrys_rymx set gllc= ? where id = ?  and tenant_key = ? and delete_type = 0  " ;
            List<String> paramList2 = new ArrayList<>(100);
            paramList2.add(requestid);
            paramList2.add(dtid+"");
            paramList2.add(tenant_key);
            List<SqlParamEntity> sqlParamList2 = databaseUtils.querySqlParamEntity(paramList2);
            result2 = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList2);

        }else{
            String dataSql = " update ft_mbtxrys_rymx set cwxx=? where id = ?  and tenant_key = ? and delete_type = 0  " ;
            List<String> paramList2 = new ArrayList<>(100);
            paramList2.add(msg);
            paramList2.add(dtid+"");
            paramList2.add(tenant_key);
            List<SqlParamEntity> sqlParamList2 = databaseUtils.querySqlParamEntity(paramList2);
            result2 = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList2);
        }
        return result2;
    }

    /***
     *
     * @param tenant_key
     * @param bmfzr
     * @param deptid
     * @return
     */
    public String queryDepartmentLevel(String tenant_key,long bmfzr,String deptid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String bmcj = "";
        try{
            String dataSql =" select t2.bmcj\n" +
                    " from ft_1090502818035523594 t2\n" +
                    " inner join eteams.department k on k.formdata = t2.form_data_id\n" +
                    " where t2.bmfzr = ? \n" +
                    " and k.id= ? " +
                    " and k.tenant_key=? \n" +
                    " and k.delete_type=0" +
                    " and t2.tenant_key=? \n" +
                    " and t2.delete_type=0" ;

            log.error("queryDepartmentLevel--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(String.valueOf(bmfzr));
            paramList.add(String.valueOf(deptid));
            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("queryDepartmentLevel:"+recordList.size());
            if(recordList.size()>0) {
                bmcj = String.valueOf(recordList.get(0).get("bmcj"));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return bmcj;
    }
}
