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.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

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

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

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

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

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

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

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

                if(rycount > 0){
                    if(!"".equals(hrmwhere)){
                        hrmwhere = " and "+hrmwhere ;
                    }
                    String sql =" select distinct u.* from ( \n" +
                            "   select t.bt,t.xzbh,t.cjr,t.cjrq,t.szbm,t.szjg,t.xzzt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid\n" +
                            "   from uf_xzsy t " +
                            "   left join modeinfo h on h.id = t.formmodeid" +
                            "   where 1=1 \n" + hrmwhere +
                            "   union all \n" +
                            "   select t.bt,t.xzbh,t.cjr,t.cjrq,t.szbm,t.szjg,t.xzzt,to_char(t.jbr) as jbr,to_char(t.tzydr) as tzydr,t.formmodeid,t.id,h.formid \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 l.bgxr in("+userids+") " +
                            " ) u " ;

                    //bb.writeLog("sql:"+sql);

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

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

                    sql =  " select w.* from ("+sql+") w "+where;
                    //bb.writeLog("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("xzbh"));
                        String szbm = Util.null2String(rs.getString("szbm"));
                        String cjrq = Util.null2String(rs.getString("cjrq"));
                        String szjg = Util.null2String(rs.getString("szjg"));
                        String cjr = Util.null2String(rs.getString("cjr"));
                        String jbr = Util.null2String(rs.getString("jbr"));
                        String tzydr = Util.null2String(rs.getString("tzydr"));
                        String xzzt = Util.null2String(rs.getString("xzzt"));

                        itemMap = new HashMap<String,String>();
                        itemMap.put("bt",bt);
                        itemMap.put("wh",wh);
                        itemMap.put("szbm",szbm);
                        itemMap.put("cjrq",cjrq);
                        itemMap.put("cjr",cjr);
                        itemMap.put("jbr",jbr);
                        itemMap.put("tzydr",tzydr);
                        itemMap.put("xzzt",xzzt);
                        itemMap.put("szjg",szjg);

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

                        itemList.add(itemMap);
                    }
                }
            }

            appdate.put("data",itemList);
            if(itemList !=null && itemList.size() > 0){
                appdate.put("total",itemList.size());
            }else{
                appdate.put("total","0");
            }
        }catch (Exception 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","20");
        params.put("page","0");
        params.put("workcode","016500");
        HtscCoworkListCmd htscMhjcCmd=new HtscCoworkListCmd(params);
        CommandContext ceshi = new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);
    }
}
