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.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.HT_Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    public HtscMhjcKanyueCmd(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>();
//        ResourceComInfo resComInfo;
//        DepartmentComInfo deptComInfo;
        BaseBean bb = new BaseBean();
        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);

            String workcode = Util.null2String(params.get("workcode"));
            String month = Util.null2String(params.get("month"));
            String type = Util.null2String(params.get("type"));

//            bb.writeLog("size:"+size);
//            bb.writeLog("page:"+page);
//            bb.writeLog("workcode:"+workcode);
//            bb.writeLog("month:"+month);
//            bb.writeLog("type:"+type);

            int total = 0;
            if(!"".equals(workcode))
            {
                PropBean pb = new PropBean();
                String oahost = pb.getPropName("OAHOST");

//                resComInfo = new ResourceComInfo();
//                deptComInfo = new DepartmentComInfo();

                String sql =" with a as(\n" +
                            " select t1.bt,t1.wh, t1.syqz,t1.syjg,t1.szbm,t1.ngr,t1.modedatacreatedate as fbrq,t1.modedatacreatetime as fbsj,t1.id,t1.formmodeid,m.formid,\n" +
                            "        t1.sysyr,t1.syry,t1.sybm,t1.oldoaurl \n" +
                            " from uf_gwky t1\n" +
                            " left join modeinfo m on m.id = t1.formmodeid\n" +
                            " where substr(t1.modedatacreatedate,0,7) = '"+month+"'\n" +
                            " ) " ;
                //bb.writeLog("sql:"+sql);
                int rycount = 0;
                String userids = "" ;
                String hrmwhere = "";
                String deptwhere  = "";
                String jgwhere="";
                String qzwhere="";
                HT_Util ht_util=new HT_Util();
                rs.executeQuery(" select id,departmentid from hrmresource where workcode=? and status = 1 ",workcode);
                while (rs.next())
                {
                    String id = Util.null2String(rs.getString("id"));

                    userids += userids=="" ? id : ","+id;
                    String departmentid = Util.null2String(rs.getString("departmentid"));

                    hrmwhere += hrmwhere =="" ? " instr(','||a1.syry||',',',"+id+",')>0" : " or instr(','||a1.syry||',',',"+id+",')>0";
                    deptwhere += deptwhere == "" ? " instr(','||a1.sybm||',',',"+departmentid+",')>0 " : " or instr(','||a1.sybm||',',',"+departmentid+",')>0 ";

                    String szjgid= ht_util.getszjg(id);
                    if(szjgid!=""){
                        jgwhere += jgwhere == "" ? " instr(','||a1.syjg||',',',"+szjgid+",')>0 " : " or instr(','||a1.syjg||',',',"+szjgid+",')>0 ";
                    }

                    rycount++;
                }


                if(rycount > 0){
                    if(!"".equals(userids))
                    {
                        List<String> listvalue = ht_util.getszqz(userids);
                        if(listvalue != null && listvalue.size()>0)
                        {
                            for (int i=0;i<listvalue.size();i++){
                                String value = listvalue.get(i);
                                if(value!="")
                                {
                                    qzwhere += qzwhere == "" ? " instr(','||a1.syqz||',',',"+value+",')>0 " : " or instr(','||a1.syqz||',',',"+value+",')>0 ";
                                }
                            }
                        }
                    }

                    String usql = " with a as(\n" +
                            "  select listagg (DISTINCT bm,',') within group (order by bm) bm,\n" +
                            "         listagg (DISTINCT ry,',') within group (order by ry) ry\n" +
                            "  from uf_kygl\n" +
                            " ),\n" +
                            " b as(\n" +
                            "  select regexp_substr(aa.bm, '[^,]+', 1, level) col\n" +
                            "  from a aa\n" +
                            "  connect by level <= regexp_count(aa.bm, '\\,') + 1\n" +
                            " ),\n" +
                            " c as(\n" +
                            "   select regexp_substr(aa.ry, '[^,]+', 1, level) col\n" +
                            "   from a aa\n" +
                            "   connect by level <= regexp_count(aa.ry, '\\,') + 1\n" +
                            " )\n" +
                            " select w.id,w.lastname\n" +
                            "   from (\n" +
                            "   select h.id,h.lastname from hrmresource h\n" +
                            "   where h.departmentid in(\n" +
                            "   select id from hrmdepartment k\n" +
                            "   connect by prior k.id = k.supdepid\n" +
                            "   start with id in(select bb.col as bm from b bb)\n" +
                            " )\n" +
                            " and h.status = 1 and h.belongto is null\n" +
                            " union \n" +
                            " select h.id,h.lastname from hrmresource h\n" +
                            " where h.id in(select cc.col as bm from c cc)\n" +
                            " and h.status = 1 and h.belongto is null\n" +
                            " ) w\n" +
                            " where w.id in("+userids+")";

                    //System.out.println("usql:"+usql);
                    int count = 1;
                    rs.execute(usql);
                    if(rs.next()){
                        count--;
                    }
                    //System.out.println("count:"+count);

                    String userSqls =   " select a1.*" +
                            " from a a1 " +
                            " where a1.sysyr = 1 " +
                            " union all " +
                            " select a1.*" +
                            " from a a1 " +
                            " where " +hrmwhere+
                            " union all " +
                            " select a1.*" +
                            " from a a1 " +
                            " where " +deptwhere;

                    if(jgwhere!=""){
                        userSqls+=" union all " +
                                " select a1.*" +
                                " from a a1 " +
                                " where " +jgwhere;
                    }

                    if(qzwhere!=""){
                        userSqls+=" union all " +
                                " select a1.*" +
                                " from a a1 " +
                                " where " +qzwhere;
                    }


                    //bb.writeLog("userSqls:"+userSqls);
                    String sql_px = "" ;
                    if("0".equals(type))
                    {
                        sql_px =  " select p.*,rownum as px from("+userSqls  +") p where exists(select 1 from uf_gwky_dt5 l where instr(',"+userids+",',','||l.ry||',')>0 and l.mainid = p.id) order by fbrq desc,fbsj desc " ;
                        //bb.writeLog("sql_px:"+sql_px);
                    }else{
                        sql_px = " select p.*,rownum as px from("+userSqls  +") p where not exists(select 1 from uf_gwky_dt5 l where instr(',"+userids+",',','||l.ry||',')>0 and l.mainid = p.id) order by fbrq desc,fbsj desc " ;
                        //bb.writeLog("sql_px:"+sql_px);
                    }

                    String countsql = sql + " select count(1) as total from ("+sql_px+") w ";
                    //bb.writeLog("countsql:"+countsql);
                    rs.execute(countsql);
                    if(rs.next()){
                        total = Util.getIntValue(rs.getString("total"));
                    }

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

                    sql = sql + " select w.* from ("+sql_px+") w "+where;
//                bb.writeLog("sql:"+sql);
//                System.out.println("sql:"+sql);
                    rs.execute(sql);

                    Map<String,String> itemMap = null;
                    while(rs.next())
                    {
                        String bt = Util.null2String(rs.getString("bt"));
                        String wh = Util.null2String(rs.getString("wh"));
                        String szbm = Util.null2String(rs.getString("szbm"));
                        String ngr = "";//resComInfo.getLastname(Util.null2String(rs.getString("ngr")));

                        String id = Util.null2String(rs.getString("id"));
                        String formmodeid = Util.null2String(rs.getString("formmodeid"));
                        String formid = Util.null2String(rs.getString("formid"));
                        String fbrq = Util.null2String(rs.getString("fbrq"));
                        String fbsj = Util.null2String(rs.getString("fbsj"));
                        String ngbm = "";//deptComInfo.getDepartmentname(szbm);

                        String oldoaurl = Util.null2String(rs.getString("oldoaurl"));

                        itemMap = new HashMap<String,String>();
                        itemMap.put("bt",bt);
                        itemMap.put("wh",wh);
                        itemMap.put("ngbm",ngbm);
                        itemMap.put("ngr",ngr);

                        if(count > 0){
                            itemMap.put("url",oahost+"spa/cube/index.html#/main/cube/card?type=0&modeId="+formmodeid+"&formId="+formid+"&billid="+id);
                        }else{
                            itemMap.put("url",oldoaurl);
                        }

                        itemMap.put("fbrq",fbrq+" "+fbsj);
                        itemList.add(itemMap);
                    }

                }

            }

            appdate.put("data",itemList);
            appdate.put("total",total);
        }catch (Exception e){
            //bb.writeLog("e-1"+e);
        }
        return appdate;
    }

    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("size","300");
        params.put("page","0");
        params.put("workcode","002180");
        params.put("month","2021-01");
        params.put("type","1");
        HtscMhjcKanyueCmd htscMhjcCmd=new HtscMhjcKanyueCmd(params);
        CommandContext ceshi= new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);

    }


}
