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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();

        HtscPortalDao htscPortalDao = new HtscPortalDao();

        Map<String, Object> appdate = new HashMap<String, Object>();
        List<Map<String,String>> itemList = new ArrayList<Map<String,String>>();
        try{
            int num = Util.getIntValue(Util.null2String(params.get("num")),10);
            String workcode = Util.null2String(params.get("workcode"));
            String type = Util.null2String(params.get("type"));

            PropBean pb = new PropBean();
            String oahost = pb.getPropName("OAHOST");

            if(!"".equals(workcode))
            {
                StringBuffer queryBuffer = new StringBuffer();
                queryBuffer.append( " select p.bt,\n" +
                                    "         p.id,\n" +
                                    "         p.syqz,\n" +
                                    "         p.syjg,\n" +
                                    "         p.szbm,\n" +
                                    "         p.formmodeid," +
                                    "         m.formid," +
                                    "         p.modedatacreatedate as fbrq,\n" +
                                    "         p.modedatacreatetime as fbsj,\n" +
                                    "         p.sysyr,\n" +
                                    "         to_char(p.syry) as syry,\n" +
                                    "         p.sybm\n" +
                                    "  from uf_gwky p\n" +
                                    "  left join modeinfo m on m.id = p.formmodeid \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_data =   " 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_data："+sql_data);
                log.info("sql_data："+sql_data);
                Map<String,String> itemMap = null;
                rs.execute(sql_data);
                while(rs.next())
                {
                    String bt = Util.null2String(rs.getString("bt"));

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

                    itemMap = new HashMap<String,String>();
                    itemMap.put("createDate",createdate);

                    itemMap.put("title",bt);

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

            appdate.put("dataList",itemList);
            appdate.put("success","true");
            appdate.put("moreurl","");
            appdate.put("userCode","");
            appdate.put("userName","");

        }catch (Exception e){
            log.info("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("num","3");
        params.put("workcode","002447");
        params.put("type","1");
        HtscKanyueForPortalCmd htscMhjcCmd=new HtscKanyueForPortalCmd(params);
        CommandContext ceshi= new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);

    }


}
