package com.engine.htsc.htjjh.cmd;

import com.api.doc.detail.util.DocDownloadCheckUtil;
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.interfaces.htsc.comInfo.PropBean;

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

public class FoundationHallDataListCmd extends AbstractCommonCommand<Map<String, Object>> {

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

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();
        String ecologyContentPath = GCONST.getContextPath();
        BaseBean bb = new BaseBean();
        Map<String, Object> apimap = new HashMap<String, Object>();
        List<Map<String,Object>> uflist = new ArrayList<Map<String,Object>>();
        Map<String,Object> ufmap = null;
        int userid = this.user.getUID();
        //http://eipsit.htsc.com.cn/htoa/spa/workflow/static4form/index.html?_rdm=1649382905129#/main/workflow/req?iscreate=1&workflowid=24&isagent=0&beagenter=0&f_weaver_belongto_userid=&f_weaver_belongto_usertype=0&menuIds=1,12&_key=269vcq
        PropBean pb = new PropBean();
        String jjhcode = Util.null2String(pb.getPropName("JJH_BM_BM"));

        String f_weaver_belongto_userid = "" ;
        if(!"".equals(userid+"") && !"".equals(jjhcode)){
            String sql =" select t.id\n" +
                    "   from hrmresource t\n" +
                    "   where workcode in(\n" +
                    "   select workcode from\n" +
                    "   hrmresource h where h.id=\n" +userid+
                    "   )\n" +
                    "   and t.departmentid in(\n" +
                    "   select d.id\n" +
                    "   from hrmdepartment d\n" +
                    "   connect by prior d.id = d.supdepid\n" +
                    "   start with d.departmentcode='"+jjhcode+"'\n" +
                    ") ";
            rs.executeQuery(sql);
            if(rs.next()){
                f_weaver_belongto_userid = Util.null2String(rs.getString("id"));
            }
        }


        int count = 0 ;
        try{
            String sql1 =   " select lcmc,lclj,tbsc from uf_bsdtpz order by nvl(sx,0)  ";
            bb.writeLog("sql1:"+sql1);
            rs.executeQuery(sql1);
            while (rs.next()){
                String lcmc = Util.null2String(rs.getString("lcmc"));
                String lclj = Util.null2String(rs.getString("lclj"));
//                if(!"".equals(lclj) && lclj.indexOf("main/workflow/req")>0){
//                    lclj = lclj+"&f_weaver_belongto_userid="+f_weaver_belongto_userid+"&f_weaver_belongto_usertype=0";
//                }
                String tbsc = Util.null2String(rs.getString("tbsc"));
                ufmap = new HashMap<String,Object>();
                ufmap.put("lcmc",lcmc);
                ufmap.put("tb",tbsc);
                ufmap.put("lclj",lclj);
                ufmap.put("f_weaver_belongto_userid",f_weaver_belongto_userid);
                uflist.add(ufmap);
                count++ ;
            }

            for(int i=0;i<uflist.size();i++){
                Map<String,Object> dataMap = uflist.get(i);
                String tb = Util.null2String(dataMap.get("tb"));
                String imagefileid = "" ;
                if(!"".equals(tb)){
                    String sql = "  select k.imagefileid\n" +
                            " from docdetail t\n" +
                            " inner join docimagefile l on l.docid = t.id\n" +
                            " inner join imagefile k on k.imagefileid = l.imagefileid\n" +
                            " where docid= "+ tb;
                    rs.executeQuery(sql);
                    if(rs.next()){
                        imagefileid = Util.null2String(rs.getString("imagefileid"));
                    }
                }
                String fileid = "";
                try{
                    if(!"".equals(imagefileid)){
                        fileid = DocDownloadCheckUtil.checkPermission(imagefileid+"",null);
                    }
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }
                String tburl = ecologyContentPath+"/weaver/weaver.file.FileDownload?fileid="+fileid+"";
                dataMap.put("tburl",tburl);
            }
        }catch (Exception e){
            System.out.println(e);
        }
        apimap.put("data",uflist);
        apimap.put("count",count);
        return apimap;
    }
}
