package weaver.interfaces.zjrb.mode;

import oracle.sql.CLOB;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;

public class ExaminationPaperModeExpand2 extends AbstractModeExpandJavaCodeNew {

    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User)param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    //------请在下面编写业务逻辑代码------

                    saveUserData2(billid+"");
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

    public void saveUserData2(String billid){
        BaseBean bb = new BaseBean();

        long start = System.currentTimeMillis();
        bb.writeLog("ExaminationPaperModeExpand2-"+billid+":"+start);

        try{
            if(StringUtils.isNotBlank(billid)){
                RecordSet rs = new RecordSet();
                if(("oracle").equals(rs.getDBType())){
                    bb.writeLog("oracle");
                    String ksfw = "";
                    ConnStatement statement = null;
                    try{
                        statement = new ConnStatement();
                        String sql = "select ksfw from uf_ks_kjb where id = " + billid;
                        statement.setStatementSql(sql, false);
                        statement.executeQuery();
                        if (statement.next()) {
                            CLOB clob = statement.getClob("ksfw");
                            String readline = "";
                            StringBuffer clobStrBuff = new StringBuffer("");
                            BufferedReader clobin = new BufferedReader(clob.getCharacterStream());
                            while ((readline = clobin.readLine()) != null)
                                clobStrBuff = clobStrBuff.append(readline);
                            clobin.close();
                            ksfw = clobStrBuff.toString();
                        }
                    }catch(Exception e){
                        bb.writeLog("saveUserData2-e:"+e);
                    }finally {
                        statement.close();
                    }

                    long start2 = System.currentTimeMillis();
                    bb.writeLog("ExaminationPaperModeExpand2-"+billid+":"+start2);

                    if(StringUtils.isNotBlank(ksfw)) {
                        ExaminationPaperModeExpand2Runnable examinationPaperModeExpand2Runnable = new ExaminationPaperModeExpand2Runnable(ksfw,billid);
                        new Thread(examinationPaperModeExpand2Runnable).start();
                    }

                    long end = System.currentTimeMillis();
                    bb.writeLog("ExaminationPaperModeExpand2-"+billid+":"+end);
                }
            }
        }catch (Exception e){
            bb.writeLog("e:"+e+"</br>");
        }
    }
}
