package com.engine.htsc.mhjc.cmd;

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 weaver.interfaces.htsc.comInfo.PropBean;

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

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();
        Map<String, Object> appdate = new HashMap<String, Object>();
//        BaseBean bb = new BaseBean();

        ResourceComInfo resourceComInfo = null;
        try{
            List<Map<String,String>> itemList = new ArrayList<Map<String,String>>();

            int size = Util.getIntValue(Util.null2String(params.get("size")),-1);
            int page = Util.getIntValue(Util.null2String(params.get("page")),-1);
            int count = 0;
            String workcode = Util.null2String(params.get("workcode"));
            String type = Util.null2String(params.get("type"));

//            bb.writeLog("size:"+size);
//            bb.writeLog("page:"+page);
//            bb.writeLog("workcode:"+workcode);
//            int total = 0;
            if(!"".equals(workcode))
            {
                PropBean pb = new PropBean();
                String oahost = pb.getPropName("OAHOST");

                String hrmwhere = "";
                String userids = "";
                String mainuserid = "" ;
                int rycount = 0;
                rs.executeQuery(" select id,belongto from hrmresource where workcode=? and status = 1 ",workcode);
                while (rs.next())
                {
                    String id = Util.null2String(rs.getString("id"));
                    String belongto = Util.null2String(rs.getString("belongto"));
                    hrmwhere += hrmwhere =="" ? " instr(','||to_char(t.jbr)||',',',"+id+",')>0 " : " or  instr(','||t.jbr||',',',"+id+",')>0 ";
                    userids += userids == "" ? id : ","+id ;
                    if("".equals(belongto)){
                        mainuserid = id;
                    }
                    rycount++;
                }


                if(rycount > 0)
                {
                    String sql = "";
                    if("1".equals(type))
                    {
                        sql = "select distinct u1.* from ( \n" +
                                "select t.rwbt,t.rwbh,t.cjr,t.cjrq,t.rwzt,to_char(t.jbr) as jbr,t.formmodeid,t.id,h.formid,t.yxj \n" +
                                "from uf_rw t \n" +
                                "left join modeinfo h on h.id = t.formmodeid \n" +
                                "where 1=1 \n" +
                                "and (" + hrmwhere +") \n" +
                                "and t.rwzt in(2) and not exists(select 1 from uf_rw_dt3 p where p.ry="+mainuserid+" and p.mainid=t.id ) \n" +
                                ") u1 \n" +
                                "union all \n" +
                                "select distinct u2.* from ( \n" +
                                "select t.bt,t.xzbh,t.cjr,t.cjrq,t.xzzt,to_char(t.jbr) as jbr,t.formmodeid,t.id,h.formid,t.yxj \n" +
                                "from uf_xzsy t \n" +
                                "left join modeinfo h on h.id = t.formmodeid \n" +
                                "where 1=1 \n" +
                                "and ( \n" +
                                "((" + hrmwhere +") and t.xzzt in(2) and not exists(select 1 from uf_xzsy_dt2 p where p.ry="+mainuserid+" and p.mainid=t.id))\n" +
                                "or (t.xzzt in(0,2) and exists(select 1 from uf_xzsy_dt3 p where to_char(p.shr)="+mainuserid+" and p.mainid=t.id and p.sfsh=0))\n" +
                                "or (t.cjr="+mainuserid+" and t.xzzt in(0) and exists(select 1 from uf_xzsy_dt3 p where p.ry="+mainuserid+" and p.mainid=t.id and p.sfsh=1) and not exists(select 1 from uf_xzsy_dt3 p where p.ry="+mainuserid+" and p.mainid=t.id and p.sfsh=0))) \n" +
                                ") u2 " +
                                "union all \n" +
                                "select distinct u3.* from ( \n" +
                                "select t.bt,t.xzbh,t.cjr,t.cjrq,t.xzzt,to_char(t.jbr) as jbr,t.formmodeid,t.id,h.formid,t.yxj \n" +
                                "from uf_xzsx t \n" +
                                "left join modeinfo h on h.id = t.formmodeid \n" +
                                "where 1=1 \n" +
                                "and (" + hrmwhere +") \n" +
                                "and t.xzzt in(2) and not exists(select 1 from uf_xzsx_dt2 p where p.ry="+mainuserid+" and p.mainid=t.id) \n" +
                                ") u3";
//                        sql =" select distinct u.* from ( \n" +
//                            "   select t.rwbt,t.rwbh,t.cjr,t.cjrq,t.rwzt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.yxj\n" +
//                            "   from uf_rw t \n" +
//                            "   left join modeinfo h on h.id = t.formmodeid\n" +
//                            "   where 1=1\n" +
//                            "   and (" + hrmwhere +")"+
//                            "   and t.rwzt in(2) and not exists(select 1 from uf_rw_dt3 p where p.ry="+mainuserid+" and p.mainid=t.id )" +
//                            " ) u " ;

                    }else{
//                        sql =" select distinct u.* from ( \n" +
//                            "   select t.rwbt,t.rwbh,t.cjr,t.cjrq,t.rwzt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.yxj\n" +
//                            "   from uf_rw t \n" +
//                            "   left join modeinfo h on h.id = t.formmodeid\n" +
//                            "   where 1=1\n" +
//                            "   and (" + hrmwhere +")"+
//                            "   and t.rwzt in(2) and not exists(select 1 from uf_rw_dt3 p where p.ry="+mainuserid+" and p.mainid=t.id ) \n" +
//                            "   union all \n" +
//                            "   select t.rwbt,t.rwbh,t.cjr,t.cjrq,t.rwzt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.yxj \n" +
//                            "   from uf_rw t \n" +
//                            "   left join modeinfo h on h.id = t.formmodeid\n" +
//                            "   where t.cjr in("+userids+") \n" +
//                            "   and t.rwzt in(4)\n" +
//                            " ) u " ;


                        sql =" select distinct u.* from ( \n" +
//                                "   select t.rwbt,t.rwbh,t.cjr,t.cjrq,t.rwzt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.yxj\n" +
//                                "   from uf_rw t \n" +
//                                "   left join modeinfo h on h.id = t.formmodeid\n" +
//                                "   where 1=1\n" +
//                                "   and (" + hrmwhere +")"+
//                                "   and t.rwzt in(2) and exists(select 1 from uf_rw_dt3 p where p.ry="+mainuserid+" and p.mainid=t.id ) \n" +
//                                "   union all \n" +
                                "   select t.rwbt,t.rwbh,t.cjr,t.cjrq,t.rwzt,to_char(t.jbr) as jbr,to_char(t.tzydry) as tzydr,t.formmodeid,t.id,h.formid,t.yxj \n" +
                                "   from uf_rw t \n" +
                                "   left join modeinfo h on h.id = t.formmodeid\n" +
                                "   where t.cjr in("+userids+") \n" +
                                "   and t.rwzt in(4)\n" +
                                " ) u " ;
                    }

                    //bb.writeLog("sql:"+sql);
                    sql  = " select l.* \n" +
                        " from (\n" + sql + ") l \n" +
                        " order by l.cjrq desc " ;

                    String where  = "";
                    if(page>=0 && size > 0)
                    {
                        where  = " where rownum <"+page*size;
                    }
                    sql =  " select w.* from ("+sql+") w "+where;
                    //System.out.println("sql:"+sql);
                    rs.execute(sql);
                    resourceComInfo = new ResourceComInfo();
                    Map<String,String> itemMap = null;
                    while(rs.next())
                    {
                        String bt = Util.null2String(rs.getString("rwbt"));
                        String rwbh = Util.null2String(rs.getString("rwbh"));
                        String cjrq = Util.null2String(rs.getString("cjrq"));
                        String cjr = Util.null2String(rs.getString("cjr"));
//                    String jbr = Util.null2String(rs.getString("jbr"));
//                    String tzydr = Util.null2String(rs.getString("tzydr"));
//                    String rwzt = Util.null2String(rs.getString("rwzt"));
                        String yxj = Util.null2String(rs.getString("yxj"));

//                    if("0".equals(yxj)){
//                        yxj = "高";
//                    }else if("1".equals(yxj)){
//                        yxj = "中";
//                    }else if("2".equals(yxj)){
//                        yxj = "低";
//                    }else{
//                        yxj = "低";
//                    }

                        String rq = cjrq.substring(0,10);
                        String sj = cjrq.substring(11,16)+":00" ;

                        itemMap = new HashMap<String,String>();
                        itemMap.put("createrCode",workcode);
                        itemMap.put("priority","一般");
                        itemMap.put("title",bt);
                        itemMap.put("type","协同中心");
                        itemMap.put("createrName",resourceComInfo.getLastname(cjr));
                        itemMap.put("action","");

                        itemMap.put("createTime",sj);
                        itemMap.put("createDate",rq);
                        itemMap.put("referencenumber",rwbh);
                        itemMap.put("fromSystem","数字办公平台");

                        String id = Util.null2String(rs.getString("id"));
                        String modeId = Util.null2String(rs.getString("formmodeid"));
                        String formid = Util.null2String(rs.getString("formid"));
                        //itemMap.put("url",oahost+"spa/cube/index.html#/main/cube/card?type=0&modeId="+modeId+"&formId="+formid+"&billid="+id);

                        itemMap.put("url",oahost+"interface/htsc/cowork/jump.jsp?modeId="+modeId+"&formId="+formid+"&billid="+id);
                        itemList.add(itemMap);
                    }
                }
            }
            appdate.put("dataList",itemList);
            appdate.put("success", true);
        }catch (Exception e){
            appdate.put("success", false);
        }
        return appdate;
    }

    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","1");
        params.put("type","1");
        params.put("workcode","011413");

        String cjrq = "2021-12-12 10:20:30";
        String rq = cjrq.substring(0,10);
        String sj = cjrq.substring(11,16)+":00" ;

        System.out.println("rq:"+rq);
        System.out.println("sj:"+sj);

//        HtscCoworkTaskListCmd htscMhjcCmd=new HtscCoworkTaskListCmd(params);
//        CommandContext ceshi = new CommandContext(null,null);
//        htscMhjcCmd.execute(ceshi);
    }
}
