package com.engine.htsc.aqsc.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.htsc.itlxlc.util.RequestShareUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

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


    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> apimap = new HashMap<String, Object>();

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String customid = Util.null2String(params.get("customid"));

        bb.writeLog("customidL："+customid);
//con_56991

        String departmentid = user.getUserDepartment()+"";
        String departmentcode = "" ;
        if(!"".equals(departmentid)){
            String sql = " select departmentcode from hrmdepartment where id="+departmentid;
            rs.execute(sql);
            if(rs.next()){
                departmentcode = Util.null2String(rs.getString("departmentcode"));
            }
        }

        //ZZ001126
        String where = "";
        if(!"".equals(customid)){
            String sql = "select defaultsql from mode_customsearch where id="+customid ;
            rs.execute(sql);
            if(rs.next()){
                where += Util.null2String(rs.getString("defaultsql"));
            }
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            System.out.println("Key = " + entry.getKey() + " Value = "+ entry.getValue());
            String key = entry.getKey();
            String value = Util.null2String(entry.getValue());

            System.out.println(key + ":" + value);
            String fieldid = "" ;
            if(key.contains("con")){
                fieldid = key.substring(4);
            }else{
                boolean bool = isNumeric(key);
                if(bool){
                    fieldid = key;
                }
            }

            if(!"".equals(fieldid)){
                String filename = "" ;
                String sql = " select filename from workflow_billfield where id= "+fieldid;
                rs.execute(sql);
                if(rs.next()){
                    filename = Util.null2String(rs.getString("filename"));
                }
                if(!"".equals(filename)){
                    where += where == "" ? " t1."+filename +"='"+value+"'" : " and t1."+ filename +"='"+value+"'";
                }
            }
        }

        if(!"ZZ001126".equals(departmentcode)){
            where += " and t1.rwdwbm="+departmentid ;
        }

        String wcsj = "" ;
        String sql = " select min(wcsj) as wcsj from uf_aqscglbm t1 where "+where ;
        bb.writeLog("sql："+sql);
        rs.execute(sql);
        if(rs.next()){
            wcsj = Util.null2String(rs.getString("wcsj"));
        }
        apimap.put("wcsj",wcsj);
        return apimap;
    }


    /***
     *
     * @param str
     * @return
     */
    public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }


}
