package com.engine.htsc.fzjgtravel.Dao;

import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public  class SubsidyDao {
    public  List<Map<String,Object>> getWorkflowInfo_old(Map<String,Object> map){

        String startdate=Util.null2String(map.get("startdate"));
        String enddate=Util.null2String(map.get("enddate"));
        String type=Util.null2String(map.get("type"));
        String numbertable = new PropBean().getPropName("travelsubsidy_"+type);
        String tablename= JAVATools.GetUnumbertotable(numbertable);
        if(tablename==null||tablename.equals("")){
            return  null;
        }

        String sql="select distinct t1.requestid," +
                "       t1.ngr," +
                "       t1.lcbh," +
                "       t2.requestname," +
                "       t1.glcclc," +
                "       t2.LASTOPERATEDATE," +
                "     (select wm_concat_old(dt12.id) from {3}_dt12 dt12 where dt12.mainid=t1.id) as detailidsum, "+
                "       t3.ccr, (select workcode from hrmresource hr where  hr.id= t3.ccr )  as workcode" +
                "  from {0} t1" +
                "  left join workflow_requestbase t2" +
                "    on t1.requestid = t2.requestid" +
                "  left join {1}_dt12 t3" +
                "    on t1.id = t3.mainid" +
                " where  t1.sfzf=1 and t1.ngr != 1 and t1.sfldbx=0 and t2.currentnodetype = 3 ";
        sql= MessageFormat.format(sql,tablename,tablename,tablename,tablename);

        if(startdate!=null && !"".equals(startdate)){
            sql += " and to_date(LASTOPERATEDATE,'yyyy-MM-dd')>=to_date('"+startdate+"','yyyy-MM-dd') ";
        }
        if(enddate!=null && !"".equals(enddate)){
            sql += " and to_date(LASTOPERATEDATE,'yyyy-MM-dd')<=to_date('"+enddate+"','yyyy-MM-dd') ";
        }
        JAVATools.SetCmdwritelog("分支机构差旅报销",sql);
        return  JAVATools.GetTableToListMap(sql,null);
    }

    public List<Map<String,Object>> getWorkflowInfo(Map<String,Object> map){

        String startdate=JAVATools.getpreventsql(Util.null2String(map.get("startdate")));
        String enddate=JAVATools.getpreventsql(Util.null2String(map.get("enddate")));
        String type=Util.null2String(map.get("type"));
        String numbertable = new PropBean().getPropName("travelsubsidy_"+type);
        String tablename=JAVATools.GetUnumbertotable(numbertable);


        String sql="select distinct t1.requestid," +
                "  t1.ngr," +
                " t1.lcbh," +
                " t2.requestname," +
                " t1.glcclc," +
                " t2.LASTOPERATEDATE," +
                " t3.ccr  as bxr, (select workcode from hrmresource hr where  hr.id= t3.ccr )  as workcode, " +
                " t3.yxzh," +
                " t3.khxmc," +
                " t3.yxzfje," +
                " (select mt.departmentcode from HRMDEPARTMENT mt where mt.id=t3.bm) as departmentcode ," +
                " (select mt.departmentname from HRMDEPARTMENT mt where mt.id=t3.bm) as departmentname " +
                " from " +
                " {0} t1 " +
                " left join workflow_requestbase t2 " +
                " on t1.requestid = t2.requestid " +
                " left join {1}_dt10 t3 " +
                " on t1.id = t3.mainid " +
                " where  t1.sfzf=1 and t1.ngr != 1 and t1.sfldbx=0 and t2.currentnodetype = 3 " +
                " and t3.yxzfje is not null and t3.yxzfje >0 ";
        sql= MessageFormat.format(sql,tablename,tablename);

        if(startdate!=null && !"".equals(startdate)){
            // startdate=JAVATools.getpreventsql(startdate);
            sql += " and to_date(LASTOPERATEDATE,'yyyy-MM-dd')>=to_date('"+startdate+"','yyyy-MM-dd') ";
        }
        if(enddate!=null && !"".equals(enddate)){
            //   enddate=JAVATools.getpreventsql(enddate);
            sql += " and to_date(LASTOPERATEDATE,'yyyy-MM-dd')<=to_date('"+enddate+"','yyyy-MM-dd') ";
        }





        JAVATools.SetCmdwritelog("分支机构差旅报销",sql);
        return  JAVATools.GetTableToListMap(sql,null);
    }


    public List<Map<String,Object>> getsubsidyinfo(Map<String,String> map){
        String detailidsum=Util.null2String(map.get("detailidsum"));
        String requestid=Util.null2String(map.get("requestid"));
        if(requestid==null||requestid.equals("") || detailidsum==null||detailidsum.equals("")){
            return  null;
        }
        String detailid=Arrays.asList(detailidsum.split(",")).stream().distinct().map(item->"\'\'"+item+"\'\'").collect(Collectors.joining(","));
        String tablename=JAVATools.getMaintable(requestid);
        String sql="select dt12.bt, dt12.cclc, dt12.cclccn,(select workcode from hrmresource hr where  hr.id= dt12.ccr ) as ccr  from {0}_dt12 dt12  " +
                "where dt12.id in ("+detailid+")";
        sql=  MessageFormat.format(sql,tablename);
        JAVATools.SetCmdwritelog("分支机构差旅接口查询",sql);
        return JAVATools.GetTableToListMap(sql,null);
    }


    public Map<String,Object> getbankCardNum(Map<String,String> map){
        String requestid=Util.null2String(map.get("requestid"));
        String ccr=Util.null2String(map.get("ccr"));
        if(requestid==null||requestid.equals("") || ccr==null||ccr.equals("")){
            return  null;
        }
        String ccrstr=Arrays.asList(ccr.split(",")).stream().distinct().map(item->"\'\'"+item+"\'\'").collect(Collectors.joining(","));
        String tablename=JAVATools.getMaintable(requestid);
        String sql=" select distinct yxzh,khxmc " +
                "   from {0}_dt10 " +
                "  where mainid in    (select id from {1} where requestid = ''{2}'')" +
                "    and ccr in ( select id from hrmresource where workcode in ("+ccrstr+") )";
        sql=  MessageFormat.format(sql,tablename,tablename,requestid,ccrstr);
        JAVATools.SetCmdwritelog("分支差旅接口查询",sql);
        return JAVATools.GetTableToMap(sql,null);

    }


    public Map<String,Object> getuserbaseinfo(String userid ){

        if(userid==null||userid.equals("")){
            return  null;
        }
        String ccrstr= Arrays.stream(userid.split(",")).map(item->"\'"+item+"\'").collect(Collectors.joining(","));


        String sql="select   t2.lastname,t2.workcode," +

                "               t3.DEPARTMENTCODE, " +
                "               t4.SUBCOMPANYCODE, " +
                "               t2.id " +
                "           from   hrmresource t2     " +
                "          left join hrmdepartment t3 on t2.departmentid = t3.id" +
                "          left join hrmsubcompany t4 on t4.id = t2.SUBCOMPANYID1 " +
                " where rownum = 1 and t2.workcode in ({0})";
        sql= MessageFormat.format(sql,ccrstr);

        JAVATools.SetCmdwritelog("分支机构接口查询",sql);
        return JAVATools.GetTableToMap(sql,null);

    }

}
