package com.engine.htsc.coworkreport.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 CoworkReportCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public CoworkReportCmd(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();
            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"));


            if("1".equals(userid)){
                CoworkReportUtil CoworkReportUtil = new CoworkReportUtil();
                if("0".equals(type)){
                    appdate = CoworkReportUtil.allList(userid+"",page,size);
                }
                if("1".equals(type)){
                    appdate = CoworkReportUtil.wcjList(userid+"",page,size);
                }
                if("2".equals(type)){
                    appdate = CoworkReportUtil.wcyList(userid+"",page,size);
                }
            }else{
                String workcode = resComInfo.getWorkcode(userid+"");

                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+")";

            //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_xzsy 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_xzsy t \n" +
                        "   inner join uf_xzsy_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_rw 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_rw t \n" +
                        " inner join uf_rw_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 workcode,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 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.* 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_xzsy 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_xzsy t \n" +
                "   inner join uf_xzsy_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_rw 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_rw t \n" +
                        " inner join uf_rw_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 workcode,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 hrmresource where workcode=? and status = 1 ",workcode);
        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_xzsy 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_xzsy t \n" +
                        "   inner join uf_xzsy_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_rw 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_rw t \n" +
                        " inner join uf_rw_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;
    }



    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);
        CoworkReportCmd htscMhjcCmd=new CoworkReportCmd(params,user);
        CommandContext ceshi = new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);
    }
}
