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.mhjc.dao.HtscPortalDao;
import lombok.extern.slf4j.Slf4j;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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


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

    public HtscKanyueForOldCmd(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>();
        HtscPortalDao htscPortalDao = new HtscPortalDao();

        try{
            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"));
            String type = Util.null2String(params.get("type"));

            int total = 0;
            if(!"".equals(workcode))
            {
                PropBean pb = new PropBean();
                String oahost = pb.getPropName("OAHOST");
                StringBuffer queryBuffer = new StringBuffer();
                queryBuffer.append(" select p.bt,p.wh, p.syqz,p.syjg,p.szbm,p.ngr,h.lastname,\n" +
                            " p.modedatacreatedate as fbrq,p.modedatacreatetime as fbsj,p.id,p.formmodeid,m.formid,\n" +
                            " p.sysyr,to_char(p.syry) as syry,p.sybm,p.oldoaurl,p.jjcd,p.lcid,\n" +
                            " b.workflowname,k.createdate,k.createtime\n" +
                            " from uf_gwky p\n" +
                            " left join modeinfo m on m.id = p.formmodeid\n" +
                            " left join hrmresource h on h.id = p.ngr\n" +
                            " left join workflow_requestbase k on to_char(k.requestid) = p.lcid\n" +
                            " left join workflow_base b on b.id = k.workflowid\n" +
                            " where (p.gwgg = 0 or p.gwgg is null) " );


                String queryWhere = htscPortalDao.getQueryDataSql(workcode);
                queryBuffer.append(queryWhere);

                String dt5Where  = htscPortalDao.getKanyueUserWhere(workcode);
                if("0".equals(type))
                {
                    queryBuffer.append(" and exists(select 1 from uf_gwky_dt5 l where 1=1 "+dt5Where+" and l.mainid = p.id) " ) ;

                }else{
                    queryBuffer.append(" and not exists(select 1 from uf_gwky_dt5 l where 1=1 "+dt5Where+" and l.mainid = p.id) " )  ;
                }

                String sql =" select k.* from (" +
                            "    select w.*,rownum as px from ( " + queryBuffer.toString() + " order by p.modedatacreatedate desc,p.modedatacreatetime desc ) w "+
                            " ) k where k.px >0 and k.px <="+num;
//                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 lastname = Util.null2String(rs.getString("lastname"));

                    String id = Util.null2String(rs.getString("id"));
                    String formmodeid = Util.null2String(rs.getString("formmodeid"));
                    String formid = Util.null2String(rs.getString("formid"));
                    String createdate = Util.null2String(rs.getString("fbrq"));
                    String createtime = Util.null2String(rs.getString("fbsj"));
                    String oldoaurl = Util.null2String(rs.getString("oldoaurl"));
                    String workflowname = Util.null2String(rs.getString("workflowname"));
                    String jjcd = Util.null2String(rs.getString("jjcd"));
                    String priority = "";
                    if("0".equals(jjcd)){
                        priority = "一般";
                    }else if("1".equals(jjcd)){
                        priority = "紧急";
                    }else if("2".equals(jjcd)){
                        priority = "特急";
                    }

                    itemMap = new HashMap<String,String>();
                    itemMap.put("action","");
                    itemMap.put("createDate",createdate);
                    itemMap.put("createTime",createtime);
                    itemMap.put("createrName",lastname);
                    itemMap.put("effective","");
                    itemMap.put("fromSystem","htmoa");
                    itemMap.put("priority",priority);
                    itemMap.put("referencenumber",wh);
                    itemMap.put("title",bt);
                    itemMap.put("type",workflowname);

                    itemMap.put("url",oahost+"spa/cube/index.html#/main/cube/card?type=0&modeId="+formmodeid+"&formId="+formid+"&billid="+id);
                    itemList.add(itemMap);
                }


                String sql_total = " select count(1) as total from ( " + queryBuffer.toString() + " ) w ";
//                System.out.println("sql_total:"+sql_total);
                rs.execute(sql_total);
                if(rs.next()){
                    total = Util.getIntValue(rs.getString("total"));
                }
            }

            System.out.println("total:"+total);
            appdate.put("dataList",itemList);
            appdate.put("success","true");
            appdate.put("moreurl","");

            appdate.put("userCode","");
            appdate.put("userName","");

            appdate.put("num",total);

        }catch (Exception e){
            log.info("e:"+e.getMessage());
        }
        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");
        HtscKanyueForOldCmd htscMhjcCmd=new HtscKanyueForOldCmd(params);
        CommandContext ceshi= new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);

    }


}
