package com.engine.htsc.bulletintoberead.cmd;

import com.alibaba.fastjson.JSONObject;
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.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.gwky.GonggaoDataCustomSql3;

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

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

    private final static String BASE_URL = "http://eipsit.htsc.com.cn/htoa/mobilemode/mobile/view.html?appid=1502#&page_2016?billid=";

    public BulletinToBeReadCmd(Map<String, Object> params){
        this.params = params;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        List<Map<String, Object>> result = new ArrayList<>();
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String workCode = Util.null2String(params.get("workCode"));
        int size = Util.getIntValue(Util.null2String(params.get("size")),-1);
        int page = Util.getIntValue(Util.null2String(params.get("page")),-1);
        String sql = " select id from hrmresource where workcode = '"+ workCode + "'";
        rs.execute(sql);
        String userIdStr = "";
        if(rs.next()){
            userIdStr = Util.null2String(rs.getString("id"));
        }
        // 获取sql查询条件
        GonggaoDataCustomSql3 gonggaoDataCustomSql3 = new GonggaoDataCustomSql3();
        String conditionsUserWork = gonggaoDataCustomSql3.doGetSqlCondition(userIdStr);
        String dataSql =" select t1.bt,t1.wh, t1.syqz,t1.syjg,t1.szbm,t1.ngr,t1.modedatacreatedate as fbrq,t1.modedatacreatetime as fbsj,t1.id,t1.formmodeid,m.formid,\n" +
                "        t1.sysyr,t1.syry,t1.sybm,t1.oldoaurl \n" +
                " from uf_gwky t1\n" +
                " left join modeinfo m on m.id = t1.formmodeid\n" +
                " where "+conditionsUserWork+"\n" +
                " order by t1.modedatacreatetime desc ";
        String countSql = "select count(1) as total from ("+ dataSql +")";
        int total = 0;
        rs.execute(countSql);
        if(rs.next()){
            total = Util.getIntValue(rs.getString("total"));
        }

        String where = "";
        if(page>=0 && size > 0)
        {
            where  = " where w.px>="+(page-1)*size +" and w.px<"+page*size;
        }

        String gwkySql = "select w.* from (select p.*,rownum as px from("+ dataSql  +") p) w" + where;
        rs.executeQuery(gwkySql);
        bb.writeLog("====执行的查询公告sql===="+gwkySql);
        rs.executeQuery(gwkySql);
        String[] columnNames = rs.getColumnName();
        bb.writeLog("columnNames:" + JSONObject.toJSONString(columnNames));
        int count = 0;
        while (rs.next()) {
            count += 1;
            Map<String, Object> oneRow = new HashMap<>();
            for (String columnName : columnNames) {
                if ("NGR".equals(columnName)) {
                    String ngrIdStr = Util.null2String(rs.getString(columnName.toLowerCase()));
                    int ngrId = ngrIdStr.equals("") ? 0 : Integer.parseInt(ngrIdStr);
                    // 拟稿人
                    String ngr = new User(ngrId).getUsername();
                    oneRow.put("ngrName", ngr);
                }
                if ("ID".equals(columnName)) {
                    // 公文ID
                    String billId = Util.null2String(rs.getString("id"));
                    // 跳转url
                    String url = BASE_URL + billId;
                    oneRow.put("url", url);
                }
                oneRow.put(columnName.toLowerCase(), rs.getString(columnName.toLowerCase()));
            }
            result.add(oneRow);
        }
        Map<String, Object> appData = new HashMap<>();
        appData.put("data", result);
        appData.put("total", total);
        return appData;
    }
}
