package com.engine.htsc.duban.util;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CoworkReportUtil {


    public Map<String,Object> allList(String userid, int page, int size){
        Map<String,Object> dataMap = new HashMap<String,Object>();

        RecordSet rs = new RecordSet();

        String allxzwhere = "";
        String allrwwhere = "";

        String userids = "";
        rs.executeQuery(" select id from hrmresourcemanager where id=? and status = 1 ",userid);
        while (rs.next())
        {
            String id = Util.null2String(rs.getString("id"));

            allxzwhere += allxzwhere =="" ? " (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0 or t.cjr="+id+")" : " or (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0 or t.cjr="+id+")";

            userids += userids == "" ? id : ","+id ;
        }
        if(!"".equals(allxzwhere))
        {
            allxzwhere = " and ("+ allxzwhere +")";
        }

        String xzsql =  " select distinct u.* from ( \n" +
                "   select t.bt,t.xzbh as bh,t.cjr,t.cjrq,t.xzzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzsj,'0' as type\n" +
                "   from uf_xzsx t " +
                "   left join modeinfo h on h.id = t.formmodeid" +
                "   where cjzt= 1 \n" + allxzwhere +
                "   union all \n" +
                "   select t.bt,t.xzbh as bh,t.cjr,t.cjrq,t.xzzt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzsj,'0' as type \n" +
                "   from uf_xzsx t \n" +
                "   inner join uf_xzsx_dt1 l on l.mainid = t.id \n" +
                "   left join modeinfo h on h.id = t.formmodeid" +
                "   where cjzt= 1 and l.bgxr in("+userids+") " +
                " ) u where 1=1" ;
        System.out.println("xzsql:"+xzsql);

        String rwsql =  " select distinct u.* from (" +
                " select t.rwbt as bt,t.rwbh as bh,t.cjr,t.cjrq,t.rwzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzrq as jzsj,'1' as type\n" +
                " from uf_dbrw t \n" +
                " left join modeinfo h on h.id = t.formmodeid\n" +
                " where cjzt= 1 \n" + allrwwhere +
                " union all \n" +
                " select t.rwbt as bt,t.rwbh as bh,t.cjr,t.cjrq,t.rwzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzrq as jzsj,'1' as type\n" +
                " from uf_dbrw t \n" +
                " inner join uf_dbrw_dt2 l on l.mainid = t.id \n" +
                " left join modeinfo h on h.id = t.formmodeid\n" +
                " where cjzt= 1 and l.bgxr in("+userids+") "+
                " ) u where 1=1 " ;

        Map<String,Object> wksMap = typeList(xzsql,rwsql,"1","wks",page,size);

        Map<String,Object> jxzMap = typeList(xzsql,rwsql,"2","jxz",page,size);

        Map<String,Object> ywcMap = typeList(xzsql,rwsql,"3","ywc",page,size);

        dataMap.putAll(wksMap);
        dataMap.putAll(jxzMap);
        dataMap.putAll(ywcMap);
        return dataMap;
    }




    /***
     *
     * @return
     */
    public Map<String,Object> wcjList(String userid,int page,int size){

        Map<String,Object> dataMap = new HashMap<String,Object>();

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String wcjwhere = "";
        String userids = "";
        rs.executeQuery(" select id from hrmresourcemanager where id=? and status = 1 ",userid);
        while (rs.next())
        {
            String id = Util.null2String(rs.getString("id"));

            wcjwhere += wcjwhere =="" ? " t.cjr="+id : " or t.cjr="+id;

            userids += userids == "" ? id : ","+id ;
        }
        if(!"".equals(wcjwhere))
        {
            wcjwhere = " and ("+ wcjwhere +")";
        }

        String xzsql =  " select distinct u.* from ( \n" +
                "   select t.bt,t.xzbh as bh,t.cjr,t.cjrq,t.xzzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzsj,'0' as type\n" +
                "   from uf_xzsx t " +
                "   left join modeinfo h on h.id = t.formmodeid" +
                "   where cjzt= 1 \n" + wcjwhere +
                "   union all \n" +
                "   select t.bt,t.xzbh as bh,t.cjr,t.cjrq,t.xzzt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzsj,'0' as type \n" +
                "   from uf_xzsx t \n" +
                "   inner join uf_xzsx_dt1 l on l.mainid = t.id \n" +
                "   left join modeinfo h on h.id = t.formmodeid" +
                "   where cjzt= 1 and l.bgxr in("+userids+") " +
                " ) u where 1=1" ;
        System.out.println("xzsql:"+xzsql);

        String rwsql =  " select distinct u.* from (" +
                " select t.rwbt as bt,t.rwbh as bh,t.cjr,t.cjrq,t.rwzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzrq as jzsj,'1' as type\n" +
                " from uf_dbrw t \n" +
                " left join modeinfo h on h.id = t.formmodeid\n" +
                " where cjzt= 1 \n" + wcjwhere +
                " union all \n" +
                " select t.rwbt as bt,t.rwbh as bh,t.cjr,t.cjrq,t.rwzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzrq as jzsj,'1' as type\n" +
                " from uf_dbrw t \n" +
                " inner join uf_dbrw_dt2 l on l.mainid = t.id \n" +
                " left join modeinfo h on h.id = t.formmodeid\n" +
                " where cjzt= 1 and l.bgxr in("+userids+") "+
                " ) u where 1=1 " ;


        Map<String,Object> wksMap = typeList(xzsql,rwsql,"1","wks",page,size);

        Map<String,Object> jxzMap = typeList(xzsql,rwsql,"2","jxz",page,size);

        Map<String,Object> ywcMap = typeList(xzsql,rwsql,"3","ywc",page,size);

        dataMap.putAll(wksMap);
        dataMap.putAll(jxzMap);
        dataMap.putAll(ywcMap);
        return dataMap;
    }

    /***
     *
     * @return
     */
    public Map<String,Object> wcyList(String userid,int page,int size){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        RecordSet rs = new RecordSet();
        String xzwcywhere = "";
        String rwwcywhere = "";
        String userids = "";
        rs.executeQuery(" select id from hrmresourcemanager where id=? and status = 1 ",userid);
        while (rs.next())
        {
            String id = Util.null2String(rs.getString("id"));

            xzwcywhere += xzwcywhere =="" ? " (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0 )" : " or (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydr||',',',"+id+",')>0)";

            rwwcywhere += rwwcywhere =="" ? " (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydry||',',',"+id+",')>0 )" : " or (instr(','||t.jbr||',',',"+id+",')>0 or instr(','||t.tzydry||',',',"+id+",')>0)";

            userids += userids == "" ? id : ","+id ;
        }

        if(!"".equals(xzwcywhere))
        {
            xzwcywhere = " and ("+ xzwcywhere +")";
        }

        if(!"".equals(rwwcywhere))
        {
            rwwcywhere = " and ("+ rwwcywhere +")";
        }

        String xzsql =  " select distinct u.* from ( \n" +
                "   select t.bt,t.xzbh as bh,t.cjr,t.cjrq,t.xzzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzsj,'0' as type\n" +
                "   from uf_xzsx t " +
                "   left join modeinfo h on h.id = t.formmodeid" +
                "   where cjzt= 1 \n" + xzwcywhere +
                "   union all \n" +
                "   select t.bt,t.xzbh as bh,t.cjr,t.cjrq,t.xzzt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzsj,'0' as type \n" +
                "   from uf_xzsx t \n" +
                "   inner join uf_xzsx_dt1 l on l.mainid = t.id \n" +
                "   left join modeinfo h on h.id = t.formmodeid" +
                "   where cjzt= 1 and l.bgxr in("+userids+") " +
                " ) u where 1=1" ;
        System.out.println("xzsql:"+xzsql);

        String rwsql =  " select distinct u.* from (" +
                " select t.rwbt as bt,t.rwbh as bh,t.cjr,t.cjrq,t.rwzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzrq as jzsj,'1' as type\n" +
                " from uf_dbrw t \n" +
                " left join modeinfo h on h.id = t.formmodeid\n" +
                " where cjzt= 1 \n" + rwwcywhere +
                " union all \n" +
                " select t.rwbt as bt,t.rwbh as bh,t.cjr,t.cjrq,t.rwzt as zt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.modelableid,t.jzrq as jzsj,'1' as type\n" +
                " from uf_dbrw t \n" +
                " inner join uf_dbrw_dt2 l on l.mainid = t.id \n" +
                " left join modeinfo h on h.id = t.formmodeid\n" +
                " where cjzt= 1 and l.bgxr in("+userids+") "+
                " ) u where 1=1 " ;


        Map<String,Object> wksMap = typeList(xzsql,rwsql,"1","wks",page,size);

        Map<String,Object> jxzMap = typeList(xzsql,rwsql,"2","jxz",page,size);

        Map<String,Object> ywcMap = typeList(xzsql,rwsql,"3","ywc",page,size);

        dataMap.putAll(wksMap);
        dataMap.putAll(jxzMap);
        dataMap.putAll(ywcMap);
        return dataMap;
    }


    public Map<String,Object> typeList(String xzsql,String rwsql,String xzzt,String type,int page,int size){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        ResourceComInfo resComInfo;
        if("".equals(type)){
            return dataMap;
        }
        try{
            resComInfo = new ResourceComInfo();
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            RecordSet rs = new RecordSet();
            String rwztwhere =" and u.zt="+xzzt;
            String xzztwhere =" and u.zt="+xzzt;

            xzsql = xzsql + xzztwhere ;
            rwsql = rwsql + rwztwhere ;

            String sql  =  " select rownum as px,l.* \n" +
                    " from (\n" + xzsql +
                    " union all"+ rwsql+
                    " ) l " +
                    " order by l.cjrq desc " ;

            System.out.println("sql:"+sql);
            int count = 0;
            String cousql =  " select count(w.id) as cou from ("+sql+") w ";
            System.out.println("cousql:"+cousql);
            rs.execute(cousql);
            if(rs.next()){
                count = Util.getIntValue(rs.getString("cou"));
            }
            System.out.println("count:"+count);
            String where  = "";
            if(page>0 && size > 0)
            {
                //where  = " where w.px>="+(page-1)*size +" and w.px<"+page*size;
            }
            String listsql =  " select w.* from ("+sql+") w "+where;
            System.out.println("listsql:"+listsql);
            rs.execute(listsql);
            Map<String,Object> itemMap = null;
            while(rs.next())
            {
                String bt = Util.null2String(rs.getString("bt"));
                String jbr = Util.null2String(rs.getString("jbr"));
                String jzsj = Util.null2String(rs.getString("jzsj"));
                String modelableid = Util.null2String(rs.getString("modelableid"));

                String cjr = Util.null2String(rs.getString("cjr"));
                String t_type = Util.null2String(rs.getString("type"));

                itemMap = new HashMap<String,Object>();
                itemMap.put("bt",bt);
                itemMap.put("jzsj",jzsj);
                itemMap.put("jbr",jbr);


                itemMap.put("cjr",cjr);
                itemMap.put("cjrname",resComInfo.getLastname(cjr));

                itemMap.put("type",t_type);

                itemMap.put("jbrname",resComInfo.getLastname(jbr));
                itemMap.put("modelableid",modelableid);

                String id = Util.null2String(rs.getString("id"));
                String modeId = Util.null2String(rs.getString("formmodeid"));
                String formid = Util.null2String(rs.getString("formid"));
                itemMap.put("billid",id);
                itemMap.put("modeId",modeId);
                itemMap.put("formid",formid);
                list.add(itemMap);
            }

            System.out.println("list:"+list.size());

            for(Map<String,Object> datamap : list)
            {
                String modelableid  = Util.null2String(datamap.get("modelableid"));
                String modeId  = Util.null2String(datamap.get("modeId"));
                List<Map<String,String>> lablist = new ArrayList<Map<String,String>>();
                if(!"".equals(modelableid))
                {
                    sql = "select l.tabname,l.color,l.bgcolor,l.bdcolor from modeTabs_"+modeId+" l where id in("+modelableid+") ";
                    rs.execute(sql);
                    Map<String,String> labmap = null;
                    while (rs.next()){
                        String tabname = Util.null2String(rs.getString("tabname"));
                        String color = Util.null2String(rs.getString("color"));
                        String bgcolor = Util.null2String(rs.getString("bgcolor"));
                        String bdcolor = Util.null2String(rs.getString("bdcolor"));
                        labmap = new HashMap<String,String>();
                        labmap.put("tabname",tabname);
                        labmap.put("color",color);
                        labmap.put("bgcolor",bgcolor);
                        labmap.put("bdcolor",bdcolor);
                        lablist.add(labmap);
                    }
                }
                datamap.put("lable",lablist);
            }
            dataMap.put(type+"count",count);
            dataMap.put(type,list);
        }catch (Exception e){

        }

        return dataMap;
    }
}
