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 com.engine.htsc.workflow.util.WorkflowUtil;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
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 HtscTzydZyCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public HtscTzydZyCmd(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 resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            List<Map<String,String>> itemList = new ArrayList<Map<String,String>>();
            int num = Util.getIntValue(Util.null2String(params.get("num")),-1);
            String workcode = Util.null2String(params.get("workcode"));
            int total = 0;
            if(!"".equals(workcode))
            {
                PropBean pb = new PropBean();
                String oahost = pb.getPropName("OAHOST");
                String userids = "";

                int rycount = 0;
                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;
                    rycount++;
                }

                if(rycount >0)
                {
                    String sql =" select (case requestlevel\n" +
                            "         when 0 then\n" +
                            "          1\n" +
                            "         when 1 then\n" +
                            "          2\n" +
                            "         when 2 then\n" +
                            "          3\n" +
                            "         else\n" +
                            "          -1\n" +
                            "       end) as requestlevelorder,\n" +
                            "       t1.requestid,\n" +
                            "       t1.requestmark,\n" +
                            "       t1.createdate,\n" +
                            "       t1.createtime,\n" +
                            "       t1.creater,\n" +
                            "       t1.creatertype,\n" +
                            "       t1.workflowid,\n" +
                            "       t1.requestname,\n" +
                            "       t1.requestnamenew,\n" +
                            "       t1.status,\n" +
                            "       t1.requestlevel,\n" +
                            "       t1.currentnodeid,\n" +
                            "       t2.viewtype,\n" +
                            "       t2.userid,\n" +
                            "       t2.receivedate,\n" +
                            "       t2.receivetime,\n" +
                            "       t2.isremark,\n" +
                            "       t2.nodeid,\n" +
                            "       t2.agentorbyagentid,\n" +
                            "       t2.agenttype,\n" +
                            "       t2.isprocessed,\n" +
                            "       t1.seclevel,\n" +
                            "       '0' as systype,\n" +
                            "       t2.workflowtype,\n" +
                            "       t2.viewDate,\n" +
                            "       t2.viewTime,\n" +
                            "       t1.lastFeedBackDate,\n" +
                            "       t1.lastFeedBackTime,\n" +
                            "       t2.needwfback,\n" +
                            "       t1.lastFeedBackOperator\n" +
                            "  from workflow_requestbase     t1,\n" +
                            "       workflow_currentoperator t2,\n" +
                            "       workflow_base            t3\n" +
                            " where t1.requestid = t2.requestid\n" +
                            "   and t1.workflowid = t3.id\n" +
                            "   and t2.userid in ("+userids+")\n" +
                            "   and t2.usertype = 0\n" +
                            "   and (nvl(t1.currentstatus, -1) = -1 or\n" +
                            "       (nvl(t1.currentstatus, -1) = 0 and t1.creater in (" + userids + ")))\n" +
                            "   and (t1.deleted <> 1 or t1.deleted is null or t1.deleted = '')\n" +
                            "   and t2.preisremark in ('1', '8', '9', '11')\n" +
                            "   and t2.takisremark is null\n" +
                            "   and nvl(t1.isstopped, 0) != 1\n" +
                            "   and t2.islasttimes = 1\n" +
                            "   and t2.viewtype != '0'\n" +
                            "   and t2.isremark not in ('4', '2')\n" +
                            "   and (t1.currentnodetype <> '3' or\n" +
                            "       (t2.isremark in ('1', '8', '9', '11') and t1.currentnodetype = '3'))\n" +
                            "   and (isprocessing = '' or isprocessing is null)\n" +
                            "   and t3.isvalid in (1, 3)\n" +
                            " order by receivedate desc, receivetime desc\n ";

                    //System.out.println("tzydzy:" + sql);
                    rs.execute(sql);
                    Map<String, String> itemMap = null;
                    while (rs.next()) {
                        String userid = Util.null2String(rs.getString("userid"));
                        String creater = Util.null2String(rs.getString("creater"));
                        String lastname = resourceComInfo.getLastname(creater);

                        String requestid = Util.null2String(rs.getString("requestid"));
                        String priority = Util.null2String(rs.getString("requestlevel"));

                        if ("0".equals(priority)) {
                            priority = "一般";
                        } else if ("1".equals(priority)) {
                            priority = "紧急";
                        } else if ("2".equals(priority)) {
                            priority = "特急";
                        } else {
                            priority = "一般";
                        }

                        String title = Util.null2String(rs.getString("requestname"));
                        String workflowname = Util.null2String(rs.getString("workflowname"));
                        String action = WorkflowUtil.getNodeName(Util.null2String(rs.getString("currentnodeid")));
                        String createTime = Util.null2String(rs.getString("createtime"));
                        String createDate = Util.null2String(rs.getString("createdate"));
                        String referencenumber = Util.null2String(rs.getString("requestmark"));
                        String workflowid = Util.null2String(rs.getString("workflowid"));


                        itemMap = new HashMap<String, String>();
                        itemMap.put("createrCode", workcode);
                        itemMap.put("priority", priority);
                        itemMap.put("title", title);
                        itemMap.put("type", workflowname);
                        itemMap.put("createrName", lastname);
                        itemMap.put("action", action);
                        itemMap.put("createTime", createTime);
                        itemMap.put("createDate", createDate);
                        itemMap.put("referencenumber", referencenumber);
                        itemMap.put("fromSystem", "数字办公平台");

                        long sjc = System.currentTimeMillis();
                        itemMap.put("url", oahost + "workflow/request/ViewRequestForwardSPA.jsp?requestid=" + requestid + "" +
                                "&_workflowid=" + workflowid + "&f_weaver_belongto_userid=" + userid + "&isovertime=0&preloadkey=" + sjc + "&timestamp=" + sjc);

                        itemList.add(itemMap);
                    }
                }

                appdate.put("dataList", itemList);
                appdate.put("success", "true");
                appdate.put("moreurl", "");
                appdate.put("userCode", "");
                appdate.put("userName", "");
                if (total < num) {
                    appdate.put("num", total);
                } else {
                    appdate.put("num", total);
                }
            }
        }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("num","300");
        params.put("workcode","012901");
        params.put("type","1");
        HtscTzydZyCmd HtscTzydZyCmd=new HtscTzydZyCmd(params);
        CommandContext ceshi= new CommandContext(null,null);
        HtscTzydZyCmd.execute(ceshi);

    }


}
