package com.engine.htsc.duban.cmd;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;

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

public class DynamicsReportCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public DynamicsReportCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> appdate = new HashMap<String, Object>();
        ResourceComInfo resComInfo;
        BaseBean bb = new BaseBean();
        try{
            resComInfo = new ResourceComInfo();
            int userid = user.getUID();
            String workcode = resComInfo.getWorkcode(userid+"");

            int size = Util.getIntValue(Util.null2String(params.get("size")),-1);
            int page = Util.getIntValue(Util.null2String(params.get("page")),-1);
            String type = Util.null2String(params.get("type"));
            System.out.println("size:"+size);
            System.out.println("page:"+page);
            System.out.println("workcode:"+workcode);

            if("0".equals(type)){
                appdate = allList(workcode,page,size);
            }
            if("1".equals(type)){
                appdate = wcjList(workcode,page,size);
            }
            if("2".equals(type)){
                appdate = wcyList(workcode,page,size);
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data",appdate);
            System.out.println(jsonObject.toJSONString());
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return appdate;
    }

    public Map<String,Object> allList(String workcode,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 hrmresource where workcode=? and status = 1 ",workcode);
        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+")";

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


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

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

        String xzsql =" select distinct u.id from ( \n" +
                "   select t.id\n" +
                "   from uf_xzsx t " +
                "   where 1=1 \n" + allxzwhere +
                "   union all \n" +
                "   select t.id \n" +
                "   from uf_xzsx t \n" +
                "   inner join uf_xzsx_dt1 l on l.mainid = t.id \n" +
                "   where l.bgxr in("+userids+") " +
                " ) u " ;
        //bb.writeLog("xzsql:"+xzsql);

        String rwsql =  " select distinct u.id from ( \n" +
                "   select t.id\n" +
                "   from uf_rw t \n" +
                "   where 1=1 \n" + allrwwhere+
                "   union all \n" +
                "   select t.id \n" +
                "   from uf_rw t \n" +
                "   inner join uf_rw_dt2 l on l.mainid = t.id \n" +
                "   where l.bgxr in("+userids+") \n" +
                "  ) u  " ;
        //bb.writeLog("rwsql:"+rwsql);

        String btsql =  " select t.bt,t.cjsj,t.cjr,k.rwbt,h.lastname \n" +
                " from uf_xzrwfkpl t\n" +
                " inner join uf_rw k on k.id = t.glid\n" +
                " inner join hrmresource h on h.id = t.cjr\n" +
                " where jlzt=0 and jllx=1 and glid in("+rwsql+")\n" +
                " union all\n" +
                " select t.bt,t.cjsj,t.cjr,k.bt as rwbt,h.lastname \n" +
                " from uf_xzrwfkpl t\n" +
                " inner join uf_xzsx k on k.id = t.glid\n" +
                " inner join hrmresource h on h.id = t.cjr\n" +
                " where jlzt=0 and jllx=0 and glid in("+xzsql+")" ;

        btsql  =  " select rownum as px,l.* \n" +
                " from (" + btsql + ") l where 1=1 \n" +
                " order by l.cjsj desc " ;
        int count = 0;
        String  cousql = " select count(w.bt) as cou from ("+btsql+") 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 ("+btsql+") w "+where;
        System.out.println("listsql:"+listsql);
        rs.execute(listsql);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> itemMap = null;
        while(rs.next())
        {
            String bt = Util.null2String(rs.getString("bt"));
            String cjr = Util.null2String(rs.getString("cjr"));
            String lastname = Util.null2String(rs.getString("lastname"));
            String rwbt = Util.null2String(rs.getString("rwbt"));
            String cjsj = Util.null2String(rs.getString("cjsj"));

            itemMap = new HashMap<String,Object>();
            itemMap.put("bt",bt);
            itemMap.put("cjrname",lastname);
            itemMap.put("cjr",cjr);
            itemMap.put("rwbt",rwbt);
            itemMap.put("cjsj",cjsj);
            list.add(itemMap);
        }
        dataMap.put("count",count);
        dataMap.put("data",list);
        return dataMap;
    }


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

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

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String wcjwhere = "";
        String wcyxzwhere = "";
        String userids = "";
        rs.executeQuery(" select id from hrmresource where workcode=? and status = 1 ",workcode);
        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.id from ( \n" +
                "   select t.id\n" +
                "   from uf_xzsx t " +
                "   where 1=1 \n" + wcjwhere +
                "   union all \n" +
                "   select t.id \n" +
                "   from uf_xzsx t \n" +
                "   inner join uf_xzsx_dt1 l on l.mainid = t.id \n" +
                "   where l.bgxr in("+userids+") " +
                " ) u " ;
        //bb.writeLog("xzsql:"+xzsql);

        String rwsql =  " select distinct u.id from ( \n" +
                "   select t.id\n" +
                "   from uf_rw t \n" +
                "   where 1=1 \n" + wcjwhere+
                "   union all \n" +
                "   select t.id \n" +
                "   from uf_rw t \n" +
                "   inner join uf_rw_dt2 l on l.mainid = t.id \n" +
                "   where l.bgxr in("+userids+") \n" +
                "  ) u  " ;
        //bb.writeLog("rwsql:"+rwsql);

        String btsql =  " select t.bt,t.cjsj,t.cjr,k.rwbt,h.lastname \n" +
                " from uf_xzrwfkpl t\n" +
                " inner join uf_rw k on k.id = t.glid\n" +
                " inner join hrmresource h on h.id = t.cjr\n" +
                " where jlzt=0 and jllx=1 and glid in("+rwsql+")\n" +
                " union all\n" +
                " select t.bt,t.cjsj,t.cjr,k.bt as rwbt,h.lastname \n" +
                " from uf_xzrwfkpl t\n" +
                " inner join uf_xzsx k on k.id = t.glid\n" +
                " inner join hrmresource h on h.id = t.cjr\n" +
                " where jlzt=0 and jllx=0 and glid in("+xzsql+")" ;

        btsql  =  " select rownum as px,l.* \n" +
                " from (" + btsql + ") l where 1=1 \n" +
                " order by l.cjsj desc " ;

        int count = 0;
        String  cousql = " select count(w.bt) as cou from ("+btsql+") 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 ("+btsql+") w "+where;
        System.out.println("listsql:"+listsql);
        rs.execute(listsql);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> itemMap = null;
        while(rs.next())
        {
            String bt = Util.null2String(rs.getString("bt"));
            String cjr = Util.null2String(rs.getString("cjr"));
            String lastname = Util.null2String(rs.getString("lastname"));
            String rwbt = Util.null2String(rs.getString("rwbt"));
            String cjsj = Util.null2String(rs.getString("cjsj"));

            itemMap = new HashMap<String,Object>();
            itemMap.put("bt",bt);
            itemMap.put("cjrname",lastname);
            itemMap.put("cjr",cjr);
            itemMap.put("rwbt",rwbt);
            itemMap.put("cjsj",cjsj);
            list.add(itemMap);
        }
        dataMap.put("count",count);
        dataMap.put("data",list);
        return dataMap;
    }

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

        Map<String,Object> dataMap = new HashMap<String,Object>();
        RecordSet rs = new RecordSet();
        String wcyxzwhere = "";
        String wcyrwwhere = "";
        String userids = "";
        rs.executeQuery(" select id from hrmresource where workcode=? and status = 1 ",workcode);
        while (rs.next())
        {
            String id = Util.null2String(rs.getString("id"));

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

            wcyrwwhere += wcyrwwhere =="" ? " (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(wcyxzwhere))
        {
            wcyxzwhere = " and ("+ wcyxzwhere +")";
        }

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

        String xzsql =" select distinct u.id from ( \n" +
                    "   select t.id\n" +
                    "   from uf_xzsx t " +
                    "   where 1=1 \n" + wcyxzwhere +
                    "   union all \n" +
                    "   select t.id \n" +
                    "   from uf_xzsx t \n" +
                    "   inner join uf_xzsx_dt1 l on l.mainid = t.id \n" +
                    "   where l.bgxr in("+userids+") " +
                    " ) u " ;
        //bb.writeLog("xzsql:"+xzsql);

        String rwsql =  " select distinct u.id from ( \n" +
                        "   select t.id\n" +
                        "   from uf_rw t \n" +
                        "   where 1=1 \n" + wcyrwwhere+
                        "   union all \n" +
                        "   select t.id \n" +
                        "   from uf_rw t \n" +
                        "   inner join uf_rw_dt2 l on l.mainid = t.id \n" +
                        "   where l.bgxr in("+userids+") \n" +
                        "  ) u  " ;
        //bb.writeLog("rwsql:"+rwsql);

        String btsql =  " select t.bt,t.cjsj,t.cjr,k.rwbt,h.lastname \n" +
                        " from uf_xzrwfkpl t\n" +
                        " inner join uf_rw k on k.id = t.glid\n" +
                        " inner join hrmresource h on h.id = t.cjr\n" +
                        " where jlzt=0 and jllx=1 and glid in("+rwsql+")\n" +
                        " union all\n" +
                        " select t.bt,t.cjsj,t.cjr,k.bt as rwbt,h.lastname \n" +
                        " from uf_xzrwfkpl t\n" +
                        " inner join uf_xzsx k on k.id = t.glid\n" +
                        " inner join hrmresource h on h.id = t.cjr\n" +
                        " where jlzt=0 and jllx=0 and glid in("+xzsql+")" ;

        btsql  =  " select rownum as px,l.* \n" +
                " from (" + btsql + ") l where 1=1 \n" +
                " order by l.cjsj desc " ;

        String where  = "";
        if(page>=0 && size > 0)
        {
            where  = " where w.px>="+(page-1)*size +" and w.px<"+page*size;
        }

        int count = 0;
        String  cousql = " select count(w.bt) as cou from ("+btsql+") w ";
        System.out.println("cousql:"+cousql);
        rs.execute(cousql);
        if(rs.next()){
            count = Util.getIntValue(rs.getString("cou"));
        }
        System.out.println("count:"+count);


        String listsql =  " select w.* from ("+btsql+") w "+where;
        System.out.println("listsql:"+listsql);
        rs.execute(listsql);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> itemMap = null;
        while(rs.next())
        {
            String bt = Util.null2String(rs.getString("bt"));
            String cjr = Util.null2String(rs.getString("cjr"));
            String lastname = Util.null2String(rs.getString("lastname"));
            String rwbt = Util.null2String(rs.getString("rwbt"));
            String cjsj = Util.null2String(rs.getString("cjsj"));

            itemMap = new HashMap<String,Object>();
            itemMap.put("bt",bt);
            itemMap.put("cjrname",lastname);
            itemMap.put("cjr",cjr);
            itemMap.put("rwbt",rwbt);
            itemMap.put("cjsj",cjsj);
            list.add(itemMap);
        }
        dataMap.put("count",count);
        dataMap.put("data",list);
        return dataMap;
    }


//    public Map<String,Object> typeList(String sql,String xzzt,String type,int page,int size){
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        BaseBean bb = new BaseBean();
//        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 ztwhere =" and l.xzzt="+xzzt;
//
//            sql  =  " select rownum as px,l.* \n" +
//                    " from (\n" + sql + ") l where 1=1 \n" +ztwhere +
//                    " order by l.cjrq desc " ;
//
//            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*size +" and w.px<"+(page+1)*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"));
//
//                itemMap = new HashMap<String,Object>();
//                itemMap.put("bt",bt);
//                itemMap.put("jzsj",jzsj);
//                itemMap.put("jbr",jbr);
//
//                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;
//    }



    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("size","20");
        params.put("page","0");
        params.put("workcode","012901");
        params.put("type","0");

        User user = new User();
        user.setUid(11260);
        DynamicsReportCmd htscMhjcCmd=new DynamicsReportCmd(params,user);
        CommandContext ceshi = new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);
    }
}
