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

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


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

    public HtscGongGaoForPortalCmd(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>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HT_Util ht_util = new HT_Util();
        try{
            List<Map<String,String>> itemList = new ArrayList<Map<String,String>>();
            int num = Util.getIntValue(Util.null2String(params.get("num")),10);
            String workcode = Util.null2String(params.get("workcode"));
            if(!"".equals(workcode))
            {
                PropBean pb = new PropBean();
                String oahost = pb.getPropName("OAHOST");

                String sql =" select p.bt,\n" +
                        "         p.id,\n" +
                        "         p.syqz,\n" +
                        "         p.syjg,\n" +
                        "         p.szbm,\n" +
                        "         p.formmodeid,\n" +
                        "         m.formid,\n" +
                        "         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 = 1 " ;

                String userids = "" ;
                StringBuffer userWhere = new StringBuffer(" and (p.sysyr = 1 ");
                rs.executeQuery(" select id,departmentid from hrmresource where workcode=? and status = 1 ",workcode);
                while (rs.next()) {
                    String id = Util.null2String(rs.getString("id"));
                    String departmentid = Util.null2String(rs.getString("departmentid"));
                    userids += "".equals(userids)? id : "," + id;

                    userWhere.append(" or instr(','||p.syry||',',',"+id+",')>0 ");
                    userWhere.append(" or  instr(','||p.sybm||',',',"+departmentid+",')>0 ");

                    String szjgid = ht_util.getszjg(id);
                    if (szjgid != "") {
                        userWhere.append(" or instr(','||p.syjg||',',',"+szjgid+",')>0 ");
                    }
                }

                if (userids.length() > 0)
                {
                    List<String> listvalue = ht_util.getszqz(userids);
                    if (listvalue != null && listvalue.size() > 0)
                    {
                        for (int i = 0; i < listvalue.size(); i++)
                        {
                            String value = listvalue.get(i);
                            if (!"".equals(value))
                            {
                                userWhere.append(" or instr(','||p.syqz||',',',"+value+",')>0 ");
                            }
                        }
                    }
                }


                String userSqls = userWhere.append(")").toString() ;

                String sql_data =   " select k.* from (" +
                                    "    select w.*,rownum as px from ( " + sql  + userSqls + " and ((to_char(sysdate,'yyyy-mm-dd') <=  p.yxqx and  p.yxqx is not null) or p.yxqx is null) 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);

                rs.execute(sql_data);
                Map<String,String> itemMap = null;
                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("action","");
                    itemMap.put("createDate",createdate);
                    itemMap.put("fromSystem","htmoa");
                    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");
        HtscGongGaoForPortalCmd htscMhjcCmd=new HtscGongGaoForPortalCmd(params);
        CommandContext ceshi= new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);
    }


}
