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

import com.alibaba.fastjson.JSON;
import com.weaver.common.base.entity.result.WeaResult;
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 UpdatePerformanceDao {

    private final static Logger log = LoggerFactory.getLogger(UpdatePerformanceDao.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 bm,yjtbnf,zq,dxwb_p8r4,txktbt,qz,dxwb_3m7o,txkt2bt,qz2,kts,txkt3bt,qz3 " +
                            " from ft_msmbjx " +
                            " where form_data_id = ?\n" +
                            " and tenant_key=?\n" +
                            " and delete_type=0 " ;


            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.dxwb_ckts,t.dxwb_07dy\n" +
                            " from ft_msmbjx_kty t \n" +
                            " where t.form_data_id = ? \n" +
                            " and t.tenant_key=? \n" +
                            " and t.delete_type=0 \n" +
                            " order by data_index\n" ;

            log.error("queryDt1DataByFormDataId--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);
            recordList = databaseUtils.getDataSourceList(result);
            log.error("queryDt1DataByFormDataId-recordList:"+recordList.size());

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


    public List<Map<String,Object>> queryDt2DataByFormDataId(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.dxwb_ckts,t.dxwb_07dy\n" +
                    " from ft_msmbjx_kte t \n" +
                    " where t.form_data_id = ? \n" +
                    " and t.tenant_key=? \n" +
                    " and t.delete_type=0 \n" +
                    " order by data_index\n" ;

            log.error("queryDt1DataByFormDataId--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);
            recordList = databaseUtils.getDataSourceList(result);
            log.error("queryDt1DataByFormDataId-recordList:"+recordList.size());

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


    public List<Map<String,Object>> queryDt3DataByFormDataId(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.dxwb_ckts,t.dxwb_07dy\n" +
                    " from ft_msmbjx_kts t \n" +
                    " where t.form_data_id = ? \n" +
                    " and t.tenant_key=? \n" +
                    " and t.delete_type=0 \n" +
                    " order by data_index\n" ;

            log.error("queryDt1DataByFormDataId--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);
            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
     * @return
     */
    public  Map<String, Object> updateFormDataByFormdataid(String tenant_key,String upSet,String bm,String yjtbnf,String zq,String bmfzr){

        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result2 = new HashMap<String, Object>();

        if(StringUtils.isNotBlank(bm)){
            String upSql =  "  update ft_msmbjx set \n" + upSet+
                            "  where tjr = ? \n" +
                            "  and bm = ? \n" +
                            "  and tenant_key = ? and delete_type = 0  \n" +
                            "  and yjtbnf = ? and zq = ?" ;

            log.error("upSql22:"+upSql);
            List<String> paramList2 = new ArrayList<>(100);
            paramList2.add(bmfzr);
            paramList2.add(bm);
            paramList2.add(tenant_key);
            paramList2.add(yjtbnf);
            paramList2.add(zq);
            List<SqlParamEntity> sqlParamList2 = databaseUtils.querySqlParamEntity(paramList2);
            result2 = databaseUtils.executeForQuery(sourceType, groupId, upSql, sqlParamList2);
        }
        return result2;
    }


    public  Map<String, Object> updateFormDataByDeptManager(String tenant_key,String upSet,String bm,String yjtbnf,String zq,Long userid){

        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result2 = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(bm)){
            String upSql =  "  update ft_msmbjx set \n" + upSet+
                            "  where bm ='"+bm+"'  and tjr <> '"+ userid+"'" +
                            "  and tenant_key = ? and delete_type = 0  " +
                            "  and yjtbnf = ? and zq = ? " ;

            log.error("upSql2:"+upSql);
            List<String> paramList2 = new ArrayList<>(100);
            paramList2.add(tenant_key);
            paramList2.add(yjtbnf);
            paramList2.add(zq);
            List<SqlParamEntity> sqlParamList2 = databaseUtils.querySqlParamEntity(paramList2);
            result2 = databaseUtils.executeForQuery(sourceType, groupId, upSql, sqlParamList2);
            log.error(JSON.toJSONString(result2));
        }
        return result2;
    }

    public boolean compareDepartmentManagerByUser(String tenant_key,long userid,String deptid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        boolean isTrue = false;
        try{
            String dataSql =" select t2.id\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("compareDepartmentManagerByUser--dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(String.valueOf(userid));
            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("compareDepartmentManagerByUser:"+recordList.size());
            if(recordList.size()>0) {
                isTrue = true;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("compareDepartmentManagerByUser:e:" + e);
        }
        return isTrue;
    }

    /***
     *
     * @param tenant_key
     * @param deptid
     * @return
     */
    public List<Map<String, Object>> queryDepartmentManager(String tenant_key,String deptid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select k.id,k.name,t2.bmfzr \n" +
                            "    from eteams.department k\n" +
                            "    inner join ft_1090502818035523594 t2 on k.formdata = t2.form_data_id\n" +
                            "    where k.parent = ? \n" +
                            "    and k.tenant_key = ?  and k.delete_type = 0\n" +
                            "    and t2.tenant_key = ?  and t2.delete_type = 0" ;

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


}
